-
Notifications
You must be signed in to change notification settings - Fork 199
/
genesis.go
95 lines (78 loc) · 2.6 KB
/
genesis.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
package sharding
import (
"fmt"
"math/big"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data/state"
)
var log = logger.GetOrCreate("sharding")
// InitialBalance holds data from json and decoded data from genesis process
type InitialBalance struct {
PubKey string `json:"pubkey"`
Balance string `json:"balance"`
pubKey []byte
balance *big.Int
}
// Genesis hold data for decoded data from json file
type Genesis struct {
pubkeyConverter state.PubkeyConverter
InitialBalances []*InitialBalance `json:"initialBalances"`
}
// NewGenesisConfig creates a new decoded genesis structure from json config file
func NewGenesisConfig(genesisFilePath string, pubkeyConverter state.PubkeyConverter) (*Genesis, error) {
if check.IfNil(pubkeyConverter) {
return nil, fmt.Errorf("%w for NewGenesisConfig", ErrNilPubkeyConverter)
}
genesis := &Genesis{
pubkeyConverter: pubkeyConverter,
}
err := core.LoadJsonFile(genesis, genesisFilePath)
if err != nil {
return nil, err
}
err = genesis.processConfig()
if err != nil {
return nil, err
}
return genesis, nil
}
func (g *Genesis) processConfig() error {
var err error
var ok bool
for i := 0; i < len(g.InitialBalances); i++ {
g.InitialBalances[i].pubKey, err = g.pubkeyConverter.Decode(g.InitialBalances[i].PubKey)
// decoder treats empty string as correct, it is not allowed to have empty string as public key
if g.InitialBalances[i].PubKey == "" || err != nil {
g.InitialBalances[i].pubKey = nil
return fmt.Errorf("%w for pubkey %s", ErrCouldNotParsePubKey, g.InitialBalances[i].PubKey)
}
g.InitialBalances[i].balance, ok = new(big.Int).SetString(g.InitialBalances[i].Balance, 10)
if !ok {
log.Debug("error decoding balance for public key - setting to 0",
"balance", g.InitialBalances[i].Balance,
"pubkey", g.InitialBalances[i].PubKey)
g.InitialBalances[i].balance = big.NewInt(0)
}
}
return nil
}
// InitialNodesBalances - gets the initial balances of the nodes
func (g *Genesis) InitialNodesBalances(shardCoordinator Coordinator) (map[string]*big.Int, error) {
if check.IfNil(shardCoordinator) {
return nil, ErrNilShardCoordinator
}
var balances = make(map[string]*big.Int)
for _, in := range g.InitialBalances {
address, err := g.pubkeyConverter.CreateAddressFromBytes(in.pubKey)
if err != nil {
return nil, err
}
addressShard := shardCoordinator.ComputeId(address)
if addressShard == shardCoordinator.SelfId() {
balances[string(in.pubKey)] = in.balance
}
}
return balances, nil
}