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

added egnaddrs binary #50

Merged
merged 4 commits into from
Nov 1, 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
41 changes: 41 additions & 0 deletions cmd/egnaddrs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## egnaddrs
This tool is used to help debug and test eigenlayer/avs deployments and contract setups.

### How to install
#### Install from repository
```bash
go install github.com/Layr-Labs/eigensdk-go/cmd/egnaddrs@latest
```

#### Install from local source
If you have git cloned eigensdk-go to your machine, navigate to [cmd/egnaddrs](.) directory and run
```bash
go install
```

### Usage

Currently egnaddrs only supports deriving contract addresses starting from a registry-coordinator or service-manager address. It then prints the following datastructure:

```bash
$$$ egnaddrs --registry-coordinator 0x9E545E3C0baAB3E08CdfD552C960A1050f373042
samlaf marked this conversation as resolved.
Show resolved Hide resolved
{
"avs": {
"bls-pubkey-compendium (shared)": "0x322813Fd9A801c5507c9de605d63CEA4f2CE6c44",
"bls-pubkey-registry": "0xa82fF9aFd8f496c3d6ac40E2a0F282E47488CFc9",
"index-registry": "0x1613beB3B2C4f22Ee086B2b38C1476A3cE7f78E8",
"registry-coordinator": "0x9E545E3C0baAB3E08CdfD552C960A1050f373042",
"service-manager": "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690",
"stake-registry": "0x851356ae760d987E095750cCeb3bC6014560891C"
},
"eigenlayer": {
"delegation-manager": "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
"slasher": "0x5FC8d32690cc91D4c39d9d3abcBD16989F875707",
"strategy-manager": "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
},
"network": {
"chain-id": "31337",
"rpc-url": "http://localhost:8545"
}
}
```
210 changes: 210 additions & 0 deletions cmd/egnaddrs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/urfave/cli/v2"

blspubkeyreg "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSPubkeyRegistry"
blsregistrycoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSRegistryCoordinatorWithIndices"
iblssigchecker "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IBLSSignatureChecker"
slasher "github.com/Layr-Labs/eigensdk-go/contracts/bindings/Slasher"
)

var (
ServiceManagerAddrFlag = &cli.StringFlag{
Name: "service-manager",
Usage: "ServiceManager contract address",
Required: false,
}
RegistryCoordinatorAddrFlag = &cli.StringFlag{
Name: "registry-coordinator",
Usage: "BLSRegistryCoordinatorWithIndices contract address",
Required: false,
}
RpcUrlFlag = &cli.StringFlag{
Name: "rpc-url",
Usage: "rpc url",
Value: "http://localhost:8545",
DefaultText: "http://localhost:8545",
}
)

func main() {
run(os.Args)
}

// We structure run in this way to make it testable.
// see https://github.com/urfave/cli/issues/731
func run(args []string) {
app := cli.NewApp()
app.Name = "egnaddrs"
app.Usage = "Used to help debug and test deployments and contract setups."
// TODO: add a much more descriptive description
app.Description = "Prints all reachable Eigenlayer and AVS contract addresses starting from any one contract."
app.Action = printAddrs
app.Flags = []cli.Flag{
ServiceManagerAddrFlag,
RegistryCoordinatorAddrFlag,
RpcUrlFlag,
}

if err := app.Run(args); err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}

}

func printAddrs(c *cli.Context) error {
client, err := ethclient.Dial(c.String(RpcUrlFlag.Name))
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
chainId, err := client.ChainID(ctx)
if err != nil {
return err
}

registryCoordinatorAddr, err := getRegistryCoordinatorAddr(c, client)
if err != nil {
return err
}
eigenlayerContractAddrs, err := getEigenlayerContractAddrs(client, registryCoordinatorAddr)
if err != nil {
return err
}
avsContractAddrs, err := getAvsContractAddrs(client, registryCoordinatorAddr)
if err != nil {
return err
}

addrsDict := map[string]any{
"network": map[string]string{
"rpc-url": c.String(RpcUrlFlag.Name),
"chain-id": chainId.String(),
},
"eigenlayer": eigenlayerContractAddrs,
"avs": avsContractAddrs,
}
addrsMarshaled, err := json.MarshalIndent(addrsDict, "", " ")
if err != nil {
return err
}
fmt.Println(string(addrsMarshaled))
return nil
}

func getRegistryCoordinatorAddr(c *cli.Context, client *ethclient.Client) (common.Address, error) {
registryCoordinatorAddrString := c.String(RegistryCoordinatorAddrFlag.Name)
if registryCoordinatorAddrString != "" {
registryCoordinatorAddr := common.HexToAddress(registryCoordinatorAddrString)
return registryCoordinatorAddr, nil
}
serviceManagerAddrString := c.String(ServiceManagerAddrFlag.Name)
if serviceManagerAddrString != "" {
serviceManagerAddr := common.HexToAddress(serviceManagerAddrString)
// we use the IBLSSignatureChecker interface because the IServiceManager interface doesn't have a getter for the registry coordinator
// because we don't want to restrict teams to use our registry contracts.
// However, egnaddrs is targetted at teams using our registry contracts, so we assume that they are using our registries
// and that their service manager inherits the IBLSSignatureChecker interface (to check signatures against the BLSPubkeyRegistry).
serviceManagerContract, err := iblssigchecker.NewContractIBLSSignatureChecker(serviceManagerAddr, client)
if err != nil {
return common.Address{}, err
}
registryCoordinatorAddr, err := serviceManagerContract.RegistryCoordinator(&bind.CallOpts{})
if err != nil {
return common.Address{}, err
}
return registryCoordinatorAddr, nil
}
return common.Address{}, errors.New("must provide either --registry-coordinator or --service-manager flag")
}

