-
Notifications
You must be signed in to change notification settings - Fork 245
/
blockchainstate.go
149 lines (131 loc) · 3.75 KB
/
blockchainstate.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
package wallet
import (
"context"
"sync"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/wallet/async"
"github.com/status-im/status-go/services/wallet/common"
)
const (
fetchLatestBlockNumbersInterval = 10 * time.Minute
)
type fetchLatestBlockNumberCommand struct {
state *BlockChainState
rpcClient *rpc.Client
accountsDB *accounts.Database
}
func (c *fetchLatestBlockNumberCommand) Command() async.Command {
return async.InfiniteCommand{
Interval: fetchLatestBlockNumbersInterval,
Runable: c.Run,
}.Run
}
func (c *fetchLatestBlockNumberCommand) Run(parent context.Context) (err error) {
log.Debug("start fetchLatestBlockNumberCommand")
networks, err := c.rpcClient.NetworkManager.Get(false)
if err != nil {
return nil
}
areTestNetworksEnabled, err := c.accountsDB.GetTestNetworksEnabled()
if err != nil {
return
}
ctx := context.Background()
for _, network := range networks {
if network.IsTest != areTestNetworksEnabled {
continue
}
_, _ = c.state.fetchLatestBlockNumber(ctx, network.ChainID)
}
return nil
}
type LatestBlockData struct {
blockNumber uint64
timestamp time.Time
blockDuration time.Duration
}
type BlockChainState struct {
rpcClient *rpc.Client
accountsDB *accounts.Database
blkMu sync.RWMutex
latestBlockNumbers map[uint64]LatestBlockData
group *async.Group
cancelFn context.CancelFunc
sinceFn func(time.Time) time.Duration
}
func NewBlockChainState(rpcClient *rpc.Client, accountsDb *accounts.Database) *BlockChainState {
return &BlockChainState{
rpcClient: rpcClient,
accountsDB: accountsDb,
blkMu: sync.RWMutex{},
latestBlockNumbers: make(map[uint64]LatestBlockData),
sinceFn: time.Since,
}
}
func (s *BlockChainState) Start() {
ctx, cancel := context.WithCancel(context.Background())
s.cancelFn = cancel
s.group = async.NewGroup(ctx)
command := &fetchLatestBlockNumberCommand{
state: s,
accountsDB: s.accountsDB,
rpcClient: s.rpcClient,
}
s.group.Add(command.Command())
}
func (s *BlockChainState) Stop() {
if s.cancelFn != nil {
s.cancelFn()
s.cancelFn = nil
}
if s.group != nil {
s.group.Stop()
s.group.Wait()
s.group = nil
}
}
func (s *BlockChainState) GetEstimatedLatestBlockNumber(ctx context.Context, chainID uint64) (uint64, error) {
blockNumber, ok := s.estimateLatestBlockNumber(chainID)
if ok {
return blockNumber, nil
}
return s.fetchLatestBlockNumber(ctx, chainID)
}
func (s *BlockChainState) fetchLatestBlockNumber(ctx context.Context, chainID uint64) (uint64, error) {
client, err := s.rpcClient.EthClient(chainID)
if err != nil {
return 0, err
}
blockNumber, err := client.BlockNumber(ctx)
if err != nil {
return 0, err
}
blockDuration, found := common.AverageBlockDurationForChain[common.ChainID(chainID)]
if !found {
blockDuration = common.AverageBlockDurationForChain[common.ChainID(common.UnknownChainID)]
}
s.setLatestBlockDataForChain(chainID, LatestBlockData{
blockNumber: blockNumber,
timestamp: time.Now(),
blockDuration: blockDuration,
})
return blockNumber, nil
}
func (s *BlockChainState) setLatestBlockDataForChain(chainID uint64, latestBlockData LatestBlockData) {
s.blkMu.Lock()
defer s.blkMu.Unlock()
s.latestBlockNumbers[chainID] = latestBlockData
}
func (s *BlockChainState) estimateLatestBlockNumber(chainID uint64) (uint64, bool) {
s.blkMu.RLock()
defer s.blkMu.RUnlock()
blockData, ok := s.latestBlockNumbers[chainID]
if !ok {
return 0, false
}
timeDiff := s.sinceFn(blockData.timestamp)
return blockData.blockNumber + uint64((timeDiff / blockData.blockDuration)), true
}