Skip to content

Commit

Permalink
Fix condition check for tx key dissemination (#149)
Browse files Browse the repository at this point in the history
* Fix condition check for tx key dissemination

* chckpoint

* Fix test
  • Loading branch information
philipsu522 committed Jun 22, 2023
1 parent cbbd14c commit 6762b3e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,11 @@ func (cs *State) fsyncAndCompleteProposal(ctx context.Context, fsyncUponCompleti
cs.handleCompleteProposal(ctx, height, span)
}

// We only used tx key based dissemination if configured to do so and we are a validator
func (cs *State) gossipTransactionKeyOnly() bool {
return cs.config.GossipTransactionKeyOnly && cs.privValidatorPubKey != nil
}

// state transitions on complete-proposal, 2/3-any, 2/3-one
func (cs *State) handleMsg(ctx context.Context, mi msgInfo, fsyncUponCompletion bool) {
cs.mtx.Lock()
Expand All @@ -1047,14 +1052,14 @@ func (cs *State) handleMsg(ctx context.Context, mi msgInfo, fsyncUponCompletion

// will not cause transition.
// once proposal is set, we can receive block parts
err = cs.setProposal(msg.Proposal, mi.ReceiveTime)
// See if we can try creating the proposal block if keys exist
if err != nil && cs.config.GossipTransactionKeyOnly && cs.privValidatorPubKey != nil {
isProposer := cs.isProposer(cs.privValidatorPubKey.Address())
if !isProposer && cs.roundState.ProposalBlock() == nil {
created := cs.tryCreateProposalBlock(spanCtx, msg.Proposal.Height, msg.Proposal.Round, msg.Proposal.Header, msg.Proposal.LastCommit, msg.Proposal.Evidence, msg.Proposal.ProposerAddress)
if created {
cs.fsyncAndCompleteProposal(ctx, fsyncUponCompletion, msg.Proposal.Height, span)
if err = cs.setProposal(msg.Proposal, mi.ReceiveTime); err == nil {
if cs.gossipTransactionKeyOnly() {
isProposer := cs.isProposer(cs.privValidatorPubKey.Address())
if !isProposer && cs.roundState.ProposalBlock() == nil {
created := cs.tryCreateProposalBlock(spanCtx, msg.Proposal.Height, msg.Proposal.Round, msg.Proposal.Header, msg.Proposal.LastCommit, msg.Proposal.Evidence, msg.Proposal.ProposerAddress)
if created {
cs.fsyncAndCompleteProposal(ctx, fsyncUponCompletion, msg.Proposal.Height, span)
}
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions internal/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,49 @@ func TestStateOutputsBlockPartsStats(t *testing.T) {

}

func TestGossipTransactionKeyOnlyConfig(t *testing.T) {
config := configSetup(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cs1, vss := makeState(ctx, t, makeStateArgs{config: config, validators: 2})
vs2 := vss[1]
cs1.config.GossipTransactionKeyOnly = true
propBlock, err := cs1.createProposalBlock(ctx)
require.NoError(t, err)
height, round := cs1.roundState.Height(), cs1.roundState.Round()

// make the second validator the proposer by incrementing the round
round++
incrementRound(vss[1:]...)
propBlockParts, err := propBlock.MakePartSet(types.BlockPartSizeBytes)
require.NoError(t, err)
blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
pubKey, err := vss[1].PrivValidator.GetPubKey(ctx)
require.NoError(t, err)
proposal := *types.NewProposal(height, round, -1, blockID, propBlock.Time, propBlock.GetTxKeys(), propBlock.Header, propBlock.LastCommit, propBlock.Evidence, pubKey.Address())
p := proposal.ToProto()
err = vs2.SignProposal(ctx, config.ChainID(), p)
require.NoError(t, err)
proposal.Signature = p.Signature

proposalMsg := ProposalMessage{&proposal}
peerID, err := types.NewNodeID("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
startTestRound(ctx, cs1, height, round)
cs1.handleMsg(ctx, msgInfo{&proposalMsg, peerID, time.Now()}, false)
rs := cs1.GetRoundState()
// Proposal, ProposalBlock and ProposalBlockParts sohuld be set since gossip-tx-key is true
if rs.Proposal == nil {
t.Error("rs.Proposal should be set")
}
if rs.ProposalBlock == nil {
t.Error("rs.ProposalBlock should be set")
}
if rs.ProposalBlockParts.Total() == 0 {
t.Error("rs.ProposalBlockParts should be set")
}
}

func TestStateOutputVoteStats(t *testing.T) {
config := configSetup(t)
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 6762b3e

Please sign in to comment.