Browse Source

state save/load test.

pull/9/head
Jae Kwon 10 years ago
parent
commit
08f86176fc
11 changed files with 99 additions and 15 deletions
  1. +1
    -1
      blocks/block.go
  2. +3
    -3
      config/config.go
  3. +1
    -1
      consensus/state.go
  4. +3
    -1
      log.go
  5. +3
    -0
      main.go
  6. +2
    -2
      mempool/mempool.go
  7. +1
    -1
      merkle/util.go
  8. +15
    -0
      state/log.go
  9. +22
    -2
      state/state.go
  10. +33
    -4
      state/state_test.go
  11. +15
    -0
      state/validator.go

+ 1
- 1
blocks/block.go View File

@ -47,7 +47,7 @@ func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte) erro
if b.Header.Network != Config.Network {
return ErrBlockInvalidNetwork
}
if b.Header.Height != lastBlockHeight {
if b.Header.Height != lastBlockHeight+1 {
return ErrBlockInvalidBlockHeight
}
if !bytes.Equal(b.Header.LastBlockHash, lastBlockHash) {


+ 3
- 3
config/config.go View File

@ -19,13 +19,13 @@ import (
var RootDir string
var Config Config_
func initFlags(printHelp *bool) {
func setFlags(printHelp *bool) {
flag.BoolVar(printHelp, "help", false, "Print this help message.")
flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)")
flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node")
}
func init() {
func ParseFlags() {
RootDir = os.Getenv("TMROOT")
if RootDir == "" {
RootDir = os.Getenv("HOME") + "/.tendermint"
@ -54,7 +54,7 @@ func init() {
// try to parse arg flags, which can override file configuration.
var printHelp bool
initFlags(&printHelp)
setFlags(&printHelp)
flag.Parse()
if printHelp {
fmt.Println("----------------------------------")


+ 1
- 1
consensus/state.go View File

@ -418,7 +418,7 @@ func (cs *ConsensusState) stageBlock(block *Block) error {
stateCopy := cs.state.Copy() // Deep copy the state before staging.
// Commit block onto the copied state.
err := stateCopy.CommitBlock(block)
err := stateCopy.AppendBlock(block)
if err != nil {
return err
} else {


+ 3
- 1
log.go View File

@ -8,6 +8,7 @@ import (
"github.com/tendermint/tendermint/consensus"
"github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/state"
)
var log = logging.MustGetLogger("main")
@ -31,6 +32,7 @@ func init() {
blocks.SetBlocksLogger(log)
consensus.SetConsensusLogger(log)
p2p.SetP2PLogger(log)
mempool.SetMempoolLogger(log)
p2p.SetP2PLogger(log)
state.SetStateLogger(log)
}

+ 3
- 0
main.go View File

@ -78,6 +78,9 @@ func (n *Node) inboundConnectionRoutine(l p2p.Listener) {
func main() {
// Parse config flags
config.ParseFlags()
// Create & start node
n := NewNode()
l := p2p.NewDefaultListener("tcp", config.Config.LAddr)


+ 2
- 2
mempool/mempool.go View File

@ -34,7 +34,7 @@ func NewMempool(lastBlock *Block, state *State) *Mempool {
func (mem *Mempool) AddTx(tx Tx) (err error) {
mem.mtx.Lock()
defer mem.mtx.Unlock()
err = mem.state.CommitTx(tx)
err = mem.state.ExecTx(tx)
if err != nil {
return err
} else {
@ -82,7 +82,7 @@ func (mem *Mempool) ResetForBlockAndState(block *Block, state *State) {
// Next, filter all txs that aren't valid given new state.
validTxs := []Tx{}
for _, tx := range txs {
err := mem.state.CommitTx(tx)
err := mem.state.ExecTx(tx)
if err != nil {
validTxs = append(validTxs, tx)
} else {


+ 1
- 1
merkle/util.go View File

@ -42,7 +42,7 @@ will be at different levels.
func HashFromHashes(hashes [][]byte) []byte {
switch len(hashes) {
case 0:
panic("Cannot compute hash of empty slice")
return nil
case 1:
return hashes[0]
default:


+ 15
- 0
state/log.go View File

@ -0,0 +1,15 @@
package state
import (
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("state")
func init() {
logging.SetFormatter(logging.MustStringFormatter("[%{level:.1s}] %{message}"))
}
func SetStatesLogger(l *logging.Logger) {
log = l
}

+ 22
- 2
state/state.go View File

@ -13,7 +13,9 @@ import (
)
var (
ErrStateInvalidSequenceNumber = errors.New("Error State invalid sequence number")
ErrStateInvalidSequenceNumber = errors.New("Error State invalid sequence number")
ErrStateInvalidValidationStateHash = errors.New("Error State invalid ValidationStateHash")
ErrStateInvalidAccountStateHash = errors.New("Error State invalid AccountStateHash")
stateKey = []byte("stateKey")
)
@ -33,6 +35,7 @@ func (abc accountBalanceCodec) Read(accBalBytes []byte) (interface{}, error) {
//-----------------------------------------------------------------------------
// TODO: make it unsafe, remove mtx, and export fields?
type State struct {
mtx sync.Mutex
db DB
@ -175,7 +178,17 @@ func (s *State) AppendBlock(b *Block) error {
}
}
// Increment validator AccumPowers
s.validators.IncrementAccum()
// State hashes should match
if !bytes.Equal(s.validators.Hash(), b.ValidationStateHash) {
return ErrStateInvalidValidationStateHash
}
if !bytes.Equal(s.accountBalances.Tree.Hash(), b.AccountStateHash) {
return ErrStateInvalidAccountStateHash
}
s.height = b.Height
s.blockHash = b.Hash()
return nil
@ -193,13 +206,20 @@ func (s *State) CommitTime() time.Time {
return s.commitTime
}
// The returned ValidatorSet gets mutated upon s.Commit*().
// The returned ValidatorSet gets mutated upon s.ExecTx() and s.AppendBlock().
// Caller should copy the returned set before mutating.
func (s *State) Validators() *ValidatorSet {
s.mtx.Lock()
defer s.mtx.Unlock()
return s.validators
}
func (s *State) BlockHash() []byte {
s.mtx.Lock()
defer s.mtx.Unlock()
return s.blockHash
}
func (s *State) AccountBalance(accountId uint64) *AccountBalance {
s.mtx.Lock()
defer s.mtx.Unlock()


+ 33
- 4
state/state_test.go View File

@ -1,9 +1,12 @@
package state
import (
. "github.com/tendermint/tendermint/blocks"
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/config"
. "github.com/tendermint/tendermint/db"
"bytes"
"testing"
"time"
)
@ -38,9 +41,27 @@ func TestGenesisSaveLoad(t *testing.T) {
// Generate a state, save & load it.
s0 := randGenesisState(10, 5)
// Mutate the state to append one block.
block := &Block{Data: Data{Txs: []Tx{}}}
s0.AppendBlock(block)
// Figure out what the next state hashes should be.
s0ValsCopy := s0.Validators().Copy()
s0ValsCopy.IncrementAccum()
nextValidationStateHash := s0ValsCopy.Hash()
nextAccountStateHash := s0.accountBalances.Tree.Hash()
// 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)
}
// Save s0, load s1.
commitTime := time.Now()
@ -53,7 +74,15 @@ func TestGenesisSaveLoad(t *testing.T) {
t.Error("CommitTime was not the same")
}
// Compare height & blockHash
// XXX
if s0.Height() != 1 {
t.Error("s0 Height should be 1, got", s0.Height())
}
if s0.Height() != s1.Height() {
t.Error("Height mismatch")
}
if !bytes.Equal(s0.BlockHash(), s1.BlockHash()) {
t.Error("BlockHash mismatch")
}
// Compare Validators
s0Vals := s0.Validators()
s1Vals := s1.Validators()


+ 15
- 0
state/validator.go View File

@ -5,6 +5,7 @@ import (
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/merkle"
)
// Holds state for a Validator at a given height+round.
@ -155,3 +156,17 @@ func (vset *ValidatorSet) GetProposer() (proposer *Validator) {
}
return
}
// Should uniquely determine the state of the ValidatorSet.
func (vset *ValidatorSet) Hash() []byte {
ids := []uint64{}
for id, _ := range vset.validators {
ids = append(ids, id)
}
UInt64Slice(ids).Sort()
sortedValidators := make([]Binary, len(ids))
for i, id := range ids {
sortedValidators[i] = vset.validators[id]
}
return merkle.HashFromBinaries(sortedValidators)
}

Loading…
Cancel
Save