Skip to content

Commit

Permalink
Remove NIPoST and NodeID from types.ActivationTx (#5870)
Browse files Browse the repository at this point in the history
## Motivation

Trimming down types.ActivationTx from unwanted fields. Prerequisite for ATX V2.
  • Loading branch information
poszu committed Apr 22, 2024
1 parent 213a585 commit a2cdfd3
Show file tree
Hide file tree
Showing 25 changed files with 158 additions and 340 deletions.
29 changes: 15 additions & 14 deletions activation/activation_multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/sql"
Expand Down Expand Up @@ -409,7 +410,7 @@ func Test_Builder_Multi_HappyPath(t *testing.T) {
atxChan := make(chan struct{})
atxStep := make(map[types.NodeID]chan struct{})
var atxMtx sync.Mutex
atxs := make(map[types.NodeID]types.ActivationTx)
atxs := make(map[types.NodeID]wire.ActivationTxV1)
endChan := make(chan struct{})
for _, sig := range tab.signers {
ch := make(chan struct{})
Expand All @@ -429,9 +430,9 @@ func Test_Builder_Multi_HappyPath(t *testing.T) {
func(ctx context.Context, _ string, got []byte) error {
atxMtx.Lock()
defer atxMtx.Unlock()
atx, err := wire.ActivationTxFromBytes(got)
require.NoError(t, err)
atxs[atx.SmesherID] = *atx
var atx wire.ActivationTxV1
codec.MustDecode(got, &atx)
atxs[atx.SmesherID] = atx
return nil
},
)
Expand Down Expand Up @@ -488,21 +489,21 @@ func Test_Builder_Multi_HappyPath(t *testing.T) {

for _, sig := range tab.signers {
atx := atxs[sig.NodeID()]
require.Equal(t, initialPost[sig.NodeID()].Nonce, atx.NIPostChallenge.InitialPost.Nonce)
require.Equal(t, initialPost[sig.NodeID()].Pow, atx.NIPostChallenge.InitialPost.Pow)
require.Equal(t, initialPost[sig.NodeID()].Indices, atx.NIPostChallenge.InitialPost.Indices)
require.Equal(t, initialPost[sig.NodeID()].Nonce, atx.InitialPost.Nonce)
require.Equal(t, initialPost[sig.NodeID()].Pow, atx.InitialPost.Pow)
require.Equal(t, initialPost[sig.NodeID()].Indices, atx.InitialPost.Indices)

require.Equal(t, initialPost[sig.NodeID()].CommitmentATX, *atx.NIPostChallenge.CommitmentATX)
require.Equal(t, postGenesisEpoch+1, atx.NIPostChallenge.PublishEpoch)
require.Equal(t, types.EmptyATXID, atx.NIPostChallenge.PrevATXID)
require.Equal(t, tab.goldenATXID, atx.NIPostChallenge.PositioningATX)
require.Equal(t, uint64(0), atx.NIPostChallenge.Sequence)
require.Equal(t, initialPost[sig.NodeID()].CommitmentATX, *atx.CommitmentATXID)
require.Equal(t, postGenesisEpoch+1, atx.PublishEpoch)
require.Equal(t, types.EmptyATXID, atx.PrevATXID)
require.Equal(t, tab.goldenATXID, atx.PositioningATXID)
require.Equal(t, uint64(0), atx.Sequence)

require.Equal(t, types.Address{}, atx.Coinbase)
require.Equal(t, nipostState[sig.NodeID()].NumUnits, atx.NumUnits)
require.Equal(t, nipostState[sig.NodeID()].NIPost, atx.NIPost)
require.Equal(t, nipostState[sig.NodeID()].NIPost, wire.NiPostFromWireV1(atx.NIPost))
require.Equal(t, sig.NodeID(), *atx.NodeID)
require.Equal(t, nipostState[sig.NodeID()].VRFNonce, *atx.VRFNonce)
require.Equal(t, uint64(nipostState[sig.NodeID()].VRFNonce), *atx.VRFNonce)
}

// stop smeshing
Expand Down
115 changes: 38 additions & 77 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
Expand Down Expand Up @@ -51,11 +52,10 @@ func TestMain(m *testing.M) {

func newAtx(
challenge types.NIPostChallenge,
nipost *types.NIPost,
numUnits uint32,
coinbase types.Address,
) *types.ActivationTx {
atx := types.NewActivationTx(challenge, coinbase, nipost, numUnits, nil)
atx := types.NewActivationTx(challenge, coinbase, numUnits, nil)
atx.SetEffectiveNumUnits(numUnits)
atx.SetReceived(time.Now())
atx.SetValidity(types.Valid)
Expand Down Expand Up @@ -353,8 +353,7 @@ func TestBuilder_PublishActivationTx_HappyFlow(t *testing.T) {
PositioningATX: tab.goldenATXID,
CommitmentATX: &tab.goldenATXID,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
prevAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -396,8 +395,7 @@ func TestBuilder_Loop_WaitsOnStaleChallenge(t *testing.T) {
PositioningATX: tab.goldenATXID,
CommitmentATX: &tab.goldenATXID,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
prevAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -454,8 +452,7 @@ func TestBuilder_PublishActivationTx_FaultyNet(t *testing.T) {
PositioningATX: tab.goldenATXID,
CommitmentATX: &tab.goldenATXID,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
prevAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -543,8 +540,7 @@ func TestBuilder_PublishActivationTx_UsesExistingChallengeOnLatePublish(t *testi
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
prevAtx := newAtx(challenge, nipostData.NIPost, posEpoch.Uint32(), types.Address{})
prevAtx := newAtx(challenge, posEpoch.Uint32(), types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -628,8 +624,7 @@ func TestBuilder_PublishActivationTx_RebuildNIPostWhenTargetEpochPassed(t *testi
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
prevAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -700,7 +695,7 @@ func TestBuilder_PublishActivationTx_RebuildNIPostWhenTargetEpochPassed(t *testi
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
posAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, posAtx))
vPosAtx, err := posAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -731,10 +726,9 @@ func TestBuilder_PublishActivationTx_NoPrevATX(t *testing.T) {
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
otherSigner, err := signing.NewEdSigner()
require.NoError(t, err)
posAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(otherSigner, posAtx))
vPosAtx, err := posAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -786,10 +780,9 @@ func TestBuilder_PublishActivationTx_NoPrevATX_PublishFails_InitialPost_preserve
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
otherSigner, err := signing.NewEdSigner()
require.NoError(t, err)
posAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(otherSigner, posAtx))
vPosAtx, err := posAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -894,8 +887,7 @@ func TestBuilder_PublishActivationTx_PrevATXWithoutPrevATX(t *testing.T) {
CommitmentATX: &tab.goldenATXID,
}
poetBytes := []byte("66666")
nipostData := newNIPostWithPoet(t, poetBytes)
posAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(otherSigner, posAtx))
vPosAtx, err := posAtx.Verify(0, 2)
r.NoError(err)
Expand All @@ -909,7 +901,7 @@ func TestBuilder_PublishActivationTx_PrevATXWithoutPrevATX(t *testing.T) {
CommitmentATX: nil,
}
challenge.InitialPost = initialPost
prevAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(challenge, 2, types.Address{})
prevAtx.InitialPost = initialPost
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
Expand Down Expand Up @@ -956,31 +948,22 @@ func TestBuilder_PublishActivationTx_PrevATXWithoutPrevATX(t *testing.T) {
tab.mpub.EXPECT().
Publish(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, _ string, msg []byte) error {
atx, err := wire.ActivationTxFromBytes(msg)
require.NoError(t, err)
atx.SetReceived(time.Now().Local())

atx.SetEffectiveNumUnits(atx.NumUnits)
vAtx, err := atx.Verify(0, 1)
r.NoError(err)
r.Equal(sig.NodeID(), vAtx.SmesherID)

r.NoError(atxs.Add(tab.db, vAtx))
var atx wire.ActivationTxV1
codec.MustDecode(msg, &atx)

r.Equal(sig.NodeID(), atx.SmesherID)
r.Equal(prevAtx.Sequence+1, atx.Sequence)
r.Equal(prevAtx.ID(), atx.PrevATXID)
r.Nil(atx.InitialPost)
r.Nil(atx.CommitmentATX)
r.Nil(atx.CommitmentATXID)
r.Nil(atx.VRFNonce)

r.Equal(posAtx.ID(), atx.PositioningATX)
r.Equal(posAtx.ID(), atx.PositioningATXID)
r.Equal(postAtxPubEpoch+1, atx.PublishEpoch)
r.Equal(types.BytesToHash(poetBytes), atx.GetPoetProofRef())

r.Equal(poetBytes, atx.NIPost.PostMetadata.Challenge)
return nil
})

tab.mnipost.EXPECT().ResetState(sig.NodeID()).Return(nil)
tab.mnipost.EXPECT().ResetState(sig.NodeID())

r.NoError(tab.PublishActivationTx(context.Background(), sig))

Expand Down Expand Up @@ -1009,8 +992,7 @@ func TestBuilder_PublishActivationTx_TargetsEpochBasedOnPosAtx(t *testing.T) {
CommitmentATX: &types.ATXID{4, 5, 6},
}
poetBytes := []byte("66666")
nipostData := newNIPostWithPoet(t, poetBytes)
posAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(otherSigner, posAtx))
vPosAtx, err := posAtx.Verify(0, 1)
r.NoError(err)
Expand Down Expand Up @@ -1055,24 +1037,16 @@ func TestBuilder_PublishActivationTx_TargetsEpochBasedOnPosAtx(t *testing.T) {
tab.mpub.EXPECT().
Publish(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, _ string, msg []byte) error {
atx, err := wire.ActivationTxFromBytes(msg)
require.NoError(t, err)
atx.SetReceived(time.Now().Local())

atx.SetEffectiveNumUnits(atx.NumUnits)
vAtx, err := atx.Verify(0, 1)
r.NoError(err)
r.Equal(sig.NodeID(), vAtx.SmesherID)

r.NoError(atxs.Add(tab.db, vAtx))
var atx wire.ActivationTxV1
codec.MustDecode(msg, &atx)

r.Equal(sig.NodeID(), atx.SmesherID)
r.Zero(atx.Sequence)
r.Equal(types.EmptyATXID, atx.PrevATXID)
r.NotNil(atx.InitialPost)

r.Equal(posAtx.ID(), atx.PositioningATX)
r.Equal(posAtx.ID(), atx.PositioningATXID)
r.Equal(posEpoch+1, atx.PublishEpoch)
r.Equal(types.BytesToHash(poetBytes), atx.GetPoetProofRef())
r.Equal(poetBytes, atx.NIPost.PostMetadata.Challenge)

return nil
})
Expand Down Expand Up @@ -1120,8 +1094,7 @@ func TestBuilder_PublishActivationTx_FailsWhenNIPostBuilderFails(t *testing.T) {
PositioningATX: types.ATXID{1, 2, 3},
CommitmentATX: nil,
}
nipostData := newNIPostWithPoet(t, []byte("66666"))
posAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
posAtx := newAtx(ch, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, posAtx))
vPosAtx, err := posAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -1157,8 +1130,7 @@ func TestBuilder_SignAtx(t *testing.T) {
PositioningATX: prevAtx,
CommitmentATX: nil,
}
nipost := newNIPostWithPoet(t, []byte("66666"))
atx := newAtx(challenge, nipost.NIPost, 100, types.Address{})
atx := newAtx(challenge, 100, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, atx))

msg := wire.ActivationTxToWireV1(atx).SignedBytes()
Expand Down Expand Up @@ -1196,8 +1168,7 @@ func TestBuilder_RetryPublishActivationTx(t *testing.T) {
CommitmentATX: nil,
}
poetBytes := []byte("66666")
nipostData := newNIPostWithPoet(t, poetBytes)
prevAtx := newAtx(challenge, nipostData.NIPost, 2, types.Address{})
prevAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -1354,9 +1325,7 @@ func TestBuilder_InitialProofGeneratedOnce(t *testing.T) {
PositioningATX: tab.goldenATXID,
CommitmentATX: &tab.goldenATXID,
}
poetByte := []byte("66666")
nipost := newNIPostWithPoet(t, poetByte)
prevAtx := newAtx(challenge, nipost.NIPost, 2, types.Address{})
prevAtx := newAtx(challenge, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sig, prevAtx))
vPrevAtx, err := prevAtx.Verify(0, 1)
require.NoError(t, err)
Expand All @@ -1375,7 +1344,6 @@ func TestBuilder_InitialProofGeneratedOnce(t *testing.T) {
require.Nil(t, atx.VRFNonce)
require.Equal(t, vPrevAtx.ID(), atx.PositioningATX)
require.Equal(t, vPrevAtx.PublishEpoch+1, atx.PublishEpoch)
require.Equal(t, types.BytesToHash(poetByte), atx.GetPoetProofRef())

// postClient.Proof() should not be called again
require.NoError(t, tab.buildInitialPost(context.Background(), sig.NodeID()))
Expand Down Expand Up @@ -1579,28 +1547,21 @@ func TestGetPositioningAtxPicksAtxWithValidChain(t *testing.T) {
// Invalid chain with high height
sigInvalid, err := signing.NewEdSigner()
require.NoError(t, err)
ch := types.NIPostChallenge{
Sequence: 1,
PrevATXID: types.EmptyATXID,
PublishEpoch: postGenesisEpoch,
PositioningATX: tab.goldenATXID,
CommitmentATX: &tab.goldenATXID,
}
nipostData := newNIPostWithPoet(t, []byte("0"))
invalidAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sigInvalid, invalidAtx))
vInvalidAtx, err := invalidAtx.Verify(0, 100)
invalidAtx := newInitialATXv1(t, tab.goldenATXID)
invalidAtx.Sign(sigInvalid)
vInvalidAtx := toVerifiedAtx(t, invalidAtx)
vInvalidAtx, err = vInvalidAtx.Verify(0, 100)
require.NoError(t, err)
require.NoError(t, atxs.Add(tab.db, vInvalidAtx))

// Valid chain with lower height
sigValid, err := signing.NewEdSigner()
require.NoError(t, err)
nipostData = newNIPostWithPoet(t, []byte("1"))
validAtx := newAtx(ch, nipostData.NIPost, 2, types.Address{})
require.NoError(t, SignAndFinalizeAtx(sigValid, validAtx))
vValidAtx, err := validAtx.Verify(0, 1)
require.NoError(t, err)
validAtx := newInitialATXv1(t, tab.goldenATXID)
validAtx.NumUnits += 10
validAtx.Sign(sigValid)
vValidAtx := toVerifiedAtx(t, validAtx)
vValidAtx.Verify(0, 1)
require.NoError(t, atxs.Add(tab.db, vValidAtx))

tab.mValidator.EXPECT().
Expand Down
Loading

0 comments on commit a2cdfd3

Please sign in to comment.