Skip to content

Commit

Permalink
Simplify as suggested.
Browse files Browse the repository at this point in the history
(I used parts of `serverless_feeder.go` which has a somewhat similar client)
  • Loading branch information
lapo-luchini committed Dec 9, 2024
1 parent f0978e7 commit 425e49b
Showing 1 changed file with 13 additions and 86 deletions.
99 changes: 13 additions & 86 deletions internal/feeder/tiles/tiles_feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package tiles is an implementation of a witness feeder for the Tessera tiles log.
// Package tiles is an implementation of a witness feeder for C2SP tlog-tiles compatible logs.
package tiles

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"

"github.com/transparency-dev/formats/log"
"github.com/transparency-dev/trillian-tessera/api/layout"
"github.com/transparency-dev/trillian-tessera/client"
"github.com/transparency-dev/witness/internal/config"
"github.com/transparency-dev/witness/internal/feeder"
"golang.org/x/mod/sumdb/tlog"
"k8s.io/klog/v2"
)

const (
// tileHeight is the tlog tile height.
// From: https://developers.google.com/android/binary_transparency/tile
tileHeight = 8
)

// FeedLog periodically feeds checkpoints from the log to the witness.
Expand All @@ -45,45 +35,31 @@ func FeedLog(ctx context.Context, l config.Log, w feeder.Witness, c *http.Client
if err != nil {
return fmt.Errorf("invalid LogURL %q: %v", l.URL, err)
}

fetchCP := func(ctx context.Context) ([]byte, error) {
cpTxt, err := fetch(ctx, c, lURL, "checkpoint")
if err != nil {
return nil, fmt.Errorf("failed to fetch checkpoint: %v", err)
}
return cpTxt, err
f, err := client.NewHTTPFetcher(lURL, c)
if err != nil {
return fmt.Errorf("failed to create fetcher: %v", err)
}

fetchProof := func(ctx context.Context, from, to log.Checkpoint) ([][]byte, error) {
if from.Size == 0 {
return [][]byte{}, nil
}
var h [32]byte
copy(h[:], to.Hash)
tree := tlog.Tree{
N: int64(to.Size),
Hash: h,
pb, err := client.NewProofBuilder(ctx, to, f.ReadTile)
if err != nil {
return nil, fmt.Errorf("failed to create proof builder for %q: %v", l.Origin, err)
}
tr := tileReader{fetch: func(p string) ([]byte, error) {
return fetch(ctx, c, lURL, p)
}}

proof, err := tlog.ProveTree(int64(to.Size), int64(from.Size), tlog.TileHashReader(tree, tr))
conP, err := pb.ConsistencyProof(ctx, from.Size, to.Size)
if err != nil {
return nil, fmt.Errorf("ProveTree: %v", err)
return nil, fmt.Errorf("failed to create proof for %q(%d -> %d): %v", l.Origin, from.Size, to.Size, err)
}
r := make([][]byte, 0, len(proof))
for _, h := range proof {
h := h
r = append(r, h[:])
}
klog.V(1).Infof("Fetched proof from %d -> %d: %x", from.Size, to.Size, r)
return r, nil
return conP, nil
}

opts := feeder.FeedOpts{
LogID: l.ID,
LogOrigin: l.Origin,
FetchCheckpoint: fetchCP,
FetchCheckpoint: f.ReadCheckpoint,
FetchProof: fetchProof,
LogSigVerifier: l.Verifier,
Witness: w,
Expand All @@ -94,52 +70,3 @@ func FeedLog(ctx context.Context, l config.Log, w feeder.Witness, c *http.Client
_, err = feeder.FeedOnce(ctx, opts)
return err
}

type tileReader struct {
fetch func(p string) ([]byte, error)
}

func (tr tileReader) Height() int { return tileHeight }

func (tr tileReader) SaveTiles([]tlog.Tile, [][]byte) {}

func (tr tileReader) ReadTiles(tiles []tlog.Tile) ([][]byte, error) {
r := make([][]byte, 0, len(tiles))
for _, t := range tiles {
path := layout.TilePath(uint64(t.L), uint64(t.N), uint64(t.W))
tile, err := tr.fetch(path)
if err != nil {
return nil, fmt.Errorf("failed to fetch %q: %v", path, err)
}
r = append(r, tile)
}
return r, nil
}

func fetch(ctx context.Context, c *http.Client, base *url.URL, path string) ([]byte, error) {
u, err := base.Parse(path)
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %v", err)
}
klog.V(2).Infof("GET %s", u.String())
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
req = req.WithContext(ctx)

rsp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request to %q: %v", u.String(), err)
}
defer rsp.Body.Close()

if rsp.StatusCode == 404 {
return nil, os.ErrNotExist
}
if rsp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status fetching %q: %s", u.String(), rsp.Status)
}

return io.ReadAll(rsp.Body)
}

0 comments on commit 425e49b

Please sign in to comment.