1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"encoding/binary"
"github.com/ethereum/go-ethereum/core/types"
"bytes"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto"
)
var (
headHeaderKey = []byte("LastHeader")
headBlockKey = []byte("LastBlock")
headFastKey = []byte("LastFast")
bodyPrefix = []byte("b")
blockHashPrefix = []byte("H")
headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
)
func main() {
db, err := ethdb.NewLDBDatabase("/Users/bing/test/data/geth/chaindata", 768, 1024)
if nil != err {
fmt.Println(err)
return
}
headBlockHashData, _ := db.Get(headBlockKey)
headBlockHash := common.BytesToHash(headBlockHashData);
headBlockNumData, _ := db.Get(append(blockHashPrefix, headBlockHashData...))
headBlockNum := binary.BigEndian.Uint64(headBlockNumData)
fmt.Printf("block Number of hash %x is %d\n", headBlockHash, headBlockNum)
// Connection to leveldb
//db, _ := leveldb.OpenFile("/Users/bing/test/data/geth/chaindata", nil)
//374,327,307,294,287
//18, contract creation
for i:=headBlockNum; i > 0; i-- {
// 40 to bytes (Big endian)
blockNumber := make([]byte, 8)
binary.BigEndian.PutUint64(blockNumber, uint64(i))
fmt.Printf("Details of Blocknumber:- \nHex: %x \nBytes: %d\n\n\n", blockNumber, blockNumber)
// create key to get hash (headerPrefix + num (uint64 big endian) + numSuffix)
hashKey := append(headerPrefix, blockNumber...) // adding prefix
hashKey = append(hashKey, numSuffix...) // adding suffix
fmt.Printf("Details of leveldb key for Block Hash:- \nType: %T \nHex: %x \nbytes: %v \nLength: %d\n\n\n", hashKey, hashKey, hashKey, len(hashKey))
// Getting hash using hashKey
blockHash, _ := db.Get(hashKey)
fmt.Printf("Details of Block hash:- \nType: %T \nHex: %x \nBytes: %v\n\n\n", blockHash, blockHash, blockHash)
//Create key to get header (headerPrefix + num (uint64 big endian) + hash)
headerKey := append(headerPrefix, blockNumber...) // adding prefix
headerKey = append(headerKey, blockHash...) // adding suffix
fmt.Printf("Details of leveldb key for Block Header:- \nType: %T \nHex: %x \nVytes: %v \nLength: %d\n\n\n", headerKey, headerKey, headerKey, len(headerKey))
//get Block Header data from db
blockHeaderData, _ := db.Get(headerKey)
fmt.Printf("Details of Raw Block Header:- \nType: %T \nHex: %x \nBytes: %v \nLength: %d\n\n\n", blockHeaderData, blockHeaderData, blockHeaderData, len(blockHeaderData))
//new Blockheader type
blockHeader := new(types.Header)
fmt.Printf("Details of new Header Type:- \nType: %T \nHex: %x \nValue: %v\n\n\n", blockHeader, blockHeader, blockHeader)
// Read blockHeaderData in a tmp variable
tmpByteData := bytes.NewReader(blockHeaderData)
fmt.Printf("Details of tmpByteData:- \nType: %T \nHex: %x \nValue: %v\n\n\n", tmpByteData, tmpByteData, tmpByteData)
//Decode tmpByteData to new blockHeader
rlp.Decode(tmpByteData, blockHeader)
fmt.Printf("Details of Header for block number:- \nType: %T \nHex: %x \nValue: %v\n\n\n", blockHeader, blockHeader, blockHeader)
blockBodyData, _ := db.Get(append(append(bodyPrefix, blockNumber...), blockHash...))
blockBody := new(types.Body)
if err = rlp.Decode(bytes.NewReader(blockBodyData), blockBody); err != nil {
fmt.Println(err)
}
fmt.Printf("Details of Body for block number:- \nType: %T \nHex: %x \nValue: %v\n\n\n", blockBody, blockBody, blockBody)
if len(blockBody.Transactions) != 0 {
fmt.Printf("Block with noempty body found: %d\n", i)
break;
}
}
headBlockHeaderData, _ := db.Get(append(append(headerPrefix, headBlockNumData...), headBlockHashData...))
headBlockHeader := new(types.Header)
rlp.Decode(bytes.NewReader(headBlockHeaderData), headBlockHeader);
fmt.Printf("head block Header: %v\n", headBlockHeader)
headBlockBodyData, _ := db.Get(append(append(bodyPrefix, headBlockNumData...), headBlockHashData...))
//headBlockBodyData, _ := db.Get(append(append(bodyPrefix, blockNumber...), blockHash...))
headBlockBody := new(types.Body)
err = rlp.Decode(bytes.NewReader(headBlockBodyData), headBlockBody)
if err != nil {
fmt.Println(err)
}
fmt.Printf("head block Body: %v\n", headBlockBody)
//trie, err := trie.New(common.StringToHash("0x5891bbf641aaf627dcce8164be0ff9670efeb7bdde188d29134b3a1963ab738a"), trie.NewDatabase(ldb))
trieSecure, err := trie.NewSecure(headBlockHeader.Root, trie.NewDatabase(db), 120)
//trieSecure, err := state.NewDatabase(db).OpenTrie(common.HexToHash("0x5a3aa9413dae5b3b87008d8a1e5817a736417d1f70e7351c7a803508134a670c"))
if nil != err {
fmt.Println(err)
return
}
addr := common.HexToAddress("0x328c2504e15b05572df94f0df385cf3c4b130879")
val, err := trieSecure.TryGet(addr[:])
if nil != err {
fmt.Println(err)
return
}
//fmt.Printf("%v\n", val)
var data state.Account
if err := rlp.DecodeBytes(val, &data); err != nil {
fmt.Print("Failed to decode state object", ", addr: ", addr, ", err: ", err)
return
}
fmt.Printf("account state: %+v\n", data)
//fmt.Printf("%x\n", blockHeader.Root)
fmt.Printf("%x\t%x\n", data.Root, common.BytesToHash(data.CodeHash))
fmt.Printf("%x\n", crypto.Keccak256(nil))
}
|