tendermint/state/state_test.go

306 lines
8.1 KiB
Go
Raw Normal View History

2014-10-04 19:16:49 -07:00
package state
import (
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
2014-12-17 01:37:13 -08:00
. "github.com/tendermint/tendermint/block"
2014-10-07 00:43:34 -07:00
. "github.com/tendermint/tendermint/config"
2014-10-04 19:16:49 -07:00
2014-10-07 00:43:34 -07:00
"bytes"
2014-10-04 19:16:49 -07:00
"testing"
"time"
)
func TestCopyState(t *testing.T) {
2014-12-23 23:20:49 -08:00
// Generate a random state
2014-12-17 01:37:13 -08:00
s0, privAccounts, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
s0Hash := s0.Hash()
if len(s0Hash) == 0 {
t.Error("Expected state hash")
}
// Check hash of copy
s0Copy := s0.Copy()
if !bytes.Equal(s0Hash, s0Copy.Hash()) {
t.Error("Expected state copy hash to be the same")
}
// Mutate the original; hash should change.
2014-12-17 01:37:13 -08:00
acc0Address := privAccounts[0].PubKey.Address()
acc := s0.GetAccount(acc0Address)
acc.Balance += 1
2014-12-23 23:20:49 -08:00
// The account balance shouldn't have changed yet.
2014-12-17 01:37:13 -08:00
if s0.GetAccount(acc0Address).Balance == acc.Balance {
t.Error("Account balance changed unexpectedly")
}
2014-12-23 23:20:49 -08:00
// Setting, however, should change the balance.
2014-12-17 01:37:13 -08:00
s0.SetAccount(acc)
if s0.GetAccount(acc0Address).Balance != acc.Balance {
t.Error("Account balance wasn't set")
}
2014-12-23 23:20:49 -08:00
// Now that the state changed, the hash should change too.
if bytes.Equal(s0Hash, s0.Hash()) {
t.Error("Expected state hash to have changed")
}
2014-12-23 23:20:49 -08:00
// The s0Copy shouldn't have changed though.
if !bytes.Equal(s0Hash, s0Copy.Hash()) {
t.Error("Expected state copy hash to have not changed")
}
}
2014-10-04 19:16:49 -07:00
func TestGenesisSaveLoad(t *testing.T) {
2014-10-06 21:28:49 -07:00
// Generate a state, save & load it.
2014-12-17 01:37:13 -08:00
s0, _, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
2014-12-23 23:20:49 -08:00
2014-10-07 00:43:34 -07:00
// Mutate the state to append one empty block.
block := &Block{
2014-11-03 15:50:23 -08:00
Header: &Header{
Network: Config.Network,
Height: 1,
Time: s0.LastBlockTime.Add(time.Minute),
Fees: 0,
2014-12-23 01:35:54 -08:00
NumTxs: 0,
LastBlockHash: s0.LastBlockHash,
LastBlockParts: s0.LastBlockParts,
StateHash: nil,
2014-10-07 00:43:34 -07:00
},
2014-11-03 15:50:23 -08:00
Validation: &Validation{},
Data: &Data{
2014-10-07 00:43:34 -07:00
Txs: []Tx{},
},
}
blockParts := NewPartSetFromData(BinaryBytes(block))
2014-12-23 23:20:49 -08:00
// The last argument to AppendBlock() is `false`,
// which sets Block.Header.StateHash.
err := s0.Copy().AppendBlock(block, blockParts.Header(), false)
if err != nil {
t.Error("Error appending initial block:", err)
}
if len(block.Header.StateHash) == 0 {
t.Error("Expected StateHash but got nothing.")
}
2014-12-23 23:20:49 -08:00
// Now append the block to s0.
// This time we also check the StateHash (as computed above).
err = s0.AppendBlock(block, blockParts.Header(), true)
2014-10-07 00:43:34 -07:00
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
s0.Save()
2014-10-07 13:39:21 -07:00
// Sanity check s0
//s0.DB.(*db_.MemDB).Print()
2014-10-12 21:14:10 -07:00
if s0.BondedValidators.TotalVotingPower() == 0 {
t.Error("s0 BondedValidators TotalVotingPower should not be 0")
2014-10-07 13:39:21 -07:00
}
if s0.LastBlockHeight != 1 {
t.Error("s0 LastBlockHeight should be 1, got", s0.LastBlockHeight)
2014-10-07 13:39:21 -07:00
}
// Load s1
2014-10-07 01:05:54 -07:00
s1 := LoadState(s0.DB)
2014-10-06 21:28:49 -07:00
// Compare height & blockHash
if s0.LastBlockHeight != s1.LastBlockHeight {
t.Error("LastBlockHeight mismatch")
2014-10-07 00:43:34 -07:00
}
if !bytes.Equal(s0.LastBlockHash, s1.LastBlockHash) {
t.Error("LastBlockHash mismatch")
2014-10-07 00:43:34 -07:00
}
2014-12-23 23:20:49 -08:00
// Compare state merkle trees
2014-10-12 21:14:10 -07:00
if s0.BondedValidators.Size() != s1.BondedValidators.Size() {
t.Error("BondedValidators Size mismatch")
2014-10-06 21:28:49 -07:00
}
2014-10-12 21:14:10 -07:00
if s0.BondedValidators.TotalVotingPower() != s1.BondedValidators.TotalVotingPower() {
t.Error("BondedValidators TotalVotingPower mismatch")
2014-10-07 13:39:21 -07:00
}
if !bytes.Equal(s0.BondedValidators.Hash(), s1.BondedValidators.Hash()) {
t.Error("BondedValidators hash mismatch")
}
if s0.UnbondingValidators.Size() != s1.UnbondingValidators.Size() {
t.Error("UnbondingValidators Size mismatch")
}
if s0.UnbondingValidators.TotalVotingPower() != s1.UnbondingValidators.TotalVotingPower() {
t.Error("UnbondingValidators TotalVotingPower mismatch")
}
if !bytes.Equal(s0.UnbondingValidators.Hash(), s1.UnbondingValidators.Hash()) {
t.Error("UnbondingValidators hash mismatch")
}
2014-12-17 01:37:13 -08:00
if !bytes.Equal(s0.accounts.Hash(), s1.accounts.Hash()) {
t.Error("Accounts mismatch")
2014-10-06 21:28:49 -07:00
}
2014-12-23 23:20:49 -08:00
if !bytes.Equal(s0.validatorInfos.Hash(), s1.validatorInfos.Hash()) {
t.Error("Accounts mismatch")
}
2014-10-04 19:16:49 -07:00
}
func TestTxSequence(t *testing.T) {
2014-12-17 01:37:13 -08:00
state, privAccounts, _ := RandGenesisState(3, true, 1000, 1, true, 1000)
acc0 := state.GetAccount(privAccounts[0].PubKey.Address())
acc0PubKey := privAccounts[0].PubKey
2014-12-17 01:37:13 -08:00
acc1 := state.GetAccount(privAccounts[1].PubKey.Address())
// Try executing a SendTx with various sequence numbers.
2014-12-17 01:37:13 -08:00
makeSendTx := func(sequence uint) *SendTx {
return &SendTx{
Inputs: []*TxInput{
&TxInput{
Address: acc0.Address,
Amount: 1,
Sequence: sequence,
PubKey: acc0PubKey,
2014-12-17 01:37:13 -08:00
},
},
Outputs: []*TxOutput{
&TxOutput{
Address: acc1.Address,
Amount: 1,
},
},
}
}
// Test a variety of sequence numbers for the tx.
// The tx should only pass when i == 1.
for i := -1; i < 3; i++ {
2014-12-17 01:37:13 -08:00
sequence := acc0.Sequence + uint(i)
tx := makeSendTx(sequence)
tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
stateCopy := state.Copy()
2014-12-17 01:37:13 -08:00
err := stateCopy.ExecTx(tx)
if i == 1 {
// Sequence is good.
if err != nil {
2014-12-17 01:37:13 -08:00
t.Errorf("Expected good sequence to pass: %v", err)
}
2014-12-17 01:37:13 -08:00
// Check acc.Sequence.
newAcc0 := stateCopy.GetAccount(acc0.Address)
if newAcc0.Sequence != sequence {
t.Errorf("Expected account sequence to change to %v, got %v",
sequence, newAcc0.Sequence)
}
} else {
// Sequence is bad.
if err == nil {
t.Errorf("Expected bad sequence to fail")
}
2014-12-17 01:37:13 -08:00
// Check acc.Sequence. (shouldn't have changed)
newAcc0 := stateCopy.GetAccount(acc0.Address)
if newAcc0.Sequence != acc0.Sequence {
t.Errorf("Expected account sequence to not change from %v, got %v",
acc0.Sequence, newAcc0.Sequence)
}
}
}
}
2014-12-17 01:37:13 -08:00
// TODO: test overflows.
// TODO: test for unbonding validators.
func TestTxs(t *testing.T) {
2014-12-17 01:37:13 -08:00
state, privAccounts, _ := RandGenesisState(3, true, 1000, 1, true, 1000)
2014-12-17 01:37:13 -08:00
//val0 := state.GetValidatorInfo(privValidators[0].Address)
acc0 := state.GetAccount(privAccounts[0].PubKey.Address())
acc0PubKey := privAccounts[0].PubKey
2014-12-17 01:37:13 -08:00
acc1 := state.GetAccount(privAccounts[1].PubKey.Address())
// SendTx.
{
state := state.Copy()
2014-12-17 01:37:13 -08:00
tx := &SendTx{
Inputs: []*TxInput{
&TxInput{
Address: acc0.Address,
Amount: 1,
Sequence: acc0.Sequence + 1,
PubKey: acc0PubKey,
2014-12-17 01:37:13 -08:00
},
},
Outputs: []*TxOutput{
&TxOutput{
Address: acc1.Address,
Amount: 1,
},
},
}
2014-12-17 01:37:13 -08:00
tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
err := state.ExecTx(tx)
if err != nil {
t.Errorf("Got error in executing send transaction, %v", err)
}
2014-12-17 01:37:13 -08:00
newAcc0 := state.GetAccount(acc0.Address)
if acc0.Balance-1 != newAcc0.Balance {
t.Errorf("Unexpected newAcc0 balance. Expected %v, got %v",
acc0.Balance-1, newAcc0.Balance)
}
2014-12-17 01:37:13 -08:00
newAcc1 := state.GetAccount(acc1.Address)
if acc1.Balance+1 != newAcc1.Balance {
t.Errorf("Unexpected newAcc1 balance. Expected %v, got %v",
acc1.Balance+1, newAcc1.Balance)
}
}
// BondTx.
{
state := state.Copy()
2014-12-17 01:37:13 -08:00
tx := &BondTx{
PubKey: acc0PubKey.(PubKeyEd25519),
2014-12-17 01:37:13 -08:00
Inputs: []*TxInput{
&TxInput{
Address: acc0.Address,
Amount: 1,
Sequence: acc0.Sequence + 1,
PubKey: acc0PubKey,
2014-12-17 01:37:13 -08:00
},
},
UnbondTo: []*TxOutput{
&TxOutput{
Address: acc0.Address,
Amount: 1,
},
},
}
2014-12-17 01:37:13 -08:00
tx.Inputs[0].Signature = privAccounts[0].Sign(tx)
err := state.ExecTx(tx)
if err != nil {
t.Errorf("Got error in executing bond transaction, %v", err)
}
2014-12-17 01:37:13 -08:00
newAcc0 := state.GetAccount(acc0.Address)
if newAcc0.Balance != acc0.Balance-1 {
t.Errorf("Unexpected newAcc0 balance. Expected %v, got %v",
acc0.Balance-1, newAcc0.Balance)
}
2014-12-17 01:37:13 -08:00
_, acc0Val := state.BondedValidators.GetByAddress(acc0.Address)
if acc0Val == nil {
t.Errorf("acc0Val not present")
}
2014-12-17 01:37:13 -08:00
if acc0Val.BondHeight != state.LastBlockHeight+1 {
t.Errorf("Unexpected bond height. Expected %v, got %v",
2014-12-17 01:37:13 -08:00
state.LastBlockHeight, acc0Val.BondHeight)
}
2014-12-17 01:37:13 -08:00
if acc0Val.VotingPower != 1 {
t.Errorf("Unexpected voting power. Expected %v, got %v",
2014-12-17 01:37:13 -08:00
acc0Val.VotingPower, acc0.Balance)
}
2014-12-17 01:37:13 -08:00
if acc0Val.Accum != 0 {
t.Errorf("Unexpected accum. Expected 0, got %v",
2014-12-17 01:37:13 -08:00
acc0Val.Accum)
}
}
// TODO UnbondTx.
}