This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenesis.go
123 lines (101 loc) · 3.12 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
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
package config
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/BOPR/common"
)
type Genesis struct {
StartEthBlock uint64 `json:"startEthBlock"`
MaxTreeDepth uint64 `json:"maxTreeDepth"`
MaxDepositSubTreeHeight uint64 `json:"maxDepositSubTreeHeight"`
StakeAmount uint64 `json:"stakeAmount"`
GenesisAccountCount uint64 `json:"genesisAccountCount"`
// GenesisAccounts GenesisAccounts `json:"genesisAccounts"`
}
// Validate validates the genesis file and checks for basic things
func (g Genesis) Validate() error {
// if int(math.Exp2(float64(g.MaxTreeDepth)))-len(g.GenesisAccounts.Accounts) < 0 {
// return errors.New("More accounts submitted than can be accomodated")
// }
// if len(g.GenesisAccounts.Accounts) < 1 {
// return errors.New("Genesis file must contain atleast coordinator leaf")
// }
// if !g.GenesisAccounts.Accounts[0].IsCoordinator() {
// return errors.New("First account in the genesis file should be the coordinator")
// }
return nil
}
// GenUserAccount exists to allow remove circular dependency with types
// and to allow storing more data about the account than the data in UserAccount
type GenUserAccount struct {
ID uint64 `json:"ID"`
Balance uint64
TokenType uint64
Nonce uint64
Status uint64
PublicKey string
}
func (acc *GenUserAccount) IsCoordinator() bool {
if acc.ID != 0 || acc.Balance != 0 || acc.TokenType != 0 || acc.Nonce != 0 || acc.Status != 1 {
return false
}
return true
}
func NewGenUserAccount(id, balance, tokenType, nonce, status uint64, publicKey string) GenUserAccount {
return GenUserAccount{
ID: id,
Balance: balance,
TokenType: tokenType,
Nonce: nonce,
Status: status,
PublicKey: publicKey,
}
}
type GenesisAccounts struct {
Accounts []GenUserAccount `json:"gen_accounts"`
}
func NewGenesisAccounts(accounts []GenUserAccount) GenesisAccounts {
return GenesisAccounts{Accounts: accounts}
}
func EmptyGenesisAccount() GenUserAccount {
return NewGenUserAccount(0, 0, 0, 0, 100, "")
}
func DefaultGenesisAccounts() GenesisAccounts {
var accounts []GenUserAccount
// add coordinator account
acc := NewGenUserAccount(common.COORDINATOR, common.COORDINATOR, common.COORDINATOR, common.COORDINATOR, 1, "0")
accounts = append(accounts, acc)
return NewGenesisAccounts(accounts)
}
func DefaultGenesis() Genesis {
return Genesis{
StartEthBlock: 0,
MaxTreeDepth: common.DEFAULT_DEPTH,
MaxDepositSubTreeHeight: common.DEFAULT_DEPTH,
StakeAmount: 32,
GenesisAccountCount: 2,
// GenesisAccounts: DefaultGenesisAccounts(),
}
}
func ReadGenesisFile() (Genesis, error) {
var genesis Genesis
genesisFile, err := os.Open("genesis.json")
if err != nil {
return genesis, err
}
defer genesisFile.Close()
genBytes, err := ioutil.ReadAll(genesisFile)
if err != nil {
return genesis, err
}
err = json.Unmarshal(genBytes, &genesis)
return genesis, err
}
func WriteGenesisFile(genesis Genesis) error {
bz, err := json.MarshalIndent(genesis, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile("genesis.json", bz, 0644)
}