Skip to content

Commit

Permalink
Add failing deposit with mint tests (#51)
Browse files Browse the repository at this point in the history
Tests two different kinds of failing deposits:

- Garbage data, end
- A call to a contract that reverts.
  • Loading branch information
mslipper authored Sep 30, 2022
1 parent 60183ad commit 929c55f
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 18 deletions.
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ mod-tidy:

contracts:
cd contracts && \
solc --bin --abi --overwrite -o . ./SimpleERC20.sol
solc --bin --abi --overwrite -o . ./SimpleERC20.sol && \
solc --bin --abi --overwrite -o . ./Failure.sol
.PHONY: contracts

bindings: contracts
abigen \
--abi ./contracts/SimpleERC20.abi \
--bin ./contracts/SimpleERC20.bin \
--pkg bindings \
--type "SimpleERC20" \
--out ./optimism/bindings/simple_erc20.go
--abi ./contracts/SimpleERC20.abi \
--bin ./contracts/SimpleERC20.bin \
--pkg bindings \
--type "SimpleERC20" \
--out ./optimism/bindings/simple_erc20.go
abigen \
--abi ./contracts/Failure.abi \
--bin ./contracts/Failure.bin \
--pkg bindings \
--type "Failure" \
--out ./optimism/bindings/failure.go
.PHONY: bindings
1 change: 1 addition & 0 deletions contracts/Failure.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"fail","outputs":[],"stateMutability":"payable","type":"function"}]
1 change: 1 addition & 0 deletions contracts/Failure.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b5061010f806100206000396000f3fe608060405260043610601c5760003560e01c8063a9cc4718146021575b600080fd5b60276029565b005b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260040160599060bb565b60405180910390fd5b600082825260208201905092915050565b7f626967206661696c210000000000000000000000000000000000000000000000600082015250565b600060a76009836062565b915060b0826073565b602082019050919050565b6000602082019050818103600083015260d281609c565b905091905056fea264697066735822122068a0ae7d220da2e9f14cc48aec07eb227130c4a4ae2d89dca9c16c132f2c64fe64736f6c634300080d0033
8 changes: 8 additions & 0 deletions contracts/Failure.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract Failure {
function fail() public payable {
revert("big fail!");
}
}
223 changes: 223 additions & 0 deletions optimism/bindings/failure.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion optimism/bindings/simple_erc20.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 72 additions & 8 deletions simulators/optimism/l1ops/deposit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"math/big"
"math/rand"
"time"

"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum-optimism/optimism/op-chain-ops/crossdomain"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-node/withdrawals"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -36,7 +38,7 @@ func simplePortalDepositTest(t *hivesim.T, env *optimism.TestEnv) {
require.EqualValues(t, 0, startBalance.Int64())

mintAmount := big.NewInt(0.5 * params.Ether)
doDeposit(t, env, depositor, mintAmount)
doDeposit(t, env, depositor, mintAmount, false, nil)

endBalance, err := l2.BalanceAt(env.Ctx(), depositor, nil)
require.Nil(t, err)
Expand Down Expand Up @@ -105,7 +107,7 @@ func erc20RoundtripTest(t *hivesim.T, env *optimism.TestEnv) {
require.NoError(t, err)

// Deposit some ETH onto L2
doDeposit(t, env, depositor, big.NewInt(0.5*params.Ether))
doDeposit(t, env, depositor, big.NewInt(0.5*params.Ether), false, nil)

// Deploy the bridged ERC20
l2Opts := l2Vault.KeyedTransactor(depositor)
Expand Down Expand Up @@ -270,13 +272,75 @@ func erc20RoundtripTest(t *hivesim.T, env *optimism.TestEnv) {
require.EqualValues(t, big.NewInt(500), balL2)
}

func doDeposit(t *hivesim.T, env *optimism.TestEnv, depositor common.Address, mintAmount *big.Int) {
depositContract, err := bindings.NewOptimismPortal(
env.Devnet.Deployments.DeploymentsL1.OptimismPortalProxy,
env.Devnet.L1Client(0),
)
func failingDepositWithMintTest(t *hivesim.T, env *optimism.TestEnv) {
// Initial setup
l1 := env.Devnet.L1Client(0)
l2 := env.Devnet.L2Client(0)
l1Vault := env.Devnet.L1Vault
l2Vault := env.Devnet.L2Vault
depositContract := env.Devnet.Bindings.BindingsL1.OptimismPortal
depositor := l1Vault.CreateAccount(env.TimeoutCtx(time.Minute), l1, big.NewInt(3*params.Ether))
l2Vault.InsertKey(l1Vault.FindKey(depositor))
l2Opts := l2Vault.KeyedTransactor(depositor)

// Fund account on L2
doDeposit(t, env, depositor, big.NewInt(0.5*params.Ether), false, nil)

// Deploy the failure contract on L2

_, deployTx, failureContract, err := hivebindings.DeployFailure(l2Opts, l2)
require.NoError(t, err)
_, err = optimism.WaitReceipt(env.TimeoutCtx(30*time.Second), l2, deployTx.Hash())
require.NoError(t, err)

// Create the revert() call
l2Opts.NoSend = true
l2Opts.GasLimit = 1_000_000
revertTx, err := failureContract.Fail(l2Opts)
require.NoError(t, err)

// Create garbage data
randData := make([]byte, 32)
_, err = rand.Read(randData)
require.NoError(t, err)

testData := [][]byte{
randData,
revertTx.Data(),
}
mintAmount := big.NewInt(0.5 * params.Ether)
opts := l1Vault.KeyedTransactor(depositor)
opts.Value = mintAmount
opts.GasLimit = 3_000_000
for _, data := range testData {
startBal, err := l2.BalanceAt(env.Ctx(), depositor, nil)
require.NoError(t, err)
tx, err := depositContract.DepositTransaction(
opts,
depositor,
mintAmount,
1_000_000,
false,
data,
)
require.NoError(t, err)
receipt, err := optimism.WaitReceipt(env.TimeoutCtx(time.Minute), l1, tx.Hash())
require.NoError(t, err)

reconstructedDep, err := derive.UnmarshalDepositLogEvent(receipt.Logs[0])
require.NoError(t, err, "could not reconstruct L2 deposit")
tx = types.NewTx(reconstructedDep)
_, err = optimism.WaitReceipt(env.TimeoutCtx(45*time.Second), l2, tx.Hash())
require.NoError(t, err)

endBal, err := l2.BalanceAt(env.Ctx(), depositor, nil)
require.Nil(t, err)
require.True(t, testutils.BigEqual(mintAmount, new(big.Int).Sub(endBal, startBal)))
}
}

func doDeposit(t *hivesim.T, env *optimism.TestEnv, depositor common.Address, mintAmount *big.Int, isCreation bool, data []byte) {
depositContract := env.Devnet.Bindings.BindingsL1.OptimismPortal
l1 := env.Devnet.L1Client(0)
l2 := env.Devnet.L2Client(0)
l1Vault := env.Devnet.L1Vault
Expand All @@ -287,7 +351,7 @@ func doDeposit(t *hivesim.T, env *optimism.TestEnv, depositor common.Address, mi
// can sometimes be off by a bit as a result of the resource
// metering code.
opts.GasLimit = 3_000_000
tx, err := depositContract.DepositTransaction(opts, depositor, common.Big0, 1_000_000, false, nil)
tx, err := depositContract.DepositTransaction(opts, depositor, common.Big0, 1_000_000, isCreation, data)
require.NoError(t, err)
receipt, err := optimism.WaitReceipt(env.TimeoutCtx(time.Minute), l1, tx.Hash())
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 929c55f

Please sign in to comment.