Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store proof, round on block #45

Merged
merged 9 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ program](https://hackerone.com/tendermint).

- Go API

- Blockchain Protocol

- [state] [\#7](https://github.com/line/tendermint/issues/7) Add round, proof in block

### FEATURES:
- [types] [\#40](https://github.com/line/tendermint/issues/40) Add vrf interface and add a function generating vrf proof to PrivValidator

### IMPROVEMENTS:

Expand Down
8 changes: 5 additions & 3 deletions blockchain/v0/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func newBlockchainReactor(
lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
}

thisBlock := makeBlock(blockHeight, state, lastCommit)
thisBlock := makeBlock(privVals[0], blockHeight, state, lastCommit)

thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
blockID := types.BlockID{Hash: thisBlock.Hash(), PartsHeader: thisParts.Header()}
Expand Down Expand Up @@ -345,8 +345,10 @@ func makeTxs(height int64) (txs []types.Tx) {
return txs
}

func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
func makeBlock(privVal types.PrivValidator, height int64, state sm.State, lastCommit *types.Commit) *types.Block {
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address, 0, proof)
return block
}

Expand Down
8 changes: 5 additions & 3 deletions blockchain/v1/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newBlockchainReactor(
lastCommit = types.NewCommit(vote.Height, vote.Round, lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
}

thisBlock := makeBlock(blockHeight, state, lastCommit)
thisBlock := makeBlock(privVals[0], blockHeight, state, lastCommit)

thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
blockID := types.BlockID{Hash: thisBlock.Hash(), PartsHeader: thisParts.Header()}
Expand Down Expand Up @@ -419,8 +419,10 @@ func makeTxs(height int64) (txs []types.Tx) {
return txs
}

func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
func makeBlock(privVal types.PrivValidator, height int64, state sm.State, lastCommit *types.Commit) *types.Block {
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address, 0, proof)
return block
}

Expand Down
4 changes: 3 additions & 1 deletion consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,9 @@ func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.Bloc
lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
}

return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address, 0, proof)
}

type badApp struct {
Expand Down
17 changes: 13 additions & 4 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/pkg/errors"

"github.com/tendermint/tendermint/libs/fail"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
Expand Down Expand Up @@ -899,7 +898,6 @@ func (cs *State) enterPropose(height int64, round int) {
return
}
logger.Info(fmt.Sprintf("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step))

defer func() {
// Done enterPropose:
cs.updateRoundStep(round, cstypes.RoundStepPropose)
Expand Down Expand Up @@ -1026,7 +1024,19 @@ func (cs *State) createProposalBlock() (block *types.Block, blockParts *types.Pa
}

proposerAddr := cs.privValidator.GetPubKey().Address()
return cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr)

message, err := cs.state.MakeHashMessage(cs.Round)
if err != nil {
cs.Logger.Error("enterPropose: Cannot generate vrf message: %s", err.Error())
return
}

proof, err := cs.privValidator.GenerateVRFProof(message)
if err != nil {
cs.Logger.Error("enterPropose: Cannot generate vrf proof: %s", err.Error())
return
}
return cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr, cs.Round, proof)
}

// Enter: `timeoutPropose` after entering Propose.
Expand Down Expand Up @@ -1448,7 +1458,6 @@ func (cs *State) finalizeCommit(height int64) {

// NewHeightStep!
cs.updateToState(stateCopy)

fail.Fail() // XXX

// cs.StartTime is already set.
Expand Down
14 changes: 11 additions & 3 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestCreateProposalBlock(t *testing.T) {
logger := log.TestingLogger()

var height int64 = 1
state, stateDB := state(1, height)
state, stateDB, privVal := state(1, height)
maxBytes := 16384
state.ConsensusParams.Block.MaxBytes = int64(maxBytes)
proposerAddr, _ := state.Validators.GetByIndex(0)
Expand Down Expand Up @@ -280,10 +280,14 @@ func TestCreateProposalBlock(t *testing.T) {
)

commit := types.NewCommit(height-1, 0, types.BlockID{}, nil)
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
block, _ := blockExec.CreateProposalBlock(
height,
state, commit,
proposerAddr,
0,
proof,
)

err = blockExec.ValidateBlock(state, block)
Expand Down Expand Up @@ -323,11 +327,15 @@ func TestNodeNewNodeCustomReactors(t *testing.T) {
assert.Equal(t, customBlockchainReactor, n.Switch().Reactor("BLOCKCHAIN"))
}

func state(nVals int, height int64) (sm.State, dbm.DB) {
func state(nVals int, height int64) (sm.State, dbm.DB, types.PrivValidator) {
vals := make([]types.GenesisValidator, nVals)
var privVal types.PrivValidator
for i := 0; i < nVals; i++ {
secret := []byte(fmt.Sprintf("test%d", i))
pk := ed25519.GenPrivKeyFromSecret(secret)
if privVal == nil {
privVal = types.NewMockPVWithParams(pk, false, false)
}
vals[i] = types.GenesisValidator{
Address: pk.PubKey().Address(),
PubKey: pk.PubKey(),
Expand All @@ -350,5 +358,5 @@ func state(nVals int, height int64) (sm.State, dbm.DB) {
s.LastValidators = s.Validators.Copy()
sm.SaveState(stateDB, s)
}
return s, stateDB
return s, stateDB, privVal
}
3 changes: 1 addition & 2 deletions privval/signer_requestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func DefaultValidationRequestHandler(
}

case *VRFProofRequest:
message := r.Message
proof, err := privVal.GenerateVRFProof(message)
proof, err := privVal.GenerateVRFProof(r.Message)
if err != nil {
res = &VRFProofResponse{nil, &RemoteSignerError{0, err.Error()}}
} else {
Expand Down
6 changes: 5 additions & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/vrf"
"github.com/tendermint/tendermint/libs/fail"
"github.com/tendermint/tendermint/libs/log"
mempl "github.com/tendermint/tendermint/mempool"
Expand Down Expand Up @@ -92,6 +93,8 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
height int64,
state State, commit *types.Commit,
proposerAddr []byte,
round int,
proof vrf.Proof,
) (*types.Block, *types.PartSet) {

maxBytes := state.ConsensusParams.Block.MaxBytes
Expand All @@ -105,7 +108,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
maxDataBytes := types.MaxDataBytes(maxBytes, state.Validators.Size(), len(evidence))
txs := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas)

return state.MakeBlock(height, txs, commit, evidence, proposerAddr)
return state.MakeBlock(height, txs, commit, evidence, proposerAddr, round, proof)
}

// ValidateBlock validates the given block against the given state.
Expand Down Expand Up @@ -430,6 +433,7 @@ func updateState(
LastBlockHeight: header.Height,
LastBlockID: blockID,
LastBlockTime: header.Time,
LastProof: header.Proof.Bytes(),
NextValidators: nValSet,
Validators: state.NextValidators.Copy(),
LastValidators: state.Validators.Copy(),
Expand Down
22 changes: 13 additions & 9 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func TestApplyBlock(t *testing.T) {
require.Nil(t, err)
defer proxyApp.Stop()

state, stateDB, _ := makeState(1, 1)
state, stateDB, privVals := makeState(1, 1)

blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(),
mock.Mempool{}, sm.MockEvidencePool{})

block := makeBlock(state, 1)
block := makeBlockWithPrivVal(state, privVals[state.Validators.Proposer.Address.String()], 1)
blockID := types.BlockID{Hash: block.Hash(), PartsHeader: block.MakePartSet(testPartSize).Header()}

//nolint:ineffassign
Expand All @@ -56,7 +56,7 @@ func TestBeginBlockValidators(t *testing.T) {
require.Nil(t, err)
defer proxyApp.Stop()

state, stateDB, _ := makeState(2, 2)
state, stateDB, privVals := makeState(2, 2)

prevHash := state.LastBlockID.Hash
prevParts := types.PartSetHeader{}
Expand Down Expand Up @@ -88,8 +88,11 @@ func TestBeginBlockValidators(t *testing.T) {
for _, tc := range testCases {
lastCommit := types.NewCommit(1, 0, prevBlockID, tc.lastCommitSigs)

message, _ := state.MakeHashMessage(0)
proof, _ := privVals[state.Validators.GetProposer().Address.String()].GenerateVRFProof(message)

// block for height 2
block, _ := state.MakeBlock(2, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address)
block, _ := state.MakeBlock(2, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address, 0, proof)

_, err = sm.ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger(), stateDB)
require.Nil(t, err, tc.desc)
Expand Down Expand Up @@ -118,7 +121,7 @@ func TestBeginBlockByzantineValidators(t *testing.T) {
require.Nil(t, err)
defer proxyApp.Stop()

state, stateDB, _ := makeState(2, 12)
state, stateDB, privVals := makeState(2, 12)

prevHash := state.LastBlockID.Hash
prevParts := types.PartSetHeader{}
Expand Down Expand Up @@ -156,8 +159,9 @@ func TestBeginBlockByzantineValidators(t *testing.T) {
commitSigs := []types.CommitSig{commitSig0, commitSig1}
lastCommit := types.NewCommit(9, 0, prevBlockID, commitSigs)
for _, tc := range testCases {

block, _ := state.MakeBlock(10, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address)
message, _ := state.MakeHashMessage(0)
proof, _ := privVals[state.Validators.GetProposer().Address.String()].GenerateVRFProof(message)
block, _ := state.MakeBlock(10, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address, 0, proof)
block.Time = now
block.Evidence.Evidence = tc.evidence
_, err = sm.ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger(), stateDB)
Expand Down Expand Up @@ -324,7 +328,7 @@ func TestEndBlockValidatorUpdates(t *testing.T) {
require.Nil(t, err)
defer proxyApp.Stop()

state, stateDB, _ := makeState(1, 1)
state, stateDB, privVals := makeState(1, 1)

blockExec := sm.NewBlockExecutor(
stateDB,
Expand All @@ -347,7 +351,7 @@ func TestEndBlockValidatorUpdates(t *testing.T) {
)
require.NoError(t, err)

block := makeBlock(state, 1)
block := makeBlockWithPrivVal(state, privVals[state.Validators.Proposer.Address.String()], 1)
blockID := types.BlockID{Hash: block.Hash(), PartsHeader: block.MakePartSet(testPartSize).Header()}

pubkey := ed25519.GenPrivKey().PubKey()
Expand Down
21 changes: 18 additions & 3 deletions state/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func makeAndCommitGoodBlock(
privVals map[string]types.PrivValidator,
evidence []types.Evidence) (sm.State, types.BlockID, *types.Commit, error) {
// A good block passes
state, blockID, err := makeAndApplyGoodBlock(state, height, lastCommit, proposerAddr, blockExec, evidence)
state, blockID, err := makeAndApplyGoodBlock(state, privVals[state.Validators.Proposer.Address.String()], height, lastCommit, proposerAddr, blockExec, evidence)
if err != nil {
return state, types.BlockID{}, nil, err
}
Expand All @@ -58,9 +58,11 @@ func makeAndCommitGoodBlock(
return state, blockID, commit, nil
}

func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
func makeAndApplyGoodBlock(state sm.State, privVal types.PrivValidator, height int64, lastCommit *types.Commit, proposerAddr []byte,
blockExec *sm.BlockExecutor, evidence []types.Evidence) (sm.State, types.BlockID, error) {
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, evidence, proposerAddr)
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, evidence, proposerAddr, 0, proof)
if err := blockExec.ValidateBlock(state, block); err != nil {
return state, types.BlockID{}, err
}
Expand Down Expand Up @@ -99,6 +101,11 @@ func makeTxs(height int64) (txs []types.Tx) {
return txs
}

func makePrivVal() types.PrivValidator {
pk := ed25519.GenPrivKeyFromSecret([]byte("test private validator"))
return types.NewMockPVWithParams(pk, false, false)
}

func makeState(nVals, height int) (sm.State, dbm.DB, map[string]types.PrivValidator) {
vals := make([]types.GenesisValidator, nVals)
privVals := make(map[string]types.PrivValidator, nVals)
Expand Down Expand Up @@ -132,12 +139,20 @@ func makeState(nVals, height int) (sm.State, dbm.DB, map[string]types.PrivValida
}

func makeBlock(state sm.State, height int64) *types.Block {
return makeBlockWithPrivVal(state, makePrivVal(), height)
}

func makeBlockWithPrivVal(state sm.State, privVal types.PrivValidator, height int64) *types.Block {
message, _ := state.MakeHashMessage(0)
proof, _ := privVal.GenerateVRFProof(message)
block, _ := state.MakeBlock(
height,
makeTxs(state.LastBlockHeight),
new(types.Commit),
nil,
state.Validators.GetProposer().Address,
0,
proof,
)
return block
}
Expand Down
Loading