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

Add gas bumping txmgr #284

Merged
merged 29 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
53a3d68
Updated chainio and signer READMEs + some code comments
samlaf Jun 28, 2024
ba1b2fe
fix(geometric txmgr test): race condition to set congested blocks
samlaf Aug 6, 2024
a2e962d
fix compilation errors from prev rebase
samlaf Aug 6, 2024
99543df
make fmt
samlaf Aug 6, 2024
8bcd53d
fix(geometric txmgr): make ethBackend interface private
samlaf Aug 7, 2024
4818925
doc(geometri txmgr): add default GetTxReceiptTickerDuration param as…
samlaf Aug 7, 2024
6e4f47d
chore(geometric txmgr): remove no longer used GasPricePercentageMulti…
samlaf Aug 7, 2024
e811f35
doc(geometrix txmgr): comment ProcessTransaction -> processTransaction
samlaf Aug 7, 2024
7b66b65
chore(geometric txmgr): move code around to make it cleaner
samlaf Aug 7, 2024
bad9edb
chore(geometric txmgr): use utils.WrapError everywhere instead of fmt…
samlaf Aug 7, 2024
35833fa
doc(geometric txmgr): added comment saying that we (probably) can del…
samlaf Aug 7, 2024
4c94ecd
make fmt
samlaf Aug 7, 2024
38dfa87
make fmt
samlaf Aug 7, 2024
91d36d1
refactor(geometric txmgr): change gas/gastip params to use floats ins…
samlaf Aug 7, 2024
e929bf5
docs(geometric txmgr): add comments for the ensureAnyTxBroadcasted/Co…
samlaf Aug 7, 2024
5ff5718
fix(typo): READMD -> README
samlaf Aug 7, 2024
aed27a0
make fmt
samlaf Aug 7, 2024
072d670
refactor(geometric txmgr): simplify code by only calling ensureAnyTxB…
samlaf Aug 8, 2024
9752cca
fix(geometric txmgr): revert to old (correct) code for catching timeo…
samlaf Aug 8, 2024
0d62354
make fmt
samlaf Aug 8, 2024
6ae0800
refactor(ecdsa/utils): use strings.TrimPrefix to trim 0x prefix
samlaf Aug 8, 2024
0e98f3a
delete(metrics.go): duplicate file, no longer needed
samlaf Aug 8, 2024
09d3e23
refactor(geometric txmgr): rename fct with clearer name for self-docu…
samlaf Aug 8, 2024
0219820
doc(geometric txmgr): fix comment that was not true
samlaf Aug 9, 2024
e8fa6a0
doc(geometric txmgr): correct comment
samlaf Aug 9, 2024
3a12fcc
test(geometric txmgr): add test for sending tx with incorrect nonce
samlaf Aug 9, 2024
549b045
test(geometric txmgr): added parallel test that sends nonces in rever…
samlaf Aug 9, 2024
e6ab890
doc(geometric txmgr): add comment explaining that processTransaction …
samlaf Aug 9, 2024
63832fc
chore(geometric txmgr): uncomment code that was commented while debug…
samlaf Aug 9, 2024
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
34 changes: 22 additions & 12 deletions chainio/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
## ChainIO

This module is used to facilitate reading/writing/subscribing to [eigenlayer core](./clients/elcontracts/) contracts and [avs registry](./clients/avsregistry/) contracts.

To make it easier to understand the different structs in this package, and their hierarchical relationship, we describe each of them below:
- geth's ethClient
- eigensdk [ethClient](./clients/eth/client.go)
- wraps geth's ethClient and adds convenience methods
- [eigenlayerContractBindings](../contracts/bindings/)
### Interacting with a json-rpc node

