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-13 23:34:23 -07:00
|
|
|
func randAccountDetail(id uint64, status byte) (*AccountDetail, *PrivAccount) {
|
|
|
|
privAccount := GenPrivAccount()
|
|
|
|
privAccount.Id = id
|
|
|
|
account := privAccount.Account
|
2014-10-11 21:27:58 -07:00
|
|
|
return &AccountDetail{
|
2014-10-13 23:34:23 -07:00
|
|
|
Account: account,
|
2014-10-11 21:27:58 -07:00
|
|
|
Sequence: RandUInt(),
|
2014-10-13 23:34:23 -07:00
|
|
|
Balance: RandUInt64() + 1000, // At least 1000.
|
2014-10-11 21:27:58 -07:00
|
|
|
Status: status,
|
2014-10-13 23:34:23 -07:00
|
|
|
}, privAccount
|
2014-10-04 19:16:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The first numValidators accounts are validators.
|
2014-10-13 23:34:23 -07:00
|
|
|
func randGenesisState(numAccounts int, numValidators int) (*State, []*PrivAccount) {
|
2014-10-04 19:16:49 -07:00
|
|
|
db := NewMemDB()
|
2014-10-11 21:27:58 -07:00
|
|
|
accountDetails := make([]*AccountDetail, numAccounts)
|
2014-10-13 23:34:23 -07:00
|
|
|
privAccounts := make([]*PrivAccount, numAccounts)
|
2014-10-04 19:16:49 -07:00
|
|
|
for i := 0; i < numAccounts; i++ {
|
|
|
|
if i < numValidators {
|
2014-10-13 23:34:23 -07:00
|
|
|
accountDetails[i], privAccounts[i] =
|
|
|
|
randAccountDetail(uint64(i), AccountStatusBonded)
|
2014-10-04 19:16:49 -07:00
|
|
|
} else {
|
2014-10-13 23:34:23 -07:00
|
|
|
accountDetails[i], privAccounts[i] =
|
|
|
|
randAccountDetail(uint64(i), AccountStatusNominal)
|
2014-10-04 19:16:49 -07:00
|
|
|
}
|
|
|
|
}
|
2014-10-11 21:27:58 -07:00
|
|
|
s0 := GenesisState(db, time.Now(), accountDetails)
|
2014-10-13 20:07:26 -07:00
|
|
|
s0.Save(time.Now())
|
2014-10-13 23:34:23 -07:00
|
|
|
return s0, privAccounts
|
2014-10-04 19:16:49 -07:00
|
|
|
}
|
|
|
|
|
2014-10-13 20:07:26 -07:00
|
|
|
func TestCopyState(t *testing.T) {
|
|
|
|
// Generate a state
|
2014-10-13 23:34:23 -07:00
|
|
|
s0, _ := randGenesisState(10, 5)
|
2014-10-13 20:07:26 -07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:18:06 -07:00
|
|
|
// Mutate the original; hash should change.
|
|
|
|
accDet := s0.GetAccountDetail(0)
|
2014-10-13 20:07:26 -07:00
|
|
|
accDet.Balance += 1
|
2014-10-14 01:18:06 -07:00
|
|
|
// The account balance shouldn't have changed yet.
|
|
|
|
if s0.GetAccountDetail(0).Balance == accDet.Balance {
|
|
|
|
t.Error("Account balance changed unexpectedly")
|
|
|
|
}
|
|
|
|
// Setting, however, should change the balance.
|
|
|
|
s0.SetAccountDetail(accDet)
|
|
|
|
if s0.GetAccountDetail(0).Balance != accDet.Balance {
|
|
|
|
t.Error("Account balance wasn't set")
|
|
|
|
}
|
|
|
|
// How that the state changed, the hash should change too.
|
2014-10-13 20:07:26 -07:00
|
|
|
if bytes.Equal(s0Hash, s0.Hash()) {
|
|
|
|
t.Error("Expected state hash to have changed")
|
|
|
|
}
|
2014-10-14 01:18:06 -07:00
|
|
|
// The s0Copy shouldn't have changed though.
|
2014-10-13 20:07:26 -07:00
|
|
|
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-10-13 23:34:23 -07:00
|
|
|
s0, _ := randGenesisState(10, 5)
|
2014-10-07 00:43:34 -07:00
|
|
|
// Mutate the state to append one empty block.
|
|
|
|
block := &Block{
|
|
|
|
Header: Header{
|
2014-10-13 20:07:26 -07:00
|
|
|
Network: Config.Network,
|
|
|
|
Height: 1,
|
|
|
|
StateHash: nil,
|
2014-10-07 00:43:34 -07:00
|
|
|
},
|
|
|
|
Data: Data{
|
|
|
|
Txs: []Tx{},
|
|
|
|
},
|
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
// The second argument to AppendBlock() is false,
|
|
|
|
// which sets Block.Header.StateHash.
|
|
|
|
err := s0.Copy().AppendBlock(block, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error appending initial block:", err)
|
|
|
|
}
|
|
|
|
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, 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
|
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-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.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-13 20:07:26 -07: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
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
if bytes.Equal(s0.BondedValidators.Hash(), s1.BondedValidators.Hash()) {
|
|
|
|
// The BondedValidators hash should have changed because
|
|
|
|
// each AppendBlock() calls IncrementAccum(),
|
|
|
|
// changing each validator's Accum.
|
|
|
|
t.Error("BondedValidators hash should have changed")
|
|
|
|
}
|
|
|
|
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-10-14 01:18:06 -07:00
|
|
|
if !bytes.Equal(s0.accountDetails.Hash(), s1.accountDetails.Hash()) {
|
2014-10-11 21:27:58 -07:00
|
|
|
t.Error("AccountDetail mismatch")
|
2014-10-06 21:28:49 -07:00
|
|
|
}
|
2014-10-04 19:16:49 -07:00
|
|
|
}
|
2014-10-13 23:34:23 -07:00
|
|
|
|
|
|
|
func TestTxSequence(t *testing.T) {
|
|
|
|
|
|
|
|
state, privAccounts := randGenesisState(3, 1)
|
|
|
|
acc1 := state.GetAccountDetail(1) // Non-validator
|
|
|
|
|
|
|
|
// Try executing a SendTx with various sequence numbers.
|
|
|
|
stx := &SendTx{
|
|
|
|
BaseTx: BaseTx{
|
|
|
|
Sequence: acc1.Sequence + 1,
|
|
|
|
Fee: 0},
|
|
|
|
To: 2,
|
|
|
|
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++ {
|
|
|
|
stx.Sequence = uint(int(acc1.Sequence) + i)
|
|
|
|
privAccounts[1].Sign(stx)
|
|
|
|
stateCopy := state.Copy()
|
|
|
|
err := stateCopy.ExecTx(stx)
|
|
|
|
if i >= 1 {
|
|
|
|
// Sequence is good.
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected good sequence to pass")
|
|
|
|
}
|
|
|
|
// Check accDet.Sequence.
|
|
|
|
newAcc1 := stateCopy.GetAccountDetail(1)
|
|
|
|
if newAcc1.Sequence != stx.Sequence {
|
|
|
|
t.Errorf("Expected account sequence to change")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Sequence is bad.
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected bad sequence to fail")
|
|
|
|
}
|
|
|
|
// Check accDet.Sequence. (shouldn't have changed)
|
|
|
|
newAcc1 := stateCopy.GetAccountDetail(1)
|
|
|
|
if newAcc1.Sequence != acc1.Sequence {
|
|
|
|
t.Errorf("Expected account sequence to not change")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxs(t *testing.T) {
|
|
|
|
|
|
|
|
state, privAccounts := randGenesisState(3, 1)
|
|
|
|
|
|
|
|
acc0 := state.GetAccountDetail(0) // Validator
|
|
|
|
acc1 := state.GetAccountDetail(1) // Non-validator
|
|
|
|
acc2 := state.GetAccountDetail(2) // Non-validator
|
|
|
|
|
|
|
|
// SendTx.
|
2014-10-14 01:18:06 -07:00
|
|
|
{
|
|
|
|
state := state.Copy()
|
|
|
|
stx := &SendTx{
|
|
|
|
BaseTx: BaseTx{
|
|
|
|
Sequence: acc1.Sequence + 1,
|
|
|
|
Fee: 0},
|
|
|
|
To: 2,
|
|
|
|
Amount: 1,
|
|
|
|
}
|
|
|
|
privAccounts[1].Sign(stx)
|
|
|
|
err := state.ExecTx(stx)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error in executing send transaction, %v", err)
|
|
|
|
}
|
|
|
|
newAcc1 := state.GetAccountDetail(1)
|
|
|
|
if acc1.Balance-1 != newAcc1.Balance {
|
|
|
|
t.Errorf("Unexpected newAcc1 balance. Expected %v, got %v",
|
|
|
|
acc1.Balance-1, newAcc1.Balance)
|
|
|
|
}
|
|
|
|
newAcc2 := state.GetAccountDetail(2)
|
|
|
|
if acc2.Balance+1 != newAcc2.Balance {
|
|
|
|
t.Errorf("Unexpected newAcc2 balance. Expected %v, got %v",
|
|
|
|
acc2.Balance+1, newAcc2.Balance)
|
|
|
|
}
|
2014-10-13 23:34:23 -07:00
|
|
|
}
|
2014-10-14 01:18:06 -07:00
|
|
|
|
|
|
|
// TODO: test overflows.
|
|
|
|
|
|
|
|
// SendTx should fail for bonded validators.
|
|
|
|
{
|
|
|
|
state := state.Copy()
|
|
|
|
stx := &SendTx{
|
|
|
|
BaseTx: BaseTx{
|
|
|
|
Sequence: acc0.Sequence + 1,
|
|
|
|
Fee: 0},
|
|
|
|
To: 2,
|
|
|
|
Amount: 1,
|
|
|
|
}
|
|
|
|
privAccounts[0].Sign(stx)
|
|
|
|
err := state.ExecTx(stx)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error, SendTx should fail for bonded validators")
|
|
|
|
}
|
2014-10-13 23:34:23 -07:00
|
|
|
}
|
|
|
|
|
2014-10-14 01:18:06 -07:00
|
|
|
// TODO: test for unbonding validators.
|
|
|
|
|
2014-10-13 23:34:23 -07:00
|
|
|
// BondTx.
|
2014-10-14 01:18:06 -07:00
|
|
|
{
|
|
|
|
state := state.Copy()
|
|
|
|
btx := &BondTx{
|
|
|
|
BaseTx: BaseTx{
|
|
|
|
Sequence: acc1.Sequence + 1,
|
|
|
|
Fee: 0},
|
|
|
|
}
|
|
|
|
privAccounts[1].Sign(btx)
|
|
|
|
err := state.ExecTx(btx)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error in executing bond transaction, %v", err)
|
|
|
|
}
|
|
|
|
newAcc1 := state.GetAccountDetail(1)
|
|
|
|
if acc1.Balance != newAcc1.Balance {
|
|
|
|
t.Errorf("Unexpected newAcc1 balance. Expected %v, got %v",
|
|
|
|
acc1.Balance, newAcc1.Balance)
|
|
|
|
}
|
|
|
|
if newAcc1.Status != AccountStatusBonded {
|
|
|
|
t.Errorf("Unexpected newAcc1 status.")
|
|
|
|
}
|
|
|
|
_, acc1Val := state.BondedValidators.GetById(acc1.Id)
|
|
|
|
if acc1Val == nil {
|
|
|
|
t.Errorf("acc1Val not present")
|
|
|
|
}
|
|
|
|
if acc1Val.BondHeight != state.Height {
|
|
|
|
t.Errorf("Unexpected bond height. Expected %v, got %v",
|
|
|
|
state.Height, acc1Val.BondHeight)
|
|
|
|
}
|
|
|
|
if acc1Val.VotingPower != acc1.Balance {
|
|
|
|
t.Errorf("Unexpected voting power. Expected %v, got %v",
|
|
|
|
acc1Val.VotingPower, acc1.Balance)
|
|
|
|
}
|
|
|
|
if acc1Val.Accum != 0 {
|
|
|
|
t.Errorf("Unexpected accum. Expected 0, got %v",
|
|
|
|
acc1Val.Accum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO UnbondTx.
|
|
|
|
// TODO NameTx.
|
2014-10-13 23:34:23 -07:00
|
|
|
|
|
|
|
}
|