Skip to content
Snippets Groups Projects
Unverified Commit 804cadcf authored by Benjamin Bollen's avatar Benjamin Bollen
Browse files

genesis: MakeGenesisDocFromAccountsAndValidators

parent aaf27e9b
No related branches found
No related tags found
No related merge requests found
......@@ -24,12 +24,12 @@ import (
wire "github.com/tendermint/go-wire"
)
// MakeGenesisDocFromAccounts takes a chainName and a slice of GenesisAccount,
// and a slice of GenesisValidator to construct a GenesisDoc, or returns an error on
// MakeGenesisDocFromAccounts takes a chainName and a slice of pointers to GenesisAccount,
// and a slice of pointers to GenesisValidator 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, accounts []*GenesisAccount, validators []*GenesisValidator) (
GenesisDoc, error) {
func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount,
validators []*GenesisValidator) (GenesisDoc, error) {
// TODO: assert valid accounts and validators
// TODO: [ben] expose setting global permissions
......@@ -37,18 +37,28 @@ func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, va
genesisParameters := &GenesisParams{
GlobalPermissions: &globalPermissions,
}
// copy slices of validators and accounts
// copy slice of pointers to accounts into slice of accounts
accountsCopy := make([]GenesisAccount, len(accounts))
copy(accountsCopy, accounts)
for _, genesisAccount := range accounts {
accountsCopy = append(accountsCopy, genesisAccount.Clone())
}
// copy slice of pointers to validators into slice of validators
validatorsCopy := make([]GenesisValidator, len(validators))
for _, genesisValidator := range validators {
genesisValidatorCopy, err := genesisValidator.Clone()
if err != nil {
return GenesisDoc{}, err
}
validatorsCopy = append(validatorsCopy, genesisValidatorCopy)
}
genesisDoc := GenesisDoc{
GenesisTime: time.Now(),
// TODO: this needs to be corrected for ChainName, and ChainId
// is the derived hash from the GenesisDoc serialised bytes
ChainID: chainName,
Params: genesisParameters,
Accounts: accounts,
Validators: validators,
Accounts: accountsCopy,
Validators: validatorsCopy,
}
return genesisDoc, nil
}
......@@ -59,9 +69,9 @@ func MakeGenesisDocFromAccounts(chainName string, accounts []*GenesisAccount, va
func GetGenesisFileBytes(genesisDoc *GenesisDoc) ([]byte, error) {
// TODO: write JSON in canonical order
buffer, n, err := new(bytes.Buffer), new(int), new(error)
// write JSON with go-wire type-bytes (for public keys); deprecate
var err error
buffer, n := new(bytes.Buffer), new(int)
// write JSON with go-wire type-bytes (for public keys)
wire.WriteJSON(genesisDoc, buffer, n, &err)
if err != nil {
return nil, err
......
// Copyright 2015-2017 Monax Industries Limited.
// This file is part of the Monax platform (Monax)
// Monax is free software: you can use, redistribute it and/or modify
// it only under the terms of the GNU General Public License, version
// 3, as published by the Free Software Foundation.
// Monax is distributed WITHOUT ANY WARRANTY pursuant to
// the terms of the Gnu General Public Licence, version 3, including
// (but not limited to) Clause 15 thereof. See the text of the
// GNU General Public License, version 3 for full terms.
// You should have received a copy of the GNU General Public License,
// version 3, with Monax. If not, see <http://www.gnu.org/licenses/>.
package genesis
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestGenesisFileBytes(t *testing.T) {
}
......@@ -21,6 +21,7 @@ import (
"time"
ptypes "github.com/eris-ltd/eris-db/permission/types"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
)
......@@ -121,17 +122,24 @@ func (genesisAccount *GenesisAccount) Clone() GenesisAccount {
// GenesisValidator methods
// Clone clones the genesis validator
func (genesisValidator *GenesisValidator) Clone() GenesisValidator {
// clone the public key
// clone the account permissions
accountPermissionsClone := genesisAccount.Permissions.Clone()
return GenesisAccount{
Address: addressClone,
Amount: genesisAccount.amount,
Name: genesisAccount.Name,
Permissions: &accountPermissionsClone,
func (genesisValidator *GenesisValidator) Clone() (GenesisValidator, error) {
// clone the public key by writing and reading over go-wire serialisation
// TODO! write unit test to see whether this is correct
publicKeyClone, err := crypto.PubKeyFromBytes(genesisValidator.PubKey.Bytes())
if err != nil {
return GenesisValidator{}, err
}
// clone the addresses to unbond to
unbondToClone := make([]BasicAccount, len(genesisValidator.UnbondTo))
for _, basicAccount := range genesisValidator.UnbondTo {
unbondToClone = append(unbondToClone, basicAccount.Clone())
}
return GenesisValidator{
PubKey: publicKeyClone,
Amount: genesisValidator.Amount,
Name: genesisValidator.Name,
UnbondTo: unbondToClone,
}, nil
}
//------------------------------------------------------------
......@@ -142,8 +150,8 @@ func (basicAccount *BasicAccount) Clone() BasicAccount {
// clone the address
addressClone := make([]byte, len(basicAccount.Address))
copy(addressClone, basicAccount.Address)
return GenesisAccount{
Address: addressClone,
Amount: basicAccount.Amount,
return BasicAccount{
Address: addressClone,
Amount: basicAccount.Amount,
}
}
\ No newline at end of file
}
......@@ -19,7 +19,7 @@ import (
"github.com/eris-ltd/eris-db/txs"
"github.com/eris-ltd/eris-db/word256"
state_types "github.com/eris-ltd/eris-db/manager/eris-mint/state/types"
genesis "github.com/eris-ltd/eris-db/genesis"
"github.com/spf13/viper"
"github.com/tendermint/go-crypto"
rpcclient "github.com/tendermint/go-rpc/client"
......@@ -33,7 +33,7 @@ var (
mempoolCount = 0
chainID string
websocketAddr string
genesisDoc *state_types.GenesisDoc
genesisDoc *genesis.GenesisDoc
websocketEndpoint string
user = makeUsers(5) // make keys
jsonRpcClient rpcclient.Client
......@@ -48,7 +48,7 @@ func initGlobalVariables(ffs *fixtures.FileFixtures) error {
rootWorkDir = ffs.AddDir("rootWorkDir")
rootDataDir := ffs.AddDir("rootDataDir")
genesisFile := ffs.AddFile("rootWorkDir/genesis.json", defaultGenesis)
genesisDoc = state_types.GenesisDocFromJSON([]byte(defaultGenesis))
genesisDoc = genesis.GenesisDocFromJSON([]byte(defaultGenesis))
if ffs.Error != nil {
return ffs.Error
......
......@@ -5,7 +5,7 @@ import (
consensus_types "github.com/eris-ltd/eris-db/consensus/types"
core_types "github.com/eris-ltd/eris-db/core/types"
event "github.com/eris-ltd/eris-db/event"
stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types"
genesis "github.com/eris-ltd/eris-db/genesis"
rpc_v0 "github.com/eris-ltd/eris-db/rpc/v0"
"github.com/eris-ltd/eris-db/rpc/v0/shared"
transaction "github.com/eris-ltd/eris-db/txs"
......@@ -540,7 +540,7 @@ var serverDuration uint = 100
type (
ChainData struct {
PrivValidator *mintTypes.PrivValidator `json:"priv_validator"`
Genesis *stypes.GenesisDoc `json:"genesis"`
Genesis *genesis.GenesisDoc `json:"genesis"`
}
GetAccountData struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment