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 19, 2021
1 parent 5717de0 commit 1ef2500
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 416 deletions.
22 changes: 10 additions & 12 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())
require.NoError(t, err)
prevote2, err := bcs.signVote(tmproto.PrevoteType, nil, types.PartSetHeader{})
prevote2, err := bcs.signVote(tmproto.PrevoteType, 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,9 +404,9 @@ 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 sendProposalAndParts(height, round, cs, peer, proposal1, block1Hash, nil)
} else {
go sendProposalAndParts(height, round, cs, peer, proposal2, block2Hash, blockParts2)
go sendProposalAndParts(height, round, cs, peer, proposal2, block2Hash, nil)
}
}
}
Expand Down Expand Up @@ -439,8 +437,8 @@ func sendProposalAndParts(

// 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)
precommit, _ := cs.signVote(tmproto.PrecommitType, blockHash)
cs.mtx.Unlock()

peer.Send(VoteChannel, MustEncode(&VoteMessage{prevote}))
Expand Down
47 changes: 31 additions & 16 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type cleanupFunc func()
var (
config *cfg.Config // NOTE: must be reset for each _test.go file
consensusReplayConfig *cfg.Config
ensureTimeout = time.Millisecond * 200
ensureTimeout = time.Millisecond * 20000000
)

func ensureDir(dir string, mode os.FileMode) {
Expand Down 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) (*types.Vote, error) {
pubKey, err := vs.PrivValidator.GetPubKey()
if err != nil {
return nil, fmt.Errorf("can't get pubkey: %w", err)
Expand All @@ -103,7 +99,7 @@ func (vs *validatorStub) signVote(
Round: vs.Round,
Timestamp: tmtime.Now(),
Type: voteType,
BlockID: types.BlockID{Hash: hash, PartSetHeader: header},
BlockID: types.BlockID{Hash: hash},
}
v := vote.ToProto()
err = vs.PrivValidator.SignVote(config.ChainID(), v)
Expand All @@ -113,8 +109,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) *types.Vote {
v, err := vs.signVote(voteType, hash)
if err != nil {
panic(fmt.Errorf("failed to sign vote: %v", err))
}
Expand All @@ -124,11 +120,10 @@ func signVote(vs *validatorStub, voteType tmproto.SignedMsgType, hash []byte, he
func signVotes(
voteType tmproto.SignedMsgType,
hash []byte,
header types.PartSetHeader,
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)
}
return votes
}
Expand Down Expand Up @@ -191,7 +186,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 +195,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 +216,9 @@ func signAddVotes(
to *State,
voteType tmproto.SignedMsgType,
hash []byte,
header types.PartSetHeader,
vss ...*validatorStub,
) {
votes := signVotes(voteType, hash, header, vss...)
votes := signVotes(voteType, hash, vss...)
addVotes(to, votes...)
}

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

func ensureDAProposal(proposalCh <-chan tmpubsub.Message, height int64, round int32, dah types.DataAvailabilityHeader) {
select {
case <-time.After(ensureTimeout):
panic("Timeout expired while waiting for NewProposal event")
case msg := <-proposalCh:
proposalEvent, ok := msg.Data().(types.EventDataCompleteDAProposal)
if !ok {
panic(fmt.Sprintf("expected a EventDataCompleteDAProposal, got %T. Wrong subscription channel?",
msg.Data()))
}
if proposalEvent.Height != height {
panic(fmt.Sprintf("expected height %v, got %v", height, proposalEvent.Height))
}
if proposalEvent.Round != round {
panic(fmt.Sprintf("expected round %v, got %v", round, proposalEvent.Round))
}
if !proposalEvent.DAHeader.Equal(&dah) {
panic(fmt.Sprintf("Proposed block does not match expected proposal (%v != %v)", proposalEvent.DAHeader, dah))
}
}
}

func ensureProposal(proposalCh <-chan tmpubsub.Message, height int64, round int32, propID types.BlockID) {
select {
case <-time.After(ensureTimeout):
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
6 changes: 2 additions & 4 deletions consensus/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestMsgToProto(t *testing.T) {
pbPsh := psh.ToProto()
bi := types.BlockID{
Hash: tmrand.Bytes(32),
PartSetHeader: psh,
}
pbBi := bi.ToProto()
bits := bits.NewBitArray(1)
Expand All @@ -51,7 +50,7 @@ func TestMsgToProto(t *testing.T) {
Height: 1,
Round: 1,
POLRound: 1,
BlockID: bi,
DAHeader: types.DataAvailabilityHeader{},
Timestamp: time.Now(),
Signature: tmrand.Bytes(20),
}
Expand Down Expand Up @@ -325,7 +324,6 @@ func TestConsMsgsVectors(t *testing.T) {

bi := types.BlockID{
Hash: []byte("add_more_exclamation_marks_code-"),
PartSetHeader: psh,
}
pbBi := bi.ToProto()
bits := bits.NewBitArray(1)
Expand All @@ -349,7 +347,7 @@ func TestConsMsgsVectors(t *testing.T) {
Height: 1,
Round: 1,
POLRound: 1,
BlockID: bi,
DAHeader: types.DataAvailabilityHeader{},
Timestamp: date,
Signature: []byte("add_more_exclamation"),
}
Expand Down
32 changes: 30 additions & 2 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,8 @@ func (ps *PeerState) SetHasProposal(proposal *types.Proposal) {
return
}

ps.PRS.ProposalBlockPartSetHeader = proposal.BlockID.PartSetHeader
ps.PRS.ProposalBlockParts = bits.NewBitArray(int(proposal.BlockID.PartSetHeader.Total))
// ps.PRS.ProposalBlockPartSetHeader = proposal.BlockID.PartSetHeader
// ps.PRS.ProposalBlockParts = bits.NewBitArray(int(proposal.BlockID.PartSetHeader.Total))
ps.PRS.ProposalPOLRound = proposal.POLRound
ps.PRS.ProposalPOL = nil // Nil until ProposalPOLMessage received.
}
Expand Down Expand Up @@ -1610,6 +1610,34 @@ func (m *BlockPartMessage) String() string {

//-------------------------------------

// BlockPartMessage is sent when gossipping a piece of the proposed block.
type BlockMessage struct {
Height int64
Round int32
Block *types.Block
}

// ValidateBasic performs basic validation.
func (m *BlockMessage) ValidateBasic() error {
if m.Height < 0 {
return errors.New("negative Height")
}
if m.Round < 0 {
return errors.New("negative Round")
}
if err := m.Block.ValidateBasic(); err != nil {
return fmt.Errorf("wrong Part: %v", err)
}
return nil
}

// String returns a string representation.
func (m *BlockMessage) String() string {
return fmt.Sprintf("[BlockPart H:%v R:%v P:%v]", m.Height, m.Round, m.Block)
}

//-------------------------------------

// VoteMessage is sent when voting for a proposal (or lack thereof).
type VoteMessage struct {
Vote *types.Vote
Expand Down
8 changes: 0 additions & 8 deletions consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,6 @@ func TestVoteSetMaj23MessageValidateBasic(t *testing.T) {
validBlockID := types.BlockID{}
invalidBlockID := types.BlockID{
Hash: bytes.HexBytes{},
PartSetHeader: types.PartSetHeader{
Total: 1,
Hash: []byte{0},
},
}

testCases := []struct { // nolint: maligned
Expand Down Expand Up @@ -943,10 +939,6 @@ func TestVoteSetBitsMessageValidateBasic(t *testing.T) {
{func(msg *VoteSetBitsMessage) {
msg.BlockID = types.BlockID{
Hash: bytes.HexBytes{},
PartSetHeader: types.PartSetHeader{
Total: 1,
Hash: []byte{0},
},
}
}, "wrong BlockID: wrong PartSetHeader: wrong Hash:"},
{func(msg *VoteSetBitsMessage) { msg.Votes = bits.NewBitArray(types.MaxVotesCount + 1) },
Expand Down
3 changes: 1 addition & 2 deletions consensus/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (cs *State) readReplayMessage(msg *TimedWALMessage, newStepSub types.Subscr
switch msg := m.Msg.(type) {
case *ProposalMessage:
p := msg.Proposal
cs.Logger.Info("Replay: Proposal", "height", p.Height, "round", p.Round, "header",
p.BlockID.PartSetHeader, "pol", p.POLRound, "peer", peerID)
cs.Logger.Info("Replay: Proposal", "height", p.Height, "round", p.Round, "pol", p.POLRound, "peer", peerID)
case *BlockPartMessage:
cs.Logger.Info("Replay: BlockPart", "height", msg.Height, "round", msg.Round, "peer", peerID)
case *VoteMessage:
Expand Down
4 changes: 2 additions & 2 deletions consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ func (pb *playback) replayConsoleLoop() int {
case "proposal":
fmt.Println(rs.Proposal)
case "proposal_block":
fmt.Printf("%v %v\n", rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort())
fmt.Printf("%v %v\n", rs.ProposalBlockID.String(), rs.ProposalBlock.StringShort())
case "locked_round":
fmt.Println(rs.LockedRound)
case "locked_block":
fmt.Printf("%v %v\n", rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort())
fmt.Printf("%v %v\n", rs.LockedBlockID.String(), rs.LockedBlock.StringShort())
case "votes":
fmt.Println(rs.Votes.StringIndented(" "))

Expand Down
Loading

0 comments on commit 1ef2500

Please sign in to comment.