From 07597dfd45dc514d0c2f7fc0690faf4ac4929771 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 16 Nov 2016 16:13:17 -0500 Subject: [PATCH] post rebase fixes for BlockID, partSize --- consensus/state.go | 2 +- glide.lock | 2 +- node/node.go | 4 ++-- state/execution.go | 17 +++++++++-------- state/execution_test.go | 35 +++++++++++++++++++++-------------- state/state.go | 2 +- types/block.go | 4 ++-- types/protobuf.go | 12 +++++++++--- 8 files changed, 46 insertions(+), 32 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 0dc80810e..59bea2315 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -933,7 +933,7 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts txs := cs.mempool.Reap(cs.config.GetInt("block_size")) return types.MakeBlock(cs.Height, cs.state.ChainID, txs, commit, - cs.state.LastBlockID, cs.state.Validators.Hash(), cs.state.AppHash) + cs.state.LastBlockID, cs.state.Validators.Hash(), cs.state.AppHash, cs.config.GetInt("block_part_size")) } // Enter: `timeoutPropose` after entering Propose. diff --git a/glide.lock b/glide.lock index 55f0b7716..613a97ec6 100644 --- a/glide.lock +++ b/glide.lock @@ -92,7 +92,7 @@ imports: subpackages: - term - name: github.com/tendermint/tmsp - version: fec038bdec3495a2a06c6aa8f63e9716bad335dd + version: 60e0842ef9a87c840d0bf95eea7b54a1e3d312b3 subpackages: - client - example/counter diff --git a/node/node.go b/node/node.go index 8b8fe16d6..f4a04d82c 100644 --- a/node/node.go +++ b/node/node.go @@ -63,7 +63,7 @@ func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreato state := sm.GetState(config, stateDB) // Create the proxyApp, which manages connections (consensus, mempool, query) - proxyApp := proxy.NewAppConns(config, clientCreator, sm.NewHandshaker(state, blockStore)) + proxyApp := proxy.NewAppConns(config, clientCreator, sm.NewHandshaker(config, state, blockStore)) if _, err := proxyApp.Start(); err != nil { Exit(Fmt("Error starting proxy app connections: %v", err)) } @@ -380,7 +380,7 @@ func newConsensusState(config cfg.Config) *consensus.ConsensusState { state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file")) // Create proxyAppConn connection (consensus, mempool, query) - proxyApp := proxy.NewAppConns(config, proxy.DefaultClientCreator(config), sm.NewHandshaker(state, blockStore)) + proxyApp := proxy.NewAppConns(config, proxy.DefaultClientCreator(config), sm.NewHandshaker(config, state, blockStore)) _, err := proxyApp.Start() if err != nil { Exit(Fmt("Error starting proxy app conns: %v", err)) diff --git a/state/execution.go b/state/execution.go index 299abbbf3..c52be41dc 100644 --- a/state/execution.go +++ b/state/execution.go @@ -7,6 +7,7 @@ import ( "github.com/ebuchman/fail-test" . "github.com/tendermint/go-common" + cfg "github.com/tendermint/go-config" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" tmsp "github.com/tendermint/tmsp/types" @@ -261,14 +262,15 @@ type BlockStore interface { } type Handshaker struct { - state *State - store BlockStore + config cfg.Config + state *State + store BlockStore nBlocks int // number of blocks applied to the state } -func NewHandshaker(state *State, store BlockStore) *Handshaker { - return &Handshaker{state, store, 0} +func NewHandshaker(config cfg.Config, state *State, store BlockStore) *Handshaker { + return &Handshaker{config, state, store, 0} } // TODO: retry the handshake once if it fails the first time @@ -320,7 +322,7 @@ func (h *Handshaker) Handshake(proxyApp proxy.AppConns) error { }*/ header = block.Header - partsHeader = block.MakePartSet().Header() + partsHeader = block.MakePartSet(h.config.GetInt("block_part_size")).Header() } if configInfo != nil { @@ -355,8 +357,7 @@ func (h *Handshaker) ReplayBlocks(appHash []byte, header *types.Header, partsHea lastVals, nextVals := stateCopy.GetValidators() if header == nil { stateCopy.LastBlockHeight = 0 - stateCopy.LastBlockHash = nil - stateCopy.LastBlockParts = types.PartSetHeader{} + stateCopy.LastBlockID = types.BlockID{} // stateCopy.LastBlockTime = ... doesnt matter stateCopy.Validators = nextVals stateCopy.LastValidators = lastVals @@ -422,7 +423,7 @@ func (h *Handshaker) loadApplyBlock(blockIndex int, state *State, appConnConsens block := h.store.LoadBlock(blockIndex) panicOnNilBlock(blockIndex, h.store.Height(), block) // XXX var eventCache types.Fireable // nil - return state.ApplyBlock(eventCache, appConnConsensus, block, block.MakePartSet().Header(), mockMempool{}) + return state.ApplyBlock(eventCache, appConnConsensus, block, block.MakePartSet(h.config.GetInt("block_part_size")).Header(), mockMempool{}) } func panicOnNilBlock(height, bsHeight int, block *types.Block) { diff --git a/state/execution_test.go b/state/execution_test.go index db724de88..dabcada63 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -2,7 +2,7 @@ package state import ( "bytes" - //"fmt" + "fmt" "path" "testing" @@ -16,10 +16,11 @@ import ( ) var ( - privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("handshake_test")) - chainID = "handshake_chain" - nBlocks = 5 - mempool = mockMempool{} + privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("handshake_test")) + chainID = "handshake_chain" + nBlocks = 5 + mempool = mockMempool{} + testPartSize = 65536 ) func TestExecBlock(t *testing.T) { @@ -53,7 +54,7 @@ func testHandshakeReplay(t *testing.T, n int) { state, store := stateAndStore() clientCreator := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.GetString("db_dir"), "1"))) clientCreator2 := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.GetString("db_dir"), "2"))) - proxyApp := proxy.NewAppConns(config, clientCreator, NewHandshaker(state, store)) + proxyApp := proxy.NewAppConns(config, clientCreator, NewHandshaker(config, state, store)) if _, err := proxyApp.Start(); err != nil { t.Fatalf("Error starting proxy app connections: %v", err) } @@ -71,7 +72,7 @@ func testHandshakeReplay(t *testing.T, n int) { state2, _ := stateAndStore() for i := 0; i < n; i++ { block := chain[i] - err := state2.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet().Header(), mempool) + err := state2.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), mempool) if err != nil { t.Fatal(err) } @@ -80,7 +81,7 @@ func testHandshakeReplay(t *testing.T, n int) { } // now start it with the handshake - handshaker := NewHandshaker(state, store) + handshaker := NewHandshaker(config, state, store) proxyApp = proxy.NewAppConns(config, clientCreator2, handshaker) if _, err := proxyApp.Start(); err != nil { t.Fatalf("Error starting proxy app connections: %v", err) @@ -116,11 +117,12 @@ func txsFunc(blockNum int) (txs []types.Tx) { // sign a commit vote func signCommit(height, round int, hash []byte, header types.PartSetHeader) *types.Vote { vote := &types.Vote{ + ValidatorIndex: 0, + ValidatorAddress: privKey.PubKey().Address(), Height: height, Round: round, Type: types.VoteTypePrecommit, - BlockHash: hash, - BlockPartsHeader: header, + BlockID: types.BlockID{hash, header}, } sig := privKey.Sign(types.SignBytes(chainID, vote)) @@ -131,22 +133,26 @@ func signCommit(height, round int, hash []byte, header types.PartSetHeader) *typ // make a blockchain with one validator func makeBlockchain(t *testing.T, proxyApp proxy.AppConns, state *State) (blockchain []*types.Block) { - prevHash := state.LastBlockHash + prevHash := state.LastBlockID.Hash lastCommit := new(types.Commit) prevParts := types.PartSetHeader{} valHash := state.Validators.Hash() + prevBlockID := types.BlockID{prevHash, prevParts} for i := 1; i < nBlocks+1; i++ { block, parts := types.MakeBlock(i, chainID, txsFunc(i), lastCommit, - prevParts, prevHash, valHash, state.AppHash) - err := state.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet().Header(), mempool) + prevBlockID, valHash, state.AppHash, testPartSize) + fmt.Println(i) + fmt.Println(prevBlockID) + fmt.Println(block.LastBlockID) + err := state.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), mempool) if err != nil { t.Fatal(i, err) } voteSet := types.NewVoteSet(chainID, i, 0, types.VoteTypePrecommit, state.Validators) vote := signCommit(i, 0, block.Hash(), parts.Header()) - _, _, err = voteSet.AddByIndex(0, vote) + _, err = voteSet.AddVote(vote) if err != nil { t.Fatal(err) } @@ -155,6 +161,7 @@ func makeBlockchain(t *testing.T, proxyApp proxy.AppConns, state *State) (blockc prevHash = block.Hash() prevParts = parts.Header() lastCommit = voteSet.MakeCommit() + prevBlockID = types.BlockID{prevHash, prevParts} } return blockchain } diff --git a/state/state.go b/state/state.go index 033c24132..4a54dfe6d 100644 --- a/state/state.go +++ b/state/state.go @@ -97,7 +97,7 @@ func (s *State) Bytes() []byte { // Since we don't have the new AppHash yet, we set s.Stale=true func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) { s.LastBlockHeight = header.Height - s.LastBlockID = types.BlockID{block.Hash(), blockPartsHeader} + s.LastBlockID = types.BlockID{header.Hash(), blockPartsHeader} s.LastBlockTime = header.Time s.Validators = nextValSet s.LastValidators = prevValSet diff --git a/types/block.go b/types/block.go index ec04e4ab8..80a59f8a3 100644 --- a/types/block.go +++ b/types/block.go @@ -22,7 +22,7 @@ type Block struct { } func MakeBlock(height int, chainID string, txs []Tx, commit *Commit, - prevBlockID BlockID, valHash, appHash []byte) (*Block, *PartSet) { + prevBlockID BlockID, valHash, appHash []byte, partSize int) (*Block, *PartSet) { block := &Block{ Header: &Header{ ChainID: chainID, @@ -39,7 +39,7 @@ func MakeBlock(height int, chainID string, txs []Tx, commit *Commit, }, } block.FillHeader() - return block, block.MakePartSet() + return block, block.MakePartSet(partSize) } // Basic validation that doesn't involve state data. diff --git a/types/protobuf.go b/types/protobuf.go index 8d2f9b819..a7a95d121 100644 --- a/types/protobuf.go +++ b/types/protobuf.go @@ -12,17 +12,23 @@ type tm2pb struct{} func (tm2pb) Header(header *Header) *types.Header { return &types.Header{ ChainId: header.ChainID, - Height: uint64(header.Height), + Height: int32(header.Height), Time: uint64(header.Time.Unix()), NumTxs: uint64(header.NumTxs), - LastBlockHash: header.LastBlockHash, - LastBlockParts: TM2PB.PartSetHeader(header.LastBlockParts), + LastBlockId: TM2PB.BlockID(header.LastBlockID), LastCommitHash: header.LastCommitHash, DataHash: header.DataHash, AppHash: header.AppHash, } } +func (tm2pb) BlockID(blockID BlockID) *types.BlockID { + return &types.BlockID{ + Hash: blockID.Hash, + Parts: TM2PB.PartSetHeader(blockID.PartsHeader), + } +} + func (tm2pb) PartSetHeader(partSetHeader PartSetHeader) *types.PartSetHeader { return &types.PartSetHeader{ Total: uint64(partSetHeader.Total),