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

tx mgr for onchain transactions #75

Merged
merged 3 commits into from
Dec 9, 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
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
############################# HELP MESSAGE #############################
# Make sure the help command stays first, so that it's printed by default when `make` is called without arguments
.PHONY: help bindings mocks tests tests-cover fmt format-lines
.PHONY: help bindings mocks tests tests-cover fmt format-lines lint

GO_LINES_IGNORED_DIRS=contracts
GO_PACKAGES=./chainio/... ./crypto/... ./logging/... \
./types/... ./utils/... ./signer/... ./cmd/...
./types/... ./utils/... ./signer/... ./cmd/... \
./signerv2/...
GO_FOLDERS=$(shell echo ${GO_PACKAGES} | sed -e "s/\.\///g" | sed -e "s/\/\.\.\.//g")
help:
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down Expand Up @@ -43,4 +44,8 @@ fmt: ## formats all go files

format-lines: ## formats all go files with golines
go install github.com/segmentio/golines@latest
golines -w -m 120 --ignore-generated --shorten-comments --ignored-dirs=${GO_LINES_IGNORED_DIRS} ${GO_FOLDERS}
golines -w -m 120 --ignore-generated --shorten-comments --ignored-dirs=${GO_LINES_IGNORED_DIRS} ${GO_FOLDERS}

lint: ## runs all linters
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run ./...
45 changes: 32 additions & 13 deletions chainio/clients/avsregistry/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package avsregistry

