Skip to content

Commit

Permalink
Merge pull request #41 from EspressoSystems/update-block-merkle-fetch
Browse files Browse the repository at this point in the history
Update light client method to fetch block merkle snapshot
  • Loading branch information
nomaxg authored Jun 7, 2024
2 parents 4ba7659 + 7eddb09 commit 1d048d9
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 10 deletions.
2 changes: 1 addition & 1 deletion espresso-sequencer
20 changes: 12 additions & 8 deletions light-client/light_client_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

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

// A mock function for now
IsHotShotAvailable(maxDriftTime time.Duration) bool
Expand Down Expand Up @@ -50,17 +50,21 @@ func (l *LightClientReader) ValidatedHeight() (validatedHeight uint64, l1Height
return state.BlockHeight, header.Number.Uint64(), nil
}

// Fetch the merkle root at a given L1 checkpoint
func (l *LightClientReader) FetchMerkleRootAtL1Block(l1BlockHeight uint64) (types.BlockMerkleRoot, error) {
state, err := l.LightClient.GetFinalizedState(&bind.CallOpts{BlockNumber: new(big.Int).SetUint64(l1BlockHeight)})
// Fetch the merkle root at the first light client snapshot that proves the provided hotshot leaf height.
// CallOpt included as a parameter in case validators need to fetch historical merkle roots if they are catching up.
func (l *LightClientReader) FetchMerkleRoot(hotShotHeight uint64, opts *bind.CallOpts) (types.BlockMerkleSnapshot, error) {
snapshot, err := l.LightClient.GetHotShotCommitment(opts, new(big.Int).SetUint64(hotShotHeight))
if err != nil {
return types.Commitment{}, err
return types.BlockMerkleSnapshot{}, err
}
root, err := types.CommitmentFromUint256(types.NewU256().SetBigInt(state.BlockCommRoot))
root, err := types.CommitmentFromUint256(types.NewU256().SetBigInt(snapshot.BlockCommRoot))
if err != nil {
return types.Commitment{}, err
return types.BlockMerkleSnapshot{}, err
}
return root, nil
return types.BlockMerkleSnapshot{
Root: root,
Height: snapshot.BlockHeight,
}, nil
}

func (l *LightClientReader) IsHotShotAvailable(t time.Duration) bool {
Expand Down
208 changes: 207 additions & 1 deletion light-client/lightclient.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ type NsTable struct {

type NamespaceProof = json.RawMessage

type BlockMerkleSnapshot struct {
Root BlockMerkleRoot
Height uint64
}

type BlockMerkleRoot = Commitment

type HotShotBlockMerkleProof struct {
Expand Down

0 comments on commit 1d048d9

Please sign in to comment.