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] - Simplify VRFSigner() method to not return an error #5147

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 2 additions & 5 deletions beacon/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func New(
publisher pubsub.Publisher,
edSigner *signing.EdSigner,
edVerifier *signing.EdVerifier,
vrfSigner vrfSigner,
vrfVerifier vrfVerifier,
cdb *datastore.CachedDB,
clock layerClock,
Expand All @@ -123,7 +122,6 @@ func New(
publisher: publisher,
edSigner: edSigner,
edVerifier: edVerifier,
vrfSigner: vrfSigner,
vrfVerifier: vrfVerifier,
cdb: cdb,
clock: clock,
Expand All @@ -147,7 +145,7 @@ func New(
}

if pd.weakCoin == nil {
pd.weakCoin = weakcoin.New(pd.publisher, vrfSigner, vrfVerifier, pd.nonceFetcher, pd,
pd.weakCoin = weakcoin.New(pd.publisher, edSigner.VRFSigner(), vrfVerifier, pd.nonceFetcher, pd,
pd.msgTimes,
weakcoin.WithLog(pd.logger.WithName("weakCoin")),
weakcoin.WithMaxRound(pd.config.RoundsNumber),
Expand All @@ -173,7 +171,6 @@ type ProtocolDriver struct {
publisher pubsub.Publisher
edSigner *signing.EdSigner
edVerifier *signing.EdVerifier
vrfSigner vrfSigner
vrfVerifier vrfVerifier
nonceFetcher nonceFetcher
weakCoin coin
Expand Down Expand Up @@ -834,7 +831,7 @@ func (pd *ProtocolDriver) sendProposal(ctx context.Context, epoch types.EpochID,
}

logger := pd.logger.WithContext(ctx).WithFields(epoch)
vrfSig := buildSignedProposal(ctx, pd.logger, pd.vrfSigner, epoch, nonce)
vrfSig := buildSignedProposal(ctx, pd.logger, pd.edSigner.VRFSigner(), epoch, nonce)
proposal := ProposalFromVrf(vrfSig)
m := ProposalMessage{
EpochID: epoch,
Expand Down
17 changes: 5 additions & 12 deletions beacon/beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type testProtocolDriver struct {
cdb *datastore.CachedDB
mClock *MocklayerClock
mSync *mocks.MockSyncStateProvider
mSigner *MockvrfSigner
mVerifier *MockvrfVerifier
mNonceFetcher *MocknonceFetcher
}
Expand All @@ -90,7 +89,6 @@ func newTestDriver(tb testing.TB, cfg Config, p pubsub.Publisher) *testProtocolD
ctrl: ctrl,
mClock: NewMocklayerClock(ctrl),
mSync: mocks.NewMockSyncStateProvider(ctrl),
mSigner: NewMockvrfSigner(ctrl),
mVerifier: NewMockvrfVerifier(ctrl),
mNonceFetcher: NewMocknonceFetcher(ctrl),
}
Expand All @@ -101,12 +99,11 @@ func newTestDriver(tb testing.TB, cfg Config, p pubsub.Publisher) *testProtocolD
minerID := edSgn.NodeID()
lg := logtest.New(tb).WithName(minerID.ShortString())

tpd.mSigner.EXPECT().Sign(gomock.Any()).AnyTimes().Return(types.EmptyVrfSignature)
tpd.mVerifier.EXPECT().Verify(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(true)
tpd.mNonceFetcher.EXPECT().VRFNonce(gomock.Any(), gomock.Any()).AnyTimes().Return(types.VRFPostIndex(1), nil)

tpd.cdb = datastore.NewCachedDB(sql.InMemory(), lg)
tpd.ProtocolDriver = New(minerID, p, edSgn, edVerify, tpd.mSigner, tpd.mVerifier, tpd.cdb, tpd.mClock,
tpd.ProtocolDriver = New(minerID, p, edSgn, edVerify, tpd.mVerifier, tpd.cdb, tpd.mClock,
WithConfig(cfg),
WithLogger(lg),
withWeakCoin(coinValueMock(tb, true)),
Expand Down Expand Up @@ -988,9 +985,7 @@ func TestBeacon_proposalPassesEligibilityThreshold(t *testing.T) {
for i := 0; i < tc.wEarly; i++ {
signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
proposal := buildSignedProposal(context.Background(), logtest.New(t), vrfSigner, 3, types.VRFPostIndex(1))
proposal := buildSignedProposal(context.Background(), logtest.New(t), signer.VRFSigner(), 3, types.VRFPostIndex(1))
if checker.PassThreshold(proposal) {
numEligible++
}
Expand Down Expand Up @@ -1036,8 +1031,6 @@ func TestBeacon_getSignedProposal(t *testing.T) {

edSgn, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := edSgn.VRFSigner()
require.NoError(t, err)

tt := []struct {
name string
Expand All @@ -1047,12 +1040,12 @@ func TestBeacon_getSignedProposal(t *testing.T) {
{
name: "Case 1",
epoch: 1,
result: vrfSigner.Sign([]byte{0x04, 0x04, 0x04}),
result: edSgn.VRFSigner().Sign([]byte{0x04, 0x04, 0x04}),
},
{
name: "Case 2",
epoch: 2,
result: vrfSigner.Sign([]byte{0x04, 0x04, 0x08}),
result: edSgn.VRFSigner().Sign([]byte{0x04, 0x04, 0x08}),
},
}

Expand All @@ -1061,7 +1054,7 @@ func TestBeacon_getSignedProposal(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

result := buildSignedProposal(context.Background(), logtest.New(t), vrfSigner, tc.epoch, types.VRFPostIndex(1))
result := buildSignedProposal(context.Background(), logtest.New(t), edSgn.VRFSigner(), tc.epoch, types.VRFPostIndex(1))
require.Equal(t, tc.result, result)
})
}
Expand Down
57 changes: 19 additions & 38 deletions beacon/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ func Test_HandleProposal_InitEpoch(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()
epochStart := time.Now()
createATX(t, tpd.cdb, epoch.FirstLayer().Sub(1), signer, 10, epochStart.Add(-1*time.Minute))

Expand All @@ -214,13 +213,11 @@ func Test_HandleProposal_Success(t *testing.T) {

signer1, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner1, err := signer1.VRFSigner()
require.NoError(t, err)
vrfSigner1 := signer1.VRFSigner()

signer2, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner2, err := signer2.VRFSigner()
require.NoError(t, err)
vrfSigner2 := signer2.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -269,13 +266,11 @@ func Test_HandleProposal_Malicious(t *testing.T) {

signer1, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner1, err := signer1.VRFSigner()
require.NoError(t, err)
vrfSigner1 := signer1.VRFSigner()

signer2, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner2, err := signer2.VRFSigner()
require.NoError(t, err)
vrfSigner2 := signer2.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -324,8 +319,7 @@ func Test_HandleProposal_Shutdown(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := createProposal(t, vrfSigner, epoch, false)
msgBytes, err := codec.Encode(msg)
Expand All @@ -347,8 +341,7 @@ func Test_HandleProposal_NotInProtocolStillWorks(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := createProposal(t, vrfSigner, epoch, false)
msgBytes, err := codec.Encode(msg)
Expand Down Expand Up @@ -387,8 +380,7 @@ func Test_handleProposal_Corrupted(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := []byte("guaranteed to be malformed")
got := tpd.HandleProposal(context.Background(), "peerID", msg)
Expand All @@ -407,8 +399,7 @@ func Test_handleProposal_EpochTooOld(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := createProposal(t, vrfSigner, epoch-1, false)
msgBytes, err := codec.Encode(msg)
Expand All @@ -434,8 +425,7 @@ func Test_handleProposal_NextEpoch(t *testing.T) {
rng := rand.New(rand.NewSource(1))
signer, err := signing.NewEdSigner(signing.WithKeyFromRand(rng))
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

now := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -478,8 +468,7 @@ func Test_handleProposal_NextEpochTooEarly(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := createProposal(t, vrfSigner, nextEpoch, false)
msgBytes, err := codec.Encode(msg)
Expand Down Expand Up @@ -510,8 +499,7 @@ func Test_handleProposal_EpochTooFarAhead(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

msg := createProposal(t, vrfSigner, epoch+2, false)
msgBytes, err := codec.Encode(msg)
Expand All @@ -535,8 +523,7 @@ func Test_handleProposal_BadVrfSignature(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -573,8 +560,7 @@ func Test_handleProposal_AlreadyProposed(t *testing.T) {
rng := rand.New(rand.NewSource(101))
signer, err := signing.NewEdSigner(signing.WithKeyFromRand(rng))
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -622,8 +608,7 @@ func Test_handleProposal_PotentiallyValid_Timing(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -659,8 +644,7 @@ func Test_handleProposal_PotentiallyValid_Threshold(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -697,8 +681,7 @@ func Test_handleProposal_Invalid_Timing(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -729,8 +712,7 @@ func Test_handleProposal_Invalid_threshold(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down Expand Up @@ -763,8 +745,7 @@ func Test_handleProposal_MinerMissingATX(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

epochStart := time.Now()
mockChecker := NewMockeligibilityChecker(gomock.NewController(t))
Expand Down
6 changes: 2 additions & 4 deletions beacon/weakcoin/weak_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ func TestWeakCoinEncodingRegression(t *testing.T) {
signing.WithKeyFromRand(rng),
)
require.NoError(t, err)
vrfSig, err := signer.VRFSigner()
require.NoError(t, err)
vrfSig := signer.VRFSigner()

mockAllowance := weakcoin.NewMockallowance(gomock.NewController(t))
mockAllowance.EXPECT().MinerAllowance(epoch, gomock.Any()).DoAndReturn(
Expand Down Expand Up @@ -473,8 +472,7 @@ func TestWeakCoinExchangeProposals(t *testing.T) {

signer, err := signing.NewEdSigner(signing.WithKeyFromRand(rng))
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

vrfSigners[i] = vrfSigner
}
Expand Down
9 changes: 3 additions & 6 deletions hare/eligibility/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ func Test_VrfSignVerify(t *testing.T) {
require.NoError(t, err)

o := defaultOracle(t)
o.vrfSigner, err = signer.VRFSigner()
require.NoError(t, err)
o.vrfSigner = signer.VRFSigner()
nid := signer.NodeID()

lid := types.EpochID(5).FirstLayer()
Expand Down Expand Up @@ -395,8 +394,7 @@ func Test_Proof_BeaconError(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
o.vrfSigner, err = signer.VRFSigner()
require.NoError(t, err)
o.vrfSigner = signer.VRFSigner()

layer := types.LayerID(2)
errUnknown := errors.New("unknown")
Expand All @@ -413,8 +411,7 @@ func Test_Proof(t *testing.T) {

signer, err := signing.NewEdSigner()
require.NoError(t, err)
vrfSigner, err := signer.VRFSigner()
require.NoError(t, err)
vrfSigner := signer.VRFSigner()

o.vrfSigner = vrfSigner
sig, err := o.Proof(context.Background(), layer, 3)
Expand Down
8 changes: 2 additions & 6 deletions hare3/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,13 @@ func (n *node) withSigner() *node {
signer, err := signing.NewEdSigner(signing.WithKeyFromRand(n.t.rng))
require.NoError(n.t, err)
n.signer = signer
vrfsigner, err := signer.VRFSigner()
require.NoError(n.t, err)
n.vrfsigner = vrfsigner
n.vrfsigner = signer.VRFSigner()
return n
}

func (n *node) reuseSigner(signer *signing.EdSigner) *node {
n.signer = signer
vrfsigner, err := signer.VRFSigner()
require.NoError(n.t, err)
n.vrfsigner = vrfsigner
n.vrfsigner = signer.VRFSigner()
return n
}

Expand Down
6 changes: 1 addition & 5 deletions hare3/legacy_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ func (lg *legacyOracle) validate(msg *Message) grade {
}

func (lg *legacyOracle) active(signer *signing.EdSigner, beacon types.Beacon, layer types.LayerID, ir IterRound) *types.HareEligibility {
vrfs, err := signer.VRFSigner()
if err != nil {
panic("can't cast ed signer to vrf signer")
}
vrf := lg.oracle.GenVRF(context.Background(), vrfs, beacon, layer, ir.Absolute())
vrf := lg.oracle.GenVRF(context.Background(), signer.VRFSigner(), beacon, layer, ir.Absolute())
committee := int(lg.config.Committee)
if ir.Round == propose {
committee = int(lg.config.Leaders)
Expand Down
4 changes: 2 additions & 2 deletions miner/proposal_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (pb *ProposalBuilder) initSessionData(ctx context.Context, lid types.LayerI
weight, set, err := generateActiveSet(
pb.logger,
pb.cdb,
pb.signer.MustVRFSigner(),
pb.signer.VRFSigner(),
pb.session.epoch,
pb.clock.LayerToTime(pb.session.epoch.FirstLayer()),
pb.cfg.GoodAtxPercent,
Expand Down Expand Up @@ -410,7 +410,7 @@ func (pb *ProposalBuilder) initSessionData(ctx context.Context, lid types.LayerI
}
if pb.session.eligibilities.proofs == nil {
pb.session.eligibilities.proofs = calcEligibilityProofs(
pb.signer.MustVRFSigner(),
pb.signer.VRFSigner(),
pb.session.epoch,
pb.session.beacon,
pb.session.nonce,
Expand Down