func getAvsContractAddrs(client *ethclient.Client, registryCoordinatorAddr common.Address) (map[string]string, error) {
blsRegistryCoordinatorWithIndicesC, err := blsregistrycoordinator.NewContractBLSRegistryCoordinatorWithIndices(registryCoordinatorAddr, client)
if err != nil {
return nil, err
}
_ = blsRegistryCoordinatorWithIndicesC

serviceManagerAddr, err := blsRegistryCoordinatorWithIndicesC.ServiceManager(&bind.CallOpts{})
if err != nil {
return nil, err
}

// 3 registries
blsPubkeyRegistryAddr, err := blsRegistryCoordinatorWithIndicesC.BlsPubkeyRegistry(&bind.CallOpts{})
if err != nil {
return nil, err
}
indexRegistryAddr, err := blsRegistryCoordinatorWithIndicesC.IndexRegistry(&bind.CallOpts{})
if err != nil {
return nil, err
}
stakeRegistryAddr, err := blsRegistryCoordinatorWithIndicesC.StakeRegistry(&bind.CallOpts{})
if err != nil {
return nil, err
}

// pubkey compendium (shared avs contract)
contractBlsPubkeyRegistry, err := blspubkeyreg.NewContractBLSPubkeyRegistry(blsPubkeyRegistryAddr, client)
if err != nil {
return nil, err
}
blsPubKeyCompendiumAddr, err := contractBlsPubkeyRegistry.PubkeyCompendium(&bind.CallOpts{})
if err != nil {
return nil, err
}

addrsDict := map[string]string{
"service-manager": serviceManagerAddr.Hex(),
"registry-coordinator": registryCoordinatorAddr.Hex(),
"bls-pubkey-registry": blsPubkeyRegistryAddr.Hex(),
"index-registry": indexRegistryAddr.Hex(),
"stake-registry": stakeRegistryAddr.Hex(),
"bls-pubkey-compendium (shared)": blsPubKeyCompendiumAddr.Hex(),
}
return addrsDict, nil
}

func getEigenlayerContractAddrs(client *ethclient.Client, registryCoordinatorAddr common.Address) (map[string]string, error) {
blsRegistryCoordinatorWithIndicesC, err := blsregistrycoordinator.NewContractBLSRegistryCoordinatorWithIndices(registryCoordinatorAddr, client)
if err != nil {
return nil, err
}

slasherAddr, err := blsRegistryCoordinatorWithIndicesC.Slasher(&bind.CallOpts{})
if err != nil {
return nil, err
}
slasherC, err := slasher.NewContractSlasher(slasherAddr, client)
if err != nil {
return nil, err
}
delegationManagerAddr, err := slasherC.Delegation(&bind.CallOpts{})
if err != nil {
return nil, err
}
strategyManagerAddr, err := slasherC.StrategyManager(&bind.CallOpts{})
if err != nil {
return nil, err
}

addrsDict := map[string]string{
"slasher": slasherAddr.Hex(),
"delegation-manager": delegationManagerAddr.Hex(),
"strategy-manager": strategyManagerAddr.Hex(),
}
return addrsDict, nil
}
80 changes: 80 additions & 0 deletions cmd/egnaddrs/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
// TODO: these are currently hardcoded for the contracts deployed in
// test_data/avs-and-eigenlayer-deplyed-anvil-state.json
serviceManagerAddr = "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690"
registryCoordinatorAddr = "0x9E545E3C0baAB3E08CdfD552C960A1050f373042"
)

func TestEgnAddrsWithServiceManagerFlag(t *testing.T) {

anvilC := startAnvilTestContainer()
anvilEndpoint, err := anvilC.Endpoint(context.Background(), "")
if err != nil {
t.Error(err)
}

args := []string{"egnaddrs"}
args = append(args, "--service-manager", serviceManagerAddr)
args = append(args, "--rpc-url", "http://"+anvilEndpoint)
// we just make sure it doesn't crash
run(args)
}

func TestEgnAddrsWithRegistryCoordinatorFlag(t *testing.T) {

anvilC := startAnvilTestContainer()
anvilEndpoint, err := anvilC.Endpoint(context.Background(), "")
if err != nil {
t.Error(err)
}

args := []string{"egnaddrs"}
args = append(args, "--registry-coordinator", registryCoordinatorAddr)
args = append(args, "--rpc-url", "http://"+anvilEndpoint)
// we just make sure it doesn't crash
run(args)
}

func startAnvilTestContainer() testcontainers.Container {
integrationDir, err := os.Getwd()
if err != nil {
panic(err)
}

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "ghcr.io/foundry-rs/foundry:latest",
Mounts: testcontainers.ContainerMounts{
testcontainers.ContainerMount{
Source: testcontainers.GenericBindMountSource{
HostPath: filepath.Join(integrationDir, "test_data", "avs-and-eigenlayer-deployed-anvil-state.json"),
},
Target: "/root/.anvil/state.json",
},
},
Entrypoint: []string{"anvil"},
Cmd: []string{"--host", "0.0.0.0", "--load-state", "/root/.anvil/state.json"},
ExposedPorts: []string{"8545/tcp"},
WaitingFor: wait.ForLog("Listening on"),
}
anvilC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
panic(err)
}
return anvilC
}

Large diffs are not rendered by default.

Loading
Loading