Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ProofBuilder in LogStateTracker #8

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Load testdata from golden files
  • Loading branch information
AlCutter committed Sep 21, 2023
commit 514ddedaf7933a7f2d094aa71d4c876c941018fa
18 changes: 12 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ type LogStateTracker struct {
// LatestConsistentRaw holds the raw bytes of the latest proven-consistent
// LogState seen by this tracker.
LatestConsistentRaw []byte

// LatestConsistent is the deserialised form of LatestConsistentRaw
LatestConsistent log.Checkpoint

// The note with signatures and other metadata about the checkpoint
CheckpointNote *note.Note
// ProofBuilder for building proofs at LatestConsistent checkpoint.
ProofBuilder *ProofBuilder

CpSigVerifier note.Verifier
}
Expand All @@ -348,6 +348,10 @@ func NewLogStateTracker(ctx context.Context, f Fetcher, h merkle.LogHasher, chec
return ret, err
}
ret.LatestConsistent = *cp
ret.ProofBuilder, err = NewProofBuilder(ctx, ret.LatestConsistent, ret.Hasher.HashChildren, ret.Fetcher)
if err != nil {
return ret, fmt.Errorf("NewProofBuilder: %v", err)
}
return ret, nil
}
_, _, _, err := ret.Update(ctx)
Expand Down Expand Up @@ -387,13 +391,13 @@ func (lst *LogStateTracker) Update(ctx context.Context) ([]byte, [][]byte, []byt
if err != nil {
return nil, nil, nil, err
}
builder, err := NewProofBuilder(ctx, *c, lst.Hasher.HashChildren, lst.Fetcher)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create proof builder: %w", err)
}
var p [][]byte
if lst.LatestConsistent.Size > 0 {
if c.Size > lst.LatestConsistent.Size {
builder, err := NewProofBuilder(ctx, *c, lst.Hasher.HashChildren, lst.Fetcher)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create proof builder: %w", err)
}
p, err = builder.ConsistencyProof(ctx, lst.LatestConsistent.Size, c.Size)
if err != nil {
return nil, nil, nil, err
Expand All @@ -406,10 +410,12 @@ func (lst *LogStateTracker) Update(ctx context.Context) ([]byte, [][]byte, []byt
Wrapped: err,
}
}
// Update is consistent,
}
}
oldRaw := lst.LatestConsistentRaw
lst.LatestConsistentRaw, lst.LatestConsistent, lst.CheckpointNote = cRaw, *c, cn
lst.ProofBuilder = builder
return oldRaw, p, lst.LatestConsistentRaw, nil
}

Expand Down
87 changes: 44 additions & 43 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -26,48 +28,14 @@ import (
"github.com/transparency-dev/merkle/compact"
"github.com/transparency-dev/merkle/rfc6962"
"github.com/transparency-dev/serverless-log/api"
"golang.org/x/mod/sumdb/note"
)

var (
testOrigin = "Log Checkpoint v0"
testLogVerifier = mustMakeVerifier("astra+cad5a3d2+AZJqeuyE/GnknsCNh1eCtDtwdAwKBddOlS8M2eI1Jt4b")
// Built using serverless/testdata/build_log.sh
testCheckpoints = []log.Checkpoint{
{
Size: 1,
Hash: b64("0Nc2CrefWKseHj/mStd+LqC8B+NrX0btIiPt2SmN+ek="),
},
{
Size: 2,
Hash: b64("T1X2GdkhUjV3iyufF9b0kVsWFxIU0VI4EpNml2Teci4="),
},
{
Size: 3,
Hash: b64("Wqx3HImawpLnS/Gv4ubjAvi1WIOy0b8Ze0amvqbavKk="),
},
{
Size: 4,
Hash: b64("zY1lN35vrXYAPixXSd59LsU29xUJtuW4o2dNNg5Y2Co="),
},
{
Size: 5,
Hash: b64("gy5gl3aksFyiCO95a/1vLXz88A3dRq+0l9Sxte8ZqZQ="),
},
{
Size: 6,
Hash: b64("a6sWvsc2eEzmj72vah7mZ5dwFltivehh2b11qwlp5Jg="),
},
{
Size: 7,
Hash: b64("IrSXADBqJ7EQoUODSDKROySgNveeL6CFhik2w/+fS7U="),
},
{
Size: 14,
Hash: b64("SvCd38yNade7xEPY1a/aAc1M3A2AHYVF8lIiUnsH1ao="),
},
{
Size: 15,
Hash: b64("rKbDipCvhuX1GZ7g5BBe8sA6BbJ7ja/1nk427v383cs="),
},
}
testCheckpointsRaw, testCheckpoints = mustLoadTestCheckpoints()
)

func b64(r string) []byte {
Expand All @@ -78,15 +46,48 @@ func b64(r string) []byte {
return ret
}

func mustMakeVerifier(vs string) note.Verifier {
v, err := note.NewVerifier(vs)
if err != nil {
panic(fmt.Errorf("NewVerifier(%q): %v", vs, err))
}
return v
}

func mustLoadTestCheckpoints() ([][]byte, []log.Checkpoint) {
raws, cps := make([][]byte, 0), make([]log.Checkpoint, 0)
for i := 1; ; i++ {
cpName := fmt.Sprintf("checkpoint.%d", i)
r, err := testLogFetcher(context.Background(), cpName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Probably just no more checkpoints left
break
}
panic(err)
}
cp, _, _, err := log.ParseCheckpoint(r, testOrigin, testLogVerifier)
if err != nil {
panic(fmt.Errorf("ParseCheckpoint(%s): %v", cpName, err))
}
raws, cps = append(raws, r), append(cps, *cp)
}
if len(raws) == 0 {
panic("no checkpoints loaded")
}
return raws, cps
}

func testLogFetcher(_ context.Context, p string) ([]byte, error) {
path := filepath.Join("../testdata/log", p)
return os.ReadFile(path)
}

func TestCheckConsistency(t *testing.T) {
ctx := context.Background()

h := rfc6962.DefaultHasher

f := func(_ context.Context, p string) ([]byte, error) {
path := filepath.Join("../testdata/log", p)
return os.ReadFile(path)
}
for _, test := range []struct {
desc string
cp []log.Checkpoint
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestCheckConsistency(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
err := CheckConsistency(ctx, h, f, test.cp)
err := CheckConsistency(ctx, h, testLogFetcher, test.cp)
if gotErr := err != nil; gotErr != test.wantErr {
t.Fatalf("wantErr: %t, got %v", test.wantErr, err)
}
Expand Down