import (
"context"
"errors"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/signer"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

Expand Down Expand Up @@ -40,9 +41,9 @@ type AvsRegistryChainWriter struct {
blsOperatorStateRetriever *blsoperatorstateretriever.ContractBLSOperatorStateRetriever
stakeRegistry *stakeregistry.ContractStakeRegistry
blsPubkeyRegistry *blspubkeyregistry.ContractBLSPubkeyRegistry
signer signer.Signer
logger logging.Logger
ethClient eth.EthClient
txMgr txmgr.TxManager
}

var _ AvsRegistryWriter = (*AvsRegistryChainWriter)(nil)
Expand All @@ -53,17 +54,17 @@ func NewAvsRegistryWriter(
stakeRegistry *stakeregistry.ContractStakeRegistry,
blsPubkeyRegistry *blspubkeyregistry.ContractBLSPubkeyRegistry,
logger logging.Logger,
signer signer.Signer,
ethClient eth.EthClient,
txMgr txmgr.TxManager,
) (*AvsRegistryChainWriter, error) {
return &AvsRegistryChainWriter{
registryCoordinator: registryCoordinator,
blsOperatorStateRetriever: blsOperatorStateRetriever,
stakeRegistry: stakeRegistry,
blsPubkeyRegistry: blsPubkeyRegistry,
signer: signer,
logger: logger,
ethClient: ethClient,
txMgr: txMgr,
}, nil
}

Expand All @@ -74,15 +75,21 @@ func (w *AvsRegistryChainWriter) RegisterOperatorWithAVSRegistryCoordinator(
socket string,
) (*types.Receipt, error) {
w.logger.Info("registering operator with the AVS's registry coordinator")
txOpts := w.signer.GetTxOpts()
noSendTxOpts, err := w.txMgr.GetNoSendTxOpts()
if err != nil {
return nil, err
}
// TODO: this call will fail if max number of operators are already registered
// in that case, need to call churner to kick out another operator. See eigenDA's node/operator.go implementation
tx, err := w.registryCoordinator.RegisterOperatorWithCoordinator1(txOpts, quorumNumbers, pubkey, socket)
tx, err := w.registryCoordinator.RegisterOperatorWithCoordinator1(noSendTxOpts, quorumNumbers, pubkey, socket)
if err != nil {
return nil, err
}
receipt, err := w.txMgr.Send(ctx, tx)
if err != nil {
return nil, errors.New("failed to send tx with err: " + err.Error())
}
w.logger.Infof("tx hash: %s", tx.Hash().String())
receipt := w.ethClient.WaitForTransactionReceipt(ctx, tx.Hash())
w.logger.Info("registered operator with the AVS's registry coordinator")
return receipt, nil
}
Expand All @@ -92,13 +99,19 @@ func (w *AvsRegistryChainWriter) UpdateStakes(
operators []gethcommon.Address,
) (*types.Receipt, error) {
w.logger.Info("updating stakes")
txOpts := w.signer.GetTxOpts()
tx, err := w.stakeRegistry.UpdateStakes(txOpts, operators)
noSendTxOpts, err := w.txMgr.GetNoSendTxOpts()
if err != nil {
return nil, err
}
tx, err := w.stakeRegistry.UpdateStakes(noSendTxOpts, operators)
if err != nil {
return nil, err
}
receipt, err := w.txMgr.Send(ctx, tx)
if err != nil {
return nil, errors.New("failed to send tx with err: " + err.Error())
}
w.logger.Infof("tx hash: %s", tx.Hash().String())
receipt := w.ethClient.WaitForTransactionReceipt(ctx, tx.Hash())
w.logger.Info("updated stakes")
return receipt, nil

Expand All @@ -110,13 +123,19 @@ func (w *AvsRegistryChainWriter) DeregisterOperator(
pubkey blsregistrycoordinator.BN254G1Point,
) (*types.Receipt, error) {
w.logger.Info("deregistering operator with the AVS's registry coordinator")
txOpts := w.signer.GetTxOpts()
tx, err := w.registryCoordinator.DeregisterOperatorWithCoordinator(txOpts, quorumNumbers, pubkey)
noSendTxOpts, err := w.txMgr.GetNoSendTxOpts()
if err != nil {
return nil, err
}
tx, err := w.registryCoordinator.DeregisterOperatorWithCoordinator(noSendTxOpts, quorumNumbers, pubkey)
if err != nil {
return nil, err
}
receipt, err := w.txMgr.Send(ctx, tx)
if err != nil {
return nil, errors.New("failed to send tx with err: " + err.Error())
}
w.logger.Infof("tx hash: %s", tx.Hash().String())
receipt := w.ethClient.WaitForTransactionReceipt(ctx, tx.Hash())
w.logger.Info("deregistered operator with the AVS's registry coordinator")
return receipt, nil
}
29 changes: 17 additions & 12 deletions chainio/clients/builder.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package clients

import (
avsregistry "github.com/Layr-Labs/eigensdk-go/chainio/clients/avsregistry"
elcontracts "github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/avsregistry"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
chainioutils "github.com/Layr-Labs/eigensdk-go/chainio/utils"
blspubkeycompendium "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSPublicKeyCompendium"
logging "github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/metrics"
"github.com/Layr-Labs/eigensdk-go/signer"
"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -40,7 +41,7 @@ type Clients struct {
PrometheusRegistry *prometheus.Registry // Used if avs teams need to register avs-specific metrics
}

func BuildAll(config BuildAllConfig, signer signer.Signer, logger logging.Logger) (*Clients, error) {
func BuildAll(config BuildAllConfig, signer signerv2.SignerFn, logger logging.Logger) (*Clients, error) {
config.validate(logger)

// Create the metrics server
Expand All @@ -60,11 +61,12 @@ func BuildAll(config BuildAllConfig, signer signer.Signer, logger logging.Logger
return nil, err
}

txMgr := txmgr.NewSimpleTxManager(ethHttpClient, logger, signer, gethcommon.Address{})
// creating EL clients: Reader, Writer and Subscriber
elChainReader, elChainWriter, elChainSubscriber, err := config.buildElClients(
ethHttpClient,
ethWsClient,
signer,
txMgr,
logger,
eigenMetrics,
)
Expand All @@ -76,7 +78,7 @@ func BuildAll(config BuildAllConfig, signer signer.Signer, logger logging.Logger
// creating AVS clients: Reader and Writer
avsRegistryChainReader, avsRegistryChainWriter, err := config.buildAvsClients(
ethHttpClient,
signer,
txMgr,
logger,
)
if err != nil {
Expand All @@ -101,7 +103,7 @@ func BuildAll(config BuildAllConfig, signer signer.Signer, logger logging.Logger
func (config *BuildAllConfig) buildElClients(
ethHttpClient eth.EthClient,
ethWsClient eth.EthClient,
signer signer.Signer,
txMgr txmgr.TxManager,
logger logging.Logger,
eigenMetrics *metrics.EigenMetrics,
) (elcontracts.ELReader, elcontracts.ELWriter, elcontracts.ELSubscriber, error) {
Expand Down Expand Up @@ -145,7 +147,10 @@ func (config *BuildAllConfig) buildElClients(
)

// get the Subscriber for the EL contracts
contractBlsPubkeyCompendiumWs, err := blspubkeycompendium.NewContractBLSPublicKeyCompendium(elContractBindings.BlspubkeyCompendiumAddr, ethWsClient)
contractBlsPubkeyCompendiumWs, err := blspubkeycompendium.NewContractBLSPublicKeyCompendium(
elContractBindings.BlspubkeyCompendiumAddr,
ethWsClient,
)
if err != nil {
logger.Fatal("Failed to fetch BLSPublicKeyCompendium contract", "err", err)
}
Expand All @@ -167,9 +172,9 @@ func (config *BuildAllConfig) buildElClients(
elContractBindings.BlspubkeyCompendiumAddr,
elChainReader,
ethHttpClient,
signer,
logger,
eigenMetrics,
txMgr,
)
if err != nil {
logger.Error("Failed to create ELChainWriter", "err", err)
Expand All @@ -181,7 +186,7 @@ func (config *BuildAllConfig) buildElClients(

func (config *BuildAllConfig) buildAvsClients(
ethHttpClient eth.EthClient,
signer signer.Signer,
txMgr txmgr.TxManager,
logger logging.Logger,
) (avsregistry.AvsRegistryReader, avsregistry.AvsRegistryWriter, error) {

Expand Down Expand Up @@ -215,8 +220,8 @@ func (config *BuildAllConfig) buildAvsClients(
avsRegistryContractBindings.StakeRegistry,
avsRegistryContractBindings.BlsPubkeyRegistry,
logger,
signer,
ethHttpClient,
txMgr,
)
if err != nil {
logger.Error("Failed to create AVSRegistryChainWriter", "err", err)
Expand Down
7 changes: 6 additions & 1 deletion chainio/clients/elcontracts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ func BuildELChainReader(
ethClient eth.EthClient,
logger logging.Logger,
) (*ELChainReader, error) {
elContractBindings, err := chainioutils.NewEigenlayerContractBindings(slasherAddr, blsPubKeyCompendiumAddr, ethClient, logger)
elContractBindings, err := chainioutils.NewEigenlayerContractBindings(
slasherAddr,
blsPubKeyCompendiumAddr,
ethClient,
logger,
)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading