This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
genesis.go
282 lines (245 loc) · 7.8 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package genesis
import (
"crypto/sha256"
"encoding/json"
"fmt"
"sort"
"time"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/validator"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/permission"
)
// How many bytes to take from the front of the GenesisDoc hash to append
// to the ChainName to form the ChainID. The idea is to avoid some classes
// of replay attack between chains with the same name.
const ShortHashSuffixBytes = 3
//------------------------------------------------------------
// core types for a genesis definition
type BasicAccount struct {
// Address is convenient to have in file for reference, but otherwise ignored since derived from PublicKey
Address crypto.Address
PublicKey crypto.PublicKey
Amount uint64
}
type Account struct {
BasicAccount
Name string
Permissions permission.AccountPermissions
}
type Validator struct {
BasicAccount
Name string
UnbondTo []BasicAccount
}
const DefaultProposalThreshold uint64 = 3
var DefaultPermissionsAccount = PermissionsAccount(permission.DefaultAccountPermissions)
type params struct {
ProposalThreshold uint64
}
type GenesisDoc struct {
GenesisTime time.Time
ChainName string
AppHash binary.HexBytes `json:",omitempty" toml:",omitempty"`
Params params `json:",omitempty" toml:",omitempty"`
Salt []byte `json:",omitempty" toml:",omitempty"`
GlobalPermissions permission.AccountPermissions
Accounts []Account
Validators []Validator
// memo
hash []byte
chainID string
}
func (genesisDoc *GenesisDoc) GlobalPermissionsAccount() *acm.Account {
return PermissionsAccount(genesisDoc.GlobalPermissions)
}
func PermissionsAccount(globalPerms permission.AccountPermissions) *acm.Account {
// Ensure the set bits are all true otherwise the HasPermission() functions will fail
globalPerms.Base.SetBit = permission.AllPermFlags
return &acm.Account{
Address: acm.GlobalPermissionsAddress,
Balance: 1337,
Permissions: globalPerms,
}
}
func (genesisDoc *GenesisDoc) JSONString() string {
bs, err := genesisDoc.JSONBytes()
if err != nil {
return fmt.Sprintf("error marshalling GenesisDoc: %v", err)
}
return string(bs)
}
// JSONBytes returns the JSON canonical bytes for a given GenesisDoc or an error.
func (genesisDoc *GenesisDoc) JSONBytes() ([]byte, error) {
// Just in case
genesisDoc.GenesisTime = genesisDoc.GenesisTime.UTC()
return json.MarshalIndent(genesisDoc, "", "\t")
}
func (genesisDoc *GenesisDoc) Hash() []byte {
if genesisDoc.hash != nil {
return genesisDoc.hash
}
genesisDocBytes, err := genesisDoc.JSONBytes()
if err != nil {
panic(fmt.Errorf("could not create hash of GenesisDoc: %v", err))
}
hasher := sha256.New()
hasher.Write(genesisDocBytes)
return hasher.Sum(nil)
}
func (genesisDoc *GenesisDoc) ShortHash() []byte {
return genesisDoc.Hash()[:ShortHashSuffixBytes]
}
func (genesisDoc *GenesisDoc) ChainID() string {
if genesisDoc.chainID == "" {
genesisDoc.chainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
}
return genesisDoc.chainID
}
//------------------------------------------------------------
// Make genesis state from file
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
genDoc := new(GenesisDoc)
err := json.Unmarshal(jsonBlob, genDoc)
if err != nil {
return nil, fmt.Errorf("couldn't read GenesisDoc: %v", err)
}
if len(genDoc.AppHash) != 0 {
genDoc.hash = genDoc.AppHash
}
return genDoc, nil
}
//------------------------------------------------------------
// Account methods
func GenesisAccountFromAccount(name string, account *acm.Account) Account {
return Account{
Name: name,
Permissions: account.Permissions,
BasicAccount: BasicAccount{
Address: account.Address,
Amount: account.Balance,
},
}
}
// Clone clones the genesis account
func (genesisAccount *Account) Clone() Account {
// clone the account permissions
return Account{
BasicAccount: BasicAccount{
Address: genesisAccount.Address,
Amount: genesisAccount.Amount,
},
Name: genesisAccount.Name,
Permissions: genesisAccount.Permissions.Clone(),
}
}
func (genesisAccount *Account) AcmAccount() *acm.Account {
return &acm.Account{
Address: genesisAccount.Address,
PublicKey: genesisAccount.PublicKey,
Balance: genesisAccount.Amount,
Permissions: genesisAccount.Permissions,
}
}
//------------------------------------------------------------
// Validator methods
func (gv *Validator) Validator() validator.Validator {
address := gv.PublicKey.GetAddress()
return validator.Validator{
Address: &address,
PublicKey: gv.PublicKey,
Power: uint64(gv.Amount),
}
}
// Clone clones the genesis validator
func (gv *Validator) Clone() Validator {
// clone the addresses to unbond to
unbondToClone := make([]BasicAccount, len(gv.UnbondTo))
for i, basicAccount := range gv.UnbondTo {
unbondToClone[i] = basicAccount.Clone()
}
return Validator{
BasicAccount: BasicAccount{
PublicKey: gv.PublicKey,
Amount: gv.Amount,
},
Name: gv.Name,
UnbondTo: unbondToClone,
}
}
//------------------------------------------------------------
// BasicAccount methods
// Clone clones the basic account
func (basicAccount *BasicAccount) Clone() BasicAccount {
return BasicAccount{
Address: basicAccount.Address,
Amount: basicAccount.Amount,
}
}
// MakeGenesisDocFromAccounts takes a chainName and a slice of pointers to Account,
// and a slice of pointers to Validator to construct a GenesisDoc, or returns an error on
// failure. In particular MakeGenesisDocFromAccount uses the local time as a
// timestamp for the GenesisDoc.
func MakeGenesisDocFromAccounts(chainName string, salt []byte, genesisTime time.Time, accounts map[string]*acm.Account,
validators map[string]*validator.Validator) *GenesisDoc {
// Establish deterministic order of accounts by name so we obtain identical GenesisDoc
// from identical input
names := make([]string, 0, len(accounts))
for name := range accounts {
names = append(names, name)
}
sort.Strings(names)
// copy slice of pointers to accounts into slice of accounts
genesisAccounts := make([]Account, 0, len(accounts))
for _, name := range names {
genesisAccounts = append(genesisAccounts, GenesisAccountFromAccount(name, accounts[name]))
}
// Sigh...
names = names[:0]
for name := range validators {
names = append(names, name)
}
sort.Strings(names)
// copy slice of pointers to validators into slice of validators
genesisValidators := make([]Validator, 0, len(validators))
for _, name := range names {
val := validators[name]
genesisValidators = append(genesisValidators, Validator{
Name: name,
BasicAccount: BasicAccount{
Address: *val.Address,
PublicKey: val.PublicKey,
Amount: val.Power,
},
// Simpler to just do this by convention
UnbondTo: []BasicAccount{
{
Amount: val.Power,
Address: *val.Address,
},
},
})
}
return &GenesisDoc{
ChainName: chainName,
Salt: salt,
GenesisTime: genesisTime,
GlobalPermissions: permission.DefaultAccountPermissions.Clone(),
Accounts: genesisAccounts,
Validators: genesisValidators,
}
}