From c2ac4d6f9c90c3d4c98e1a43e5941b8c9c3e8bec Mon Sep 17 00:00:00 2001 From: Samuel Laferriere Date: Thu, 20 Jun 2024 09:00:16 -0700 Subject: [PATCH] update startAnvil function to advance chain by 1 block to fix current bug --- testutils/anvil.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/testutils/anvil.go b/testutils/anvil.go index af5f2046..04b4e552 100644 --- a/testutils/anvil.go +++ b/testutils/anvil.go @@ -3,6 +3,7 @@ package testutils import ( "context" "fmt" + "os/exec" "path/filepath" "runtime" @@ -37,10 +38,24 @@ func StartAnvilContainer(anvilStateFileName string) (testcontainers.Container, e }, } } - return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ + anvilC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, }) + if err != nil { + return nil, err + } + + anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http") + if err != nil { + return nil, err + } + // Still need to advance the chain by at least 1 block b/c some tests need to query the latest block, + // and the blocks dumped/loaded by anvil don't contain full transactions, which leads to panics in tests. + // See https://github.com/foundry-rs/foundry/issues/8213, which will hopefully get fixed soon. + AdvanceChainByNBlocks(1, anvilHttpEndpoint) + + return anvilC, nil } type ContractAddresses struct { @@ -107,3 +122,16 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac } return mockAvsContracts } + +func AdvanceChainByNBlocks(n int, anvilEndpoint string) { + cmd := exec.Command("bash", "-c", + fmt.Sprintf( + // see https://book.getfoundry.sh/reference/anvil/#custom-methods + `cast rpc anvil_mine %d --rpc-url %s`, + n, anvilEndpoint), + ) + err := cmd.Run() + if err != nil { + panic(err) + } +}