From 60f128b61fa48950b4b0afd6797626d4f03e070f Mon Sep 17 00:00:00 2001 From: David Date: Tue, 23 Jan 2024 11:23:27 +0800 Subject: [PATCH] fix(test): fix workflow errors (#525) --- internal/testutils/helper.go | 15 ----- internal/testutils/suite.go | 52 +++++++++------- prover/prover_test.go | 113 +++++++++++++++++++---------------- 3 files changed, 92 insertions(+), 88 deletions(-) diff --git a/internal/testutils/helper.go b/internal/testutils/helper.go index b5d7f9600..46834a331 100644 --- a/internal/testutils/helper.go +++ b/internal/testutils/helper.go @@ -12,7 +12,6 @@ import ( "time" "github.com/cenkalti/backoff/v4" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" @@ -156,20 +155,6 @@ func ProposeAndInsertValidBlock( return event } -func DepositEtherToL2(s *ClientTestSuite, depositerPrivKey *ecdsa.PrivateKey, recipient common.Address) { - config, err := s.RPCClient.TaikoL1.GetConfig(nil) - s.Nil(err) - - opts, err := bind.NewKeyedTransactorWithChainID(depositerPrivKey, s.RPCClient.L1ChainID) - s.Nil(err) - opts.Value = config.EthDepositMinAmount - - for i := 0; i < int(config.EthDepositMinCountPerBlock); i++ { - _, err = s.RPCClient.TaikoL1.DepositEtherToL2(opts, recipient) - s.Nil(err) - } -} - // NewTestProverServer starts a new prover server that has channel listeners to respond and react // to requests for capacity, which provers can call. func NewTestProverServer( diff --git a/internal/testutils/suite.go b/internal/testutils/suite.go index f51216307..630e2bd8d 100644 --- a/internal/testutils/suite.go +++ b/internal/testutils/suite.go @@ -96,35 +96,41 @@ func (s *ClientTestSuite) SetupTest() { _, err = rpc.WaitReceipt(context.Background(), rpcCli.L1, tx) s.Nil(err) - decimal, err := rpcCli.TaikoToken.Decimals(nil) - s.Nil(err) - // Increase allowance for AssignmentHook and TaikoL1 - opts, err = bind.NewKeyedTransactorWithChainID(l1ProverPrivKey, rpcCli.L1ChainID) - s.Nil(err) - - bigInt := new(big.Int).Exp(big.NewInt(1_000_000_000), new(big.Int).SetUint64(uint64(decimal)), nil) - _, err = rpcCli.TaikoToken.Approve( - opts, - common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")), - bigInt, - ) - s.Nil(err) - - _, err = rpcCli.TaikoToken.Approve( - opts, - common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")), - bigInt, - ) - s.Nil(err) - - _, err = rpc.WaitReceipt(context.Background(), rpcCli.L1, tx) - s.Nil(err) + s.setAllowance(l1ProverPrivKey) + s.setAllowance(ownerPrivKey) } s.Nil(rpcCli.L1.CallContext(context.Background(), &s.testnetL1SnapshotID, "evm_snapshot")) s.NotEmpty(s.testnetL1SnapshotID) } +func (s *ClientTestSuite) setAllowance(key *ecdsa.PrivateKey) { + decimal, err := s.RPCClient.TaikoToken.Decimals(nil) + s.Nil(err) + + bigInt := new(big.Int).Exp(big.NewInt(1_000_000_000), new(big.Int).SetUint64(uint64(decimal)), nil) + + opts, err := bind.NewKeyedTransactorWithChainID(key, s.RPCClient.L1ChainID) + s.Nil(err) + + _, err = s.RPCClient.TaikoToken.Approve( + opts, + common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")), + bigInt, + ) + s.Nil(err) + + tx, err := s.RPCClient.TaikoToken.Approve( + opts, + common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")), + bigInt, + ) + s.Nil(err) + + _, err = rpc.WaitReceipt(context.Background(), s.RPCClient.L1, tx) + s.Nil(err) +} + func (s *ClientTestSuite) setAddress(ownerPrivKey *ecdsa.PrivateKey, name [32]byte, address common.Address) { var ( salt = RandomHash() diff --git a/prover/prover_test.go b/prover/prover_test.go index d9e2f5031..0bffd81e5 100644 --- a/prover/prover_test.go +++ b/prover/prover_test.go @@ -2,6 +2,7 @@ package prover import ( "context" + "crypto/ecdsa" "math/big" "net/url" "os" @@ -42,57 +43,8 @@ func (s *ProverTestSuite) SetupTest() { l1ProverPrivKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_PROVER_PRIVATE_KEY"))) s.Nil(err) - proverServerURL := testutils.LocalRandomProverEndpoint() - port, err := strconv.Atoi(proverServerURL.Port()) - s.Nil(err) - - decimal, err := s.RPCClient.TaikoToken.Decimals(nil) - s.Nil(err) - - allowance := new(big.Int).Exp(big.NewInt(1_000_000_100), new(big.Int).SetUint64(uint64(decimal)), nil) - ctx, cancel := context.WithCancel(context.Background()) - p := new(Prover) - s.Nil(InitFromConfig(ctx, p, &Config{ - L1WsEndpoint: os.Getenv("L1_NODE_WS_ENDPOINT"), - L1HttpEndpoint: os.Getenv("L1_NODE_HTTP_ENDPOINT"), - L2WsEndpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"), - L2HttpEndpoint: os.Getenv("L2_EXECUTION_ENGINE_HTTP_ENDPOINT"), - TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")), - TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")), - TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")), - AssignmentHookAddress: common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")), - GuardianProverAddress: common.HexToAddress(os.Getenv("GUARDIAN_PROVER_CONTRACT_ADDRESS")), - L1ProverPrivKey: l1ProverPrivKey, - Dummy: true, - ProveUnassignedBlocks: true, - Capacity: 1024, - MinOptimisticTierFee: common.Big1, - MinSgxTierFee: common.Big1, - MinPseZkevmTierFee: common.Big1, - MinSgxAndPseZkevmTierFee: common.Big1, - HTTPServerPort: uint64(port), - WaitReceiptTimeout: 12 * time.Second, - DatabasePath: "", - Allowance: allowance, - RPCTimeout: 3 * time.Second, - BackOffMaxRetrys: 3, - })) - p.srv = testutils.NewTestProverServer( - &s.ClientTestSuite, - l1ProverPrivKey, - proverServerURL, - ) - - p.guardianProverSender = guardianproversender.New( - p.cfg.L1ProverPrivKey, - p.cfg.GuardianProverHealthCheckServerEndpoint, - memorydb.New(), - p.rpc, - p.proverAddress, - ) - - s.p = p + proverServerURL := s.initProver(ctx, l1ProverPrivKey) s.cancel = cancel // Init driver @@ -280,6 +232,12 @@ func (s *ProverTestSuite) TestContestWrongBlocks() { close(contestedSink) }() + contesterKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_CONTRACT_OWNER_PRIVATE_KEY"))) + s.Nil(err) + + s.NotNil(s.initProver(context.Background(), contesterKey)) + s.p.cfg.ContesterMode = true + s.Greater(header.Number.Uint64(), uint64(0)) s.Nil(s.p.onTransitionProved(context.Background(), event)) @@ -496,3 +454,58 @@ func (s *ProverTestSuite) TestSetApprovalAlreadySetHigher() { func TestProverTestSuite(t *testing.T) { suite.Run(t, new(ProverTestSuite)) } + +func (s *ProverTestSuite) initProver(ctx context.Context, key *ecdsa.PrivateKey) *url.URL { + proverServerURL := testutils.LocalRandomProverEndpoint() + port, err := strconv.Atoi(proverServerURL.Port()) + s.Nil(err) + + decimal, err := s.RPCClient.TaikoToken.Decimals(nil) + s.Nil(err) + + allowance := new(big.Int).Exp(big.NewInt(1_000_000_100), new(big.Int).SetUint64(uint64(decimal)), nil) + + p := new(Prover) + s.Nil(InitFromConfig(ctx, p, &Config{ + L1WsEndpoint: os.Getenv("L1_NODE_WS_ENDPOINT"), + L1HttpEndpoint: os.Getenv("L1_NODE_HTTP_ENDPOINT"), + L2WsEndpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"), + L2HttpEndpoint: os.Getenv("L2_EXECUTION_ENGINE_HTTP_ENDPOINT"), + TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")), + TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")), + TaikoTokenAddress: common.HexToAddress(os.Getenv("TAIKO_TOKEN_ADDRESS")), + AssignmentHookAddress: common.HexToAddress(os.Getenv("ASSIGNMENT_HOOK_ADDRESS")), + GuardianProverAddress: common.HexToAddress(os.Getenv("GUARDIAN_PROVER_CONTRACT_ADDRESS")), + L1ProverPrivKey: key, + Dummy: true, + ProveUnassignedBlocks: true, + Capacity: 1024, + MinOptimisticTierFee: common.Big1, + MinSgxTierFee: common.Big1, + MinPseZkevmTierFee: common.Big1, + MinSgxAndPseZkevmTierFee: common.Big1, + HTTPServerPort: uint64(port), + WaitReceiptTimeout: 12 * time.Second, + DatabasePath: "", + Allowance: allowance, + RPCTimeout: 3 * time.Second, + BackOffMaxRetrys: 3, + })) + p.srv = testutils.NewTestProverServer( + &s.ClientTestSuite, + key, + proverServerURL, + ) + + p.guardianProverSender = guardianproversender.New( + key, + p.cfg.GuardianProverHealthCheckServerEndpoint, + memorydb.New(), + p.rpc, + p.proverAddress, + ) + + s.p = p + + return proverServerURL +}