@@ -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
104105func NewBlockFromBytes (raw []byte ) * Block {
@@ -151,7 +152,7 @@ func (block *Block) Hash() ethutil.Bytes {
151152func (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
193194func (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{} {
236237func (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
245243func (self * Block ) SetReceipts (receipts []* Receipt , txs []* Transaction ) {
@@ -249,32 +247,20 @@ func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) {
249247
250248func (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
276262func (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
280266func (block * Block ) RlpEncode () []byte {
@@ -289,33 +275,20 @@ func (block *Block) RlpDecode(data []byte) {
289275}
290276
291277func (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+
333324func 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 {
437423func (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+ */
0 commit comments