-
Notifications
You must be signed in to change notification settings - Fork 36
/
db.go
192 lines (155 loc) · 4.49 KB
/
db.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package ethereummock
import (
"bytes"
"context"
"math/big"
"sync"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/enclave/core"
)
// Received blocks ar stored here
type blockResolverInMem struct {
blockCache map[common.L1BlockHash]*types.Block
m sync.RWMutex
}
func (n *blockResolverInMem) IsBlockCanonical(ctx context.Context, blockHash common.L1BlockHash) (bool, error) {
// TODO implement me
panic("implement me")
}
func (n *blockResolverInMem) FetchCanonicaBlockByHeight(_ context.Context, _ *big.Int) (*types.Block, error) {
panic("implement me")
}
func (n *blockResolverInMem) Proof(_ context.Context, _ *core.Rollup) (*types.Block, error) {
panic("implement me")
}
func NewResolver() *blockResolverInMem {
return &blockResolverInMem{
blockCache: map[common.L1BlockHash]*types.Block{},
m: sync.RWMutex{},
}
}
func (n *blockResolverInMem) StoreBlock(_ context.Context, block *types.Block, _ *common.ChainFork) error {
n.m.Lock()
defer n.m.Unlock()
n.blockCache[block.Hash()] = block
return nil
}
func (n *blockResolverInMem) FetchBlock(_ context.Context, hash common.L1BlockHash) (*types.Block, error) {
n.m.RLock()
defer n.m.RUnlock()
block, f := n.blockCache[hash]
if !f {
return nil, errutil.ErrNotFound
}
return block, nil
}
func (n *blockResolverInMem) FetchHeadBlock(_ context.Context) (*types.Block, error) {
n.m.RLock()
defer n.m.RUnlock()
var max *types.Block
for k := range n.blockCache {
bh := n.blockCache[k]
if max == nil || max.NumberU64() < bh.NumberU64() {
max = bh
}
}
if max == nil {
return nil, errutil.ErrNotFound
}
return max, nil
}
func (n *blockResolverInMem) ParentBlock(ctx context.Context, b *types.Block) (*types.Block, error) {
return n.FetchBlock(ctx, b.Header().ParentHash)
}
func (n *blockResolverInMem) IsAncestor(ctx context.Context, block *types.Block, maybeAncestor *types.Block) bool {
if bytes.Equal(maybeAncestor.Hash().Bytes(), block.Hash().Bytes()) {
return true
}
if maybeAncestor.NumberU64() >= block.NumberU64() {
return false
}
p, err := n.ParentBlock(ctx, block)
if err != nil {
return false
}
return n.IsAncestor(ctx, p, maybeAncestor)
}
func (n *blockResolverInMem) IsBlockAncestor(ctx context.Context, block *types.Block, maybeAncestor common.L1BlockHash) bool {
if bytes.Equal(maybeAncestor.Bytes(), block.Hash().Bytes()) {
return true
}
if bytes.Equal(maybeAncestor.Bytes(), MockGenesisBlock.Hash().Bytes()) {
return true
}
if block.NumberU64() == common.L1GenesisHeight {
return false
}
resolvedBlock, err := n.FetchBlock(ctx, maybeAncestor)
if err == nil {
if resolvedBlock.NumberU64() >= block.NumberU64() {
return false
}
}
p, err := n.ParentBlock(ctx, block)
if err != nil {
// todo (@tudor) - if error is not `errutil.ErrNotFound`, throw
return false
}
return n.IsBlockAncestor(ctx, p, maybeAncestor)
}
// The cache of included transactions
type txDBInMem struct {
transactionsPerBlockCache map[common.L1BlockHash]map[common.TxHash]*types.Transaction
rpbcM *sync.RWMutex
}
func NewTxDB() TxDB {
return &txDBInMem{
transactionsPerBlockCache: make(map[common.L1BlockHash]map[common.TxHash]*types.Transaction),
rpbcM: &sync.RWMutex{},
}
}
func (n *txDBInMem) Txs(b *types.Block) (map[common.TxHash]*types.Transaction, bool) {
n.rpbcM.RLock()
val, found := n.transactionsPerBlockCache[b.Hash()]
n.rpbcM.RUnlock()
return val, found
}
func (n *txDBInMem) AddTxs(b *types.Block, newMap map[common.TxHash]*types.Transaction) {
n.rpbcM.Lock()
n.transactionsPerBlockCache[b.Hash()] = newMap
n.rpbcM.Unlock()
}
// removeCommittedTransactions returns a copy of `mempool` where all transactions that are exactly `committedBlocks`
// deep have been removed.
func (m *Node) removeCommittedTransactions(
ctx context.Context,
cb *types.Block,
mempool []*types.Transaction,
resolver *blockResolverInMem,
db TxDB,
) []*types.Transaction {
if cb.NumberU64() <= common.HeightCommittedBlocks {
return mempool
}
b := cb
i := 0
for {
if i == common.HeightCommittedBlocks {
break
}
p, err := resolver.FetchBlock(ctx, b.ParentHash())
if err != nil {
m.logger.Crit("Could not retrieve parent block.", log.ErrKey, err)
}
b = p
i++
}
val, _ := db.Txs(b)
//if !found {
// panic("should not fail here")
//}
return removeExisting(mempool, val)
}