mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-12 04:41:22 +00:00
refactor some state functions into state/types
This commit is contained in:
@ -473,3 +473,11 @@ func MakeGenesisState(db dbm.DB, genDoc *GenesisDoc) *State {
|
|||||||
nameReg: nameReg,
|
nameReg: nameReg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*acm.PrivAccount, []*types.PrivValidator) {
|
||||||
|
db := dbm.NewMemDB()
|
||||||
|
genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded)
|
||||||
|
s0 := MakeGenesisState(db, genDoc)
|
||||||
|
s0.Save()
|
||||||
|
return s0, privAccounts, privValidators
|
||||||
|
}
|
||||||
|
@ -1,76 +0,0 @@
|
|||||||
package state
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
acm "github.com/tendermint/tendermint/account"
|
|
||||||
. "github.com/tendermint/tendermint/common"
|
|
||||||
dbm "github.com/tendermint/tendermint/db"
|
|
||||||
ptypes "github.com/tendermint/tendermint/permission/types"
|
|
||||||
. "github.com/tendermint/tendermint/state/types"
|
|
||||||
"github.com/tendermint/tendermint/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RandAccount(randBalance bool, minBalance int64) (*acm.Account, *acm.PrivAccount) {
|
|
||||||
privAccount := acm.GenPrivAccount()
|
|
||||||
perms := ptypes.DefaultAccountPermissions
|
|
||||||
acc := &acm.Account{
|
|
||||||
Address: privAccount.PubKey.Address(),
|
|
||||||
PubKey: privAccount.PubKey,
|
|
||||||
Sequence: RandInt(),
|
|
||||||
Balance: minBalance,
|
|
||||||
Permissions: perms,
|
|
||||||
}
|
|
||||||
if randBalance {
|
|
||||||
acc.Balance += int64(RandUint32())
|
|
||||||
}
|
|
||||||
return acc, privAccount
|
|
||||||
}
|
|
||||||
|
|
||||||
func RandGenesisDoc(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*GenesisDoc, []*acm.PrivAccount, []*types.PrivValidator) {
|
|
||||||
accounts := make([]GenesisAccount, numAccounts)
|
|
||||||
privAccounts := make([]*acm.PrivAccount, numAccounts)
|
|
||||||
defaultPerms := ptypes.DefaultAccountPermissions
|
|
||||||
for i := 0; i < numAccounts; i++ {
|
|
||||||
account, privAccount := RandAccount(randBalance, minBalance)
|
|
||||||
accounts[i] = GenesisAccount{
|
|
||||||
Address: account.Address,
|
|
||||||
Amount: account.Balance,
|
|
||||||
Permissions: &defaultPerms, // This will get copied into each state.Account.
|
|
||||||
}
|
|
||||||
privAccounts[i] = privAccount
|
|
||||||
}
|
|
||||||
validators := make([]GenesisValidator, numValidators)
|
|
||||||
privValidators := make([]*types.PrivValidator, numValidators)
|
|
||||||
for i := 0; i < numValidators; i++ {
|
|
||||||
valInfo, _, privVal := types.RandValidator(randBonded, minBonded)
|
|
||||||
validators[i] = GenesisValidator{
|
|
||||||
PubKey: valInfo.PubKey,
|
|
||||||
Amount: valInfo.FirstBondAmount,
|
|
||||||
UnbondTo: []BasicAccount{
|
|
||||||
{
|
|
||||||
Address: valInfo.PubKey.Address(),
|
|
||||||
Amount: valInfo.FirstBondAmount,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
privValidators[i] = privVal
|
|
||||||
}
|
|
||||||
sort.Sort(types.PrivValidatorsByAddress(privValidators))
|
|
||||||
return &GenesisDoc{
|
|
||||||
GenesisTime: time.Now(),
|
|
||||||
ChainID: "tendermint_test",
|
|
||||||
Accounts: accounts,
|
|
||||||
Validators: validators,
|
|
||||||
}, privAccounts, privValidators
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func RandGenesisState(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*State, []*acm.PrivAccount, []*types.PrivValidator) {
|
|
||||||
db := dbm.NewMemDB()
|
|
||||||
genDoc, privAccounts, privValidators := RandGenesisDoc(numAccounts, randBalance, minBalance, numValidators, randBonded, minBonded)
|
|
||||||
s0 := MakeGenesisState(db, genDoc)
|
|
||||||
s0.Save()
|
|
||||||
return s0, privAccounts, privValidators
|
|
||||||
}
|
|
@ -4,11 +4,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
stypes "github.com/tendermint/tendermint/state/types"
|
||||||
"github.com/tendermint/tendermint/wire"
|
"github.com/tendermint/tendermint/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStateToFromVMAccount(t *testing.T) {
|
func TestStateToFromVMAccount(t *testing.T) {
|
||||||
acmAcc1, _ := RandAccount(true, 456)
|
acmAcc1, _ := stypes.RandAccount(true, 456)
|
||||||
vmAcc := toVMAccount(acmAcc1)
|
vmAcc := toVMAccount(acmAcc1)
|
||||||
acmAcc2 := toStateAccount(vmAcc)
|
acmAcc2 := toStateAccount(vmAcc)
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
acm "github.com/tendermint/tendermint/account"
|
acm "github.com/tendermint/tendermint/account"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
ptypes "github.com/tendermint/tendermint/permission/types"
|
ptypes "github.com/tendermint/tendermint/permission/types"
|
||||||
|
"github.com/tendermint/tendermint/types"
|
||||||
"github.com/tendermint/tendermint/wire"
|
"github.com/tendermint/tendermint/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,3 +61,61 @@ func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------
|
||||||
|
// Make random genesis state
|
||||||
|
|
||||||
|
func RandAccount(randBalance bool, minBalance int64) (*acm.Account, *acm.PrivAccount) {
|
||||||
|
privAccount := acm.GenPrivAccount()
|
||||||
|
perms := ptypes.DefaultAccountPermissions
|
||||||
|
acc := &acm.Account{
|
||||||
|
Address: privAccount.PubKey.Address(),
|
||||||
|
PubKey: privAccount.PubKey,
|
||||||
|
Sequence: RandInt(),
|
||||||
|
Balance: minBalance,
|
||||||
|
Permissions: perms,
|
||||||
|
}
|
||||||
|
if randBalance {
|
||||||
|
acc.Balance += int64(RandUint32())
|
||||||
|
}
|
||||||
|
return acc, privAccount
|
||||||
|
}
|
||||||
|
|
||||||
|
func RandGenesisDoc(numAccounts int, randBalance bool, minBalance int64, numValidators int, randBonded bool, minBonded int64) (*GenesisDoc, []*acm.PrivAccount, []*types.PrivValidator) {
|
||||||
|
accounts := make([]GenesisAccount, numAccounts)
|
||||||
|
privAccounts := make([]*acm.PrivAccount, numAccounts)
|
||||||
|
defaultPerms := ptypes.DefaultAccountPermissions
|
||||||
|
for i := 0; i < numAccounts; i++ {
|
||||||
|
account, privAccount := RandAccount(randBalance, minBalance)
|
||||||
|
accounts[i] = GenesisAccount{
|
||||||
|
Address: account.Address,
|
||||||
|
Amount: account.Balance,
|
||||||
|
Permissions: &defaultPerms, // This will get copied into each state.Account.
|
||||||
|
}
|
||||||
|
privAccounts[i] = privAccount
|
||||||
|
}
|
||||||
|
validators := make([]GenesisValidator, numValidators)
|
||||||
|
privValidators := make([]*types.PrivValidator, numValidators)
|
||||||
|
for i := 0; i < numValidators; i++ {
|
||||||
|
valInfo, _, privVal := types.RandValidator(randBonded, minBonded)
|
||||||
|
validators[i] = GenesisValidator{
|
||||||
|
PubKey: valInfo.PubKey,
|
||||||
|
Amount: valInfo.FirstBondAmount,
|
||||||
|
UnbondTo: []BasicAccount{
|
||||||
|
{
|
||||||
|
Address: valInfo.PubKey.Address(),
|
||||||
|
Amount: valInfo.FirstBondAmount,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
privValidators[i] = privVal
|
||||||
|
}
|
||||||
|
sort.Sort(types.PrivValidatorsByAddress(privValidators))
|
||||||
|
return &GenesisDoc{
|
||||||
|
GenesisTime: time.Now(),
|
||||||
|
ChainID: "tendermint_test",
|
||||||
|
Accounts: accounts,
|
||||||
|
Validators: validators,
|
||||||
|
}, privAccounts, privValidators
|
||||||
|
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user