Newer
Older
// 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.
"crypto/sha256"
"fmt"
"sort"
acm "github.com/hyperledger/burrow/account"
"github.com/hyperledger/burrow/permission"
ptypes "github.com/hyperledger/burrow/permission/types"
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
// 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 acm.Address
PublicKey acm.PublicKey
Amount uint64
}
type Account struct {
BasicAccount
Name string
Permissions ptypes.AccountPermissions
}
type Validator struct {
BasicAccount
Name string
UnbondTo []BasicAccount
}
//------------------------------------------------------------
// GenesisDoc is stored in the state database
type GenesisDoc struct {
GenesisTime time.Time
ChainName string
Salt []byte `json:",omitempty"`
GlobalPermissions ptypes.AccountPermissions
Accounts []Account
Validators []Validator
}
// JSONBytes returns the JSON (not-yet) canonical bytes for a given
// GenesisDoc or an error.
func (genesisDoc *GenesisDoc) JSONBytes() ([]byte, error) {
// TODO: write JSON in canonical order
return json.MarshalIndent(genesisDoc, "", "\t")
}
func (genesisDoc *GenesisDoc) Hash() []byte {
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 {
return fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
}
//------------------------------------------------------------
// 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)
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(),
//------------------------------------------------------------
// Validator methods
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
func (gv *Validator) Validator() acm.Validator {
return acm.ConcreteValidator{
Address: gv.PublicKey.Address(),
PublicKey: gv.PublicKey,
Power: uint64(gv.Amount),
}.Validator()
}
// 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]acm.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,