github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/evmstore/store_receipts.go (about) 1 package evmstore 2 3 /* 4 In LRU cache data stored like value 5 */ 6 7 import ( 8 "github.com/unicornultrafoundation/go-helios/native/idx" 9 "github.com/unicornultrafoundation/go-u2u/common" 10 "github.com/unicornultrafoundation/go-u2u/core/types" 11 "github.com/unicornultrafoundation/go-u2u/rlp" 12 ) 13 14 // SetReceipts stores transaction receipts. 15 func (s *Store) SetReceipts(n idx.Block, receipts types.Receipts) { 16 receiptsStorage := make([]*types.ReceiptForStorage, receipts.Len()) 17 for i, r := range receipts { 18 receiptsStorage[i] = (*types.ReceiptForStorage)(r) 19 } 20 21 size := s.SetRawReceipts(n, receiptsStorage) 22 23 // Add to LRU cache. 24 s.cache.Receipts.Add(n, receipts, uint(size)) 25 } 26 27 // SetRawReceipts stores raw transaction receipts. 28 func (s *Store) SetRawReceipts(n idx.Block, receipts []*types.ReceiptForStorage) (size int) { 29 buf, err := rlp.EncodeToBytes(receipts) 30 if err != nil { 31 s.Log.Crit("Failed to encode rlp", "err", err) 32 } 33 34 if err := s.table.Receipts.Put(n.Bytes(), buf); err != nil { 35 s.Log.Crit("Failed to put key-value", "err", err) 36 } 37 38 // Remove from LRU cache. 39 s.cache.Receipts.Remove(n) 40 41 return len(buf) 42 } 43 44 func (s *Store) GetRawReceiptsRLP(n idx.Block) rlp.RawValue { 45 buf, err := s.table.Receipts.Get(n.Bytes()) 46 if err != nil { 47 s.Log.Crit("Failed to get key-value", "err", err) 48 } 49 return buf 50 } 51 52 func (s *Store) GetRawReceipts(n idx.Block) ([]*types.ReceiptForStorage, int) { 53 buf := s.GetRawReceiptsRLP(n) 54 if buf == nil { 55 return nil, 0 56 } 57 58 var receiptsStorage []*types.ReceiptForStorage 59 err := rlp.DecodeBytes(buf, &receiptsStorage) 60 if err != nil { 61 s.Log.Crit("Failed to decode rlp", "err", err, "size", len(buf)) 62 } 63 return receiptsStorage, len(buf) 64 } 65 66 func UnwrapStorageReceipts(receiptsStorage []*types.ReceiptForStorage, n idx.Block, signer types.Signer, hash common.Hash, txs types.Transactions) (types.Receipts, error) { 67 receipts := make(types.Receipts, len(receiptsStorage)) 68 for i, r := range receiptsStorage { 69 receipts[i] = (*types.Receipt)(r) 70 } 71 err := receipts.DeriveFields(signer, hash, uint64(n), txs) 72 return receipts, err 73 } 74 75 // GetReceipts returns stored transaction receipts. 76 func (s *Store) GetReceipts(n idx.Block, signer types.Signer, hash common.Hash, txs types.Transactions) types.Receipts { 77 // Get data from LRU cache first. 78 if s.cache.Receipts != nil { 79 if c, ok := s.cache.Receipts.Get(n); ok { 80 return c.(types.Receipts) 81 } 82 } 83 84 receiptsStorage, size := s.GetRawReceipts(n) 85 86 receipts, err := UnwrapStorageReceipts(receiptsStorage, n, signer, hash, txs) 87 if err != nil { 88 s.Log.Crit("Failed to derive receipts", "err", err) 89 } 90 91 // Add to LRU cache. 92 s.cache.Receipts.Add(n, receipts, uint(size)) 93 94 return receipts 95 }