diff options
Diffstat (limited to 'internal/db')
| -rw-r--r-- | internal/db/client.go | 18 | ||||
| -rw-r--r-- | internal/db/trillian.go | 232 | ||||
| -rw-r--r-- | internal/db/trillian_test.go | 543 | 
3 files changed, 793 insertions, 0 deletions
| diff --git a/internal/db/client.go b/internal/db/client.go new file mode 100644 index 0000000..ce3bb2b --- /dev/null +++ b/internal/db/client.go @@ -0,0 +1,18 @@ +package db + +import ( +	"context" + +	"git.sigsum.org/sigsum-go/pkg/requests" +	"git.sigsum.org/sigsum-go/pkg/types" +) + +// Client is an interface that interacts with a log's database backend +type Client interface { +	AddLeaf(context.Context, *requests.Leaf, uint64) (bool, error) +	AddSequencedLeaves(ctx context.Context, leaves types.Leaves, index int64) error +	GetTreeHead(context.Context) (*types.TreeHead, error) +	GetConsistencyProof(context.Context, *requests.ConsistencyProof) (*types.ConsistencyProof, error) +	GetInclusionProof(context.Context, *requests.InclusionProof) (*types.InclusionProof, error) +	GetLeaves(context.Context, *requests.Leaves) (*types.Leaves, error) +} diff --git a/internal/db/trillian.go b/internal/db/trillian.go new file mode 100644 index 0000000..e8a9945 --- /dev/null +++ b/internal/db/trillian.go @@ -0,0 +1,232 @@ +package db + +import ( +	"context" +	"fmt" +	"time" + +	"git.sigsum.org/sigsum-go/pkg/log" +	"git.sigsum.org/sigsum-go/pkg/merkle" +	"git.sigsum.org/sigsum-go/pkg/requests" +	"git.sigsum.org/sigsum-go/pkg/types" +	"github.com/google/trillian" +	trillianTypes "github.com/google/trillian/types" +	"google.golang.org/grpc/codes" +	"google.golang.org/grpc/status" +) + +// TrillianClient implements the Client interface for Trillian's gRPC backend +type TrillianClient struct { +	// TreeID is a Merkle tree identifier that Trillian uses +	TreeID int64 + +	// GRPC is a Trillian gRPC client +	GRPC trillian.TrillianLogClient +} + +// AddLeaf adds a leaf to the tree and returns true if the leaf has +// been sequenced into the tree of size treeSize. +func (c *TrillianClient) AddLeaf(ctx context.Context, req *requests.Leaf, treeSize uint64) (bool, error) { +	leaf := types.Leaf{ +		Statement: types.Statement{ +			ShardHint: req.ShardHint, +			Checksum:  *merkle.HashFn(req.Message[:]), +		}, +		Signature: req.Signature, +		KeyHash:   *merkle.HashFn(req.PublicKey[:]), +	} +	serialized := leaf.ToBinary() + +	log.Debug("queueing leaf request: %x", merkle.HashLeafNode(serialized)) +	_, err := c.GRPC.QueueLeaf(ctx, &trillian.QueueLeafRequest{ +		LogId: c.TreeID, +		Leaf: &trillian.LogLeaf{ +			LeafValue: serialized, +		}, +	}) +	switch status.Code(err) { +	case codes.OK: +	case codes.AlreadyExists: +	default: +		log.Warning("gRPC error: %v", err) +		return false, fmt.Errorf("back-end failure") +	} +	_, err = c.GetInclusionProof(ctx, &requests.InclusionProof{treeSize, *merkle.HashLeafNode(serialized)}) +	return err == nil, nil +} + +// AddSequencedLeaves adds a set of already sequenced leaves to the tree. +func (c *TrillianClient) AddSequencedLeaves(ctx context.Context, leaves types.Leaves, index int64) error { +	trilLeaves := make([]*trillian.LogLeaf, len(leaves)) +	for i, leaf := range leaves { +		trilLeaves[i] = &trillian.LogLeaf{ +			LeafValue: leaf.ToBinary(), +			LeafIndex: index + int64(i), +		} +	} + +	req := trillian.AddSequencedLeavesRequest{ +		LogId:  c.TreeID, +		Leaves: trilLeaves, +	} +	log.Debug("adding sequenced leaves: count %d", len(trilLeaves)) +	var err error +	for wait := 1; wait < 30; wait *= 2 { +		var rsp *trillian.AddSequencedLeavesResponse +		rsp, err = c.GRPC.AddSequencedLeaves(ctx, &req) +		switch status.Code(err) { +		case codes.ResourceExhausted: +			log.Info("waiting %d seconds before retrying to add %d leaves, reason: %v", wait, len(trilLeaves), err) +			time.Sleep(time.Second * time.Duration(wait)) +			continue +		case codes.OK: +			if rsp == nil { +				return fmt.Errorf("GRPC.AddSequencedLeaves no response") +			} +			// FIXME: check rsp.Results.QueuedLogLeaf +			return nil +		default: +			return fmt.Errorf("GRPC.AddSequencedLeaves error: %v", err) +		} +	} + +	return fmt.Errorf("giving up on adding %d leaves", len(trilLeaves)) +} + +func (c *TrillianClient) GetTreeHead(ctx context.Context) (*types.TreeHead, error) { +	rsp, err := c.GRPC.GetLatestSignedLogRoot(ctx, &trillian.GetLatestSignedLogRootRequest{ +		LogId: c.TreeID, +	}) +	if err != nil { +		return nil, fmt.Errorf("backend failure: %v", err) +	} +	if rsp == nil { +		return nil, fmt.Errorf("no response") +	} +	if rsp.SignedLogRoot == nil { +		return nil, fmt.Errorf("no signed log root") +	} +	if rsp.SignedLogRoot.LogRoot == nil { +		return nil, fmt.Errorf("no log root") +	} +	var r trillianTypes.LogRootV1 +	if err := r.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil { +		return nil, fmt.Errorf("no log root: unmarshal failed: %v", err) +	} +	if len(r.RootHash) != merkle.HashSize { +		return nil, fmt.Errorf("unexpected hash length: %d", len(r.RootHash)) +	} +	return treeHeadFromLogRoot(&r), nil +} + +func (c *TrillianClient) GetConsistencyProof(ctx context.Context, req *requests.ConsistencyProof) (*types.ConsistencyProof, error) { +	rsp, err := c.GRPC.GetConsistencyProof(ctx, &trillian.GetConsistencyProofRequest{ +		LogId:          c.TreeID, +		FirstTreeSize:  int64(req.OldSize), +		SecondTreeSize: int64(req.NewSize), +	}) +	if err != nil { +		return nil, fmt.Errorf("backend failure: %v", err) +	} +	if rsp == nil { +		return nil, fmt.Errorf("no response") +	} +	if rsp.Proof == nil { +		return nil, fmt.Errorf("no consistency proof") +	} +	if len(rsp.Proof.Hashes) == 0 { +		return nil, fmt.Errorf("not a consistency proof: empty") +	} +	path, err := nodePathFromHashes(rsp.Proof.Hashes) +	if err != nil { +		return nil, fmt.Errorf("not a consistency proof: %v", err) +	} +	return &types.ConsistencyProof{ +		OldSize: req.OldSize, +		NewSize: req.NewSize, +		Path:    path, +	}, nil +} + +func (c *TrillianClient) GetInclusionProof(ctx context.Context, req *requests.InclusionProof) (*types.InclusionProof, error) { +	rsp, err := c.GRPC.GetInclusionProofByHash(ctx, &trillian.GetInclusionProofByHashRequest{ +		LogId:           c.TreeID, +		LeafHash:        req.LeafHash[:], +		TreeSize:        int64(req.TreeSize), +		OrderBySequence: true, +	}) +	if err != nil { +		return nil, fmt.Errorf("backend failure: %v", err) +	} +	if rsp == nil { +		return nil, fmt.Errorf("no response") +	} +	if len(rsp.Proof) != 1 { +		return nil, fmt.Errorf("bad proof count: %d", len(rsp.Proof)) +	} +	proof := rsp.Proof[0] +	if len(proof.Hashes) == 0 { +		return nil, fmt.Errorf("not an inclusion proof: empty") +	} +	path, err := nodePathFromHashes(proof.Hashes) +	if err != nil { +		return nil, fmt.Errorf("not an inclusion proof: %v", err) +	} +	return &types.InclusionProof{ +		TreeSize:  req.TreeSize, +		LeafIndex: uint64(proof.LeafIndex), +		Path:      path, +	}, nil +} + +func (c *TrillianClient) GetLeaves(ctx context.Context, req *requests.Leaves) (*types.Leaves, error) { +	rsp, err := c.GRPC.GetLeavesByRange(ctx, &trillian.GetLeavesByRangeRequest{ +		LogId:      c.TreeID, +		StartIndex: int64(req.StartSize), +		Count:      int64(req.EndSize-req.StartSize) + 1, +	}) +	if err != nil { +		return nil, fmt.Errorf("backend failure: %v", err) +	} +	if rsp == nil { +		return nil, fmt.Errorf("no response") +	} +	if got, want := len(rsp.Leaves), int(req.EndSize-req.StartSize+1); got != want { +		return nil, fmt.Errorf("unexpected number of leaves: %d", got) +	} +	var list types.Leaves = make([]types.Leaf, 0, len(rsp.Leaves)) +	for i, leaf := range rsp.Leaves { +		leafIndex := int64(req.StartSize + uint64(i)) +		if leafIndex != leaf.LeafIndex { +			return nil, fmt.Errorf("unexpected leaf(%d): got index %d", leafIndex, leaf.LeafIndex) +		} + +		var l types.Leaf +		if err := l.FromBinary(leaf.LeafValue); err != nil { +			return nil, fmt.Errorf("unexpected leaf(%d): %v", leafIndex, err) +		} +		list = append(list[:], l) +	} +	return &list, nil +} + +func treeHeadFromLogRoot(lr *trillianTypes.LogRootV1) *types.TreeHead { +	th := types.TreeHead{ +		Timestamp: uint64(time.Now().Unix()), +		TreeSize:  uint64(lr.TreeSize), +	} +	copy(th.RootHash[:], lr.RootHash) +	return &th +} + +func nodePathFromHashes(hashes [][]byte) ([]merkle.Hash, error) { +	path := make([]merkle.Hash, len(hashes)) +	for i := 0; i < len(hashes); i++ { +		if len(hashes[i]) != merkle.HashSize { +			return nil, fmt.Errorf("unexpected hash length: %v", len(hashes[i])) +		} + +		copy(path[i][:], hashes[i]) +	} +	return path, nil +} diff --git a/internal/db/trillian_test.go b/internal/db/trillian_test.go new file mode 100644 index 0000000..9ae682e --- /dev/null +++ b/internal/db/trillian_test.go @@ -0,0 +1,543 @@ +package db + +import ( +	"bytes" +	"context" +	"fmt" +	"reflect" +	"testing" +	"time" + +	mocksTrillian "git.sigsum.org/log-go/internal/mocks/trillian" +	"git.sigsum.org/sigsum-go/pkg/merkle" +	"git.sigsum.org/sigsum-go/pkg/requests" +	"git.sigsum.org/sigsum-go/pkg/types" +	"github.com/golang/mock/gomock" +	"github.com/google/trillian" +	ttypes "github.com/google/trillian/types" +	//"google.golang.org/grpc/codes" +	//"google.golang.org/grpc/status" +) + +// TODO: Add TestAddSequencedLeaves +// TODO: Update TestAddLeaf +//func TestAddLeaf(t *testing.T) { +//	req := &requests.Leaf{ +//		ShardHint:  0, +//		Message:    merkle.Hash{}, +//		Signature:  types.Signature{}, +//		PublicKey:  types.PublicKey{}, +//		DomainHint: "example.com", +//	} +//	for _, table := range []struct { +//		description string +//		req         *requests.Leaf +//		rsp         *trillian.QueueLeafResponse +//		err         error +//		wantErr     bool +//	}{ +//		{ +//			description: "invalid: backend failure", +//			req:         req, +//			err:         fmt.Errorf("something went wrong"), +//			wantErr:     true, +//		}, +//		{ +//			description: "invalid: no response", +//			req:         req, +//			wantErr:     true, +//		}, +//		{ +//			description: "invalid: no queued leaf", +//			req:         req, +//			rsp:         &trillian.QueueLeafResponse{}, +//			wantErr:     true, +//		}, +//		{ +//			description: "invalid: leaf is already queued or included", +//			req:         req, +//			rsp: &trillian.QueueLeafResponse{ +//				QueuedLeaf: &trillian.QueuedLogLeaf{ +//					Leaf: &trillian.LogLeaf{ +//						LeafValue: []byte{0}, // does not matter for test +//					}, +//					Status: status.New(codes.AlreadyExists, "duplicate").Proto(), +//				}, +//			}, +//			wantErr: true, +//		}, +//		{ +//			description: "valid", +//			req:         req, +//			rsp: &trillian.QueueLeafResponse{ +//				QueuedLeaf: &trillian.QueuedLogLeaf{ +//					Leaf: &trillian.LogLeaf{ +//						LeafValue: []byte{0}, // does not matter for test +//					}, +//					Status: status.New(codes.OK, "ok").Proto(), +//				}, +//			}, +//		}, +//	} { +//		// Run deferred functions at the end of each iteration +//		func() { +//			ctrl := gomock.NewController(t) +//			defer ctrl.Finish() +//			grpc := mocksTrillian.NewMockTrillianLogClient(ctrl) +//			grpc.EXPECT().QueueLeaf(gomock.Any(), gomock.Any()).Return(table.rsp, table.err) +//			client := TrillianClient{GRPC: grpc} +// +//			_, err := client.AddLeaf(context.Background(), table.req, 0) +//			if got, want := err != nil, table.wantErr; got != want { +//				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) +//			} +//		}() +//	} +//} + +func TestGetTreeHead(t *testing.T) { +	// valid root +	root := &ttypes.LogRootV1{ +		TreeSize:       0, +		RootHash:       make([]byte, merkle.HashSize), +		TimestampNanos: 1622585623133599429, +	} +	buf, err := root.MarshalBinary() +	if err != nil { +		t.Fatalf("must marshal log root: %v", err) +	} +	// invalid root +	root.RootHash = make([]byte, merkle.HashSize+1) +	bufBadHash, err := root.MarshalBinary() +	if err != nil { +		t.Fatalf("must marshal log root: %v", err) +	} + +	for _, table := range []struct { +		description string +		rsp         *trillian.GetLatestSignedLogRootResponse +		err         error +		wantErr     bool +		wantTh      *types.TreeHead +	}{ +		{ +			description: "invalid: backend failure", +			err:         fmt.Errorf("something went wrong"), +			wantErr:     true, +		}, +		{ +			description: "invalid: no response", +			wantErr:     true, +		}, +		{ +			description: "invalid: no signed log root", +			rsp:         &trillian.GetLatestSignedLogRootResponse{}, +			wantErr:     true, +		}, +		{ +			description: "invalid: no log root", +			rsp: &trillian.GetLatestSignedLogRootResponse{ +				SignedLogRoot: &trillian.SignedLogRoot{}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: no log root: unmarshal failed", +			rsp: &trillian.GetLatestSignedLogRootResponse{ +				SignedLogRoot: &trillian.SignedLogRoot{ +					LogRoot: buf[1:], +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: unexpected hash length", +			rsp: &trillian.GetLatestSignedLogRootResponse{ +				SignedLogRoot: &trillian.SignedLogRoot{ +					LogRoot: bufBadHash, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "valid", +			rsp: &trillian.GetLatestSignedLogRootResponse{ +				SignedLogRoot: &trillian.SignedLogRoot{ +					LogRoot: buf, +				}, +			}, +			wantTh: &types.TreeHead{ +				Timestamp: 1622585623, +				TreeSize:  0, +				RootHash:  merkle.Hash{}, +			}, +		}, +	} { +		// Run deferred functions at the end of each iteration +		func() { +			ctrl := gomock.NewController(t) +			defer ctrl.Finish() +			grpc := mocksTrillian.NewMockTrillianLogClient(ctrl) +			grpc.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.rsp, table.err) +			client := TrillianClient{GRPC: grpc} + +			th, err := client.GetTreeHead(context.Background()) +			if got, want := err != nil, table.wantErr; got != want { +				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) +			} +			if err != nil { +				return +			} + +			// we would need a clock that can be mocked to make a nicer test +			now := uint64(time.Now().Unix()) +			if got, wantLow, wantHigh := th.Timestamp, now-5, now+5; got < wantLow || got > wantHigh { +				t.Errorf("got tree head with timestamp %d but wanted between [%d, %d] in test %q", +					got, wantLow, wantHigh, table.description) +			} +			if got, want := th.TreeSize, table.wantTh.TreeSize; got != want { +				t.Errorf("got tree head with tree size %d but wanted %d in test %q", got, want, table.description) +			} +			if got, want := th.RootHash[:], table.wantTh.RootHash[:]; !bytes.Equal(got, want) { +				t.Errorf("got root hash %x but wanted %x in test %q", got, want, table.description) +			} +		}() +	} +} + +func TestGetConsistencyProof(t *testing.T) { +	req := &requests.ConsistencyProof{ +		OldSize: 1, +		NewSize: 3, +	} +	for _, table := range []struct { +		description string +		req         *requests.ConsistencyProof +		rsp         *trillian.GetConsistencyProofResponse +		err         error +		wantErr     bool +		wantProof   *types.ConsistencyProof +	}{ +		{ +			description: "invalid: backend failure", +			req:         req, +			err:         fmt.Errorf("something went wrong"), +			wantErr:     true, +		}, +		{ +			description: "invalid: no response", +			req:         req, +			wantErr:     true, +		}, +		{ +			description: "invalid: no consistency proof", +			req:         req, +			rsp:         &trillian.GetConsistencyProofResponse{}, +			wantErr:     true, +		}, +		{ +			description: "invalid: not a consistency proof (1/2)", +			req:         req, +			rsp: &trillian.GetConsistencyProofResponse{ +				Proof: &trillian.Proof{ +					Hashes: [][]byte{}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: not a consistency proof (2/2)", +			req:         req, +			rsp: &trillian.GetConsistencyProofResponse{ +				Proof: &trillian.Proof{ +					Hashes: [][]byte{ +						make([]byte, merkle.HashSize), +						make([]byte, merkle.HashSize+1), +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "valid", +			req:         req, +			rsp: &trillian.GetConsistencyProofResponse{ +				Proof: &trillian.Proof{ +					Hashes: [][]byte{ +						make([]byte, merkle.HashSize), +						make([]byte, merkle.HashSize), +					}, +				}, +			}, +			wantProof: &types.ConsistencyProof{ +				OldSize: 1, +				NewSize: 3, +				Path: []merkle.Hash{ +					merkle.Hash{}, +					merkle.Hash{}, +				}, +			}, +		}, +	} { +		// Run deferred functions at the end of each iteration +		func() { +			ctrl := gomock.NewController(t) +			defer ctrl.Finish() +			grpc := mocksTrillian.NewMockTrillianLogClient(ctrl) +			grpc.EXPECT().GetConsistencyProof(gomock.Any(), gomock.Any()).Return(table.rsp, table.err) +			client := TrillianClient{GRPC: grpc} + +			proof, err := client.GetConsistencyProof(context.Background(), table.req) +			if got, want := err != nil, table.wantErr; got != want { +				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) +			} +			if err != nil { +				return +			} +			if got, want := proof, table.wantProof; !reflect.DeepEqual(got, want) { +				t.Errorf("got proof\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description) +			} +		}() +	} +} + +func TestGetInclusionProof(t *testing.T) { +	req := &requests.InclusionProof{ +		TreeSize: 4, +		LeafHash: merkle.Hash{}, +	} +	for _, table := range []struct { +		description string +		req         *requests.InclusionProof +		rsp         *trillian.GetInclusionProofByHashResponse +		err         error +		wantErr     bool +		wantProof   *types.InclusionProof +	}{ +		{ +			description: "invalid: backend failure", +			req:         req, +			err:         fmt.Errorf("something went wrong"), +			wantErr:     true, +		}, +		{ +			description: "invalid: no response", +			req:         req, +			wantErr:     true, +		}, +		{ +			description: "invalid: bad proof count", +			req:         req, +			rsp: &trillian.GetInclusionProofByHashResponse{ +				Proof: []*trillian.Proof{ +					&trillian.Proof{}, +					&trillian.Proof{}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: not an inclusion proof (1/2)", +			req:         req, +			rsp: &trillian.GetInclusionProofByHashResponse{ +				Proof: []*trillian.Proof{ +					&trillian.Proof{ +						LeafIndex: 1, +						Hashes:    [][]byte{}, +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: not an inclusion proof (2/2)", +			req:         req, +			rsp: &trillian.GetInclusionProofByHashResponse{ +				Proof: []*trillian.Proof{ +					&trillian.Proof{ +						LeafIndex: 1, +						Hashes: [][]byte{ +							make([]byte, merkle.HashSize), +							make([]byte, merkle.HashSize+1), +						}, +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "valid", +			req:         req, +			rsp: &trillian.GetInclusionProofByHashResponse{ +				Proof: []*trillian.Proof{ +					&trillian.Proof{ +						LeafIndex: 1, +						Hashes: [][]byte{ +							make([]byte, merkle.HashSize), +							make([]byte, merkle.HashSize), +						}, +					}, +				}, +			}, +			wantProof: &types.InclusionProof{ +				TreeSize:  4, +				LeafIndex: 1, +				Path: []merkle.Hash{ +					merkle.Hash{}, +					merkle.Hash{}, +				}, +			}, +		}, +	} { +		// Run deferred functions at the end of each iteration +		func() { +			ctrl := gomock.NewController(t) +			defer ctrl.Finish() +			grpc := mocksTrillian.NewMockTrillianLogClient(ctrl) +			grpc.EXPECT().GetInclusionProofByHash(gomock.Any(), gomock.Any()).Return(table.rsp, table.err) +			client := TrillianClient{GRPC: grpc} + +			proof, err := client.GetInclusionProof(context.Background(), table.req) +			if got, want := err != nil, table.wantErr; got != want { +				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) +			} +			if err != nil { +				return +			} +			if got, want := proof, table.wantProof; !reflect.DeepEqual(got, want) { +				t.Errorf("got proof\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description) +			} +		}() +	} +} + +func TestGetLeaves(t *testing.T) { +	req := &requests.Leaves{ +		StartSize: 1, +		EndSize:   2, +	} +	firstLeaf := &types.Leaf{ +		Statement: types.Statement{ +			ShardHint: 0, +			Checksum:  merkle.Hash{}, +		}, +		Signature: types.Signature{}, +		KeyHash:   merkle.Hash{}, +	} +	secondLeaf := &types.Leaf{ +		Statement: types.Statement{ +			ShardHint: 0, +			Checksum:  merkle.Hash{}, +		}, +		Signature: types.Signature{}, +		KeyHash:   merkle.Hash{}, +	} + +	for _, table := range []struct { +		description string +		req         *requests.Leaves +		rsp         *trillian.GetLeavesByRangeResponse +		err         error +		wantErr     bool +		wantLeaves  *types.Leaves +	}{ +		{ +			description: "invalid: backend failure", +			req:         req, +			err:         fmt.Errorf("something went wrong"), +			wantErr:     true, +		}, +		{ +			description: "invalid: no response", +			req:         req, +			wantErr:     true, +		}, +		{ +			description: "invalid: unexpected number of leaves", +			req:         req, +			rsp: &trillian.GetLeavesByRangeResponse{ +				Leaves: []*trillian.LogLeaf{ +					&trillian.LogLeaf{ +						LeafValue: firstLeaf.ToBinary(), +						LeafIndex: 1, +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: unexpected leaf (1/2)", +			req:         req, +			rsp: &trillian.GetLeavesByRangeResponse{ +				Leaves: []*trillian.LogLeaf{ +					&trillian.LogLeaf{ +						LeafValue: firstLeaf.ToBinary(), +						LeafIndex: 1, +					}, +					&trillian.LogLeaf{ +						LeafValue: secondLeaf.ToBinary(), +						LeafIndex: 3, +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "invalid: unexpected leaf (2/2)", +			req:         req, +			rsp: &trillian.GetLeavesByRangeResponse{ +				Leaves: []*trillian.LogLeaf{ +					&trillian.LogLeaf{ +						LeafValue: firstLeaf.ToBinary(), +						LeafIndex: 1, +					}, +					&trillian.LogLeaf{ +						LeafValue: secondLeaf.ToBinary()[1:], +						LeafIndex: 2, +					}, +				}, +			}, +			wantErr: true, +		}, +		{ +			description: "valid", +			req:         req, +			rsp: &trillian.GetLeavesByRangeResponse{ +				Leaves: []*trillian.LogLeaf{ +					&trillian.LogLeaf{ +						LeafValue: firstLeaf.ToBinary(), +						LeafIndex: 1, +					}, +					&trillian.LogLeaf{ +						LeafValue: secondLeaf.ToBinary(), +						LeafIndex: 2, +					}, +				}, +			}, +			wantLeaves: &types.Leaves{ +				*firstLeaf, +				*secondLeaf, +			}, +		}, +	} { +		// Run deferred functions at the end of each iteration +		func() { +			ctrl := gomock.NewController(t) +			defer ctrl.Finish() +			grpc := mocksTrillian.NewMockTrillianLogClient(ctrl) +			grpc.EXPECT().GetLeavesByRange(gomock.Any(), gomock.Any()).Return(table.rsp, table.err) +			client := TrillianClient{GRPC: grpc} + +			leaves, err := client.GetLeaves(context.Background(), table.req) +			if got, want := err != nil, table.wantErr; got != want { +				t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) +			} +			if err != nil { +				return +			} +			if got, want := leaves, table.wantLeaves; !reflect.DeepEqual(got, want) { +				t.Errorf("got leaves\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.description) +			} +		}() +	} +} | 
