/
store_receipts.go
80 lines (65 loc) · 1.96 KB
/
store_receipts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package evmstore
/*
In LRU cache data stored like value
*/
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/skyhighblockchain/push-base/inter/idx"
)
// SetReceipts stores transaction receipts.
func (s *Store) SetReceipts(n idx.Block, receipts types.Receipts) {
receiptsStorage := make([]*types.ReceiptForStorage, len(receipts))
for i, r := range receipts {
receiptsStorage[i] = (*types.ReceiptForStorage)(r)
}
size := s.SetRawReceipts(n, receiptsStorage)
// Add to LRU cache.
s.cache.Receipts.Add(n, receipts, uint(size))
}
// SetRawReceipts stores raw transaction receipts.
func (s *Store) SetRawReceipts(n idx.Block, receipts []*types.ReceiptForStorage) (size int) {
buf, err := rlp.EncodeToBytes(receipts)
if err != nil {
s.Log.Crit("Failed to encode rlp", "err", err)
}
if err := s.table.Receipts.Put(n.Bytes(), buf); err != nil {
s.Log.Crit("Failed to put key-value", "err", err)
}
// Remove from LRU cache.
s.cache.Receipts.Remove(n)
return len(buf)
}
// GetReceipts returns stored transaction receipts.
func (s *Store) GetReceipts(n idx.Block) types.Receipts {
// Get data from LRU cache first.
if s.cache.Receipts != nil {
if c, ok := s.cache.Receipts.Get(n); ok {
return c.(types.Receipts)
}
}
buf, err := s.table.Receipts.Get(n.Bytes())
if err != nil {
s.Log.Crit("Failed to get key-value", "err", err)
}
if buf == nil {
return nil
}
var receiptsStorage *[]*types.ReceiptForStorage
err = rlp.DecodeBytes(buf, &receiptsStorage)
if err != nil {
s.Log.Crit("Failed to decode rlp", "err", err, "size", len(buf))
}
receipts := make(types.Receipts, len(*receiptsStorage))
for i, r := range *receiptsStorage {
receipts[i] = (*types.Receipt)(r)
var prev uint64
if i != 0 {
prev = receipts[i-1].CumulativeGasUsed
}
receipts[i].GasUsed = receipts[i].CumulativeGasUsed - prev
}
// Add to LRU cache.
s.cache.Receipts.Add(n, receipts, uint(len(buf)))
return receipts
}