Skip to content

Commit

Permalink
Test both revisioned and unrevisioned subtrees
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutchinson committed Dec 4, 2023
1 parent f8385f6 commit 5696d6d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 40 deletions.
103 changes: 63 additions & 40 deletions storage/mysql/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestNodeRoundTrip(t *testing.T) {
{desc: "store-all-read-all", store: nodes, read: nodeIDs, want: nodes},
{desc: "store-all-read-none", store: nodes, read: nil, want: nil},
} {
t.Run(tc.desc, func(t *testing.T) {
testbody := func(treeDef *trillian.Tree) {
ctx := context.Background()
cleanTestDB(DB)
as := NewAdminStorage(DB)
tree := mustCreateTree(ctx, t, as, storageto.LogTree)
tree := mustCreateTree(ctx, t, as, treeDef)
s := NewLogStorage(DB, nil)

const writeRev = int64(100)
Expand All @@ -86,57 +86,80 @@ func TestNodeRoundTrip(t *testing.T) {
}
return nil
})
}
t.Run(tc.desc+"-revisions", func(t *testing.T) {
testbody(storageto.LogTree)
})
t.Run(tc.desc+"-norevisions", func(t *testing.T) {
testbody(storageto.RevisionlessLogTree)
})
}
}

// This test ensures that node writes cross subtree boundaries so this edge case in the subtree
// cache gets exercised. Any tree size > 256 will do this.
func TestLogNodeRoundTripMultiSubtree(t *testing.T) {
ctx := context.Background()
cleanTestDB(DB)
as := NewAdminStorage(DB)
tree := mustCreateTree(ctx, t, as, storageto.LogTree)
s := NewLogStorage(DB, nil)

const writeRev = int64(100)
const size = 871
nodesToStore, err := createLogNodesForTreeAtSize(t, size, writeRev)
if err != nil {
t.Fatalf("failed to create test tree: %v", err)
}
nodeIDsToRead := make([]compact.NodeID, len(nodesToStore))
for i := range nodesToStore {
nodeIDsToRead[i] = nodesToStore[i].ID
testCases := []struct {
desc string
tree *trillian.Tree
}{
{
desc: "Revisions",
tree: storageto.LogTree,
},
{
desc: "No revisions",
tree: storageto.RevisionlessLogTree,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ctx := context.Background()
cleanTestDB(DB)
as := NewAdminStorage(DB)
tree := mustCreateTree(ctx, t, as, tC.tree)
s := NewLogStorage(DB, nil)

{
runLogTX(s, tree, t, func(ctx context.Context, tx storage.LogTreeTX) error {
forceWriteRevision(writeRev, tx)
if err := tx.SetMerkleNodes(ctx, nodesToStore); err != nil {
t.Fatalf("Failed to store nodes: %s", err)
const writeRev = int64(100)
const size = 871
nodesToStore, err := createLogNodesForTreeAtSize(t, size, writeRev)
if err != nil {
t.Fatalf("failed to create test tree: %v", err)
}
nodeIDsToRead := make([]compact.NodeID, len(nodesToStore))
for i := range nodesToStore {
nodeIDsToRead[i] = nodesToStore[i].ID
}
return storeLogRoot(ctx, tx, uint64(size), uint64(writeRev), []byte{1, 2, 3})
})
}

{
runLogTX(s, tree, t, func(ctx context.Context, tx storage.LogTreeTX) error {
readNodes, err := tx.GetMerkleNodes(ctx, nodeIDsToRead)
if err != nil {
t.Fatalf("Failed to retrieve nodes: %s", err)
{
runLogTX(s, tree, t, func(ctx context.Context, tx storage.LogTreeTX) error {
forceWriteRevision(writeRev, tx)
if err := tx.SetMerkleNodes(ctx, nodesToStore); err != nil {
t.Fatalf("Failed to store nodes: %s", err)
}
return storeLogRoot(ctx, tx, uint64(size), uint64(writeRev), []byte{1, 2, 3})
})
}
if err := nodesAreEqual(readNodes, nodesToStore); err != nil {
missing, extra := diffNodes(readNodes, nodesToStore)
for _, n := range missing {
t.Errorf("Missing: %v", n.ID)
}
for _, n := range extra {
t.Errorf("Extra : %v", n.ID)
}
t.Fatalf("Read back different nodes from the ones stored: %s", err)

{
runLogTX(s, tree, t, func(ctx context.Context, tx storage.LogTreeTX) error {
readNodes, err := tx.GetMerkleNodes(ctx, nodeIDsToRead)
if err != nil {
t.Fatalf("Failed to retrieve nodes: %s", err)
}
if err := nodesAreEqual(readNodes, nodesToStore); err != nil {
missing, extra := diffNodes(readNodes, nodesToStore)
for _, n := range missing {
t.Errorf("Missing: %v", n.ID)
}
for _, n := range extra {
t.Errorf("Extra : %v", n.ID)
}
t.Fatalf("Read back different nodes from the ones stored: %s", err)
}
return nil
})
}
return nil
})
}
}
Expand Down
10 changes: 10 additions & 0 deletions storage/testonly/admin_storage_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ import (
var (
// LogTree is a valid, LOG-type trillian.Tree for tests.
LogTree = &trillian.Tree{
TreeState: trillian.TreeState_ACTIVE,
TreeType: trillian.TreeType_LOG,
DisplayName: "Llamas Log",
Description: "Registry of publicly-owned llamas",
MaxRootDuration: durationpb.New(0 * time.Millisecond),
}

// LogTree is a valid, LOG-type trillian.Tree for tests.
// This tree is configured not to write revisions for each subtree.
RevisionlessLogTree = &trillian.Tree{
TreeState: trillian.TreeState_ACTIVE,
TreeType: trillian.TreeType_LOG,
DisplayName: "Llamas Log",
Expand Down

0 comments on commit 5696d6d

Please sign in to comment.