-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils.go
58 lines (51 loc) · 1.53 KB
/
utils.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
package ethereummock
import (
"context"
"fmt"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ethereum/go-ethereum/core/types"
)
// findNotIncludedTxs - given a list of transactions, it keeps only the ones that were not included in the block
// todo (#1491) - inefficient
func findNotIncludedTxs(head *types.Block, txs []*types.Transaction, r *blockResolverInMem, db TxDB) []*types.Transaction {
included := allIncludedTransactions(head, r, db)
return removeExisting(txs, included)
}
func allIncludedTransactions(b *types.Block, r *blockResolverInMem, db TxDB) map[common.TxHash]*types.Transaction {
val, found := db.Txs(b)
if found {
return val
}
if b.NumberU64() == common.L1GenesisHeight {
return makeMap(b.Transactions())
}
newMap := make(map[common.TxHash]*types.Transaction)
p, err := r.FetchBlock(context.Background(), b.ParentHash())
if err != nil {
panic(fmt.Errorf("should not happen. Could not retrieve parent. Cause: %w", err))
}
for k, v := range allIncludedTransactions(p, r, db) {
newMap[k] = v
}
for _, tx := range b.Transactions() {
newMap[tx.Hash()] = tx
}
db.AddTxs(b, newMap)
return newMap
}
func removeExisting(base []*types.Transaction, toRemove map[common.TxHash]*types.Transaction) (r []*types.Transaction) {
for _, t := range base {
_, f := toRemove[t.Hash()]
if !f {
r = append(r, t)
}
}
return
}
func makeMap(txs types.Transactions) map[common.TxHash]*types.Transaction {
m := make(map[common.TxHash]*types.Transaction)
for _, tx := range txs {
m[tx.Hash()] = tx
}
return m
}