Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't memoize eth client TransactOpts #105

Merged
merged 1 commit into from
Dec 13, 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 common/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 23 additions & 24 deletions common/geth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions common/mock/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 36 additions & 7 deletions core/eth/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions inabox/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion test/synthetic-test/synthetic_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading