Skip to content

Commit

Permalink
Handle special case prev < next (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter authored Oct 21, 2024
1 parent d80638a commit 665958e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 7 additions & 0 deletions internal/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ func (w *Witness) Update(ctx context.Context, logID string, nextRaw []byte, cPro
counterUpdateSuccess.Inc(logID)
return signed, nil
}
if next.Size != prev.Size && len(cProof) == 0 {
// We require a proof, but we were given an empty one - the submitter likely thinks we've not seen a checkpoint for this log
// before and is trying to get us to TOFU.
// This is a special case of "prev > next" above, so return the same code so higher layers can handle similarly (e.g. by telling
// the submitter our view of prev.size).
return prevRaw, status.Errorf(codes.AlreadyExists, "we already have a non-zero checkpoint")
}

// The only remaining option is next.Size > prev.Size. This might be
// valid so we use either plain consistency proofs or compact ranges to
Expand Down
39 changes: 32 additions & 7 deletions internal/witness/witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/transparency-dev/witness/internal/persistence/inmemory"
"github.com/transparency-dev/witness/monitoring"
"golang.org/x/mod/sumdb/note"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -281,13 +283,14 @@ func mustCreateCheckpoint(t *testing.T, sk string, size uint64, rootHash []byte)

func TestUpdate(t *testing.T) {
for _, test := range []struct {
desc string
initC []byte
newC []byte
pf [][]byte
useCR bool
initCR [][]byte
isGood bool
desc string
initC []byte
newC []byte
pf [][]byte
useCR bool
initCR [][]byte
isGood bool
wantCode codes.Code
}{
{
desc: "vanilla consistency happy path",
Expand All @@ -313,6 +316,23 @@ func TestUpdate(t *testing.T) {
useCR: false,
isGood: true,
},
{
desc: "vanilla resubmit known CP",
initC: mustCreateCheckpoint(t, mSK, 5, dh("e35b268c1522014ef412d2a54fa94838862d453631617b0307e5c77dcbeefc11", 32)),
newC: mustCreateCheckpoint(t, mSK, 5, dh("e35b268c1522014ef412d2a54fa94838862d453631617b0307e5c77dcbeefc11", 32)),
pf: [][]byte{},
useCR: false,
isGood: true,
},
{
desc: "vanilla attempt to re-TOFU",
initC: mustCreateCheckpoint(t, mSK, 4, dh("e35b268c1522014ef412d2a54fa94838862d453631617b0307e5c77dcbeefc11", 32)),
newC: mustCreateCheckpoint(t, mSK, 5, dh("e35b268c1522014ef412d2a54fa94838862d453631617b0307e5c77dcbeefc11", 32)),
pf: [][]byte{},
useCR: false,
isGood: false,
wantCode: codes.AlreadyExists,
},
{
desc: "vanilla path, but the first line changed",
initC: mInit,
Expand Down Expand Up @@ -384,6 +404,11 @@ func TestUpdate(t *testing.T) {
if err == nil {
t.Fatal("should have gotten an error but didn't")
}
if test.wantCode != 0 {
if gotCode := status.Code(err); gotCode != test.wantCode {
t.Fatalf("Got status code %v, want %v", gotCode, test.wantCode)
}
}
}
})
}
Expand Down

0 comments on commit 665958e

Please sign in to comment.