From c05c22ddbc771e7713849cae40f9d91bfafa0503 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 25 Feb 2021 14:36:35 +0100 Subject: major refactor based on README.md and TODOs Updated types, improved units tests, isolated most test data to have it in one place, renamed and created new files to improve readability, and fixed a bunch of minor TODOs. --- trillian.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'trillian.go') diff --git a/trillian.go b/trillian.go index 162ec77..2adf567 100644 --- a/trillian.go +++ b/trillian.go @@ -3,11 +3,10 @@ package stfe import ( "fmt" - "net/http" - "github.com/golang/glog" "github.com/google/trillian" "github.com/google/trillian/types" + stfetypes "github.com/system-transparency/stfe/types" "google.golang.org/grpc/codes" ) @@ -27,42 +26,42 @@ func checkQueueLeaf(rsp *trillian.QueueLeafResponse, err error) error { return nil } -func checkGetLeavesByRange(req *GetEntriesRequest, rsp *trillian.GetLeavesByRangeResponse, err error) (int, error) { +func checkGetLeavesByRange(req *stfetypes.GetEntriesV1, rsp *trillian.GetLeavesByRangeResponse, err error) error { if err != nil { - return http.StatusInternalServerError, fmt.Errorf("Trillian Error: %v", err) + return fmt.Errorf("Trillian Error: %v", err) } if rsp == nil { - return http.StatusInternalServerError, fmt.Errorf("Trillian error: empty response") + return fmt.Errorf("Trillian error: empty response") } if rsp.SignedLogRoot == nil { - return http.StatusInternalServerError, fmt.Errorf("Trillian error: no signed log root") + return fmt.Errorf("Trillian error: no signed log root") } if rsp.SignedLogRoot.LogRoot == nil { - return http.StatusInternalServerError, fmt.Errorf("Trillian error: no log root") + return fmt.Errorf("Trillian error: no log root") } if len(rsp.Leaves) == 0 { - return http.StatusInternalServerError, fmt.Errorf("Trillian error: no leaves") + return fmt.Errorf("Trillian error: no leaves") } if len(rsp.Leaves) > int(req.End-req.Start+1) { - return http.StatusInternalServerError, fmt.Errorf("too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.Start, req.End) + return fmt.Errorf("too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.Start, req.End) } // Ensure that a bad start parameter results in an error var lr types.LogRootV1 if err := lr.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil { - return http.StatusInternalServerError, fmt.Errorf("cannot unmarshal log root: %v", err) + return fmt.Errorf("cannot unmarshal log root: %v", err) } if uint64(req.Start) >= lr.TreeSize { - return http.StatusNotFound, fmt.Errorf("invalid start(%d): tree size is %d", req.Start, lr.TreeSize) + return fmt.Errorf("invalid start(%d): tree size is %d", req.Start, lr.TreeSize) } // Ensure that we got and return expected leaf indices for i, leaf := range rsp.Leaves { - if leaf.LeafIndex != req.Start+int64(i) { - return http.StatusInternalServerError, fmt.Errorf("invalid leaf index: wanted %d, got %d", req.Start+int64(i), leaf.LeafIndex) + if got, want := leaf.LeafIndex, int64(req.Start+uint64(i)); got != want { + return fmt.Errorf("invalid leaf index(%d): wanted %d", got, want) } } - return http.StatusOK, nil + return nil } func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) error { -- cgit v1.2.3