Skip to content

Commit

Permalink
some intermediate progress that will be overwritten anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Mar 24, 2021
1 parent 5717de0 commit 29f160d
Show file tree
Hide file tree
Showing 44 changed files with 1,244 additions and 1,039 deletions.
47 changes: 21 additions & 26 deletions consensus/byzantine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
// allow first height to happen normally so that byzantine validator is no longer proposer
if height == prevoteHeight {
bcs.Logger.Info("Sending two votes")
prevote1, err := bcs.signVote(tmproto.PrevoteType, bcs.ProposalBlock.Hash(), bcs.ProposalBlockParts.Header())
prevote1, err := bcs.signVote(tmproto.PrevoteType, bcs.ProposalBlock.Hash(), bcs.ProposalBlockDAHeader)
require.NoError(t, err)
prevote2, err := bcs.signVote(tmproto.PrevoteType, nil, types.PartSetHeader{})
prevote2, err := bcs.signVote(tmproto.PrevoteType, nil, nil)
require.NoError(t, err)
peerList := reactors[byzantineNode].Switch.Peers().List()
bcs.Logger.Info("Getting peer list", "peers", peerList)
Expand Down Expand Up @@ -374,9 +374,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
// Avoid sending on internalMsgQueue and running consensus state.

// Create a new proposal block from state/txs from the mempool.
block1, blockParts1 := cs.createProposalBlock()
polRound, propBlockID := cs.ValidRound, types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()}
proposal1 := types.NewProposal(height, round, polRound, propBlockID)
block1 := cs.createProposalBlock()
proposal1 := types.NewProposal(height, round, cs.ValidRound, &block1.DataAvailabilityHeader)
p1 := proposal1.ToProto()
if err := cs.privValidator.SignProposal(cs.state.ChainID, p1); err != nil {
t.Error(err)
Expand All @@ -388,9 +387,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
deliverTxsRange(cs, 0, 1)

// Create a new proposal block from state/txs from the mempool.
block2, blockParts2 := cs.createProposalBlock()
polRound, propBlockID = cs.ValidRound, types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()}
proposal2 := types.NewProposal(height, round, polRound, propBlockID)
block2 := cs.createProposalBlock()
proposal2 := types.NewProposal(height, round, cs.ValidRound, &block1.DataAvailabilityHeader)
p2 := proposal2.ToProto()
if err := cs.privValidator.SignProposal(cs.state.ChainID, p2); err != nil {
t.Error(err)
Expand All @@ -406,41 +404,38 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
t.Logf("Byzantine: broadcasting conflicting proposals to %d peers", len(peers))
for i, peer := range peers {
if i < len(peers)/2 {
go sendProposalAndParts(height, round, cs, peer, proposal1, block1Hash, blockParts1)
go sendProposalAndBlock(height, round, cs, peer, proposal1, block1Hash, block1)
} else {
go sendProposalAndParts(height, round, cs, peer, proposal2, block2Hash, blockParts2)
go sendProposalAndBlock(height, round, cs, peer, proposal2, block2Hash, block2)
}
}
}

