Skip to content

Commit

Permalink
testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Aug 6, 2024
1 parent edc9a8f commit 59e06bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GO_LINES_IGNORED_DIRS=contracts
GO_PACKAGES=./chainio/... ./crypto/... ./logging/... \
./types/... ./utils/... ./signer/... ./cmd/... \
./signerv2/... ./aws/... ./internal/... ./metrics/... \
./nodeapi/... ./cmd/... ./services/...
./nodeapi/... ./cmd/... ./services/... ./testutils/...
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
24 changes: 18 additions & 6 deletions testutils/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package testutils
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"os/exec"
"path/filepath"
"runtime"

"github.com/ethereum/go-ethereum/ethclient"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -69,8 +70,12 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac
panic(err)
}
// The ContractsRegistry contract should always be deployed at this address on anvil
// it's the address of the contract created at nonce 0 by the first anvil account (0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
contractsRegistry, err := contractreg.NewContractContractsRegistry(common.HexToAddress("0x5FbDB2315678afecb367f032d93F642f64180aa3"), ethHttpClient)
// it's the address of the contract created at nonce 0 by the first anvil account
// (0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
contractsRegistry, err := contractreg.NewContractContractsRegistry(
common.HexToAddress("0x5FbDB2315678afecb367f032d93F642f64180aa3"),
ethHttpClient,
)
if err != nil {
panic(err)
}
Expand All @@ -89,7 +94,10 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac
if mockAvsRegistryCoordinatorAddr == (common.Address{}) {
panic("mockAvsRegistryCoordinatorAddr is empty")
}
mockAvsOperatorStateRetrieverAddr, err := contractsRegistry.Contracts(&bind.CallOpts{}, "mockAvsOperatorStateRetriever")
mockAvsOperatorStateRetrieverAddr, err := contractsRegistry.Contracts(
&bind.CallOpts{},
"mockAvsOperatorStateRetriever",
)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -133,9 +141,13 @@ func AdvanceChainByNBlocks(n int, anvilEndpoint string) {
}
}

// Prefer this function over AdvanceChainByNBlocks b/c it doesn't require cast to be installed on the host machine, whereas this one doesn't.
// Prefer this function over AdvanceChainByNBlocks b/c it doesn't require cast to be installed on the host machine,
// whereas this one doesn't.
func AdvanceChainByNBlocksExecInContainer(ctx context.Context, n int, anvilC testcontainers.Container) {
c, _, err := anvilC.Exec(ctx, []string{"cast", "rpc", "anvil_mine", fmt.Sprintf("%d", n), "--rpc-url", "http://localhost:8545"})
c, _, err := anvilC.Exec(
ctx,
[]string{"cast", "rpc", "anvil_mine", fmt.Sprintf("%d", n), "--rpc-url", "http://localhost:8545"},
)
if err != nil {
panic(err)
}
Expand Down
15 changes: 11 additions & 4 deletions testutils/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const LocalStackPort = "4566"
func StartLocalstackContainer(name string) (testcontainers.Container, error) {
fmt.Println("Starting Localstack container")
req := testcontainers.ContainerRequest{
Image: "localstack/localstack:latest",
Name: fmt.Sprintf("localstack-test-%s", name),
Env: map[string]string{"LOCALSTACK_HOST": fmt.Sprintf("localhost.localstack.cloud:%s", LocalStackPort)},
Image: "localstack/localstack:latest",
Name: fmt.Sprintf("localstack-test-%s", name),
Env: map[string]string{
"LOCALSTACK_HOST": fmt.Sprintf("localhost.localstack.cloud:%s", LocalStackPort),
},
ExposedPorts: []string{LocalStackPort},
WaitingFor: wait.ForLog("Ready."),
AutoRemove: true,
Expand All @@ -30,7 +32,12 @@ func StartLocalstackContainer(name string) (testcontainers.Container, error) {
}

func NewKMSClient(localStackPort string) (*kms.Client, error) {
cfg, err := aws.GetAWSConfig("localstack", "localstack", "us-east-1", fmt.Sprintf("http://127.0.0.1:%s", localStackPort))
cfg, err := aws.GetAWSConfig(
"localstack",
"localstack",
"us-east-1",
fmt.Sprintf("http://127.0.0.1:%s", localStackPort),
)
if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %w", err)
}
Expand Down

0 comments on commit 59e06bd

Please sign in to comment.