@@ -40,11 +40,6 @@ type BlockProcessor struct {
4040
4141 txpool * TxPool
4242
43- // The last attempted block is mainly used for debugging purposes
44- // This does not have to be a valid block and will be set during
45- // 'Process' & canonical validation.
46- lastAttemptedBlock * types.Block
47-
4843 events event.Subscription
4944
5045 eventMux * event.TypeMux
@@ -188,8 +183,6 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, err erro
188183}
189184
190185func (sm * BlockProcessor ) processWithParent (block , parent * types.Block ) (logs state.Logs , err error ) {
191- sm .lastAttemptedBlock = block
192-
193186 // Create a new state based on the parent's root (e.g., create copy)
194187 state := state .New (parent .Root (), sm .db )
195188
@@ -255,6 +248,12 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
255248 return
256249 }
257250
251+ // store the receipts
252+ err = putReceipts (sm .extraDb , block .Hash (), receipts )
253+ if err != nil {
254+ return nil , err
255+ }
256+
258257 // Calculate the td for this block
259258 //td = CalculateTD(block, parent)
260259 // Sync the current block's state to the database
@@ -268,23 +267,9 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
268267 putTx (sm .extraDb , tx , block , uint64 (i ))
269268 }
270269
271- receiptsRlp := block .Receipts ().RlpEncode ()
272- sm .extraDb .Put (append (receiptsPre , block .Hash ().Bytes ()... ), receiptsRlp )
273-
274270 return state .Logs (), nil
275271}
276272
277- func (self * BlockProcessor ) GetBlockReceipts (bhash common.Hash ) (receipts types.Receipts , err error ) {
278- var rdata []byte
279- rdata , err = self .extraDb .Get (append (receiptsPre , bhash [:]... ))
280-
281- if err == nil {
282- err = rlp .DecodeBytes (rdata , & receipts )
283- }
284- return
285-
286- }
287-
288273// See YP section 4.3.4. "Block Header Validity"
289274// Validates a block. Returns an error if the block is invalid.
290275func (sm * BlockProcessor ) ValidateHeader (block , parent * types.Header , checkPow bool ) error {
@@ -391,13 +376,25 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
391376 return nil
392377}
393378
379+ // GetBlockReceipts returns the receipts beloniging to the block hash
380+ func (sm * BlockProcessor ) GetBlockReceipts (bhash common.Hash ) (receipts types.Receipts , err error ) {
381+ return getBlockReceipts (sm .extraDb , bhash )
382+ }
383+
384+ // GetLogs returns the logs of the given block. This method is using a two step approach
385+ // where it tries to get it from the (updated) method which gets them from the receipts or
386+ // the depricated way by re-processing the block.
394387func (sm * BlockProcessor ) GetLogs (block * types.Block ) (logs state.Logs , err error ) {
395- if ! sm .bc .HasBlock (block .Header ().ParentHash ) {
396- return nil , ParentError (block .Header ().ParentHash )
388+ receipts , err := sm .GetBlockReceipts (block .Hash ())
389+ if err == nil && len (receipts ) > 0 {
390+ // coalesce logs
391+ for _ , receipt := range receipts {
392+ logs = append (logs , receipt .Logs ()... )
393+ }
394+ return
397395 }
398396
399- sm .lastAttemptedBlock = block
400-
397+ // TODO: remove backward compatibility
401398 var (
402399 parent = sm .bc .GetBlock (block .Header ().ParentHash )
403400 state = state .New (parent .Root (), sm .db )
@@ -408,6 +405,16 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
408405 return state .Logs (), nil
409406}
410407
408+ func getBlockReceipts (db common.Database , bhash common.Hash ) (receipts types.Receipts , err error ) {
409+ var rdata []byte
410+ rdata , err = db .Get (append (receiptsPre , bhash [:]... ))
411+
412+ if err == nil {
413+ err = rlp .DecodeBytes (rdata , & receipts )
414+ }
415+ return
416+ }
417+
411418func putTx (db common.Database , tx * types.Transaction , block * types.Block , i uint64 ) {
412419 rlpEnc , err := rlp .EncodeToBytes (tx )
413420 if err != nil {
@@ -431,3 +438,19 @@ func putTx(db common.Database, tx *types.Transaction, block *types.Block, i uint
431438 }
432439 db .Put (append (tx .Hash ().Bytes (), 0x0001 ), rlpMeta )
433440}
441+
442+ func putReceipts (db common.Database , hash common.Hash , receipts types.Receipts ) error {
443+ storageReceipts := make ([]* types.ReceiptForStorage , len (receipts ))
444+ for i , receipt := range receipts {
445+ storageReceipts [i ] = (* types .ReceiptForStorage )(receipt )
446+ }
447+
448+ bytes , err := rlp .EncodeToBytes (storageReceipts )
449+ if err != nil {
450+ return err
451+ }
452+
453+ db .Put (append (receiptsPre , hash [:]... ), bytes )
454+
455+ return nil
456+ }
0 commit comments