From 429d114830f738da103019d8dbfdba90d2dd4fe6 Mon Sep 17 00:00:00 2001 From: Ian Shim Date: Mon, 4 Dec 2023 16:27:33 -0800 Subject: [PATCH] don't memoize nosend txn opts --- common/ethclient.go | 2 +- common/geth/client.go | 47 ++++++++++---------- common/mock/ethclient.go | 4 +- core/eth/tx.go | 43 +++++++++++++++--- inabox/tests/integration_test.go | 9 ++-- test/synthetic-test/synthetic_client_test.go | 4 +- 6 files changed, 70 insertions(+), 39 deletions(-) diff --git a/common/ethclient.go b/common/ethclient.go index c5b0c95ea..1b74259e4 100644 --- a/common/ethclient.go +++ b/common/ethclient.go @@ -12,7 +12,7 @@ import ( type EthClient interface { GetAccountAddress() common.Address - GetNoSendTransactOpts() *bind.TransactOpts + GetNoSendTransactOpts() (*bind.TransactOpts, error) ChainID(ctx context.Context) (*big.Int, error) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) diff --git a/common/geth/client.go b/common/geth/client.go index ccc7b2299..ac12241fe 100644 --- a/common/geth/client.go +++ b/common/geth/client.go @@ -25,12 +25,12 @@ var ( type EthClient struct { *ethclient.Client - RPCURL string - privateKey *ecdsa.PrivateKey - AccountAddress gethcommon.Address - NoSendTransactOpts *bind.TransactOpts - Contracts map[gethcommon.Address]*bind.BoundContract - Logger common.Logger + RPCURL string + privateKey *ecdsa.PrivateKey + chainID *big.Int + AccountAddress gethcommon.Address + Contracts map[gethcommon.Address]*bind.BoundContract + Logger common.Logger } var _ common.EthClient = (*EthClient)(nil) @@ -42,7 +42,6 @@ func NewClient(config EthClientConfig, logger common.Logger) (*EthClient, error) } var accountAddress gethcommon.Address var privateKey *ecdsa.PrivateKey - var opts *bind.TransactOpts if len(config.PrivateKeyString) != 0 { privateKey, err = crypto.HexToECDSA(config.PrivateKeyString) @@ -57,31 +56,23 @@ func NewClient(config EthClientConfig, logger common.Logger) (*EthClient, error) return nil, ErrCannotGetECDSAPubKey } accountAddress = crypto.PubkeyToAddress(*publicKeyECDSA) + } - chainIDBigInt, err := chainClient.ChainID(context.Background()) - if err != nil { - return nil, fmt.Errorf("NewClient: cannot get chainId: %w", err) - } - - // generate and memoize NoSendTransactOpts - opts, err = bind.NewKeyedTransactorWithChainID(privateKey, chainIDBigInt) - if err != nil { - return nil, fmt.Errorf("NewClient: cannot create NoSendTransactOpts: %w", err) - } - opts.NoSend = true + chainIDBigInt, err := chainClient.ChainID(context.Background()) + if err != nil { + return nil, fmt.Errorf("NewClient: cannot get chainId: %w", err) } c := &EthClient{ RPCURL: config.RPCURL, privateKey: privateKey, + chainID: chainIDBigInt, AccountAddress: accountAddress, Client: chainClient, Contracts: make(map[gethcommon.Address]*bind.BoundContract), Logger: logger, } - c.NoSendTransactOpts = opts - return c, err } @@ -94,8 +85,14 @@ func (c *EthClient) GetAccountAddress() gethcommon.Address { return c.AccountAddress } -func (c *EthClient) GetNoSendTransactOpts() *bind.TransactOpts { - return c.NoSendTransactOpts +func (c *EthClient) GetNoSendTransactOpts() (*bind.TransactOpts, error) { + opts, err := bind.NewKeyedTransactorWithChainID(c.privateKey, c.chainID) + if err != nil { + return nil, fmt.Errorf("NewClient: cannot create NoSendTransactOpts: %w", err) + } + opts.NoSend = true + + return opts, nil } // UpdateGas returns an otherwise identical txn to the one provided but with updated @@ -153,8 +150,10 @@ func (c *EthClient) UpdateGas( return nil, err } - opts := new(bind.TransactOpts) - *opts = *c.NoSendTransactOpts + opts, err := c.GetNoSendTransactOpts() + if err != nil { + return nil, err + } opts.Context = ctx opts.Nonce = new(big.Int).SetUint64(tx.Nonce()) opts.GasTipCap = gasTipCap diff --git a/common/mock/ethclient.go b/common/mock/ethclient.go index f4d81e6b5..3d9495047 100644 --- a/common/mock/ethclient.go +++ b/common/mock/ethclient.go @@ -31,10 +31,10 @@ func (mock *MockEthClient) GetAccountAddress() common.Address { return result.(common.Address) } -func (mock *MockEthClient) GetNoSendTransactOpts() *bind.TransactOpts { +func (mock *MockEthClient) GetNoSendTransactOpts() (*bind.TransactOpts, error) { args := mock.Called() result := args.Get(0) - return result.(*bind.TransactOpts) + return result.(*bind.TransactOpts), args.Error(1) } func (mock *MockEthClient) ChainID(ctx context.Context) (*big.Int, error) { diff --git a/core/eth/tx.go b/core/eth/tx.go index 39edb0562..e7559cb02 100644 --- a/core/eth/tx.go +++ b/core/eth/tx.go @@ -123,7 +123,12 @@ func (t *Transactor) RegisterBLSPublicKey(ctx context.Context, keypair *core.Key } // assemble tx - tx, err := t.Bindings.PubkeyCompendium.RegisterBLSPublicKey(t.EthClient.GetNoSendTransactOpts(), signedMessageHashParam, pubkeyG1Param, pubkeyG2Param) + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return err + } + tx, err := t.Bindings.PubkeyCompendium.RegisterBLSPublicKey(opts, signedMessageHashParam, pubkeyG1Param, pubkeyG2Param) if err != nil { t.Logger.Error("Error assembling RegisterBLSPublicKey tx") return err @@ -173,8 +178,12 @@ func (t *Transactor) RegisterOperator(ctx context.Context, pubkeyG1 *core.G1Poin Y: pubkey.Y, } quorumNumbers := quorumIDsToQuorumNumbers(quorumIds) - - tx, err := t.Bindings.BLSRegCoordWithIndices.RegisterOperatorWithCoordinator1(t.EthClient.GetNoSendTransactOpts(), quorumNumbers, g1Point, socket) + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return err + } + tx, err := t.Bindings.BLSRegCoordWithIndices.RegisterOperatorWithCoordinator1(opts, quorumNumbers, g1Point, socket) if err != nil { t.Logger.Error("Failed to register operator", "err", err) @@ -221,8 +230,13 @@ func (t *Transactor) RegisterOperatorWithChurn(ctx context.Context, pubkeyG1 *co Expiry: new(big.Int).SetInt64(churnReply.SignatureWithSaltAndExpiry.Expiry), } + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return err + } tx, err := t.Bindings.BLSRegCoordWithIndices.RegisterOperatorWithCoordinator( - t.EthClient.GetNoSendTransactOpts(), + opts, quorumNumbers, operatorToRegisterPubkey, socket, @@ -270,8 +284,13 @@ func (t *Transactor) DeregisterOperator(ctx context.Context, pubkeyG1 *core.G1Po Y: pubkey.Y, } + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return err + } tx, err := t.Bindings.BLSRegCoordWithIndices.DeregisterOperatorWithCoordinator( - t.EthClient.GetNoSendTransactOpts(), + opts, quorumNumbers, g1Point, ) @@ -290,7 +309,12 @@ func (t *Transactor) DeregisterOperator(ctx context.Context, pubkeyG1 *core.G1Po // UpdateOperatorSocket updates the socket of the operator in all the quorums that it is func (t *Transactor) UpdateOperatorSocket(ctx context.Context, socket string) error { - tx, err := t.Bindings.BLSRegCoordWithIndices.UpdateSocket(t.EthClient.GetNoSendTransactOpts(), socket) + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return err + } + tx, err := t.Bindings.BLSRegCoordWithIndices.UpdateSocket(opts, socket) if err != nil { t.Logger.Error("Failed to update operator socket", "err", err) return err @@ -441,7 +465,12 @@ func (t *Transactor) ConfirmBatch(ctx context.Context, batchHeader core.BatchHea NonSignerStakeIndices: checkSignaturesIndices.NonSignerStakeIndices, } - tx, err := t.Bindings.EigenDAServiceManager.ConfirmBatch(t.EthClient.GetNoSendTransactOpts(), batchH, signatureChecker) + opts, err := t.EthClient.GetNoSendTransactOpts() + if err != nil { + t.Logger.Error("Failed to generate transact opts", "err", err) + return nil, err + } + tx, err := t.Bindings.EigenDAServiceManager.ConfirmBatch(opts, batchH, signatureChecker) if err != nil { t.Logger.Error("Failed to confirm batch", "err", err) return nil, err diff --git a/inabox/tests/integration_test.go b/inabox/tests/integration_test.go index dc9cc77e1..a07d1fa09 100644 --- a/inabox/tests/integration_test.go +++ b/inabox/tests/integration_test.go @@ -12,7 +12,6 @@ import ( "github.com/Layr-Labs/eigenda/core" "github.com/Layr-Labs/eigenda/disperser" "github.com/Layr-Labs/eigenda/tools/traffic" - "github.com/ethereum/go-ethereum/accounts/abi/bind" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -23,8 +22,8 @@ var _ = Describe("Inabox Integration", func() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() - optsWithValue := new(bind.TransactOpts) - *optsWithValue = *ethClient.GetNoSendTransactOpts() + optsWithValue, err := ethClient.GetNoSendTransactOpts() + Expect(err).To(BeNil()) optsWithValue.Value = big.NewInt(1e18) tx, err := mockRollup.RegisterValidator(optsWithValue) Expect(err).To(BeNil()) @@ -76,7 +75,9 @@ var _ = Describe("Inabox Integration", func() { if *blobStatus == disperser.Confirmed { blobHeader := blobHeaderFromProto(reply.GetInfo().GetBlobHeader()) verificationProof := blobVerificationProofFromProto(reply.GetInfo().GetBlobVerificationProof()) - tx, err := mockRollup.PostCommitment(ethClient.GetNoSendTransactOpts(), blobHeader, verificationProof) + opts, err := ethClient.GetNoSendTransactOpts() + Expect(err).To(BeNil()) + tx, err := mockRollup.PostCommitment(opts, blobHeader, verificationProof) Expect(err).To(BeNil()) _, err = ethClient.EstimateGasPriceAndLimitAndSendTx(ctx, tx, "PostCommitment", nil) Expect(err).To(BeNil()) diff --git a/test/synthetic-test/synthetic_client_test.go b/test/synthetic-test/synthetic_client_test.go index 7dfcc0c71..b18618f85 100644 --- a/test/synthetic-test/synthetic_client_test.go +++ b/test/synthetic-test/synthetic_client_test.go @@ -328,7 +328,9 @@ loop: ethClientCtx, cancel := context.WithTimeout(context.Background(), time.Second*60) defer cancel() - tx, err := mockRollup.PostCommitment(ethClient.GetNoSendTransactOpts(), blobHeader, verificationProof) + opts, err := ethClient.GetNoSendTransactOpts() + assert.Nil(t, err) + tx, err := mockRollup.PostCommitment(opts, blobHeader, verificationProof) assert.Nil(t, err) assert.NotNil(t, tx) logger.Printf("PostCommitment Tx %v", tx)