Skip to content

Commit

Permalink
Support multiple versions of header
Browse files Browse the repository at this point in the history
- Define the Header of the verison 0.3
- Support the serde of multiple versions
- Test the client API with espresso dev ndoe
  • Loading branch information
ImJeremyHe committed Oct 21, 2024
1 parent 8e47e5d commit 86687b7
Show file tree
Hide file tree
Showing 10 changed files with 671 additions and 122 deletions.
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ func (c *Client) FetchLatestBlockHeight(ctx context.Context) (uint64, error) {
return res, nil
}

func (c *Client) FetchHeaderByHeight(ctx context.Context, blockHeight uint64) (types.Header, error) {
var res types.Header
func (c *Client) FetchHeaderByHeight(ctx context.Context, blockHeight uint64) (types.HeaderImpl, error) {
var res types.HeaderImpl
if err := c.get(ctx, &res, "availability/header/%d", blockHeight); err != nil {
return types.Header{}, err
return types.HeaderImpl{}, err
}
return res, nil
}

func (c *Client) FetchHeadersByRange(ctx context.Context, from uint64, until uint64) ([]types.Header, error) {
var res []types.Header
func (c *Client) FetchHeadersByRange(ctx context.Context, from uint64, until uint64) ([]types.HeaderImpl, error) {
var res []types.HeaderImpl
if err := c.get(ctx, &res, "availability/header/%d/%d", from, until); err != nil {
return []types.Header{}, err
return []types.HeaderImpl{}, err
}
return res, nil
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *Client) get(ctx context.Context, out any, format string, args ...any) e
return err
}
if err := json.Unmarshal(body, out); err != nil {
return err
return fmt.Errorf("request failed with body %s and error %v", string(body), err)
}
return nil
}
108 changes: 108 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package client

import (
"context"
"os/exec"
"testing"
"time"

"github.com/ethereum/go-ethereum/log"
)

var workingDir = "./dev-node"

func TestApiWithEspressoDevNode(t *testing.T) {
ctx := context.Background()
cleanup := runEspresso(t, ctx)
defer cleanup()

err := waitForEspressoNode(t, ctx)
if err != nil {
t.Fatal("failed to start espresso dev node", err)
}

client := NewClient("http://localhost:21000")

blockHeight, err := client.FetchLatestBlockHeight(ctx)
if err != nil {
t.Fatal("failed to fetch block height")
}

_, err = client.FetchHeaderByHeight(ctx, blockHeight)
if err != nil {
t.Fatal("failed to fetch header", err)
}

_, err = client.FetchVidCommonByHeight(ctx, blockHeight)
if err != nil {
t.Fatal("failed to fetch vid common", err)
}

_, err = client.FetchHeadersByRange(ctx, 1, blockHeight)
if err != nil {
t.Fatal("failed to fetch headers by range", err)
}

}

func runEspresso(t *testing.T, ctx context.Context) func() {
shutdown := func() {
p := exec.Command("docker", "compose", "down")
p.Dir = workingDir
err := p.Run()
if err != nil {
panic(err)
}
}

shutdown()
invocation := []string{"compose", "up", "-d", "--build"}
nodes := []string{
"espresso-dev-node",
}
invocation = append(invocation, nodes...)
procees := exec.Command("docker", invocation...)
procees.Dir = workingDir

go func() {
if err := procees.Run(); err != nil {
log.Error(err.Error())
panic(err)
}
}()
return shutdown
}

func waitForWith(
t *testing.T,
ctxinput context.Context,
timeout time.Duration,
interval time.Duration,
condition func() bool,
) error {
ctx, cancel := context.WithTimeout(ctxinput, timeout)
defer cancel()

for {
if condition() {
return nil
}
select {
case <-time.After(interval):
case <-ctx.Done():
return ctx.Err()
}
}
}

func waitForEspressoNode(t *testing.T, ctx context.Context) error {
return waitForWith(t, ctx, 400*time.Second, 1*time.Second, func() bool {
out, err := exec.Command("curl", "-s", "-L", "-f", "http://localhost:21000/availability/block/10").Output()
if err != nil {
log.Warn("error executing curl command:", "err", err)
return false
}

return len(out) > 0
})
}
3 changes: 3 additions & 0 deletions client/dev-node/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ESPRESSO_SEQUENCER_API_PORT=21000
ESPRESSO_BUILDER_PORT=23000
ESPRESSO_DEV_NODE_PORT=20000
16 changes: 16 additions & 0 deletions client/dev-node/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.9'
services:
espresso-dev-node:
image: ghcr.io/espressosystems/espresso-sequencer/espresso-dev-node:main
ports:
- "$ESPRESSO_SEQUENCER_API_PORT:$ESPRESSO_SEQUENCER_API_PORT"
- "$ESPRESSO_BUILDER_PORT:$ESPRESSO_BUILDER_PORT"
- "$ESPRESSO_DEV_NODE_PORT:$ESPRESSO_DEV_NODE_PORT"
environment:
- ESPRESSO_SEQUENCER_API_PORT
- ESPRESSO_BUILDER_PORT
- ESPRESSO_DEV_NODE_PORT
- RUST_LOG=info
- RUST_LOG_FORMAT
extra_hosts:
- "host.docker.internal:host-gateway"
4 changes: 2 additions & 2 deletions client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type QueryService interface {
// Get the latest block number
FetchLatestBlockHeight(ctx context.Context) (uint64, error)
// Get the header for block number `height`.
FetchHeaderByHeight(ctx context.Context, height uint64) (types.Header, error)
FetchHeaderByHeight(ctx context.Context, height uint64) (types.HeaderImpl, error)
// Get the headers starting from the given :from up until the given :until
FetchHeadersByRange(ctx context.Context, from uint64, until uint64) ([]types.Header, error)
FetchHeadersByRange(ctx context.Context, from uint64, until uint64) ([]types.HeaderImpl, error)
// Get the transactions belonging to the given namespace at the block height,
// along with a proof that these are all such transactions.
FetchTransactionsInBlock(ctx context.Context, blockHeight uint64, namespace uint64) (TransactionsInBlock, error)
Expand Down
Loading

0 comments on commit 86687b7

Please sign in to comment.