Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
countvonzero committed Sep 8, 2023
1 parent c89fa6d commit 5c90fa8
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 155 deletions.
23 changes: 23 additions & 0 deletions activation/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,29 @@ func TestHandler_ProcessAtx(t *testing.T) {
require.Equal(t, atx2.PublishEpoch.FirstLayer(), got.MalfeasanceProof.Layer)
}

func TestHandler_ProcessAtx_OwnMalfeasance(t *testing.T) {
goldenATXID := types.ATXID{2, 3, 4}
atxHdlr := newTestHandler(t, goldenATXID)

sig, err := signing.NewEdSigner()
require.NoError(t, err)
types.SetMinerNodeID(sig.NodeID())

atx1 := newActivationTx(t, sig, 0, types.EmptyATXID, types.EmptyATXID, nil, types.LayerID(layersPerEpoch).GetEpoch(), 0, 100, types.Address{1, 2, 3}, 100, &types.NIPost{})
atxHdlr.mbeacon.EXPECT().OnAtx(gomock.Any())
atxHdlr.mtortoise.EXPECT().OnAtx(gomock.Any())
require.NoError(t, atxHdlr.ProcessAtx(context.Background(), atx1))

// another atx for the same epoch is considered malicious
atx2 := newActivationTx(t, sig, 1, atx1.ID(), atx1.ID(), nil, types.LayerID(layersPerEpoch+1).GetEpoch(), 0, 100, types.Address{2, 3, 4}, 100, &types.NIPost{})
atxHdlr.mbeacon.EXPECT().OnAtx(gomock.Any())
atxHdlr.mtortoise.EXPECT().OnAtx(gomock.Any())
require.NoError(t, atxHdlr.ProcessAtx(context.Background(), atx2))
got, err := identities.IsMalicious(atxHdlr.cdb, sig.NodeID())
require.NoError(t, err)
require.False(t, got)
}

func TestHandler_ProcessAtxStoresNewVRFNonce(t *testing.T) {
// Arrange
goldenATXID := types.ATXID{2, 3, 4}
Expand Down
55 changes: 55 additions & 0 deletions hare3/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,61 @@ func TestHandler(t *testing.T) {
})
}

func TestHandlerOwnEquivocation(t *testing.T) {
t.Parallel()
tst := &tester{
TB: t,
rng: rand.New(rand.NewSource(1001)),
start: time.Now(),
cfg: DefaultConfig(),
layerDuration: 5 * time.Minute,
beacon: types.Beacon{1, 1, 1, 1},
genesis: types.GetEffectiveGenesis(),
}
cluster := newLockstepCluster(tst)
cluster.addActive(1)
n := cluster.nodes[0]
require.NoError(t, beacons.Add(n.db, tst.genesis.GetEpoch()+1, tst.beacon))
require.NoError(t, atxs.Add(n.db, n.atx))
n.oracle.UpdateActiveSet(tst.genesis.GetEpoch()+1, []types.ATXID{n.atx.ID()})
n.mpublisher.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
layer := tst.genesis + 1
n.nclock.StartLayer(layer)
n.clock.Set(tst.start.
Add(tst.layerDuration * time.Duration(layer)).
Add(tst.cfg.PreroundDelay))
elig := n.tracer.waitEligibility()

msg1 := &Message{}
msg1.Layer = layer
msg1.Value.Proposals = []types.ProposalID{{1}}
msg1.Eligibility = *elig
msg1.Sender = n.signer.NodeID()
msg1.Signature = n.signer.Sign(signing.HARE, msg1.ToMetadata().ToBytes())

msg2 := &Message{}
msg2.Layer = layer
msg2.Value.Proposals = []types.ProposalID{{2}}
msg2.Eligibility = *elig
msg2.Sender = n.signer.NodeID()
msg2.Signature = n.signer.Sign(signing.HARE, msg2.ToMetadata().ToBytes())

types.SetMinerNodeID(n.hare.signer.NodeID())

require.NoError(t, n.hare.Handler(context.Background(), "", codec.MustEncode(msg1)))
require.NoError(t, n.hare.Handler(context.Background(), "", codec.MustEncode(msg2)))

malicious, err := n.db.IsMalicious(n.signer.NodeID())
require.NoError(t, err)
require.False(t, malicious)

require.ErrorContains(t,
n.hare.Handler(context.Background(), "", codec.MustEncode(msg2)),
"dropped by graded",
)
types.SetMinerNodeID(types.EmptyNodeID)
}

func gatx(id types.ATXID, epoch types.EpochID, smesher types.NodeID, base, height uint64) types.VerifiedActivationTx {
atx := &types.ActivationTx{}
atx.NumUnits = 10
Expand Down
Loading

0 comments on commit 5c90fa8

Please sign in to comment.