/
store_tx_position.go
45 lines (36 loc) · 1.07 KB
/
store_tx_position.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
package evmstore
/*
In LRU cache data stored like pointer
*/
import (
"github.com/corex-mn/corex-base/hash"
"github.com/corex-mn/corex-base/inter/idx"
"github.com/ethereum/go-ethereum/common"
)
type TxPosition struct {
Block idx.Block
Event hash.Event
EventOffset uint32
BlockOffset uint32
}
// SetTxPosition stores transaction block and position.
func (s *Store) SetTxPosition(txid common.Hash, position TxPosition) {
s.rlp.Set(s.table.TxPositions, txid.Bytes(), &position)
// Add to LRU cache.
s.cache.TxPositions.Add(txid.String(), &position, nominalSize)
}
// GetTxPosition returns stored transaction block and position.
func (s *Store) GetTxPosition(txid common.Hash) *TxPosition {
// Get data from LRU cache first.
if c, ok := s.cache.TxPositions.Get(txid.String()); ok {
if b, ok := c.(*TxPosition); ok {
return b
}
}
txPosition, _ := s.rlp.Get(s.table.TxPositions, txid.Bytes(), &TxPosition{}).(*TxPosition)
// Add to LRU cache.
if txPosition != nil {
s.cache.TxPositions.Add(txid.String(), txPosition, nominalSize)
}
return txPosition
}