-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_syncer.go
172 lines (152 loc) · 5.81 KB
/
atomic_syncer.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
// (c) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"github.com/coinflect/coinflectchain/database/versiondb"
"github.com/coinflect/coinflectchain/utils/wrappers"
"github.com/coinflect/coreth/plugin/evm/message"
syncclient "github.com/coinflect/coreth/sync/client"
"github.com/coinflect/coreth/trie"
"github.com/ethereum/go-ethereum/common"
)
var (
_ Syncer = &atomicSyncer{}
_ syncclient.LeafSyncTask = &atomicSyncerLeafTask{}
)
// atomicSyncer is used to sync the atomic trie from the network. The CallbackLeafSyncer
// is responsible for orchestrating the sync while atomicSyncer is responsible for maintaining
// the state of progress and writing the actual atomic trie to the trieDB.
type atomicSyncer struct {
db *versiondb.Database
atomicTrie AtomicTrie
trie *trie.Trie // used to update the atomic trie
targetRoot common.Hash
targetHeight uint64
// syncer is used to sync leaves from the network.
syncer *syncclient.CallbackLeafSyncer
// nextHeight is the height which key / values
// are being inserted into [atomicTrie] for
nextHeight uint64
}
// addZeros adds [common.HashLenth] zeros to [height] and returns the result as []byte
func addZeroes(height uint64) []byte {
packer := wrappers.Packer{Bytes: make([]byte, atomicKeyLength)}
packer.PackLong(height)
packer.PackFixedBytes(bytes.Repeat([]byte{0x00}, common.HashLength))
return packer.Bytes
}
func newAtomicSyncer(client syncclient.LeafClient, atomicBackend *atomicBackend, targetRoot common.Hash, targetHeight uint64) (*atomicSyncer, error) {
atomicTrie := atomicBackend.AtomicTrie()
lastCommittedRoot, lastCommit := atomicTrie.LastCommitted()
trie, err := atomicTrie.OpenTrie(lastCommittedRoot)
if err != nil {
return nil, err
}
atomicSyncer := &atomicSyncer{
db: atomicBackend.db,
atomicTrie: atomicTrie,
trie: trie,
targetRoot: targetRoot,
targetHeight: targetHeight,
nextHeight: lastCommit + 1,
}
tasks := make(chan syncclient.LeafSyncTask, 1)
tasks <- &atomicSyncerLeafTask{atomicSyncer: atomicSyncer}
close(tasks)
atomicSyncer.syncer = syncclient.NewCallbackLeafSyncer(client, tasks)
return atomicSyncer, nil
}
// Start begins syncing the target atomic root.
func (s *atomicSyncer) Start(ctx context.Context) error {
s.syncer.Start(ctx, 1, s.onSyncFailure)
return nil
}
// onLeafs is the callback for the leaf syncer, which will insert the key-value pairs into the trie.
func (s *atomicSyncer) onLeafs(keys [][]byte, values [][]byte) error {
_, lastCommittedHeight := s.atomicTrie.LastCommitted()
lastHeight := lastCommittedHeight // track heights so we calculate roots after each height
for i, key := range keys {
if len(key) != atomicKeyLength {
return fmt.Errorf("unexpected key len (%d) in atomic trie sync", len(key))
}
// key = height + blockchainID
height := binary.BigEndian.Uint64(key[:wrappers.LongLen])
if height > lastHeight {
// If this key belongs to a new height, we commit
// the trie at the previous height before adding this key.
root, nodes, err := s.trie.Commit(false)
if err != nil {
return err
}
if err := s.atomicTrie.InsertTrie(nodes, root); err != nil {
return err
}
// AcceptTrie commits the trieDB and returns [isCommit] as true
// if we have reached or crossed a commit interval.
isCommit, err := s.atomicTrie.AcceptTrie(lastHeight, root)
if err != nil {
return err
}
if isCommit {
// Flush pending changes to disk to preserve progress and
// free up memory if the trieDB was committed.
if err := s.db.Commit(); err != nil {
return err
}
}
lastHeight = height
}
if err := s.trie.TryUpdate(key, values[i]); err != nil {
return err
}
}
return nil
}
// onFinish is called when sync for this trie is complete.
// commit the trie to disk and perform the final checks that we synced the target root correctly.
func (s *atomicSyncer) onFinish() error {
// commit the trie on finish
root, nodes, err := s.trie.Commit(false)
if err != nil {
return err
}
if err := s.atomicTrie.InsertTrie(nodes, root); err != nil {
return err
}
if _, err := s.atomicTrie.AcceptTrie(s.targetHeight, root); err != nil {
return err
}
if err := s.db.Commit(); err != nil {
return err
}
// the root of the trie should always match the targetRoot since we already verified the proofs,
// here we check the root mainly for correctness of the atomicTrie's pointers and it should never fail.
if s.targetRoot != root {
return fmt.Errorf("synced root (%s) does not match expected (%s) for atomic trie ", root, s.targetRoot)
}
return nil
}
// onSyncFailure is a no-op since we flush progress to disk at the regular commit interval when syncing
// the atomic trie.
func (s *atomicSyncer) onSyncFailure(error) error {
return nil
}
// Done returns a channel which produces any error that occurred during syncing or nil on success.
func (s *atomicSyncer) Done() <-chan error { return s.syncer.Done() }
type atomicSyncerLeafTask struct {
atomicSyncer *atomicSyncer
}
func (a *atomicSyncerLeafTask) Start() []byte { return addZeroes(a.atomicSyncer.nextHeight) }
func (a *atomicSyncerLeafTask) End() []byte { return nil }
func (a *atomicSyncerLeafTask) NodeType() message.NodeType { return message.AtomicTrieNode }
func (a *atomicSyncerLeafTask) OnFinish() error { return a.atomicSyncer.onFinish() }
func (a *atomicSyncerLeafTask) OnStart() (bool, error) { return false, nil }
func (a *atomicSyncerLeafTask) Root() common.Hash { return a.atomicSyncer.targetRoot }
func (a *atomicSyncerLeafTask) Account() common.Hash { return common.Hash{} }
func (a *atomicSyncerLeafTask) OnLeafs(keys, vals [][]byte) error {
return a.atomicSyncer.onLeafs(keys, vals)
}