Commit 6904435a authored by caopingcp's avatar caopingcp

tendermint save local state

parent 91390fe4
This diff is collapsed.
......@@ -118,9 +118,9 @@ func (s State) GetValidators() (last *ttypes.ValidatorSet, current *ttypes.Valid
// Create a block from the latest state
// MakeBlock builds a block with the given txs and commit from the current state.
func (s State) MakeBlock(height int64, round int64, Txs []*types.Transaction, commit *tmtypes.TendermintCommit) *ttypes.TendermintBlock {
func (s State) MakeBlock(height int64, round int64, pblock *types.Block, commit *tmtypes.TendermintCommit, proposerAddr []byte) *ttypes.TendermintBlock {
// build base block
block := ttypes.MakeBlock(height, round, Txs, commit)
block := ttypes.MakeBlock(height, round, pblock, commit)
// fill header with state data
block.Header.ChainID = s.ChainID
......@@ -130,6 +130,7 @@ func (s State) MakeBlock(height int64, round int64, Txs []*types.Transaction, co
block.Header.AppHash = s.AppHash
block.Header.ConsensusHash = s.ConsensusParams.Hash()
block.Header.LastResultsHash = s.LastResultsHash
block.Header.ProposerAddr = proposerAddr
return block
}
......@@ -226,6 +227,7 @@ func LoadState(state *tmtypes.State) State {
ChainID: state.GetChainID(),
LastBlockHeight: state.GetLastBlockHeight(),
LastBlockTotalTx: state.GetLastBlockTotalTx(),
LastBlockID: ttypes.BlockID{BlockID: *state.LastBlockID},
LastBlockTime: state.LastBlockTime,
Validators: nil,
LastValidators: nil,
......@@ -307,7 +309,7 @@ func (csdb *CSStateDB) LoadValidators(height int64) (*ttypes.ValidatorSet, error
if height == 0 {
return nil, nil
}
if csdb.state.LastBlockHeight+1 == height {
if csdb.state.LastBlockHeight == height {
return csdb.state.Validators, nil
}
......@@ -374,6 +376,7 @@ func SaveState(state State) *tmtypes.State {
ChainID: state.ChainID,
LastBlockHeight: state.LastBlockHeight,
LastBlockTotalTx: state.LastBlockTotalTx,
LastBlockID: &state.LastBlockID.BlockID,
LastBlockTime: state.LastBlockTime,
Validators: &tmtypes.ValidatorSet{Validators: make([]*tmtypes.Validator, 0), Proposer: &tmtypes.Validator{}},
LastValidators: &tmtypes.ValidatorSet{Validators: make([]*tmtypes.Validator, 0), Proposer: &tmtypes.Validator{}},
......@@ -458,17 +461,14 @@ func LoadProposer(source *tmtypes.Validator) (*ttypes.Validator, error) {
}
// CreateBlockInfoTx make blockInfo to the first transaction of the block and execer is valnode
func CreateBlockInfoTx(pubkey string, lastCommit *tmtypes.TendermintCommit, seenCommit *tmtypes.TendermintCommit, state *tmtypes.State, proposal *tmtypes.Proposal, block *tmtypes.TendermintBlock) *types.Transaction {
blockNoTxs := *block
blockNoTxs.Txs = make([]*types.Transaction, 0)
func CreateBlockInfoTx(pubkey string, state *tmtypes.State, block *tmtypes.TendermintBlock) *types.Transaction {
blockSave := *block
blockSave.Data = nil
blockInfo := &tmtypes.TendermintBlockInfo{
SeenCommit: seenCommit,
LastCommit: lastCommit,
State: state,
Proposal: proposal,
Block: &blockNoTxs,
State: state,
Block: &blockSave,
}
tendermintlog.Debug("CreateBlockInfoTx", "validators", blockInfo.State.Validators.Validators, "block", block, "block-notxs", blockNoTxs)
tendermintlog.Debug("CreateBlockInfoTx", "blockInfo", blockInfo)
nput := &tmtypes.ValNodeAction_BlockInfo{BlockInfo: blockInfo}
action := &tmtypes.ValNodeAction{Value: nput, Ty: tmtypes.ValNodeActionBlockInfo}
......
package tendermint
import (
"fmt"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
tmtypes "github.com/33cn/plugin/plugin/dapp/valnode/types"
)
var (
stateKey = []byte("stateKey")
)
type ConsensusStore struct {
db dbm.DB
}
// NewConsensusStore returns a new ConsensusStore with the given DB
func NewConsensusStore() *ConsensusStore {
db := DefaultDBProvider("state")
db.SetCacheSize(100)
return &ConsensusStore{
db: db,
}
}
// LoadStateFromStore
func (cs *ConsensusStore) LoadStateFromStore() *tmtypes.State {
buf, err := cs.db.Get(stateKey)
if err != nil {
tendermintlog.Error("LoadStateFromStore", "err", err)
return nil
}
state := &tmtypes.State{}
err = types.Decode(buf, state)
if err != nil {
panic(err)
}
return state
}
// LoadStateHeight
func (cs *ConsensusStore) LoadStateHeight() int64 {
state := cs.LoadStateFromStore()
if state == nil {
return int64(0)
}
return state.LastBlockHeight
}
// LoadSeenCommit by height
func (cs *ConsensusStore) LoadSeenCommit(height int64) *tmtypes.TendermintCommit {
buf, err := cs.db.Get(calcSeenCommitKey(height))
if err != nil {
tendermintlog.Error("LoadSeenCommit", "err", err)
return nil
}
commit := &tmtypes.TendermintCommit{}
err = types.Decode(buf, commit)
if err != nil {
panic(err)
}
return commit
}
// SaveConsensusState save state and seenCommit
func (cs *ConsensusStore) SaveConsensusState(height int64, state *tmtypes.State, sc *tmtypes.TendermintCommit) error {
seenCommitBytes := types.Encode(sc)
stateBytes := types.Encode(state)
batch := cs.db.NewBatch(true)
batch.Set(calcSeenCommitKey(height), seenCommitBytes)
batch.Set(stateKey, stateBytes)
err := batch.Write()
if err != nil {
tendermintlog.Error("SaveConsensusState batch.Write", "err", err)
return err
}
return nil
}
func calcSeenCommitKey(height int64) []byte {
return []byte(fmt.Sprintf("SC:%v", height))
}
This diff is collapsed.
......@@ -68,12 +68,12 @@ func TendermintPerf() {
for err != nil {
err = createConn()
}
time.Sleep(10 * time.Second)
time.Sleep(2 * time.Second)
for i := 0; i < loopCount; i++ {
NormPut()
time.Sleep(time.Second)
}
time.Sleep(10 * time.Second)
time.Sleep(2 * time.Second)
}
func initEnvTendermint() (queue.Queue, *blockchain.BlockChain, queue.Module, queue.Module, *executor.Executor, queue.Module, queue.Module) {
......
......@@ -58,18 +58,19 @@ type TendermintBlock struct {
// MakeBlock returns a new block with an empty header, except what can be computed from itself.
// It populates the same set of fields validated by ValidateBasic
func MakeBlock(height int64, round int64, Txs []*types.Transaction, commit *tmtypes.TendermintCommit) *TendermintBlock {
block := &TendermintBlock{&tmtypes.TendermintBlock{
Header: &tmtypes.TendermintBlockHeader{
Height: height,
Round: round,
Time: time.Now().UnixNano(),
NumTxs: int64(len(Txs)),
func MakeBlock(height int64, round int64, pblock *types.Block, commit *tmtypes.TendermintCommit) *TendermintBlock {
block := &TendermintBlock{
&tmtypes.TendermintBlock{
Header: &tmtypes.TendermintBlockHeader{
Height: height,
Round: round,
Time: pblock.BlockTime,
NumTxs: int64(len(pblock.Txs)),
},
Data: pblock,
LastCommit: commit,
Evidence: &tmtypes.EvidenceData{Evidence: make([]*tmtypes.EvidenceEnvelope, 0)},
},
Txs: Txs,
LastCommit: commit,
Evidence: &tmtypes.EvidenceData{Evidence: make([]*tmtypes.EvidenceEnvelope, 0)},
},
}
block.FillHeader()
return block
......@@ -97,7 +98,7 @@ func (b *TendermintBlock) AddEvidence(evidence []Evidence) {
// ValidateBasic performs basic validation that doesn't involve state data.
// It checks the internal consistency of the block.
func (b *TendermintBlock) ValidateBasic() (int64, error) {
newTxs := int64(len(b.Txs))
newTxs := int64(len(b.Data.Txs))
if b.Header.NumTxs != newTxs {
return 0, fmt.Errorf("Wrong Block.Header.NumTxs. Expected %v, got %v", newTxs, b.Header.NumTxs)
......@@ -247,17 +248,13 @@ func (h *Header) StringIndented(indent string) string {
// Commit struct
type Commit struct {
*tmtypes.TendermintCommit
firstPrecommit *tmtypes.Vote
hash []byte
bitArray *BitArray
firstPrecommit *tmtypes.Vote
}
// FirstPrecommit returns the first non-nil precommit in the commit
func (commit *Commit) FirstPrecommit() *tmtypes.Vote {
if len(commit.Precommits) == 0 {
return nil
}
if commit.firstPrecommit != nil {
return commit.firstPrecommit
}
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import "errors"
var (
// ErrBaseTxType error type
ErrBaseTxType = errors.New("ErrBaseTxType")
// ErrBlockInfoTx error type
ErrBlockInfoTx = errors.New("ErrBlockInfoTx")
// ErrBaseExecErr error type
ErrBaseExecErr = errors.New("ErrBaseExecErr")
// ErrLastBlockID error type
ErrLastBlockID = errors.New("ErrLastBlockID")
)
......@@ -440,7 +440,7 @@ func (pv *PrivValidatorImp) SignHeartbeat(chainID string, heartbeat *Heartbeat)
// String returns a string representation of the PrivValidatorImp.
func (pv *PrivValidatorImp) String() string {
return Fmt("PrivValidator{%v LH:%v, LR:%v, LS:%v}", pv.GetAddress(), pv.LastHeight, pv.LastRound, pv.LastStep)
return Fmt("PrivValidator{%X LH:%v, LR:%v, LS:%v}", pv.GetAddress(), pv.LastHeight, pv.LastRound, pv.LastStep)
}
// GetLastHeight ...
......
......@@ -39,10 +39,11 @@ const (
ProposalPOLID = byte(0x05)
VoteID = byte(0x06)
HasVoteID = byte(0x07)
VoteSetMaj23ID = byte(0X08)
VoteSetMaj23ID = byte(0x08)
VoteSetBitsID = byte(0x09)
ProposalHeartbeatID = byte(0x0a)
ProposalBlockID = byte(0x0b)
ValidBlockID = byte(0x0c)
PacketTypePing = byte(0xff)
PacketTypePong = byte(0xfe)
......@@ -62,6 +63,7 @@ func InitMessageMap() {
VoteSetBitsID: reflect.TypeOf(tmtypes.VoteSetBitsMsg{}),
ProposalHeartbeatID: reflect.TypeOf(tmtypes.Heartbeat{}),
ProposalBlockID: reflect.TypeOf(tmtypes.TendermintBlock{}),
ValidBlockID: reflect.TypeOf(tmtypes.ValidBlockMsg{}),
}
}
......@@ -108,6 +110,8 @@ type RoundState struct {
ProposalBlock *TendermintBlock
LockedRound int
LockedBlock *TendermintBlock
ValidRound int // Last known round with POL for non-nil valid block.
ValidBlock *TendermintBlock // Last known block of POL mentioned above.
Votes *HeightVoteSet
CommitRound int
LastCommit *VoteSet // Last precommits at Height-1
......@@ -117,9 +121,9 @@ type RoundState struct {
// RoundStateMessage ...
func (rs *RoundState) RoundStateMessage() *tmtypes.NewRoundStepMsg {
return &tmtypes.NewRoundStepMsg{
Height: rs.Height,
Round: int32(rs.Round),
Step: int32(rs.Step),
Height: rs.Height,
Round: int32(rs.Round),
Step: int32(rs.Step),
SecondsSinceStartTime: int32(time.Since(rs.StartTime).Seconds()),
LastCommitRound: int32(rs.LastCommit.Round()),
}
......@@ -141,6 +145,8 @@ func (rs *RoundState) StringIndented(indent string) string {
%s ProposalBlock: %v
%s LockedRound: %v
%s LockedBlock: %v
%s ValidRound: %v
%s ValidBlock: %v
%s Votes: %v
%s LastCommit: %v
%s LastValidators:%v
......@@ -153,6 +159,8 @@ func (rs *RoundState) StringIndented(indent string) string {
indent, rs.ProposalBlock.StringShort(),
indent, rs.LockedRound,
indent, rs.LockedBlock.StringShort(),
indent, rs.ValidRound,
indent, rs.ValidBlock.StringShort(),
indent, rs.Votes.StringIndented(indent+" "),
indent, rs.LastCommit.StringShort(),
indent, rs.LastValidators.StringIndented(indent+" "),
......@@ -173,14 +181,15 @@ type PeerRoundState struct {
StartTime time.Time // Estimated start of round 0 at this height
Proposal bool // True if peer has proposal for this round
ProposalBlock bool // True if peer has proposal block for this round
ProposalPOLRound int // Proposal's POL round. -1 if none.
ProposalPOL *BitArray // nil until ProposalPOLMessage received.
Prevotes *BitArray // All votes peer has for this round
Precommits *BitArray // All precommits peer has for this round
LastCommitRound int // Round of commit for last height. -1 if none.
LastCommit *BitArray // All commit precommits of commit for last height.
CatchupCommitRound int // Round that we have commit for. Not necessarily unique. -1 if none.
CatchupCommit *BitArray // All commit precommits peer has for this height & CatchupCommitRound
ProposalBlockHash []byte
ProposalPOLRound int // Proposal's POL round. -1 if none.
ProposalPOL *BitArray // nil until ProposalPOLMessage received.
Prevotes *BitArray // All votes peer has for this round
Precommits *BitArray // All precommits peer has for this round
LastCommitRound int // Round of commit for last height. -1 if none.
LastCommit *BitArray // All commit precommits of commit for last height.
CatchupCommitRound int // Round that we have commit for. Not necessarily unique. -1 if none.
CatchupCommit *BitArray // All commit precommits peer has for this height & CatchupCommitRound
}
// String returns a string representation of the PeerRoundState
......@@ -194,6 +203,7 @@ func (prs PeerRoundState) StringIndented(indent string) string {
%s %v/%v/%v @%v
%s Proposal %v
%s ProposalBlock %v
%s ProposalBlockHash %X
%s POL %v (round %v)
%s Prevotes %v
%s Precommits %v
......@@ -203,6 +213,7 @@ func (prs PeerRoundState) StringIndented(indent string) string {
indent, prs.Height, prs.Round, prs.Step, prs.StartTime,
indent, prs.Proposal,
indent, prs.ProposalBlock,
indent, prs.ProposalBlock,
indent, prs.ProposalPOL, prs.ProposalPOLRound,
indent, prs.Prevotes,
indent, prs.Precommits,
......
......@@ -70,7 +70,7 @@ func NewProposal(height int64, round int, blockhash []byte, polRound int, polBlo
// String returns a string representation of the Proposal.
func (p *Proposal) String() string {
return fmt.Sprintf("Proposal{%v/%v (%v,%v) %X %v @ %s}",
return fmt.Sprintf("Proposal{%v/%v (%v, %X) %X %X @ %s}",
p.Height, p.Round, p.POLRound, p.POLBlockID,
p.Blockhash, p.Signature, CanonicalTime(time.Unix(0, p.Timestamp)))
}
......@@ -211,7 +211,7 @@ func (vote *Vote) String() string {
PanicSanity("Unknown vote type")
}
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v @ %s}",
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
vote.ValidatorIndex, Fingerprint(vote.ValidatorAddress),
vote.Height, vote.Round, vote.Type, typeString,
Fingerprint(vote.BlockID.Hash), vote.Signature,
......
......@@ -70,7 +70,7 @@ func (v *Validator) String() string {
if v == nil {
return "nil-Validator"
}
return Fmt("Validator{%v %v VP:%v A:%v}",
return Fmt("Validator{ADDR:%X PUB:%X VP:%v A:%v}",
v.Address,
v.PubKey,
v.VotingPower,
......
syntax = "proto3";
import "transaction.proto";
import "blockchain.proto";
package types;
......@@ -30,11 +30,9 @@ message TendermintCommit {
}
message TendermintBlockInfo {
TendermintCommit SeenCommit = 1;
TendermintCommit LastCommit = 2;
State State = 3;
Proposal Proposal = 4;
TendermintBlock block = 5;
State State = 2;
Proposal Proposal = 3;
TendermintBlock block = 4;
}
message BlockSize {
......@@ -118,14 +116,14 @@ message TendermintBlockHeader {
bytes appHash = 11;
bytes lastResultsHash = 12;
bytes evidenceHash = 13;
bytes proposerAddr = 14;
}
message TendermintBlock {
TendermintBlockHeader header = 1;
repeated Transaction txs = 2;
EvidenceData evidence = 3;
TendermintCommit lastCommit = 4;
bytes proposerAddr = 5;
TendermintBlockHeader header = 1;
Block data = 2;
EvidenceData evidence = 3;
TendermintCommit lastCommit = 4;
}
message Proposal {
......@@ -146,6 +144,13 @@ message NewRoundStepMsg {
int32 lastCommitRound = 5;
}
message ValidBlockMsg {
int64 height = 1;
int32 round = 2;
bytes blockhash = 3;
bool isCommit = 4;
}
message CommitStepMsg {
int64 height = 1;
}
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment