Skip to content

Commit fb4113d

Browse files
committed
PoC 7 updates
* Bloom * Block restructure * Receipts
1 parent 665a446 commit fb4113d

14 files changed

Lines changed: 219 additions & 135 deletions

File tree

‎ethchain/block.go‎

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ type Block struct {
9696
// Block Nonce for verification
9797
Nonce ethutil.Bytes
9898
// List of transactions and/or contracts
99-
transactions []*Transaction
100-
receipts []*Receipt
101-
TxSha []byte
99+
transactions Transactions
100+
receipts Receipts
101+
TxSha, ReceiptSha []byte
102+
LogsBloom []byte
102103
}
103104

104105
func NewBlockFromBytes(raw []byte) *Block {
@@ -151,7 +152,7 @@ func (block *Block) Hash() ethutil.Bytes {
151152
func (block *Block) HashNoNonce() []byte {
152153
return ethcrypto.Sha3(ethutil.Encode([]interface{}{block.PrevHash,
153154
block.UncleSha, block.Coinbase, block.state.Trie.Root,
154-
block.TxSha, block.Difficulty, block.Number, block.MinGasPrice,
155+
block.ReceiptSha, block.Difficulty, block.Number, block.MinGasPrice,
155156
block.GasLimit, block.GasUsed, block.Time, block.Extra}))
156157
}
157158

@@ -191,9 +192,9 @@ func (block *Block) BlockInfo() BlockInfo {
191192
}
192193

193194
func (self *Block) GetTransaction(hash []byte) *Transaction {
194-
for _, receipt := range self.receipts {
195-
if bytes.Compare(receipt.Tx.Hash(), hash) == 0 {
196-
return receipt.Tx
195+
for _, tx := range self.transactions {
196+
if bytes.Compare(tx.Hash(), hash) == 0 {
197+
return tx
197198
}
198199
}
199200

@@ -236,10 +237,7 @@ func (block *Block) rlpUncles() interface{} {
236237
func (block *Block) SetUncles(uncles []*Block) {
237238
block.Uncles = uncles
238239

239-
// Sha of the concatenated uncles
240-
if len(uncles) > 0 {
241-
block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles()))
242-
}
240+
block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles()))
243241
}
244242

245243
func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) {
@@ -249,32 +247,20 @@ func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) {
249247

250248
func (block *Block) setTransactions(txs []*Transaction) {
251249
block.transactions = txs
252-
}
253-
254-
func CreateTxSha(receipts Receipts) (sha []byte) {
255-
trie := ethtrie.New(ethutil.Config.Db, "")
256-
for i, receipt := range receipts {
257-
trie.Update(string(ethutil.NewValue(i).Encode()), string(ethutil.NewValue(receipt.RlpData()).Encode()))
258-
}
259250

260-
switch trie.Root.(type) {
261-
case string:
262-
sha = []byte(trie.Root.(string))
263-
case []byte:
264-
sha = trie.Root.([]byte)
265-
default:
266-
panic(fmt.Sprintf("invalid root type %T", trie.Root))
267-
}
251+
block.LogsBloom = CreateBloom(txs)
252+
}
268253

269-
return sha
254+
func (self *Block) SetTransactionHash(transactions Transactions) {
255+
self.TxSha = DeriveSha(transactions)
270256
}
271257

272-
func (self *Block) SetTxHash(receipts Receipts) {
273-
self.TxSha = CreateTxSha(receipts)
258+
func (self *Block) SetReceiptHash(receipts Receipts) {
259+
self.ReceiptSha = DeriveSha(receipts)
274260
}
275261

276262
func (block *Block) Value() *ethutil.Value {
277-
return ethutil.NewValue([]interface{}{block.header(), block.rlpReceipts(), block.rlpUncles()})
263+
return ethutil.NewValue([]interface{}{block.header(), block.transactions, block.rlpUncles()})
278264
}
279265

280266
func (block *Block) RlpEncode() []byte {
@@ -289,33 +275,20 @@ func (block *Block) RlpDecode(data []byte) {
289275
}
290276

291277
func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
292-
header := decoder.Get(0)
293-
294-
block.PrevHash = header.Get(0).Bytes()
295-
block.UncleSha = header.Get(1).Bytes()
296-
block.Coinbase = header.Get(2).Bytes()
297-
block.state = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val))
298-
block.TxSha = header.Get(4).Bytes()
299-
block.Difficulty = header.Get(5).BigInt()
300-
block.Number = header.Get(6).BigInt()
301-
//fmt.Printf("#%v : %x\n", block.Number, block.Coinbase)
302-
block.MinGasPrice = header.Get(7).BigInt()
303-
block.GasLimit = header.Get(8).BigInt()
304-
block.GasUsed = header.Get(9).BigInt()
305-
block.Time = int64(header.Get(10).BigInt().Uint64())
306-
block.Extra = header.Get(11).Str()
307-
block.Nonce = header.Get(12).Bytes()
278+
block.setHeader(decoder.Get(0))
308279

309280
// Tx list might be empty if this is an uncle. Uncles only have their
310281
// header set.
311282
if decoder.Get(1).IsNil() == false { // Yes explicitness
312-
receipts := decoder.Get(1)
313-
block.transactions = make([]*Transaction, receipts.Len())
314-
block.receipts = make([]*Receipt, receipts.Len())
315-
for i := 0; i < receipts.Len(); i++ {
316-
receipt := NewRecieptFromValue(receipts.Get(i))
317-
block.transactions[i] = receipt.Tx
318-
block.receipts[i] = receipt
283+
//receipts := decoder.Get(1)
284+
//block.receipts = make([]*Receipt, receipts.Len())
285+
it := decoder.Get(1).NewIterator()
286+
block.transactions = make(Transactions, it.Len())
287+
for it.Next() {
288+
block.transactions[it.Idx()] = NewTransactionFromValue(it.Value())
289+
//receipt := NewRecieptFromValue(receipts.Get(i))
290+
//block.transactions[i] = receipt.Tx
291+
//block.receipts[i] = receipt
319292
}
320293

321294
}
@@ -330,22 +303,27 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
330303

331304
}
332305

306+
func (self *Block) setHeader(header *ethutil.Value) {
307+
self.PrevHash = header.Get(0).Bytes()
308+
self.UncleSha = header.Get(1).Bytes()
309+
self.Coinbase = header.Get(2).Bytes()
310+
self.state = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val))
311+
self.TxSha = header.Get(4).Bytes()
312+
self.ReceiptSha = header.Get(5).Bytes()
313+
self.LogsBloom = header.Get(6).Bytes()
314+
self.Difficulty = header.Get(7).BigInt()
315+
self.Number = header.Get(8).BigInt()
316+
self.MinGasPrice = header.Get(9).BigInt()
317+
self.GasLimit = header.Get(10).BigInt()
318+
self.GasUsed = header.Get(11).BigInt()
319+
self.Time = int64(header.Get(12).BigInt().Uint64())
320+
self.Extra = header.Get(13).Str()
321+
self.Nonce = header.Get(14).Bytes()
322+
}
323+
333324
func NewUncleBlockFromValue(header *ethutil.Value) *Block {
334325
block := &Block{}
335-
336-
block.PrevHash = header.Get(0).Bytes()
337-
block.UncleSha = header.Get(1).Bytes()
338-
block.Coinbase = header.Get(2).Bytes()
339-
block.state = ethstate.New(ethtrie.New(ethutil.Config.Db, header.Get(3).Val))
340-
block.TxSha = header.Get(4).Bytes()
341-
block.Difficulty = header.Get(5).BigInt()
342-
block.Number = header.Get(6).BigInt()
343-
block.MinGasPrice = header.Get(7).BigInt()
344-
block.GasLimit = header.Get(8).BigInt()
345-
block.GasUsed = header.Get(9).BigInt()
346-
block.Time = int64(header.Get(10).BigInt().Uint64())
347-
block.Extra = header.Get(11).Str()
348-
block.Nonce = header.Get(12).Bytes()
326+
block.setHeader(header)
349327

350328
return block
351329
}
@@ -376,8 +354,12 @@ func (block *Block) header() []interface{} {
376354
block.Coinbase,
377355
// root state
378356
block.state.Trie.Root,
379-
// Sha of tx
357+
// tx root
380358
block.TxSha,
359+
// Sha of tx
360+
block.ReceiptSha,
361+
// Bloom
362+
block.LogsBloom,
381363
// Current block Difficulty
382364
block.Difficulty,
383365
// The block number
@@ -404,7 +386,9 @@ func (block *Block) String() string {
404386
UncleSha: %x
405387
Coinbase: %x
406388
Root: %x
407-
TxSha: %x
389+
TxSha %x
390+
ReceiptSha: %x
391+
Bloom: %x
408392
Difficulty: %v
409393
Number: %v
410394
MinGas: %v
@@ -422,6 +406,8 @@ func (block *Block) String() string {
422406
block.Coinbase,
423407
block.state.Trie.Root,
424408
block.TxSha,
409+
block.ReceiptSha,
410+
block.LogsBloom,
425411
block.Difficulty,
426412
block.Number,
427413
block.MinGasPrice,
@@ -437,3 +423,23 @@ func (block *Block) String() string {
437423
func (self *Block) Size() ethutil.StorageSize {
438424
return ethutil.StorageSize(len(self.RlpEncode()))
439425
}
426+
427+
/*
428+
func DeriveReceiptHash(receipts Receipts) (sha []byte) {
429+
trie := ethtrie.New(ethutil.Config.Db, "")
430+
for i, receipt := range receipts {
431+
trie.Update(string(ethutil.NewValue(i).Encode()), string(ethutil.NewValue(receipt.RlpData()).Encode()))
432+
}
433+
434+
switch trie.Root.(type) {
435+
case string:
436+
sha = []byte(trie.Root.(string))
437+
case []byte:
438+
sha = trie.Root.([]byte)
439+
default:
440+
panic(fmt.Sprintf("invalid root type %T", trie.Root))
441+
}
442+
443+
return sha
444+
}
445+
*/

‎ethchain/bloom9.go‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
package ethchain
22

3-
import "github.com/ethereum/go-ethereum/vm"
3+
import (
4+
"math/big"
45

5-
func CreateBloom(txs Transactions) uint64 {
6-
var bin uint64
6+
"github.com/ethereum/go-ethereum/vm"
7+
)
8+
9+
func CreateBloom(txs Transactions) []byte {
10+
bin := new(big.Int)
711
for _, tx := range txs {
8-
bin |= logsBloom(tx.logs)
12+
bin.Or(bin, LogsBloom(tx.logs))
913
}
1014

11-
return bin
15+
return bin.Bytes()
1216
}
1317

14-
func logsBloom(logs []vm.Log) uint64 {
15-
var bin uint64
18+
func LogsBloom(logs []vm.Log) *big.Int {
19+
bin := new(big.Int)
1620
for _, log := range logs {
1721
data := [][]byte{log.Address}
1822
for _, topic := range log.Topics {
19-
data = append(data, topic.Bytes())
23+
data = append(data, topic)
2024
}
2125
data = append(data, log.Data)
2226

2327
for _, b := range data {
24-
bin |= bloom9(b)
28+
bin.Or(bin, bloom9(b))
2529
}
2630
}
2731

2832
return bin
2933
}
3034

31-
func bloom9(b []byte) uint64 {
32-
var r uint64
35+
func bloom9(b []byte) *big.Int {
36+
r := new(big.Int)
3337
for _, i := range []int{0, 2, 4} {
34-
r |= 1 << (uint64(b[i+1]) + 256*(uint64(b[i])&1))
38+
t := big.NewInt(1)
39+
40+
//r |= 1 << (uint64(b[i+1]) + 256*(uint64(b[i])&1))
41+
r.Or(r, t.Rsh(t, uint(b[i+1])+256*(uint(b[i])&1)))
3542
}
3643

3744
return r

‎ethchain/chain_manager.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func (bc *ChainManager) Reset() {
8787
bc.genesisBlock.state.Trie.Sync()
8888
// Prepare the genesis block
8989
bc.Add(bc.genesisBlock)
90-
fk := append([]byte("bloom"), bc.genesisBlock.Hash()...)
91-
bc.Ethereum.Db().Put(fk, make([]byte, 255))
90+
//fk := append([]byte("bloom"), bc.genesisBlock.Hash()...)
91+
//bc.Ethereum.Db().Put(fk, make([]byte, 255))
9292
bc.CurrentBlock = bc.genesisBlock
9393

9494
bc.SetTotalDifficulty(ethutil.Big("0"))

‎ethchain/filter.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ethchain
22

33
import (
44
"bytes"
5-
"fmt"
65
"math"
76

87
"github.com/ethereum/go-ethereum/ethstate"
@@ -171,13 +170,14 @@ func (self *Filter) FilterMessages(msgs []*ethstate.Message) []*ethstate.Message
171170
}
172171

173172
func (self *Filter) bloomFilter(block *Block) bool {
174-
fk := append([]byte("bloom"), block.Hash()...)
175-
bin, err := self.eth.Db().Get(fk)
176-
if err != nil {
177-
fmt.Println(err)
178-
}
179-
180-
bloom := NewBloomFilter(bin)
173+
//fk := append([]byte("bloom"), block.Hash()...)
174+
//bin, err := self.eth.Db().Get(fk)
175+
//if err != nil {
176+
// fmt.Println(err)
177+
//}
178+
179+
// TODO update to the new bloom filter
180+
bloom := NewBloomFilter(nil)
181181

182182
var fromIncluded, toIncluded bool
183183
if len(self.from) > 0 {

‎ethchain/genesis.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ import (
1313

1414
var ZeroHash256 = make([]byte, 32)
1515
var ZeroHash160 = make([]byte, 20)
16+
var ZeroHash512 = make([]byte, 64)
1617
var EmptyShaList = ethcrypto.Sha3(ethutil.Encode([]interface{}{}))
1718

1819
var GenesisHeader = []interface{}{
1920
// Previous hash (none)
2021
ZeroHash256,
2122
// Empty uncles
22-
"",
23+
EmptyShaList,
2324
// Coinbase
2425
ZeroHash160,
2526
// Root state
26-
"",
27+
EmptyShaList,
2728
// tx sha
28-
"",
29+
EmptyShaList,
30+
// receipt list
31+
EmptyShaList,
32+
// bloom
33+
ZeroHash512,
2934
// Difficulty
3035
//ethutil.BigPow(2, 22),
3136
big.NewInt(131072),

0 commit comments

Comments
 (0)