-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
285 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package atxwriter_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spacemeshos/go-spacemesh/activation/atxwriter" | ||
"github.com/spacemeshos/go-spacemesh/activation/wire" | ||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
"github.com/spacemeshos/go-spacemesh/sql/atxs" | ||
"github.com/spacemeshos/merkle-tree" | ||
poetShared "github.com/spacemeshos/poet/shared" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
) | ||
|
||
var ( | ||
postGenesisEpoch types.EpochID = 2 | ||
goldenATXID = types.RandomATXID() | ||
wAtx = newInitialATXv1(goldenATXID) | ||
atx = toAtx(wAtx) | ||
) | ||
|
||
func TestWriteCoalesce_One(t *testing.T) { | ||
w, db := newTestAtxWriter(t) | ||
|
||
ch, errfn := w.Store(atx, wAtx) | ||
var err error | ||
select { | ||
case <-ch: | ||
err = errfn() | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("timeout") | ||
} | ||
require.NoError(t, err) | ||
has, err := atxs.Has(db, atx.ID()) | ||
require.True(t, has) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestWriteCoalesce_Duplicates(t *testing.T) { | ||
w, db := newTestAtxWriter(t) | ||
|
||
ch, errfn := w.Store(atx, wAtx) | ||
_, _ = w.Store(atx, wAtx) | ||
var err error | ||
select { | ||
case <-ch: | ||
err = errfn() | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("timeout") | ||
} | ||
require.NoError(t, err) | ||
has, err := atxs.Has(db, atx.ID()) | ||
require.True(t, has) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestWriteCoalesce_MultipleBatches(t *testing.T) { | ||
w, db := newTestAtxWriter(t) | ||
|
||
ch, errfn := w.Store(atx, wAtx) | ||
var err error | ||
select { | ||
case <-ch: | ||
err = errfn() | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("timeout") | ||
} | ||
require.NoError(t, err) | ||
has, err := atxs.Has(db, atx.ID()) | ||
require.True(t, has) | ||
require.NoError(t, err) | ||
|
||
wAtx2 := newInitialATXv1(types.RandomATXID()) | ||
atx2 := toAtx(wAtx) | ||
|
||
ch, errfn = w.Store(atx2, wAtx2) | ||
select { | ||
case <-ch: | ||
err = errfn() | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("timeout") | ||
} | ||
require.NoError(t, err) | ||
has, err = atxs.Has(db, atx2.ID()) | ||
require.True(t, has) | ||
require.NoError(t, err) | ||
} | ||
|
||
func toAtx(watx *wire.ActivationTxV1) *types.ActivationTx { | ||
atx := wire.ActivationTxFromWireV1(watx) | ||
atx.SetReceived(time.Now()) | ||
atx.BaseTickHeight = uint64(atx.PublishEpoch) | ||
atx.TickCount = 1 | ||
return atx | ||
} | ||
|
||
func newTestAtxWriter(t *testing.T) (*atxwriter.AtxWriter, *sql.Database) { | ||
t.Helper() | ||
db := sql.InMemoryTest(t) | ||
log := zaptest.NewLogger(t) | ||
w := atxwriter.New(db, log) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
done := make(chan struct{}) | ||
t.Cleanup(func() { | ||
cancel() | ||
<-done | ||
}) | ||
go func() { | ||
defer close(done) | ||
w.Start(ctx) | ||
}() | ||
return w, db | ||
} | ||
|
||
func newInitialATXv1( | ||
goldenATXID types.ATXID, | ||
opts ...func(*wire.ActivationTxV1), | ||
) *wire.ActivationTxV1 { | ||
nonce := uint64(999) | ||
poetRef := types.RandomHash() | ||
atx := &wire.ActivationTxV1{ | ||
InnerActivationTxV1: wire.InnerActivationTxV1{ | ||
NIPostChallengeV1: wire.NIPostChallengeV1{ | ||
PrevATXID: types.EmptyATXID, | ||
PublishEpoch: postGenesisEpoch, | ||
PositioningATXID: goldenATXID, | ||
CommitmentATXID: &goldenATXID, | ||
InitialPost: &wire.PostV1{}, | ||
}, | ||
NIPost: newNIPostV1WithPoet(poetRef.Bytes()), | ||
VRFNonce: &nonce, | ||
Coinbase: types.GenerateAddress([]byte("aaaa")), | ||
NumUnits: 100, | ||
}, | ||
} | ||
for _, opt := range opts { | ||
opt(atx) | ||
} | ||
return atx | ||
} | ||
|
||
func newMerkleProof(leafs []types.Hash32) (types.MerkleProof, types.Hash32) { | ||
tree, err := merkle.NewTreeBuilder(). | ||
WithHashFunc(poetShared.HashMembershipTreeNode). | ||
WithLeavesToProve(map[uint64]bool{0: true}). | ||
Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
for _, m := range leafs { | ||
if err := tree.AddLeaf(m[:]); err != nil { | ||
panic(err) | ||
} | ||
} | ||
root, nodes := tree.RootAndProof() | ||
nodesH32 := make([]types.Hash32, 0, len(nodes)) | ||
for _, n := range nodes { | ||
nodesH32 = append(nodesH32, types.BytesToHash(n)) | ||
} | ||
return types.MerkleProof{ | ||
Nodes: nodesH32, | ||
}, types.BytesToHash(root) | ||
} | ||
|
||
func newNIPostV1WithPoet(poetRef []byte) *wire.NIPostV1 { | ||
proof, _ := newMerkleProof([]types.Hash32{ | ||
types.BytesToHash([]byte("challenge")), | ||
types.BytesToHash([]byte("leaf2")), | ||
types.BytesToHash([]byte("leaf3")), | ||
types.BytesToHash([]byte("leaf4")), | ||
}) | ||
|
||
return &wire.NIPostV1{ | ||
Membership: wire.MerkleProofV1{ | ||
Nodes: proof.Nodes, | ||
LeafIndex: 0, | ||
}, | ||
Post: &wire.PostV1{ | ||
Nonce: 0, | ||
Indices: []byte{1, 2, 3}, | ||
Pow: 0, | ||
}, | ||
PostMetadata: &wire.PostMetadataV1{ | ||
Challenge: poetRef, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package atxwriter | ||
|
||
import ( | ||
"github.com/spacemeshos/go-spacemesh/metrics" | ||
) | ||
|
||
const ( | ||
namespace = "activation_write_coalescer" | ||
) | ||
|
||
var BatchWriteCount = metrics.NewSimpleCounter( | ||
namespace, | ||
"batch_write_count", | ||
"number of errors when writing a batch", | ||
) | ||
|
||
var WriteBatchErrorsCount = metrics.NewSimpleCounter( | ||
namespace, | ||
"write_batch_errors", | ||
"number of errors when writing a batch", | ||
) | ||
|
||
var ErroredBatchCount = metrics.NewSimpleCounter( | ||
namespace, | ||
"errored_batch", | ||
"number of batches that errored", | ||
) | ||
|
||
var FlushBatchSize = metrics.NewSimpleCounter( | ||
namespace, | ||
"flush_batch_size", | ||
"size of flushed batch", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.