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

Commit

Permalink
feat: improve some code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Jun 15, 2023
1 parent 7f9fc17 commit ed5e749
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
41 changes: 26 additions & 15 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
)

const (
protocolStatusReportInterval = 30 * time.Second
protocolStatusReportInterval = 30 * time.Second
exchangeTransitionConfigTimeout = 30 * time.Second
exchangeTransitionConfigInterval = 1 * time.Minute
)

// Driver keeps the L2 execution engine's local block chain in sync with the TaikoL1
Expand Down Expand Up @@ -108,7 +110,7 @@ func (d *Driver) Start() error {
d.wg.Add(3)
go d.eventLoop()
go d.reportProtocolStatus()
go d.checkTransitionConfig()
go d.exchangeTransitionConfigLoop()

return nil
}
Expand Down Expand Up @@ -225,8 +227,9 @@ func (d *Driver) reportProtocolStatus() {
}
}

func (d *Driver) checkTransitionConfig() {
exchangeTransitionConfigInterval := 60 * time.Second
// exchangeTransitionConfigLoop keeps exchanging transition configs with the
// L2 execution engine.
func (d *Driver) exchangeTransitionConfigLoop() {
ticker := time.NewTicker(exchangeTransitionConfigInterval)
defer func() {
ticker.Stop()
Expand All @@ -238,17 +241,25 @@ func (d *Driver) checkTransitionConfig() {
case <-d.ctx.Done():
return
case <-ticker.C:
tc, err := d.rpc.L2Engine.ExchangeTransitionConfiguration(d.ctx, &engine.TransitionConfigurationV1{
TerminalTotalDifficulty: (*hexutil.Big)(common.Big0),
TerminalBlockHash: common.Hash{},
TerminalBlockNumber: 0,
})
if err != nil {
log.Error("Failed to exchange Transition Configuration", "error", err)
continue
}
log.Info("Exchanged transition config",
"transitionconfig", tc)
func() {
ctx, cancel := context.WithTimeout(d.ctx, exchangeTransitionConfigTimeout)
defer cancel()

tc, err := d.rpc.L2Engine.ExchangeTransitionConfiguration(ctx, &engine.TransitionConfigurationV1{
TerminalTotalDifficulty: (*hexutil.Big)(common.Big0),
TerminalBlockHash: common.Hash{},
TerminalBlockNumber: 0,
})
if err != nil {
log.Error("Failed to exchange Transition Configuration", "error", err)
return
}

log.Debug(
"Exchanged transition config",
"transitionConfig", tc,
)
}()
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,6 @@ func (s *DriverTestSuite) TestDoSyncNoNewL2Blocks() {
s.Nil(s.d.doSync())
}

func (s *DriverTestSuite) TestCheckTransitionConfig() {
go func() {
<-time.After(120 * time.Second)
s.cancel()
s.d.Close()
}()
s.d.wg.Add(1)
s.d.checkTransitionConfig()
}

func (s *DriverTestSuite) TestStartClose() {
s.Nil(s.d.Start())
s.cancel()
Expand Down
6 changes: 2 additions & 4 deletions pkg/rpc/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ func (c *EngineClient) GetPayload(
return result.ExecutionPayload, nil
}

// ExchangeTransitionConfiguration exchanges transition configs with the L2 execution engine.
func (c *EngineClient) ExchangeTransitionConfiguration(
ctx context.Context,
cfg *engine.TransitionConfigurationV1,
) (*engine.TransitionConfigurationV1, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

var result *engine.TransitionConfigurationV1
if err := c.Client.CallContext(timeoutCtx, &result, "engine_exchangeTransitionConfigurationV1", cfg); err != nil {
if err := c.Client.CallContext(ctx, &result, "engine_exchangeTransitionConfigurationV1", cfg); err != nil {
return nil, err
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/rpc/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require"
)

Expand All @@ -29,4 +31,11 @@ func TestL2EngineBorbidden(t *testing.T) {
&engine.PayloadID{},
)
require.ErrorContains(t, err, "Unauthorized")

_, err = c.L2Engine.ExchangeTransitionConfiguration(context.Background(), &engine.TransitionConfigurationV1{
TerminalTotalDifficulty: (*hexutil.Big)(common.Big0),
TerminalBlockHash: common.Hash{},
TerminalBlockNumber: 0,
})
require.ErrorContains(t, err, "Unauthorized")
}

0 comments on commit ed5e749

Please sign in to comment.