We have a basic [ethClient](./clients/eth/client.go) which simply wraps geth's ethClient and adds some convenience methods. The Client interface is also implemented by [instrumentedClient](./clients/eth/instrumented_client.go) which adds metrics to the ethClient to conform to the node spec's [rpc metrics](https://docs.eigenlayer.xyz/eigenlayer/avs-guides/spec/metrics/metrics-prom-spec#rpc-metrics) requirements.


### Building Transactions

In order to facilitate reading/writing/subscribing to [eigenlayer core](./clients/elcontracts/) contracts and [avs registry](./clients/avsregistry/) contracts, we use geth's abigen created bindings for low-level interactions, as well as our own high-level clients with higher utility functions:
- [Eigenlayer Contract Bindings](./clients/elcontracts/bindings.go)
- generated by abigen
- low level bindings to eigenlayer core contracts, which wrap our ethClient
- [elContractsClient](./clients/eth/client.go)
- wraps eigenlayerContractBindings and hides a little bit of the underlying complexity, which is not needed in 99% of cases.
- abigen also doesn't create an interface for the bindings it generates, whereas elContractsClient has a well defined interface which we use to generate mocks to help with testing.
- [ELChainReader](./clients/elcontracts/reader.go) / [ELChainWriter](./clients/elcontracts/writer.go) / [ELChainSubscriber](./clients/avsregistry/subscriber.go)
- wraps elContractsClient and adds convenience methods
- hides even more complexity than elContractsClient
- wraps bindings and adds convenience methods
- These structs should be the only ones used by AVS developers, apart from interacting with an ethClient directly to make direct json rpc calls such as waiting for a transaction receipt.

A similar hierarchy applies for the avs registry contracts.
There's a similar setup for the [avs registry](./clients/avsregistry/) contracts.

### Signing, Sending, and Managing Transactions

After building transactions, we need to sign them, send them to the network, and manage the nonce and gas price to ensure they are mined. This functionality is provided by:
- [txmgr](./txmgr/README.md)
- uses a wallet to sign and submit transactions, but then manages them by resubmitting with higher gas prices until they are mined.
- [wallet](./clients/wallet)
- uses a signerv2 to sign transactions, sends them to the network and can query for their receipts
- wallet abstraction is needed because "wallets", such as fireblocks, both sign and send transactions to the network (they don't simply return signed bytes so that we can send them ourselves)
- [signerv2](../signerv2/README.md)
- signs transactions
3 changes: 3 additions & 0 deletions chainio/clients/wallet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Wallet

TODO
43 changes: 13 additions & 30 deletions chainio/clients/wallet/privatekey_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ package wallet
import (
"context"
"fmt"
"math/big"

"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

var _ Wallet = (*privateKeyWallet)(nil)
type EthBackend interface {
shrimalmadhur marked this conversation as resolved.
Show resolved Hide resolved
SendTransaction(ctx context.Context, tx *types.Transaction) error
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
}

type privateKeyWallet struct {
ethClient ethClient
ethClient EthBackend
address common.Address
signerFn signerv2.SignerFn
logger logging.Logger

// cache
contracts map[common.Address]*bind.BoundContract
}

var _ Wallet = (*privateKeyWallet)(nil)

func NewPrivateKeyWallet(
ethClient ethClient,
ethClient EthBackend,
signer signerv2.SignerFn,
signerAddress common.Address,
logger logging.Logger,
Expand All @@ -37,7 +36,6 @@ func NewPrivateKeyWallet(
address: signerAddress,
signerFn: signer,
logger: logger,
contracts: make(map[common.Address]*bind.BoundContract, 0),
}, nil
}

Expand All @@ -50,32 +48,17 @@ func (t *privateKeyWallet) SendTransaction(ctx context.Context, tx *types.Transa
}

t.logger.Debug("Sending transaction")
opts := &bind.TransactOpts{
From: t.address,
Nonce: new(big.Int).SetUint64(tx.Nonce()),
Signer: signer,
Value: tx.Value(),
GasFeeCap: tx.GasFeeCap(),
GasTipCap: tx.GasTipCap(),
GasLimit: tx.Gas(),
Context: ctx,
}

contract := t.contracts[*tx.To()]
// if the contract has not been cached
if contract == nil {
// create a dummy bound contract tied to the `to` address of the transaction
contract = bind.NewBoundContract(*tx.To(), abi.ABI{}, t.ethClient, t.ethClient, t.ethClient)
// cache the contract for later use
t.contracts[*tx.To()] = contract
signedTx, err := signer(t.address, tx)
if err != nil {
return "", utils.WrapError(fmt.Errorf("sign: tx %v failed.", tx.Hash().String()), err)
}

sendingTx, err := contract.RawTransact(opts, tx.Data())
err = t.ethClient.SendTransaction(ctx, signedTx)
if err != nil {
return "", utils.WrapError(fmt.Errorf("send: tx %v failed.", tx.Hash().String()), err)
}

return sendingTx.Hash().Hex(), nil
return signedTx.Hash().Hex(), nil
}

func (t *privateKeyWallet) GetTransactionReceipt(ctx context.Context, txID TxID) (*types.Receipt, error) {
Expand Down
70 changes: 70 additions & 0 deletions chainio/clients/wallet/privatekey_wallet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package wallet

import (
"context"
"math/big"
"testing"
"time"

"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/Layr-Labs/eigensdk-go/testutils"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

var (
chainId = big.NewInt(31337)
)

func TestPrivateKeyWallet(t *testing.T) {
logger := testutils.NewTestLogger()

t.Run("SendTransaction + GetTransactionReceipt", func(t *testing.T) {
anvilC, err := testutils.StartAnvilContainer("")
require.NoError(t, err)
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
anvilHttpEndpoint, err := anvilC.Endpoint(ctxWithTimeout, "http")
require.NoError(t, err)
ethClient, err := ethclient.Dial(anvilHttpEndpoint)
require.NoError(t, err)

ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ecdsaPrivKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex)
require.NoError(t, err)
signerV2, signerAddr, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: ecdsaPrivKey}, chainId)
if err != nil {
panic(err)
}

skWallet, err := NewPrivateKeyWallet(ethClient, signerV2, signerAddr, logger)
require.NoError(t, err)

tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainId,
Nonce: 0,
GasTipCap: big.NewInt(1),
GasFeeCap: big.NewInt(1_000_000_000),
Gas: 21000,
To: &signerAddr,
Value: big.NewInt(1),
})
ctxWithTimeout, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
txId, err := skWallet.SendTransaction(ctxWithTimeout, tx)
require.NoError(t, err)

// need to give some time for anvil to process the tx and mine the block
// TODO: shall we expose a public WaitForTxReceipt function in the wallet interface, or somewhere else?
samlaf marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(3 * time.Second)

ctxWithTimeout, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
receipt, err := skWallet.GetTransactionReceipt(ctxWithTimeout, txId)
require.NoError(t, err)
// make sure the txHash in the mined tx receipt matches the once we sent
require.Equal(t, txId, receipt.TxHash.String())
})
}
3 changes: 2 additions & 1 deletion chainio/clients/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

