Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Apr 30, 2024
1 parent a41abaf commit bd97788
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
49 changes: 27 additions & 22 deletions activation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,32 +548,37 @@ func (h *Handler) checkWrongPrevAtx(
return proof, nil
}

// storeAtx stores an ATX and notifies subscribers of the ATXID.
func (h *Handler) storeAtx(ctx context.Context, atx *types.VerifiedActivationTx) (*mwire.MalfeasanceProof, error) {
var nonce *types.VRFPostIndex
malicious, err := h.cdb.IsMalicious(atx.SmesherID)
func (h *Handler) checkMalicious(
ctx context.Context,
tx *sql.Tx,
atx *types.VerifiedActivationTx,
) (*mwire.MalfeasanceProof, error) {
malicious, err := identities.IsMalicious(tx, atx.SmesherID)
if err != nil {
return nil, fmt.Errorf("checking if node is malicious: %w", err)
}
var proof *mwire.MalfeasanceProof
err = h.cdb.WithTx(ctx, func(tx *sql.Tx) error {
nonce, err = atxs.AddGettingNonce(tx, atx)
if err != nil && !errors.Is(err, sql.ErrObjectExists) {
return fmt.Errorf("add atx to db: %w", err)
}
if malicious {
return nil
}
proof, err = h.checkDoublePublish(ctx, tx, atx)
if err != nil {
return err
}
if proof != nil {
return nil
}
if malicious {
return nil, nil
}
proof, err := h.checkDoublePublish(ctx, tx, atx)
if proof != nil || err != nil {
return proof, err
}
return h.checkWrongPrevAtx(ctx, tx, atx)
}

proof, err = h.checkWrongPrevAtx(ctx, tx, atx)
return err
// storeAtx stores an ATX and notifies subscribers of the ATXID.
func (h *Handler) storeAtx(ctx context.Context, atx *types.VerifiedActivationTx) (*mwire.MalfeasanceProof, error) {
var nonce *types.VRFPostIndex
var proof *mwire.MalfeasanceProof
err := h.cdb.WithTx(ctx, func(tx *sql.Tx) error {
var err1, err2 error
proof, err1 = h.checkMalicious(ctx, tx, atx)
nonce, err2 = atxs.AddGettingNonce(tx, atx)
if err2 != nil && !errors.Is(err2, sql.ErrObjectExists) {
err2 = fmt.Errorf("add atx to db: %w", err2)
}
return errors.Join(err1, err2)
})
if err != nil {
return nil, fmt.Errorf("store atx: %w", err)
Expand Down
34 changes: 34 additions & 0 deletions activation/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,40 @@ func TestHandler_ProcessAtx(t *testing.T) {
require.Nil(t, proof)
}

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

sig, err := signing.NewEdSigner()
require.NoError(t, err)
require.NoError(t, identities.SetMalicious(atxHdlr.cdb, sig.NodeID(), types.RandomBytes(10), time.Now()))

coinbase := types.GenerateAddress([]byte("aaaa"))

// Act & Assert
atx1 := newActivationTx(
t,
sig,
0,
types.EmptyATXID,
types.EmptyATXID,
nil,
types.EpochID(2),
0,
100,
coinbase,
100,
&types.NIPost{PostMetadata: &types.PostMetadata{}},
withVrfNonce(7),
)
atxHdlr.mbeacon.EXPECT().OnAtx(gomock.Any())
atxHdlr.mtortoise.EXPECT().OnAtx(gomock.Any(), gomock.Any(), gomock.Any())
proof, err := atxHdlr.processVerifiedATX(context.Background(), atx1)
require.NoError(t, err)
require.Nil(t, proof)
}

func TestHandler_ProcessAtx_SamePubEpoch(t *testing.T) {
// Arrange
goldenATXID := types.ATXID{2, 3, 4}
Expand Down

0 comments on commit bd97788

Please sign in to comment.