-
Notifications
You must be signed in to change notification settings - Fork 198
/
dataTrieFactory.go
124 lines (105 loc) · 3.59 KB
/
dataTrieFactory.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
package factory
import (
"path"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"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/config"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/state"
storageFactory "github.com/ElrondNetwork/elrond-go/storage/factory"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
"github.com/ElrondNetwork/elrond-go/trie"
"github.com/ElrondNetwork/elrond-go/update"
"github.com/ElrondNetwork/elrond-go/update/genesis"
)
// ArgsNewDataTrieFactory is the argument structure for the new data trie factory
type ArgsNewDataTrieFactory struct {
StorageConfig config.StorageConfig
SyncFolder string
Marshalizer marshal.Marshalizer
Hasher hashing.Hasher
ShardCoordinator sharding.Coordinator
MaxTrieLevelInMemory uint
}
type dataTrieFactory struct {
shardCoordinator sharding.Coordinator
trieStorage common.StorageManager
marshalizer marshal.Marshalizer
hasher hashing.Hasher
maxTrieLevelInMemory uint
}
// NewDataTrieFactory creates a data trie factory
func NewDataTrieFactory(args ArgsNewDataTrieFactory) (*dataTrieFactory, error) {
if len(args.SyncFolder) < 2 {
return nil, update.ErrInvalidFolderName
}
if check.IfNil(args.ShardCoordinator) {
return nil, update.ErrNilShardCoordinator
}
if check.IfNil(args.Marshalizer) {
return nil, update.ErrNilMarshalizer
}
if check.IfNil(args.Hasher) {
return nil, update.ErrNilHasher
}
dbConfig := storageFactory.GetDBFromConfig(args.StorageConfig.DB)
dbConfig.FilePath = path.Join(args.SyncFolder, args.StorageConfig.DB.FilePath)
accountsTrieStorage, err := storageUnit.NewStorageUnitFromConf(
storageFactory.GetCacherFromConfig(args.StorageConfig.Cache),
dbConfig,
)
if err != nil {
return nil, err
}
trieStorage, err := trie.NewTrieStorageManagerWithoutPruning(accountsTrieStorage)
if err != nil {
return nil, err
}
d := &dataTrieFactory{
shardCoordinator: args.ShardCoordinator,
trieStorage: trieStorage,
marshalizer: args.Marshalizer,
hasher: args.Hasher,
maxTrieLevelInMemory: args.MaxTrieLevelInMemory,
}
return d, nil
}
// TrieStorageManager returns trie storage manager
func (d *dataTrieFactory) TrieStorageManager() common.StorageManager {
return d.trieStorage
}
// Create creates a TriesHolder container to hold all the states
func (d *dataTrieFactory) Create() (common.TriesHolder, error) {
container := state.NewDataTriesHolder()
for i := uint32(0); i < d.shardCoordinator.NumberOfShards(); i++ {
err := d.createAndAddOneTrie(i, genesis.UserAccount, container)
if err != nil {
return nil, err
}
}
err := d.createAndAddOneTrie(core.MetachainShardId, genesis.UserAccount, container)
if err != nil {
return nil, err
}
err = d.createAndAddOneTrie(core.MetachainShardId, genesis.ValidatorAccount, container)
if err != nil {
return nil, err
}
return container, nil
}
func (d *dataTrieFactory) createAndAddOneTrie(shId uint32, accType genesis.Type, container common.TriesHolder) error {
dataTrie, err := trie.NewTrie(d.trieStorage, d.marshalizer, d.hasher, d.maxTrieLevelInMemory)
if err != nil {
return err
}
trieId := genesis.CreateTrieIdentifier(shId, accType)
container.Put([]byte(trieId), dataTrie)
return nil
}
// IsInterfaceNil returns true if underlying object is nil
func (d *dataTrieFactory) IsInterfaceNil() bool {
return d == nil
}