-
Notifications
You must be signed in to change notification settings - Fork 199
/
accountDBSyncerContainerFactory.go
191 lines (173 loc) · 6.53 KB
/
accountDBSyncerContainerFactory.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
package factory
import (
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/throttler"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/state/syncer"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/trie"
"github.com/ElrondNetwork/elrond-go/update"
containers "github.com/ElrondNetwork/elrond-go/update/container"
"github.com/ElrondNetwork/elrond-go/update/genesis"
)
// ArgsNewAccountsDBSyncersContainerFactory defines the arguments needed to create accounts DB syncers container
type ArgsNewAccountsDBSyncersContainerFactory struct {
TrieCacher storage.Cacher
RequestHandler update.RequestHandler
ShardCoordinator sharding.Coordinator
Hasher hashing.Hasher
Marshalizer marshal.Marshalizer
TrieStorageManager common.StorageManager
TimoutGettingTrieNode time.Duration
MaxTrieLevelInMemory uint
NumConcurrentTrieSyncers int
MaxHardCapForMissingNodes int
TrieSyncerVersion int
CheckNodesOnDisk bool
AddressPubKeyConverter core.PubkeyConverter
}
type accountDBSyncersContainerFactory struct {
trieCacher storage.Cacher
requestHandler update.RequestHandler
container update.AccountsDBSyncContainer
shardCoordinator sharding.Coordinator
hasher hashing.Hasher
marshalizer marshal.Marshalizer
timeoutGettingTrieNode time.Duration
trieStorageManager common.StorageManager
maxTrieLevelinMemory uint
numConcurrentTrieSyncers int
maxHardCapForMissingNodes int
trieSyncerVersion int
checkNodesOnDisk bool
addressPubKeyConverter core.PubkeyConverter
}
// NewAccountsDBSContainerFactory creates a factory for trie syncers container
func NewAccountsDBSContainerFactory(args ArgsNewAccountsDBSyncersContainerFactory) (*accountDBSyncersContainerFactory, error) {
if check.IfNil(args.RequestHandler) {
return nil, update.ErrNilRequestHandler
}
if check.IfNil(args.ShardCoordinator) {
return nil, update.ErrNilShardCoordinator
}
if check.IfNil(args.TrieCacher) {
return nil, update.ErrNilCacher
}
if check.IfNil(args.Hasher) {
return nil, update.ErrNilHasher
}
if check.IfNil(args.Marshalizer) {
return nil, update.ErrNilMarshalizer
}
if check.IfNil(args.TrieStorageManager) {
return nil, update.ErrNilStorageManager
}
if args.NumConcurrentTrieSyncers < 1 {
return nil, update.ErrInvalidNumConcurrentTrieSyncers
}
if args.MaxHardCapForMissingNodes < 1 {
return nil, update.ErrInvalidMaxHardCapForMissingNodes
}
err := trie.CheckTrieSyncerVersion(args.TrieSyncerVersion)
if err != nil {
return nil, err
}
if check.IfNil(args.AddressPubKeyConverter) {
return nil, update.ErrNilPubKeyConverter
}
t := &accountDBSyncersContainerFactory{
shardCoordinator: args.ShardCoordinator,
trieCacher: args.TrieCacher,
requestHandler: args.RequestHandler,
hasher: args.Hasher,
marshalizer: args.Marshalizer,
trieStorageManager: args.TrieStorageManager,
timeoutGettingTrieNode: args.TimoutGettingTrieNode,
maxTrieLevelinMemory: args.MaxTrieLevelInMemory,
numConcurrentTrieSyncers: args.NumConcurrentTrieSyncers,
maxHardCapForMissingNodes: args.MaxHardCapForMissingNodes,
trieSyncerVersion: args.TrieSyncerVersion,
checkNodesOnDisk: args.CheckNodesOnDisk,
addressPubKeyConverter: args.AddressPubKeyConverter,
}
return t, nil
}
// Create creates all the needed syncers and returns the container
func (a *accountDBSyncersContainerFactory) Create() (update.AccountsDBSyncContainer, error) {
a.container = containers.NewAccountsDBSyncersContainer()
for i := uint32(0); i < a.shardCoordinator.NumberOfShards(); i++ {
err := a.createUserAccountsSyncer(i)
if err != nil {
return nil, err
}
}
err := a.createUserAccountsSyncer(core.MetachainShardId)
if err != nil {
return nil, err
}
err = a.createValidatorAccountsSyncer(core.MetachainShardId)
if err != nil {
return nil, err
}
return a.container, nil
}
func (a *accountDBSyncersContainerFactory) createUserAccountsSyncer(shardId uint32) error {
thr, err := throttler.NewNumGoRoutinesThrottler(int32(a.numConcurrentTrieSyncers))
if err != nil {
return err
}
args := syncer.ArgsNewUserAccountsSyncer{
ArgsNewBaseAccountsSyncer: syncer.ArgsNewBaseAccountsSyncer{
Hasher: a.hasher,
Marshalizer: a.marshalizer,
TrieStorageManager: a.trieStorageManager,
RequestHandler: a.requestHandler,
Timeout: a.timeoutGettingTrieNode,
Cacher: a.trieCacher,
MaxTrieLevelInMemory: a.maxTrieLevelinMemory,
MaxHardCapForMissingNodes: a.maxHardCapForMissingNodes,
TrieSyncerVersion: a.trieSyncerVersion,
CheckNodesOnDisk: a.checkNodesOnDisk,
},
ShardId: shardId,
Throttler: thr,
AddressPubKeyConverter: a.addressPubKeyConverter,
}
accountSyncer, err := syncer.NewUserAccountsSyncer(args)
if err != nil {
return err
}
trieId := genesis.CreateTrieIdentifier(shardId, genesis.UserAccount)
return a.container.Add(trieId, accountSyncer)
}
func (a *accountDBSyncersContainerFactory) createValidatorAccountsSyncer(shardId uint32) error {
args := syncer.ArgsNewValidatorAccountsSyncer{
ArgsNewBaseAccountsSyncer: syncer.ArgsNewBaseAccountsSyncer{
Hasher: a.hasher,
Marshalizer: a.marshalizer,
TrieStorageManager: a.trieStorageManager,
RequestHandler: a.requestHandler,
Timeout: a.timeoutGettingTrieNode,
Cacher: a.trieCacher,
MaxTrieLevelInMemory: a.maxTrieLevelinMemory,
MaxHardCapForMissingNodes: a.maxHardCapForMissingNodes,
TrieSyncerVersion: a.trieSyncerVersion,
CheckNodesOnDisk: a.checkNodesOnDisk,
},
}
accountSyncer, err := syncer.NewValidatorAccountsSyncer(args)
if err != nil {
return err
}
trieId := genesis.CreateTrieIdentifier(shardId, genesis.ValidatorAccount)
return a.container.Add(trieId, accountSyncer)
}
// IsInterfaceNil returns true if the underlying object is nil
func (a *accountDBSyncersContainerFactory) IsInterfaceNil() bool {
return a == nil
}