Skip to content

Commit bfcd2cf

Browse files
committed
block conversion
1 parent e620bde commit bfcd2cf

6 files changed

Lines changed: 75 additions & 37 deletions

File tree

‎core/types/block.go‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Header struct {
2727
// Receipt sha
2828
ReceiptHash common.Hash
2929
// Bloom
30-
Bloom [256]byte
30+
Bloom Bloom
3131
// Difficulty for the current block
3232
Difficulty *big.Int
3333
// The block number
@@ -73,28 +73,30 @@ func (self *Header) RlpData() interface{} {
7373
return self.rlpData(true)
7474
}
7575

76-
func (self *Header) Hash() []byte {
77-
return crypto.Sha3(common.Encode(self.rlpData(true)))
76+
func (self *Header) Hash() common.Hash {
77+
return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(true))))
7878
}
7979

80-
func (self *Header) HashNoNonce() []byte {
81-
return crypto.Sha3(common.Encode(self.rlpData(false)))
80+
func (self *Header) HashNoNonce() common.Hash {
81+
return common.BytesToHash(crypto.Sha3(common.Encode(self.rlpData(false))))
8282
}
8383

8484
type Block struct {
85-
// Preset Hash for mock
86-
HeaderHash []byte
87-
ParentHeaderHash []byte
88-
header *Header
89-
uncles []*Header
90-
transactions Transactions
91-
Td *big.Int
85+
// Preset Hash for mock (Tests)
86+
HeaderHash common.Hash
87+
ParentHeaderHash common.Hash
88+
// ^^^^ ignore ^^^^
89+
90+
header *Header
91+
uncles []*Header
92+
transactions Transactions
93+
Td *big.Int
9294

9395
receipts Receipts
9496
Reward *big.Int
9597
}
9698

97-
func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce uint64, extra string) *Block {
99+
func NewBlock(parentHash common.Hash, coinbase common.Address, root common.Hash, difficulty *big.Int, nonce uint64, extra string) *Block {
98100
header := &Header{
99101
Root: root,
100102
ParentHash: parentHash,
@@ -113,8 +115,7 @@ func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.I
113115
}
114116

115117
func (self *Header) setNonce(nonce uint64) {
116-
self.Nonce = make([]byte, 8)
117-
binary.BigEndian.PutUint64(self.Nonce, nonce)
118+
binary.BigEndian.PutUint64(self.Nonce[:], nonce)
118119
}
119120

120121
func NewBlockWithHeader(header *Header) *Block {
@@ -148,7 +149,7 @@ func (self *Block) Uncles() []*Header {
148149

149150
func (self *Block) SetUncles(uncleHeaders []*Header) {
150151
self.uncles = uncleHeaders
151-
self.header.UncleHash = crypto.Sha3(common.Encode(uncleHeaders))
152+
self.header.UncleHash = common.BytesToHash(crypto.Sha3(common.Encode(uncleHeaders)))
152153
}
153154

154155
func (self *Block) Transactions() Transactions {
@@ -196,23 +197,23 @@ func (self *Block) RlpDataForStorage() interface{} {
196197
}
197198

198199
// Header accessors (add as you need them)
199-
func (self *Block) Number() *big.Int { return self.header.Number }
200-
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
201-
func (self *Block) MixDigest() []byte { return self.header.MixDigest }
200+
func (self *Block) Number() *big.Int { return self.header.Number }
201+
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
202+
func (self *Block) MixDigest() common.Hash { return self.header.MixDigest }
202203
func (self *Block) Nonce() uint64 {
203-
return binary.BigEndian.Uint64(self.header.Nonce)
204+
return binary.BigEndian.Uint64(self.header.Nonce[:])
204205
}
205206
func (self *Block) SetNonce(nonce uint64) {
206207
self.header.setNonce(nonce)
207208
}
208209

209-
func (self *Block) Bloom() []byte { return self.header.Bloom }
210-
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
210+
func (self *Block) Bloom() Bloom { return self.header.Bloom }
211+
func (self *Block) Coinbase() common.Address { return self.header.Coinbase }
211212
func (self *Block) Time() int64 { return int64(self.header.Time) }
212213
func (self *Block) GasLimit() *big.Int { return self.header.GasLimit }
213214
func (self *Block) GasUsed() *big.Int { return self.header.GasUsed }
214-
func (self *Block) Root() []byte { return self.header.Root }
215-
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
215+
func (self *Block) Root() common.Hash { return self.header.Root }
216+
func (self *Block) SetRoot(root common.Hash) { self.header.Root = root }
216217
func (self *Block) Size() common.StorageSize { return common.StorageSize(len(common.Encode(self))) }
217218
func (self *Block) GetTransaction(i int) *Transaction {
218219
if len(self.transactions) > i {
@@ -228,19 +229,19 @@ func (self *Block) GetUncle(i int) *Header {
228229
}
229230

230231
// Implement pow.Block
231-
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
232-
func (self *Block) HashNoNonce() []byte { return self.header.HashNoNonce() }
232+
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
233+
func (self *Block) HashNoNonce() common.Hash { return self.header.HashNoNonce() }
233234

234-
func (self *Block) Hash() []byte {
235-
if self.HeaderHash != nil {
235+
func (self *Block) Hash() common.Hash {
236+
if (self.HeaderHash != common.Hash{}) {
236237
return self.HeaderHash
237238
} else {
238239
return self.header.Hash()
239240
}
240241
}
241242

242-
func (self *Block) ParentHash() []byte {
243-
if self.ParentHeaderHash != nil {
243+
func (self *Block) ParentHash() common.Hash {
244+
if (self.ParentHeaderHash != common.Hash{}) {
244245
return self.ParentHeaderHash
245246
} else {
246247
return self.header.ParentHash

‎core/types/block_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
package types
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/ethereum/go-ethereum/common"
9+
)
10+
11+
func TestConversion(t *testing.T) {
12+
var (
13+
parent common.Hash
14+
coinbase common.Address
15+
hash common.Hash
16+
)
17+
18+
block := NewBlock(parent, coinbase, hash, big.NewInt(0), 0, "")
19+
fmt.Println(block)
20+
}

‎core/types/bloom9.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package types
33
import (
44
"math/big"
55

6-
"github.com/ethereum/go-ethereum/crypto"
76
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/crypto"
88
"github.com/ethereum/go-ethereum/state"
99
)
1010

11-
func CreateBloom(receipts Receipts) []byte {
11+
func CreateBloom(receipts Receipts) Bloom {
1212
bin := new(big.Int)
1313
for _, receipt := range receipts {
1414
bin.Or(bin, LogsBloom(receipt.logs))
1515
}
1616

17-
return common.LeftPadBytes(bin.Bytes(), 256)
17+
return BytesToBloom(bin.Bytes())
1818
}
1919

2020
func LogsBloom(logs state.Logs) *big.Int {

‎core/types/common.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,22 @@ import "math/big"
55
type BlockProcessor interface {
66
Process(*Block) (*big.Int, error)
77
}
8+
9+
type Bloom [256]byte
10+
11+
func BytesToBloom(b []byte) Bloom {
12+
var bloom Bloom
13+
bloom.SetBytes(b)
14+
return bloom
15+
}
16+
17+
func (b *Bloom) SetBytes(d []byte) {
18+
if len(b) > len(d) {
19+
panic("bloom bytes too big")
20+
}
21+
22+
// reverse loop
23+
for i := len(d) - 1; i >= 0; i-- {
24+
b[i] = b[i]
25+
}
26+
}

‎core/types/derive_sha.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package types
22

33
import (
4-
"github.com/ethereum/go-ethereum/ethdb"
54
"github.com/ethereum/go-ethereum/common"
5+
"github.com/ethereum/go-ethereum/ethdb"
66
"github.com/ethereum/go-ethereum/trie"
77
)
88

@@ -11,12 +11,12 @@ type DerivableList interface {
1111
GetRlp(i int) []byte
1212
}
1313

14-
func DeriveSha(list DerivableList) []byte {
14+
func DeriveSha(list DerivableList) common.Hash {
1515
db, _ := ethdb.NewMemDatabase()
1616
trie := trie.New(nil, db)
1717
for i := 0; i < list.Len(); i++ {
1818
trie.Update(common.Encode(i), list.GetRlp(i))
1919
}
2020

21-
return trie.Root()
21+
return common.BytesToHash(trie.Root())
2222
}

‎state/dump.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (self *StateDB) RawDump() World {
2828

2929
it := self.trie.Iterator()
3030
for it.Next() {
31-
fmt.Printf("%x\n", it.Key, len(it.Key))
3231
stateObject := NewStateObjectFromBytes(common.BytesToAddress(it.Key), it.Value, self.db)
3332

3433
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}

0 commit comments

Comments
 (0)