func sendProposalAndParts(
func sendProposalAndBlock(
height int64,
round int32,
cs *State,
peer p2p.Peer,
proposal *types.Proposal,
blockHash []byte,
parts *types.PartSet,
block *types.Block,
) {
// proposal
msg := &ProposalMessage{Proposal: proposal}
peer.Send(DataChannel, MustEncode(msg))

// parts
for i := 0; i < int(parts.Total()); i++ {
part := parts.GetPart(i)
msg := &BlockPartMessage{
Height: height, // This tells peer that this part applies to us.
Round: round, // This tells peer that this part applies to us.
Part: part,
}
peer.Send(DataChannel, MustEncode(msg))
pmsg := &ProposalMessage{Proposal: proposal}
peer.Send(DataChannel, MustEncode(pmsg))

// block
bmsg := &BlockMessage{
Height: height, // This tells peer that this part applies to us.
Round: round, // This tells peer that this part applies to us.
Block: block,
}
peer.Send(DataChannel, MustEncode(bmsg))

// votes
cs.mtx.Lock()
prevote, _ := cs.signVote(tmproto.PrevoteType, blockHash, parts.Header())
precommit, _ := cs.signVote(tmproto.PrecommitType, blockHash, parts.Header())
prevote, _ := cs.signVote(tmproto.PrevoteType, blockHash, &block.DataAvailabilityHeader)
precommit, _ := cs.signVote(tmproto.PrecommitType, blockHash, &block.DataAvailabilityHeader)
cs.mtx.Unlock()

peer.Send(VoteChannel, MustEncode(&VoteMessage{prevote}))
Expand Down
33 changes: 15 additions & 18 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ func newValidatorStub(privValidator types.PrivValidator, valIndex int32) *valida
}
}

func (vs *validatorStub) signVote(
voteType tmproto.SignedMsgType,
hash []byte,
header types.PartSetHeader) (*types.Vote, error) {

func (vs *validatorStub) signVote(voteType tmproto.SignedMsgType, hash []byte, dah *types.DataAvailabilityHeader) (*types.Vote, error) {
pubKey, err := vs.PrivValidator.GetPubKey()
if err != nil {
return nil, fmt.Errorf("can't get pubkey: %w", err)
Expand All @@ -103,8 +99,10 @@ func (vs *validatorStub) signVote(
Round: vs.Round,
Timestamp: tmtime.Now(),
Type: voteType,
BlockID: types.BlockID{Hash: hash, PartSetHeader: header},
BlockID: types.BlockID{Hash: hash},
DAHeader: dah,
}

v := vote.ToProto()
err = vs.PrivValidator.SignVote(config.ChainID(), v)
vote.Signature = v.Signature
Expand All @@ -113,8 +111,8 @@ func (vs *validatorStub) signVote(
}

// Sign vote for type/hash/header
func signVote(vs *validatorStub, voteType tmproto.SignedMsgType, hash []byte, header types.PartSetHeader) *types.Vote {
v, err := vs.signVote(voteType, hash, header)
func signVote(vs *validatorStub, voteType tmproto.SignedMsgType, hash []byte, dah *types.DataAvailabilityHeader) *types.Vote {
v, err := vs.signVote(voteType, hash, dah)
if err != nil {
panic(fmt.Errorf("failed to sign vote: %v", err))
}
Expand All @@ -124,11 +122,11 @@ func signVote(vs *validatorStub, voteType tmproto.SignedMsgType, hash []byte, he
func signVotes(
voteType tmproto.SignedMsgType,
hash []byte,
header types.PartSetHeader,
dah *types.DataAvailabilityHeader,
vss ...*validatorStub) []*types.Vote {
votes := make([]*types.Vote, len(vss))
for i, vs := range vss {
votes[i] = signVote(vs, voteType, hash, header)
votes[i] = signVote(vs, voteType, hash, dah)
}
return votes
}
Expand Down Expand Up @@ -191,7 +189,7 @@ func decideProposal(
round int32,
) (proposal *types.Proposal, block *types.Block) {
cs1.mtx.Lock()
block, blockParts := cs1.createProposalBlock()
block = cs1.createProposalBlock()
validRound := cs1.ValidRound
chainID := cs1.state.ChainID
cs1.mtx.Unlock()
Expand All @@ -200,8 +198,7 @@ func decideProposal(
}

// Make proposal
polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()}
proposal = types.NewProposal(height, round, polRound, propBlockID)
proposal = types.NewProposal(height, round, validRound, &block.DataAvailabilityHeader)
p := proposal.ToProto()
if err := vs.SignProposal(chainID, p); err != nil {
panic(err)
Expand All @@ -222,10 +219,10 @@ func signAddVotes(
to *State,
voteType tmproto.SignedMsgType,
hash []byte,
header types.PartSetHeader,
dah *types.DataAvailabilityHeader,
vss ...*validatorStub,
) {
votes := signVotes(voteType, hash, header, vss...)
votes := signVotes(voteType, hash, dah, vss...)
addVotes(to, votes...)
}

Expand Down Expand Up @@ -589,7 +586,7 @@ func ensureNewUnlock(unlockCh <-chan tmpubsub.Message, height int64, round int32
"Timeout expired while waiting for NewUnlock event")
}

func ensureProposal(proposalCh <-chan tmpubsub.Message, height int64, round int32, propID types.BlockID) {
func ensureProposal(proposalCh <-chan tmpubsub.Message, height int64, round int32, dah *types.DataAvailabilityHeader) {
select {
case <-time.After(ensureTimeout):
panic("Timeout expired while waiting for NewProposal event")
Expand All @@ -605,8 +602,8 @@ func ensureProposal(proposalCh <-chan tmpubsub.Message, height int64, round int3
if proposalEvent.Round != round {
panic(fmt.Sprintf("expected round %v, got %v", round, proposalEvent.Round))
}
if !proposalEvent.BlockID.Equals(propID) {
panic(fmt.Sprintf("Proposed block does not match expected block (%v != %v)", proposalEvent.BlockID, propID))
if !proposalEvent.DAHeader.Equal(dah) {
panic(fmt.Sprintf("Proposed block does not match expected block (%v != %v)", proposalEvent.DAHeader, dah))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func invalidDoPrevoteFunc(t *testing.T, height int64, round int32, cs *State, sw
Type: tmproto.PrecommitType,
BlockID: types.BlockID{
Hash: blockHash,
PartSetHeader: types.PartSetHeader{Total: 1, Hash: tmrand.Bytes(32)}},
},
}
p := precommit.ToProto()
err = cs.privValidator.SignVote(cs.state.ChainID, p)
Expand Down
42 changes: 16 additions & 26 deletions consensus/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ func MsgToProto(msg Message) (*tmcons.Message, error) {
},
}
case *NewValidBlockMessage:
pbPartSetHeader := msg.BlockPartSetHeader.ToProto()
pbBits := msg.BlockParts.ToProto()
pb = tmcons.Message{
Sum: &tmcons.Message_NewValidBlock{
NewValidBlock: &tmcons.NewValidBlock{
Height: msg.Height,
Round: msg.Round,
BlockPartSetHeader: pbPartSetHeader,
BlockParts: pbBits,
DAHeader: msg.BlockDAHeader.ToProto(),
IsCommit: msg.IsCommit,
},
},
Expand All @@ -69,17 +66,17 @@ func MsgToProto(msg Message) (*tmcons.Message, error) {
},
},
}
case *BlockPartMessage:
parts, err := msg.Part.ToProto()
case *BlockMessage:
block, err := msg.Block.ToProto()
if err != nil {
return nil, fmt.Errorf("msg to proto error: %w", err)
}
pb = tmcons.Message{
Sum: &tmcons.Message_BlockPart{
BlockPart: &tmcons.BlockPart{
Sum: &tmcons.Message_Block{
Block: &tmcons.Block{
Height: msg.Height,
Round: msg.Round,
Part: *parts,
Block: block,
},
},
}
Expand Down Expand Up @@ -165,22 +162,15 @@ func MsgFromProto(msg *tmcons.Message) (Message, error) {
LastCommitRound: msg.NewRoundStep.LastCommitRound,
}
case *tmcons.Message_NewValidBlock:
pbPartSetHeader, err := types.PartSetHeaderFromProto(&msg.NewValidBlock.BlockPartSetHeader)
dah, err := types.DataAvailabilityHeaderFromProto(msg.NewValidBlock.DAHeader)
if err != nil {
return nil, fmt.Errorf("parts header to proto error: %w", err)
}

pbBits := new(bits.BitArray)
err = pbBits.FromProto(msg.NewValidBlock.BlockParts)
if err != nil {
return nil, fmt.Errorf("parts to proto error: %w", err)
return nil, fmt.Errorf("DAHeader to proto error: %w", err)
}

pb = &NewValidBlockMessage{
Height: msg.NewValidBlock.Height,
Round: msg.NewValidBlock.Round,
BlockPartSetHeader: *pbPartSetHeader,
BlockParts: pbBits,
BlockDAHeader: dah,
IsCommit: msg.NewValidBlock.IsCommit,
}
case *tmcons.Message_Proposal:
Expand All @@ -203,15 +193,15 @@ func MsgFromProto(msg *tmcons.Message) (Message, error) {
ProposalPOLRound: msg.ProposalPol.ProposalPolRound,
ProposalPOL: pbBits,
}
case *tmcons.Message_BlockPart:
parts, err := types.PartFromProto(&msg.BlockPart.Part)
case *tmcons.Message_Block:
block, err := types.BlockFromProto(msg.Block.Block)
if err != nil {
return nil, fmt.Errorf("blockpart msg to proto error: %w", err)
return nil, fmt.Errorf("block msg to proto error: %w", err)
}
pb = &BlockPartMessage{
Height: msg.BlockPart.Height,
Round: msg.BlockPart.Round,
Part: parts,
pb = &BlockMessage{
Height: msg.Block.Height,
Round: msg.Block.Round,
Block: block,
}
case *tmcons.Message_Vote:
vote, err := types.VoteFromProto(msg.Vote.Vote)
Expand Down
Loading

0 comments on commit 29f160d

Please sign in to comment.