tendermint/state/state_test.go

106 lines
2.6 KiB
Go
Raw Normal View History

2014-10-04 19:16:49 -07:00
package state
import (
2014-10-07 00:43:34 -07:00
. "github.com/tendermint/tendermint/blocks"
2014-10-04 19:16:49 -07:00
. "github.com/tendermint/tendermint/common"
2014-10-07 00:43:34 -07:00
. "github.com/tendermint/tendermint/config"
2014-10-04 19:16:49 -07:00
. "github.com/tendermint/tendermint/db"
2014-10-07 00:43:34 -07:00
"bytes"
2014-10-04 19:16:49 -07:00
"testing"
"time"
)
2014-10-11 21:27:58 -07:00
func randAccountDetail(id uint64, status byte) *AccountDetail {
return &AccountDetail{
2014-10-04 19:16:49 -07:00
Account: Account{
Id: id,
PubKey: CRandBytes(32),
},
2014-10-11 21:27:58 -07:00
Sequence: RandUInt(),
Balance: RandUInt64(),
Status: status,
2014-10-04 19:16:49 -07:00
}
}
// The first numValidators accounts are validators.
func randGenesisState(numAccounts int, numValidators int) *State {
db := NewMemDB()
2014-10-11 21:27:58 -07:00
accountDetails := make([]*AccountDetail, numAccounts)
2014-10-04 19:16:49 -07:00
for i := 0; i < numAccounts; i++ {
if i < numValidators {
2014-10-11 21:27:58 -07:00
accountDetails[i] = randAccountDetail(uint64(i), AccountDetailStatusNominal)
2014-10-04 19:16:49 -07:00
} else {
2014-10-11 21:27:58 -07:00
accountDetails[i] = randAccountDetail(uint64(i), AccountDetailStatusBonded)
2014-10-04 19:16:49 -07:00
}
}
2014-10-11 21:27:58 -07:00
s0 := GenesisState(db, time.Now(), accountDetails)
2014-10-04 19:16:49 -07:00
return s0
}
func TestGenesisSaveLoad(t *testing.T) {
2014-10-06 21:28:49 -07:00
// Generate a state, save & load it.
2014-10-04 19:16:49 -07:00
s0 := randGenesisState(10, 5)
2014-10-07 00:43:34 -07:00
// Figure out what the next state hashes should be.
2014-10-11 21:27:58 -07:00
s0.Validators.Hash()
2014-10-07 01:05:54 -07:00
s0ValsCopy := s0.Validators.Copy()
2014-10-07 00:43:34 -07:00
s0ValsCopy.IncrementAccum()
nextValidationStateHash := s0ValsCopy.Hash()
2014-10-11 21:27:58 -07:00
nextAccountStateHash := s0.AccountDetails.Hash()
2014-10-07 00:43:34 -07:00
// Mutate the state to append one empty block.
block := &Block{
Header: Header{
Network: Config.Network,
Height: 1,
ValidationStateHash: nextValidationStateHash,
AccountStateHash: nextAccountStateHash,
},
Data: Data{
Txs: []Tx{},
},
}
err := s0.AppendBlock(block)
if err != nil {
t.Error("Error appending initial block:", err)
}
2014-10-06 21:28:49 -07:00
2014-10-07 13:39:21 -07:00
// Save s0
2014-10-06 21:28:49 -07:00
commitTime := time.Now()
s0.Save(commitTime)
2014-10-07 13:39:21 -07:00
// Sanity check s0
2014-10-07 01:05:54 -07:00
//s0.DB.(*MemDB).Print()
2014-10-07 13:39:21 -07:00
if s0.Validators.TotalVotingPower() == 0 {
t.Error("s0 Validators TotalVotingPower should not be 0")
}
if s0.Height != 1 {
t.Error("s0 Height should be 1, got", s0.Height)
}
// Load s1
2014-10-07 01:05:54 -07:00
s1 := LoadState(s0.DB)
2014-10-06 21:28:49 -07:00
// Compare CommitTime
2014-10-07 13:39:21 -07:00
if !s0.CommitTime.Equal(s1.CommitTime) {
t.Error("CommitTime was not the same", s0.CommitTime, s1.CommitTime)
2014-10-06 21:28:49 -07:00
}
// Compare height & blockHash
2014-10-07 01:05:54 -07:00
if s0.Height != s1.Height {
2014-10-07 00:43:34 -07:00
t.Error("Height mismatch")
}
2014-10-07 01:05:54 -07:00
if !bytes.Equal(s0.BlockHash, s1.BlockHash) {
2014-10-07 00:43:34 -07:00
t.Error("BlockHash mismatch")
}
2014-10-06 21:28:49 -07:00
// Compare Validators
2014-10-07 01:05:54 -07:00
if s0.Validators.Size() != s1.Validators.Size() {
2014-10-07 13:39:21 -07:00
t.Error("Validators Size mismatch")
2014-10-06 21:28:49 -07:00
}
2014-10-07 01:05:54 -07:00
if s0.Validators.TotalVotingPower() != s1.Validators.TotalVotingPower() {
2014-10-07 13:39:21 -07:00
t.Error("Validators TotalVotingPower mismatch")
}
2014-10-11 21:27:58 -07:00
if !bytes.Equal(s0.AccountDetails.Hash(), s1.AccountDetails.Hash()) {
t.Error("AccountDetail mismatch")
2014-10-06 21:28:49 -07:00
}
2014-10-04 19:16:49 -07:00
}