Skip to content

Commit

Permalink
Add gas bumping txmgr (#284)
Browse files Browse the repository at this point in the history
* Updated chainio and signer READMEs + some code comments

added gas oracle

refactored txmgr module

added geometric txmgr

make fmt

log -> logger in simple txmgr

add TODO + rename constructor

update geometric txmgr to use sync api

gasoracle refactor

chore: switch to pointer to gasoracle in geometric txmgr
Also added some comments

example: add example test for geometric txmgr

fix(geometric_example_test): start anvil container

refactor(geometric txmgr): delete gasoracle and add its methods to geometric txmgr directly

make fmt

fix(lint): remove no longer used variable hundred

wip moving geometric to its own subdir

simplify privatekey_wallet

delete old moved files

add geometric txmgr metrics interface + noop metrics

docs: add default values in geometric txmgr params comment

test(geometric txmgr): add first simple test for geometric txmgr

test(geometric txmgr): add some basic tx sending tests

feat(ecdsa): add KeyAndAddressFromHexKey helper function

feat(geometric txmgr): add integration test scaffold with 1 simple tx sending test

fix(geometric txmgr): remove unneeded mutex

test(geometric txmgr): add parallel mining logic to fakeEthBackend.SendTransaction to test parallel sending of txs

feat(geometrix txmgr): refactor queryTicketDuration to be a param instead of a fixed const

refactor(txmgr test harness): refactor into a mining goroutine

fix(geometric txmgr test): use mutex to get rid of race condition

refactor(geometric txmgr params): made all params public fields

feat(geometric txmgr test): added congestedBlocks field to test network congestion where txs need to get bumped
also made tests run fast by setting txmgr's GetTxReceiptTickerDuration to 100ms instead of the default 3s

fix(geometric txmgr): bug in ensureAnyTxBroadcasted
ethereum.NotFound errors were not handled properly, causing txs to never get bumped

feat(geometric txmgr test): added congested network test

* fix(geometric txmgr test): race condition to set congested blocks

* fix compilation errors from prev rebase

* make fmt

* fix(geometric txmgr): make ethBackend interface private

* doc(geometri  txmgr): add default GetTxReceiptTickerDuration param as comment

* chore(geometric txmgr): remove no longer used GasPricePercentageMultiplier param

* doc(geometrix txmgr): comment ProcessTransaction -> processTransaction

Co-authored-by: Madhur Shrimal <[email protected]>

* chore(geometric txmgr): move code around to make it cleaner

* chore(geometric txmgr): use utils.WrapError everywhere instead of fmt.Errorf

* doc(geometric txmgr): added comment saying that we (probably) can delete while iterating over go map

* make fmt

* make fmt

* refactor(geometric txmgr): change gas/gastip params to use floats instead of percentages

* docs(geometric txmgr): add comments for the ensureAnyTxBroadcasted/Confirmed fcts

* fix(typo): READMD -> README

* make fmt

* refactor(geometric txmgr): simplify code by only calling ensureAnyTxBroadcasted for fireblock wallet

* fix(geometric txmgr): revert to old (correct) code for catching timeout errors

* make fmt

* refactor(ecdsa/utils): use strings.TrimPrefix to trim 0x prefix

* delete(metrics.go): duplicate file, no longer needed

* refactor(geometric txmgr): rename fct with clearer name for self-documentation

* doc(geometric txmgr): fix comment that was not true

* doc(geometric txmgr): correct comment

* test(geometric txmgr): add test for sending tx with incorrect nonce

* test(geometric txmgr): added parallel test that sends nonces in reverse order

* doc(geometric txmgr): add comment explaining that processTransaction is goroutine safe

* chore(geometric txmgr): uncomment code that was commented while debugging

---------

Co-authored-by: Madhur Shrimal <[email protected]>
  • Loading branch information
samlaf and shrimalmadhur authored Aug 9, 2024
1 parent 108520f commit bdeff56
Show file tree
Hide file tree
Showing 19 changed files with 1,660 additions and 247 deletions.
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 {
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?
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

0 comments on commit bdeff56

Please sign in to comment.