Skip to content

Commit

Permalink
Merge pull request #42 from EspressoSystems/jh/fix
Browse files Browse the repository at this point in the history
Fix light client reader and add CODEOWNERS
  • Loading branch information
nomaxg authored Jun 13, 2024
2 parents 1d048d9 + be7797a commit 04e78ce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These owners will be the default owners for everything in the repo. Unless a
# later match takes precedence, they will be requested for review when someone
# opens a pull request.

* @nomaxg @ImJeremyHe @sveitser
26 changes: 23 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func NewClient(url string) *Client {
}
}

func (c *Client) FetchVidCommonByHeight(ctx context.Context, blockHeight uint64) (types.VidCommon, error) {
var res types.VidCommonQueryData
if err := c.get(ctx, &res, "availability/vid/common/%d", blockHeight); err != nil {
return types.VidCommon{}, err
}
return res.Common, nil
}

func (c *Client) FetchLatestBlockHeight(ctx context.Context) (uint64, error) {
var res uint64
if err := c.get(ctx, &res, "status/block-height"); err != nil {
Expand Down Expand Up @@ -65,9 +73,7 @@ func (c *Client) FetchTransactionsInBlock(ctx context.Context, blockHeight uint6
if err := c.get(ctx, &res, "availability/block/%d/namespace/%d", blockHeight, namespace); err != nil {
return TransactionsInBlock{}, err
}
if res.Proof == nil {
return TransactionsInBlock{}, fmt.Errorf("field proof of type NamespaceResponse is required")
}

if res.Transactions == nil {
return TransactionsInBlock{}, fmt.Errorf("field transactions of type NamespaceResponse is required")
}
Expand All @@ -81,9 +87,23 @@ func (c *Client) FetchTransactionsInBlock(ctx context.Context, blockHeight uint6
txs = append(txs, tx.Payload)
}

if len(txs) > 0 && res.Proof == nil {
return TransactionsInBlock{}, fmt.Errorf("field proof of type NamespaceResponse is required")
}

if res.Proof == nil {
return TransactionsInBlock{}, nil
}

vidCommon, err := c.FetchVidCommonByHeight(ctx, blockHeight)
if err != nil {
return TransactionsInBlock{}, err
}

return TransactionsInBlock{
Transactions: txs,
Proof: *res.Proof,
VidCommon: vidCommon,
}, nil

}
Expand Down
3 changes: 2 additions & 1 deletion client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type TransactionsInBlock struct {
// The transactions.
Transactions []types.Bytes `json:"transactions"`
// A proof that these are all the transactions in the block with the requested namespace.
Proof types.NamespaceProof `json:"proof"`
Proof types.NamespaceProof `json:"proof"`
VidCommon types.VidCommon `json:"vidCommon"`
}

func (t *TransactionsInBlock) UnmarshalJSON(b []byte) error {
Expand Down
17 changes: 11 additions & 6 deletions light-client/light_client_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package lightclient
import (
"context"
"math/big"
"time"

"github.com/EspressoSystems/espresso-sequencer-go/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

var _ LightClientReaderInterface = (*LightClientReader)(nil)

type LightClientReaderInterface interface {
ValidatedHeight() (validatedHeight uint64, l1Height uint64, err error)
FetchMerkleRoot(hotShotHeight uint64, opts bind.CallOpts) (types.BlockMerkleSnapshot, error)
FetchMerkleRoot(hotShotHeight uint64, opts *bind.CallOpts) (types.BlockMerkleSnapshot, error)

// A mock function for now
IsHotShotAvailable(maxDriftTime time.Duration) bool
IsHotShotLive(delayThreshold uint64) (bool, error)
}

type LightClientReader struct {
Expand Down Expand Up @@ -67,6 +67,11 @@ func (l *LightClientReader) FetchMerkleRoot(hotShotHeight uint64, opts *bind.Cal
}, nil
}

func (l *LightClientReader) IsHotShotAvailable(t time.Duration) bool {
return true
func (l *LightClientReader) IsHotShotLive(delayThreshold uint64) (bool, error) {
header, err := l.L1Client.HeaderByNumber(context.Background(), nil)
if err != nil {
return false, err
}
threshold := new(big.Int).SetUint64(delayThreshold)
return l.LightClient.LagOverEscapeHatchThreshold(&bind.CallOpts{}, header.Number, threshold)
}
9 changes: 9 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (

type TaggedBase64 = tagged_base64.TaggedBase64

type VidCommon = json.RawMessage

type VidCommonQueryData struct {
Height uint64 `json:"height"`
BlockHash *TaggedBase64 `json:"block_hash"`
PayloadHash *TaggedBase64 `json:"payload_hash"`
Common VidCommon `json:"common"`
}

type Header struct {
ChainConfig *ResolvableChainConfig `json:"chain_config"`
Height uint64 `json:"height"`
Expand Down

0 comments on commit 04e78ce

Please sign in to comment.