type TxID = string

// Wallet is an interface for signing and sending transactions to the network
// Wallet is an interface for signing and sending transactions to the txpool.
// For a higher-level interface that includes nonce management and gas bumping, use the TxManager interface.
// This interface is used to abstract the process of sending transactions to the Ethereum network
// For example, for an MPC signer, the transaction would be broadcasted via an external API endpoint
// and the status is tracked via another external endpoint instead of being broadcasted
Expand Down
16 changes: 12 additions & 4 deletions chainio/txmgr/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
## Transaction Manager

Transaction Manager is responsible for
* Building transactions
* Estimating fees and adding buffer
* Estimating fees and adding gas limit buffer
* Signing transactions
* Sending transactions to the network
* Doing transaction nonce and gas price management to ensure transactions are mined


### Simple Transaction Manager
Here's the flow of the simple transaction manager which is used to send smart contract
transactions to the network.
Here's the flow of the simple transaction manager which is used to send smart contract transactions to the network.
![Simple Transaction Manager](./simple-tx-manager-flow.png)

### Simple Transaction Manager

The simple txmgr simply sends transactions to the network, waits for them to be mined, and returns the receipt. It doesn't do any managing.

### Geometric Transaction Manager

The geometric txmgr is a more advanced version of the simple txmgr. It sends transactions to the network, waits for them to be mined, and if they are not mined within a certain time, it bumps the gas price geometrically and resubmits the transaction. This process is repeated until the transaction is mined.
Loading