-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(op-node/op-batcher/op-proposer): add fallbackClient (#55)
* FallbackClient impl * double check fail count * RegisterSubscribeFunc * FallbackClient for op-batcher,op-proposer * miss currentClient * add log and change order * fallback client add fallbackThreshold * add validateRpc * add document * add document * Put the switching logic into goroutine and modify the code according to the comments * add metrics and don't switch url when error is Rpc.Error * use const to remove magic number * fix NoopTxMetrics * add TestL1FallbackClient_SwitchUrl e2e case * should be >= threshold * change threshold to 20 * log->logT * miss make channel * fix lint --------- Co-authored-by: Welkin <[email protected]>
- Loading branch information
Showing
25 changed files
with
861 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package actions | ||
|
||
import ( | ||
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils" | ||
"github.com/ethereum-optimism/optimism/op-node/client" | ||
"github.com/ethereum-optimism/optimism/op-node/eth" | ||
"github.com/ethereum-optimism/optimism/op-node/sources" | ||
"github.com/ethereum-optimism/optimism/op-node/testlog" | ||
service_client "github.com/ethereum-optimism/optimism/op-service/client" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/stretchr/testify/require" | ||
"math/big" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func setupFallbackClientTest(t Testing, sd *e2eutils.SetupData, log log.Logger, l1Url string) (*L1Miner, *L1Replica, *L1Replica, *L2Engine, *L2Sequencer, *sources.FallbackClient) { | ||
jwtPath := e2eutils.WriteDefaultJWT(t) | ||
|
||
miner := NewL1MinerWithPort(t, log, sd.L1Cfg, 8545) | ||
l1_2 := NewL1ReplicaWithPort(t, log, sd.L1Cfg, 8546) | ||
l1_3 := NewL1ReplicaWithPort(t, log, sd.L1Cfg, 8547) | ||
isMultiUrl, urlList := service_client.MultiUrlParse(l1Url) | ||
require.True(t, isMultiUrl) | ||
opts := []client.RPCOption{ | ||
client.WithHttpPollInterval(0), | ||
client.WithDialBackoff(10), | ||
} | ||
rpc, err := client.NewRPC(t.Ctx(), log, urlList[0], opts...) | ||
require.NoError(t, err) | ||
fallbackClient := sources.NewFallbackClient(t.Ctx(), rpc, urlList, log, sd.RollupCfg.L1ChainID, sd.RollupCfg.Genesis.L1, func(url string) (client.RPC, error) { | ||
return client.NewRPC(t.Ctx(), log, url, opts...) | ||
}) | ||
l1F, err := sources.NewL1Client(fallbackClient, log, nil, sources.L1ClientDefaultConfig(sd.RollupCfg, false, sources.RPCKindBasic)) | ||
require.NoError(t, err) | ||
engine := NewL2Engine(t, log, sd.L2Cfg, sd.RollupCfg.Genesis.L1, jwtPath) | ||
l2Cl, err := sources.NewEngineClient(engine.RPCClient(), log, nil, sources.EngineClientDefaultConfig(sd.RollupCfg)) | ||
require.NoError(t, err) | ||
|
||
sequencer := NewL2Sequencer(t, log, l1F, l2Cl, sd.RollupCfg, 0) | ||
return miner, l1_2, l1_3, engine, sequencer, fallbackClient.(*sources.FallbackClient) | ||
} | ||
|
||
func TestL1FallbackClient_SwitchUrl(gt *testing.T) { | ||
t := NewDefaultTesting(gt) | ||
p := &e2eutils.TestParams{ | ||
MaxSequencerDrift: 300, | ||
SequencerWindowSize: 200, | ||
ChannelTimeout: 120, | ||
L1BlockTime: 12, | ||
} | ||
dp := e2eutils.MakeDeployParams(t, p) | ||
sd := e2eutils.Setup(t, dp, defaultAlloc) | ||
logT := testlog.Logger(t, log.LvlDebug) | ||
miner, l1_2, _, engine, sequencer, fallbackClient := setupFallbackClientTest(t, sd, logT, "http://127.0.0.1:8545,http://127.0.0.1:8546,http://127.0.0.1:8547") | ||
miner.ActL1SetFeeRecipient(common.Address{'A'}) | ||
|
||
sequencer.ActL2PipelineFull(t) | ||
|
||
signer := types.LatestSigner(sd.L2Cfg.Config) | ||
cl := engine.EthClient() | ||
aliceTx := func() { | ||
n, err := cl.PendingNonceAt(t.Ctx(), dp.Addresses.Alice) | ||
require.NoError(t, err) | ||
tx := types.MustSignNewTx(dp.Secrets.Alice, signer, &types.DynamicFeeTx{ | ||
ChainID: sd.L2Cfg.Config.ChainID, | ||
Nonce: n, | ||
GasTipCap: big.NewInt(2 * params.GWei), | ||
GasFeeCap: new(big.Int).Add(miner.l1Chain.CurrentBlock().BaseFee, big.NewInt(2*params.GWei)), | ||
Gas: params.TxGas, | ||
To: &dp.Addresses.Bob, | ||
Value: e2eutils.Ether(2), | ||
}) | ||
require.NoError(gt, cl.SendTransaction(t.Ctx(), tx)) | ||
} | ||
makeL2BlockWithAliceTx := func() { | ||
aliceTx() | ||
sequencer.ActL2StartBlock(t) | ||
engine.ActL2IncludeTx(dp.Addresses.Alice)(t) // include a test tx from alice | ||
sequencer.ActL2EndBlock(t) | ||
} | ||
|
||
errRpc := miner.RPCClient().CallContext(t.Ctx(), nil, "admin_stopHTTP") | ||
require.NoError(t, errRpc) | ||
|
||
l2BlockCount := 0 | ||
for i := 0; i < 6; i++ { | ||
miner.ActL1StartBlock(12)(t) | ||
miner.ActL1EndBlock(t) | ||
newBlock := miner.l1Chain.GetBlockByHash(miner.l1Chain.CurrentBlock().Hash()) | ||
_, err := l1_2.l1Chain.InsertChain([]*types.Block{newBlock}) | ||
require.NoError(t, err) | ||
|
||
sequencer.L2Verifier.l1State.HandleNewL1HeadBlock(eth.L1BlockRef{ | ||
Hash: newBlock.Hash(), | ||
Number: newBlock.NumberU64(), | ||
ParentHash: newBlock.ParentHash(), | ||
Time: newBlock.Time(), | ||
}) | ||
origin := miner.l1Chain.CurrentBlock() | ||
|
||
for sequencer.SyncStatus().UnsafeL2.Time+sd.RollupCfg.BlockTime < origin.Time { | ||
makeL2BlockWithAliceTx() | ||
//require.Equal(t, uint64(i), sequencer.SyncStatus().UnsafeL2.L1Origin.Number, "no L1 origin change before time matches") | ||
l2BlockCount++ | ||
if l2BlockCount == 23 { | ||
require.Equal(t, 1, fallbackClient.GetCurrentIndex(), "fallback client should switch url to second url") | ||
errRpc2 := miner.RPCClient().CallContext(t.Ctx(), nil, "admin_startHTTP", "127.0.0.1", 8545, "*", "eth,net,web3,debug,admin,txpool", "*") | ||
require.NoError(t, errRpc2) | ||
} | ||
if l2BlockCount == 34 { | ||
require.Equal(t, 0, fallbackClient.GetCurrentIndex(), "fallback client should recover url to first url") | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.