Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(driver): improve ResetL1Current method based on the latest protocol changes #445

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b6abb0d5e06860fb731b4de0d812c82c6985f1f7
7090d5f5c7e4f290a87b42e83d73accdbfe176c7
28 changes: 27 additions & 1 deletion bindings/encoding/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ var (
Name: "l1Height",
Type: "uint64",
},
{
Name: "txListByteOffset",
Type: "uint24",
},
{
Name: "txListByteSize",
Type: "uint24",
},
{
Name: "minTier",
Type: "uint16",
Expand Down Expand Up @@ -100,6 +108,22 @@ var (
Name: "extraData",
Type: "bytes32",
},
{
Name: "blobHash",
Type: "bytes32",
},
{
Name: "txListByteOffset",
Type: "uint24",
},
{
Name: "txListByteSize",
Type: "uint24",
},
{
Name: "cacheBlobForReuse",
Type: "bool",
},
}
proverAssignmentComponents = []abi.ArgumentMarshaling{
{
Expand Down Expand Up @@ -160,6 +184,7 @@ var (
)
proverAssignmentPayloadArgs = abi.Arguments{
{Name: "PROVER_ASSIGNMENT", Type: stringType},
{Name: "taikoAddress", Type: addressType},
{Name: "blobHash", Type: bytes32Type},
{Name: "assignment.feeToken", Type: addressType},
{Name: "assignment.expiry", Type: uint64Type},
Expand Down Expand Up @@ -204,12 +229,13 @@ func EncodeBlockParams(params *BlockParams) ([]byte, error) {

// EncodeProverAssignmentPayload performs the solidity `abi.encode` for the given proverAssignment payload.
func EncodeProverAssignmentPayload(
taikoAddress common.Address,
txListHash common.Hash,
feeToken common.Address,
expiry uint64,
tierFees []TierFee,
) ([]byte, error) {
b, err := proverAssignmentPayloadArgs.Pack("PROVER_ASSIGNMENT", txListHash, feeToken, expiry, tierFees)
b, err := proverAssignmentPayloadArgs.Pack("PROVER_ASSIGNMENT", taikoAddress, txListHash, feeToken, expiry, tierFees)
if err != nil {
return nil, fmt.Errorf("failed to abi.encode prover assignment hash payload, %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions bindings/encoding/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func TestEncodeProverAssignmentPayload(t *testing.T) {
encoded, err := EncodeProverAssignmentPayload(
common.BytesToAddress(randomBytes(20)),
common.BytesToHash(randomBytes(32)),
common.BytesToAddress(randomBytes(20)),
120,
Expand Down
8 changes: 6 additions & 2 deletions bindings/encoding/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ type BlockHeader struct {

// BlockParams should be same with TaikoData.BlockParams.
type BlockParams struct {
Assignment *ProverAssignment
ExtraData [32]byte
Assignment *ProverAssignment
ExtraData [32]byte
BlobHash [32]byte
TxListByteOffset *big.Int
TxListByteSize *big.Int
CacheBlobForReuse bool
}

// TierFee should be same with TaikoData.TierFee.
Expand Down
14 changes: 7 additions & 7 deletions bindings/gen_guardian_prover.go

Large diffs are not rendered by default.

901 changes: 405 additions & 496 deletions bindings/gen_taiko_l1.go

Large diffs are not rendered by default.

86 changes: 49 additions & 37 deletions bindings/gen_taiko_l2.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error {
)

// Reset the L1Current cursor.
if _, err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil {
if err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ func (s *DriverTestSuite) TestL1Current() {
// propose and insert a block
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
// reset L1 current with increased height
_, err := s.d.state.ResetL1Current(s.d.ctx, common.Big1)
s.Nil(err)
s.Nil(s.d.state.ResetL1Current(s.d.ctx, common.Big1))
}

func TestDriverTestSuite(t *testing.T) {
Expand Down
51 changes: 11 additions & 40 deletions driver/state/l1_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/taikoxyz/taiko-client/bindings"
eventIterator "github.com/taikoxyz/taiko-client/pkg/chain_iterator/event_iterator"
)

// GetL1Current reads the L1 current cursor concurrent safely.
Expand All @@ -32,62 +31,34 @@ func (s *State) SetL1Current(h *types.Header) {
func (s *State) ResetL1Current(
ctx context.Context,
blockID *big.Int,
) (*bindings.TaikoL1ClientBlockProposed, error) {
) error {
if blockID == nil {
return nil, fmt.Errorf("empty block ID")
return fmt.Errorf("empty block ID")
}

log.Info("Reset L1 current cursor", "blockID", blockID)

if blockID.Cmp(common.Big0) == 0 {
l1Current, err := s.rpc.L1.HeaderByNumber(ctx, s.GenesisL1Height)
if err != nil {
return nil, err
return err
}
s.SetL1Current(l1Current)
return nil, nil
return nil
}

var event *bindings.TaikoL1ClientBlockProposed
iter, err := eventIterator.NewBlockProposedIterator(
ctx,
&eventIterator.BlockProposedIteratorConfig{
Client: s.rpc.L1,
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.GenesisL1Height,
EndHeight: s.GetL1Head().Number,
FilterQuery: []*big.Int{blockID},
Reverse: true,
OnBlockProposedEvent: func(
ctx context.Context,
e *bindings.TaikoL1ClientBlockProposed,
end eventIterator.EndBlockProposedEventIterFunc,
) error {
event = e
end()
return nil
},
},
)
blockInfo, err := s.rpc.TaikoL1.GetBlock(&bind.CallOpts{Context: ctx}, blockID.Uint64())
if err != nil {
return nil, err
return err
}

if err := iter.Iter(); err != nil {
return nil, err
}

if event == nil {
return nil, fmt.Errorf("BlockProposed event not found, blockID: %s", blockID)
}

l1Current, err := s.rpc.L1.HeaderByNumber(ctx, new(big.Int).SetUint64(event.Raw.BlockNumber))
l1Current, err := s.rpc.L1.HeaderByNumber(ctx, new(big.Int).SetUint64(blockInfo.ProposedIn))
if err != nil {
return nil, err
return err
}
s.SetL1Current(l1Current)

log.Info("Reset L1 current cursor", "height", s.GetL1Current().Number)
log.Info("Reset L1 current cursor", "height", s.GetL1Current().Number, "hash", s.GetL1Current().Hash())

return event, nil
return nil
}
16 changes: 4 additions & 12 deletions driver/state/l1_current_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,19 @@ func (s *DriverStateTestSuite) TestSetL1Current() {
s.Equal(h.Hash(), s.s.GetL1Current().Hash())

// should warn, but not panic
s.NotPanics(func() {
s.s.SetL1Current(nil)
})
s.NotPanics(func() { s.s.SetL1Current(nil) })
}

func (s *DriverStateTestSuite) TestResetL1CurrentEmptyHeight() {
_, err := s.s.ResetL1Current(context.Background(), common.Big0)
s.Nil(err)

_, err = s.s.ResetL1Current(context.Background(), common.Big0)
s.Nil(err)
s.Nil(s.s.ResetL1Current(context.Background(), common.Big0))
}

func (s *DriverStateTestSuite) TestResetL1CurrentEmptyID() {
_, err := s.s.ResetL1Current(context.Background(), common.Big1)
s.ErrorContains(err, "not found")
s.ErrorContains(s.s.ResetL1Current(context.Background(), common.Big1), "execution reverted")
}

func (s *DriverStateTestSuite) TestResetL1CurrentCtxErr() {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := s.s.ResetL1Current(ctx, common.Big0)
s.ErrorContains(err, "context canceled")
s.ErrorContains(s.s.ResetL1Current(ctx, common.Big0), "context canceled")
}
8 changes: 6 additions & 2 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,12 @@ func (p *Proposer) sendProposeBlockTx(
}

encodedParams, err := encoding.EncodeBlockParams(&encoding.BlockParams{
Assignment: assignment,
ExtraData: rpc.StringToBytes32(p.cfg.ExtraData),
Assignment: assignment,
ExtraData: rpc.StringToBytes32(p.cfg.ExtraData),
TxListByteOffset: common.Big0,
TxListByteSize: common.Big0,
BlobHash: [32]byte{},
CacheBlobForReuse: false,
})
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion proposer/prover_selector/eth_fee_eoa_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (s *ETHFeeEOASelector) AssignProver(
endpoint,
expiry,
tierFees,
s.taikoL1Address,
txListHash,
s.requestTimeout,
guardianProverAddress,
Expand Down Expand Up @@ -169,6 +170,7 @@ func assignProver(
endpoint *url.URL,
expiry uint64,
tierFees []encoding.TierFee,
taikoL1Address common.Address,
txListHash common.Hash,
timeout time.Duration,
guardianProverAddress common.Address,
Expand Down Expand Up @@ -215,7 +217,7 @@ func assignProver(

// Ensure prover in response is the same as the one recovered
// from the signature
payload, err := encoding.EncodeProverAssignmentPayload(txListHash, common.Address{}, expiry, tierFees)
payload, err := encoding.EncodeProverAssignmentPayload(taikoL1Address, txListHash, common.Address{}, expiry, tierFees)
if err != nil {
return nil, common.Address{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,12 +685,12 @@ func (p *Prover) onTransitionContested(ctx context.Context, e *bindings.TaikoL1C
return nil
}

l1Height, err := p.rpc.TaikoL2.LatestSyncedL1Height(&bind.CallOpts{Context: ctx, BlockNumber: e.BlockId})
blockInfo, err := p.rpc.TaikoL1.GetBlock(&bind.CallOpts{Context: ctx}, e.BlockId.Uint64())
if err != nil {
return err
}

return p.requestProofByBlockID(e.BlockId, new(big.Int).SetUint64(l1Height+1), e.Tier+1, nil)
return p.requestProofByBlockID(e.BlockId, new(big.Int).SetUint64(blockInfo.ProposedIn), e.Tier+1, nil)
}

// onBlockVerified update the latestVerified block in current state, and cancels
Expand Down Expand Up @@ -747,22 +747,22 @@ func (p *Prover) onTransitionProved(ctx context.Context, event *bindings.TaikoL1
return nil
}

l1Height, err := p.rpc.TaikoL2.LatestSyncedL1Height(&bind.CallOpts{Context: ctx, BlockNumber: event.BlockId})
blockInfo, err := p.rpc.TaikoL1.GetBlock(&bind.CallOpts{Context: ctx}, event.BlockId.Uint64())
if err != nil {
return err
}

log.Info(
"Contest a proven transition",
"blockID", event.BlockId,
"l1Height", l1Height,
"l1Height", blockInfo.ProposedIn,
"tier", event.Tier,
"parentHash", common.Bytes2Hex(event.Tran.ParentHash[:]),
"blockHash", common.Bytes2Hex(event.Tran.BlockHash[:]),
"signalRoot", common.Bytes2Hex(event.Tran.SignalRoot[:]),
)

return p.requestProofByBlockID(event.BlockId, new(big.Int).SetUint64(l1Height+1), event.Tier, event)
return p.requestProofByBlockID(event.BlockId, new(big.Int).SetUint64(blockInfo.ProposedIn), event.Tier, event)
}

// Name returns the application name.
Expand Down
17 changes: 11 additions & 6 deletions prover/prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,19 @@ func (s *ProverTestSuite) TestContestWrongBlocks() {
s.Equal(header.Hash(), common.BytesToHash(contestedEvent.Tran.BlockHash[:]))
s.Equal(header.ParentHash, common.BytesToHash(contestedEvent.Tran.ParentHash[:]))

approvedSink := make(chan *bindings.GuardianProverApproved)
approvedSub, err := s.p.rpc.GuardianProver.WatchApproved(nil, approvedSink, []uint64{})
s.Nil(err)
defer func() {
approvedSub.Unsubscribe()
close(approvedSink)
}()

s.Nil(s.p.onTransitionContested(context.Background(), contestedEvent))
s.Nil(s.p.selectSubmitter(contestedEvent.Tier+1).SubmitProof(context.Background(), <-s.p.proofGenerationCh))
provenEvent := <-sink
s.Nil(s.p.selectSubmitter(encoding.TierGuardianID).SubmitProof(context.Background(), <-s.p.proofGenerationCh))
approvedEvent := <-approvedSink

s.Equal(header.Number.Uint64(), provenEvent.BlockId.Uint64())
s.Equal(header.Hash(), common.BytesToHash(provenEvent.Tran.BlockHash[:]))
s.Equal(header.ParentHash, common.BytesToHash(provenEvent.Tran.ParentHash[:]))
s.Greater(provenEvent.Tier, contestedEvent.Tier)
s.Equal(header.Number.Uint64(), approvedEvent.BlockId)
}

func (s *ProverTestSuite) TestProveExpiredUnassignedBlock() {
Expand Down
8 changes: 7 additions & 1 deletion prover/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ func (srv *ProverServer) CreateAssignment(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnprocessableEntity, "prover does not have capacity")
}

encoded, err := encoding.EncodeProverAssignmentPayload(req.TxListHash, req.FeeToken, req.Expiry, req.TierFees)
encoded, err := encoding.EncodeProverAssignmentPayload(
srv.taikoL1Address,
req.TxListHash,
req.FeeToken,
req.Expiry,
req.TierFees,
)
if err != nil {
log.Error("Failed to encode proverAssignment payload data", "error", err)
return echo.NewHTTPError(http.StatusUnprocessableEntity, err)
Expand Down
2 changes: 1 addition & 1 deletion testutils/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *ClientTestSuite) SetupTest() {
tx, err := addressManager.SetAddress(
opts,
chainID.Uint64(),
rpc.StringToBytes32("tier_pse_zkevm"),
rpc.StringToBytes32("tier_sgx_and_pse_zkevm"),
common.Address{},
)
s.Nil(err)
Expand Down