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

[Merged by Bors] - proposal signed data was used in id #4992

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions proposals/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,26 @@ func (h *Handler) handleProposal(ctx context.Context, expHash types.Hash32, peer
if p.Layer >= h.cfg.AllowEmptyActiveSet {
p.ActiveSet = nil
}

if !h.edVerifier.Verify(signing.PROPOSAL, p.SmesherID, p.SignedBytes(), p.Signature) {
badSigProposal.Inc()
return fmt.Errorf("failed to verify proposal signature")
}
p.ActiveSet = set
if !h.edVerifier.Verify(signing.BALLOT, p.Ballot.SmesherID, p.Ballot.SignedBytes(), p.Ballot.Signature) {
badSigBallot.Inc()
return fmt.Errorf("failed to verify ballot signature")
}

// set the proposal ID when received
// p.Initialize uses SignedBytes again
dshulyak marked this conversation as resolved.
Show resolved Hide resolved
if err := p.Initialize(); err != nil {
failedInit.Inc()
return errInitialize
}

if expHash != (types.Hash32{}) && p.ID().AsHash32() != expHash {
return fmt.Errorf("%w: proposal want %s, got %s", errWrongHash, expHash.ShortString(), p.ID().AsHash32().ShortString())
}
p.ActiveSet = set

if p.AtxID == types.EmptyATXID || p.AtxID == h.cfg.GoldenATXID {
badData.Inc()
Expand Down
101 changes: 101 additions & 0 deletions proposals/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,3 +1398,104 @@ func TestHandleActiveSet(t *testing.T) {
})
}
}

func gproposal(signer *signing.EdSigner, atxid types.ATXID, activeset []types.ATXID,
layer types.LayerID, edata *types.EpochData,
) types.Proposal {
p := types.Proposal{}
p.Layer = layer
p.AtxID = atxid
p.ActiveSet = activeset
p.EpochData = edata
p.Ballot.Signature = signer.Sign(signing.BALLOT, p.Ballot.SignedBytes())
p.Ballot.SmesherID = signer.NodeID()
p.Signature = signer.Sign(signing.PROPOSAL, p.SignedBytes())
p.SmesherID = signer.NodeID()
return p
}

func TestHandleSyncedProposalActiveSet(t *testing.T) {
signer, err := signing.NewEdSigner()
require.NoError(t, err)

set := []types.ATXID{{1}, {2}}
const acceptEmpty = 20
good := gproposal(signer, types.ATXID{1}, set, acceptEmpty-1, &types.EpochData{
ActiveSetHash: types.ATXIDList(set).Hash(),
Beacon: types.Beacon{1},
})
require.NoError(t, good.Initialize())

woActive := good
woActive.ActiveSet = nil

notSigned := gproposal(signer, types.ATXID{1}, nil, acceptEmpty, &types.EpochData{
ActiveSetHash: types.ATXIDList(set).Hash(),
Beacon: types.Beacon{1},
})
require.NoError(t, notSigned.Initialize())
notSigned.ActiveSet = set

notSignedEmpty := notSigned
notSignedEmpty.ActiveSet = nil
for _, tc := range []struct {
desc string
data []byte
id types.Hash32
requestSet []types.ATXID
err string
}{
{
desc: "before empty signed active set",
data: codec.MustEncode(&good),
id: good.ID().AsHash32(),
},
{
desc: "before empty without active set",
data: codec.MustEncode(&woActive),
id: woActive.ID().AsHash32(),
err: "failed to verify proposal signature",
},
{
desc: "after empty not signed active set",
data: codec.MustEncode(&notSigned),
id: notSigned.ID().AsHash32(),
},
{
desc: "after empty not signed active set empty",
data: codec.MustEncode(&notSignedEmpty),
id: notSigned.ID().AsHash32(),
requestSet: set,
},
} {
t.Run(tc.desc, func(t *testing.T) {
th := createTestHandler(t)
th.cfg.AllowEmptyActiveSet = acceptEmpty
pid := p2p.Peer("any")

th.mclock.EXPECT().LayerToTime(gomock.Any())
th.mf.EXPECT().RegisterPeerHashes(pid, gomock.Any()).AnyTimes()
th.md.EXPECT().GetMissingActiveSet(gomock.Any(), gomock.Any()).AnyTimes()
if tc.requestSet != nil {
id := types.ATXIDList(tc.requestSet).Hash()
th.mf.EXPECT().GetActiveSet(gomock.Any(), id)
require.NoError(t, activesets.Add(th.cdb, id, &types.EpochActiveSet{
Set: tc.requestSet,
}))
}
th.mf.EXPECT().GetAtxs(gomock.Any(), gomock.Any()).AnyTimes()
th.mf.EXPECT().GetBallots(gomock.Any(), gomock.Any()).AnyTimes()
th.mockSet.decodeAnyBallots()
th.mv.EXPECT().CheckEligibility(gomock.Any(), gomock.Any()).AnyTimes().Return(true, nil)
th.mm.EXPECT().AddBallot(gomock.Any(), gomock.Any()).AnyTimes()
th.mm.EXPECT().AddTXsFromProposal(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

err := th.HandleSyncedProposal(context.Background(), tc.id, pid, tc.data)
if len(tc.err) > 0 {
require.ErrorContains(t, err, tc.err)
} else {
require.NoError(t, err)
}
})
}
}