Refactor consensus/vote_set_test.go

This commit is contained in:
Jae Kwon
2014-12-23 23:20:49 -08:00
parent fa7c83166f
commit 70eb75dca7
9 changed files with 103 additions and 88 deletions
+3 -3
View File
@@ -27,16 +27,16 @@ func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
return
}
func GenesisStateFromFile(db db_.DB, genDocFile string) *State {
func MakeGenesisStateFromFile(db db_.DB, genDocFile string) *State {
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
Panicf("Couldn't read GenesisDoc file: %v", err)
}
genDoc := GenesisDocFromJSON(jsonBlob)
return GenesisState(db, genDoc)
return MakeGenesisState(db, genDoc)
}
func GenesisState(db db_.DB, genDoc *GenesisDoc) *State {
func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
if len(genDoc.Validators) == 0 {
panic("Must have some validators")
}
+1
View File
@@ -629,6 +629,7 @@ func (s *State) Hash() []byte {
s.BondedValidators,
s.UnbondingValidators,
s.accounts,
s.validatorInfos,
}
return merkle.HashFromHashables(hashables)
}
+14 -4
View File
@@ -12,7 +12,7 @@ import (
)
func TestCopyState(t *testing.T) {
// Generate a state
// Generate a random state
s0, privAccounts, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
s0Hash := s0.Hash()
if len(s0Hash) == 0 {
@@ -29,19 +29,23 @@ func TestCopyState(t *testing.T) {
acc0Address := privAccounts[0].PubKey.Address()
acc := s0.GetAccount(acc0Address)
acc.Balance += 1
// The account balance shouldn't have changed yet.
if s0.GetAccount(acc0Address).Balance == acc.Balance {
t.Error("Account balance changed unexpectedly")
}
// Setting, however, should change the balance.
s0.SetAccount(acc)
if s0.GetAccount(acc0Address).Balance != acc.Balance {
t.Error("Account balance wasn't set")
}
// How that the state changed, the hash should change too.
// Now that the state changed, the hash should change too.
if bytes.Equal(s0Hash, s0.Hash()) {
t.Error("Expected state hash to have changed")
}
// The s0Copy shouldn't have changed though.
if !bytes.Equal(s0Hash, s0Copy.Hash()) {
t.Error("Expected state copy hash to have not changed")
@@ -52,6 +56,7 @@ func TestGenesisSaveLoad(t *testing.T) {
// Generate a state, save & load it.
s0, _, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
// Mutate the state to append one empty block.
block := &Block{
Header: &Header{
@@ -70,7 +75,8 @@ func TestGenesisSaveLoad(t *testing.T) {
},
}
blockParts := NewPartSetFromData(BinaryBytes(block))
// The second argument to AppendBlock() is false,
// The last argument to AppendBlock() is `false`,
// which sets Block.Header.StateHash.
err := s0.Copy().AppendBlock(block, blockParts.Header(), false)
if err != nil {
@@ -79,6 +85,7 @@ func TestGenesisSaveLoad(t *testing.T) {
if len(block.Header.StateHash) == 0 {
t.Error("Expected StateHash but got nothing.")
}
// Now append the block to s0.
// This time we also check the StateHash (as computed above).
err = s0.AppendBlock(block, blockParts.Header(), true)
@@ -108,6 +115,7 @@ func TestGenesisSaveLoad(t *testing.T) {
if !bytes.Equal(s0.LastBlockHash, s1.LastBlockHash) {
t.Error("LastBlockHash mismatch")
}
// Compare state merkle trees
if s0.BondedValidators.Size() != s1.BondedValidators.Size() {
t.Error("BondedValidators Size mismatch")
@@ -130,6 +138,9 @@ func TestGenesisSaveLoad(t *testing.T) {
if !bytes.Equal(s0.accounts.Hash(), s1.accounts.Hash()) {
t.Error("Accounts mismatch")
}
if !bytes.Equal(s0.validatorInfos.Hash(), s1.validatorInfos.Hash()) {
t.Error("Accounts mismatch")
}
}
func TestTxSequence(t *testing.T) {
@@ -285,6 +296,5 @@ func TestTxs(t *testing.T) {
}
// TODO UnbondTx.
// TODO NameTx.
}
+1 -1
View File
@@ -70,7 +70,7 @@ func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numV
privValidators[i] = privVal
}
sort.Sort(PrivValidatorsByAddress(privValidators))
s0 := GenesisState(db, &GenesisDoc{
s0 := MakeGenesisState(db, &GenesisDoc{
GenesisTime: time.Now(),
Accounts: accounts,
Validators: validators,
+1 -1
View File
@@ -10,7 +10,7 @@ import (
. "github.com/tendermint/tendermint/block"
)
// Persistent static data for each Validator
// Persistent (mostly) static data for each Validator
type ValidatorInfo struct {
Address []byte
PubKey PubKeyEd25519
+1 -1
View File
@@ -12,7 +12,7 @@ import (
// ValidatorSet represent a set of *Validator at a given height.
// The validators can be fetched by address or index.
// The index is in order of .Address, so the index are the same
// The index is in order of .Address, so the indices are fixed
// for all rounds of a given blockchain height.
// On the other hand, the .AccumPower of each validator and
// the designated .Proposer() of a set changes every round,