Skip to content

Commit

Permalink
Merge pull request ethereum-optimism#83 from ethstorage/test_partial_sgt
Browse files Browse the repository at this point in the history
add test for partial sgt
  • Loading branch information
blockchaindevsh authored Nov 4, 2024
2 parents 6196aab + c6ce45a commit 409ef8b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum v1.14.11 => github.com/ethstorage/op-geth v0.0.0-20241023175121-a248e780804d
replace github.com/ethereum/go-ethereum v1.14.11 => github.com/ethstorage/op-geth v0.0.0-20241101012958-eb4eccc7f2c0

// replace github.com/ethereum/go-ethereum => ../op-geth

Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ github.com/elastic/gosigar v0.14.3 h1:xwkKwPia+hSfg9GqrCUKYdId102m9qTJIIr7egmK/u
github.com/elastic/gosigar v0.14.3/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 h1:RWHKLhCrQThMfch+QJ1Z8veEq5ZO3DfIhZ7xgRP9WTc=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3/go.mod h1:QziizLAiF0KqyLdNJYD7O5cpDlaFMNZzlxYNcWsJUxs=
github.com/ethereum-optimism/op-geth v1.101411.1-rc.2 h1:nOeSTzcFWUCvJO1MJ5AyI26dqR1F7vYgz2jNNKuEtoE=
github.com/ethereum-optimism/op-geth v1.101411.1-rc.2/go.mod h1:7S4pp8KHBmEmKkRjL1BPOc6jY9hW+64YeMUjR3RVLw4=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240910145426-b3905c89e8ac h1:hCIrLuOPV3FJfMDvXeOhCC3uQNvFoMIIlkT2mN2cfeg=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240910145426-b3905c89e8ac/go.mod h1:XaVXL9jg8BcyOeugECgIUGa9Y3DjYJj71RHmb5qon6M=
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
Expand All @@ -191,10 +189,8 @@ github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
github.com/ethstorage/da-server v0.0.0-20240925084712-169d238000e5 h1:pxtnGOGOChxCETGUg2yEIC3nfB5C7ZWzblm4/JE7108=
github.com/ethstorage/da-server v0.0.0-20240925084712-169d238000e5/go.mod h1:QfuYw2wCpbpW7k+Fwq4s9EMWNu40XDc1KKJICPaNUgQ=
github.com/ethstorage/op-geth v0.0.0-20240921172637-07437abe4163 h1:3vRJrtL1o1iFHeSyBsClcMzLdSEzliXzwqcwu2xTRfk=
github.com/ethstorage/op-geth v0.0.0-20240921172637-07437abe4163/go.mod h1:Mk8AhvlqFbjI9oW2ymThSSoqc6kiEH0/tCmHGMEu6ac=
github.com/ethstorage/op-geth v0.0.0-20241023175121-a248e780804d h1:Fo+FRUc92dI/bOoou0zg57w7WwfkkTFi+Lf4PE6db2Y=
github.com/ethstorage/op-geth v0.0.0-20241023175121-a248e780804d/go.mod h1:7S4pp8KHBmEmKkRjL1BPOc6jY9hW+64YeMUjR3RVLw4=
github.com/ethstorage/op-geth v0.0.0-20241101012958-eb4eccc7f2c0 h1:S2Qiaju/vrmHajiPHbV9pZ0s/rdy8KygD8nHm0Ditgw=
github.com/ethstorage/op-geth v0.0.0-20241101012958-eb4eccc7f2c0/go.mod h1:7S4pp8KHBmEmKkRjL1BPOc6jY9hW+64YeMUjR3RVLw4=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
Expand Down
176 changes: 173 additions & 3 deletions op-e2e/opgeth/op_geth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opgeth

import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
"testing"
Expand All @@ -15,9 +16,9 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/util"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -1005,6 +1006,8 @@ func TestEcotone(t *testing.T) {
}

func TestSoulGasToken(t *testing.T) {
SoulGasTokenABI, err := util.ParseFunctionsAsABI([]string{"function deposit()"})
require.NoError(t, err)
t.Run("no SoulGasToken", func(t *testing.T) {
op_e2e.InitParallel(t)
cfg := e2esys.DefaultSystemConfigForSoulGasToken(t, true, true)
Expand Down Expand Up @@ -1055,7 +1058,7 @@ func TestSoulGasToken(t *testing.T) {
To: &types.SoulGasTokenAddr,
Value: big.NewInt(1),
Gas: 1000001,
Data: core.SoulGasTokenABI.Methods["deposit"].ID,
Data: SoulGasTokenABI.Methods["deposit"].ID,
})

_, err = opGeth.AddL2Block(ctx, mintTx)
Expand Down Expand Up @@ -1108,7 +1111,7 @@ func TestSoulGasToken(t *testing.T) {
To: &types.SoulGasTokenAddr,
Value: big.NewInt(params.Ether),
Gas: 1000001,
Data: core.SoulGasTokenABI.Methods["deposit"].ID,
Data: SoulGasTokenABI.Methods["deposit"].ID,
})
_, err = opGeth.AddL2Block(ctx, mintTx)
require.NoError(t, err)
Expand Down Expand Up @@ -1143,6 +1146,173 @@ func TestSoulGasToken(t *testing.T) {
require.NoError(t, err)
require.True(t, balanceBefore.Cmp(balanceAfter) == 0)
})

// tx.value > acc.balance & tx.cost < acc.balance + acc.sgt_balance, should fail
t.Run("insufficient native token", func(t *testing.T) {
op_e2e.InitParallel(t)
cfg := e2esys.DefaultSystemConfigForSoulGasToken(t, true, true)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

opGeth, err := NewOpGeth(t, ctx, &cfg)
require.NoError(t, err)
defer opGeth.Close()

// mint enough SoulGasToken with deposit tx, but no native balance
targetPK, _ := crypto.GenerateKey()
targetAccount := crypto.PubkeyToAddress(*targetPK.Public().(*ecdsa.PublicKey))

// fund targetAccount with 1 ether
transferEthTx := types.NewTx(&types.DepositTx{
From: cfg.Secrets.Addresses().Bob,
To: &targetAccount,
Value: big.NewInt(params.Ether),
Gas: 1000001,
})
// convert 1 ether to sgt for targetAccount
mintTx := types.NewTx(&types.DepositTx{
From: targetAccount,
To: &types.SoulGasTokenAddr,
Value: big.NewInt(params.Ether),
Gas: 1000001,
Data: SoulGasTokenABI.Methods["deposit"].ID,
})
_, err = opGeth.AddL2Block(ctx, transferEthTx, mintTx)
require.NoError(t, err)

receipt, err := opGeth.L2Client.TransactionReceipt(ctx, mintTx.Hash())
require.NoError(t, err)
assert.Equal(t, types.ReceiptStatusSuccessful, receipt.Status)

signer := types.LatestSigner(opGeth.L2ChainConfig)

tx := types.MustSignNewTx(targetPK, signer, &types.DynamicFeeTx{
ChainID: big.NewInt(int64(cfg.DeployConfig.L2ChainID)),
Nonce: 1,
GasTipCap: big.NewInt(100),
GasFeeCap: big.NewInt(100000),
Gas: 1_000_001,
To: &cfg.Secrets.Addresses().Alice,
Value: big.NewInt(params.Ether), // this tx.value will lead to tx failure
Data: nil,
})
_, err = opGeth.AddL2Block(ctx, tx)
// err is like: Invalid payload attributes:failed to force-include tx: ..., err: insufficient funds for gas * price + value
require.Error(t, err)

// TODO: investigate why the message "failed to force-include tx" is not shown, it's a bug for OP, nothing to do with us

})

// tx.cost > acc.balance + acc.sgt_balance, should fail
t.Run("cost exceed native+sgt balance", func(t *testing.T) {
op_e2e.InitParallel(t)
cfg := e2esys.DefaultSystemConfigForSoulGasToken(t, true, true)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

opGeth, err := NewOpGeth(t, ctx, &cfg)
require.NoError(t, err)
defer opGeth.Close()

// mint enough SoulGasToken with deposit tx, but no native balance
targetPK, _ := crypto.GenerateKey()
targetAccount := crypto.PubkeyToAddress(*targetPK.Public().(*ecdsa.PublicKey))

// fund targetAccount with 2 wei
transferEthTx := types.NewTx(&types.DepositTx{
From: cfg.Secrets.Addresses().Bob,
To: &targetAccount,
Value: big.NewInt(params.Wei),
Gas: 1000001,
})
// convert 1 wei to sgt for targetAccount
mintTx := types.NewTx(&types.DepositTx{
From: targetAccount,
To: &types.SoulGasTokenAddr,
Value: big.NewInt(params.Wei),
Gas: 1000001,
Data: SoulGasTokenABI.Methods["deposit"].ID,
})
_, err = opGeth.AddL2Block(ctx, transferEthTx, mintTx)
require.NoError(t, err)

receipt, err := opGeth.L2Client.TransactionReceipt(ctx, mintTx.Hash())
require.NoError(t, err)
assert.Equal(t, types.ReceiptStatusSuccessful, receipt.Status)

signer := types.LatestSigner(opGeth.L2ChainConfig)

tx := types.MustSignNewTx(targetPK, signer, &types.DynamicFeeTx{
ChainID: big.NewInt(int64(cfg.DeployConfig.L2ChainID)),
Nonce: 1,
GasTipCap: big.NewInt(100),
GasFeeCap: big.NewInt(100000),
Gas: 1_000_001,
To: &cfg.Secrets.Addresses().Alice,
Value: big.NewInt(params.Wei),
Data: nil,
})
_, err = opGeth.AddL2Block(ctx, tx)
require.Error(t, err)
})

// tx.value <= acc.balance && tx.cost <= acc.balance + acc.sgt_balance, should succeed
t.Run("happy path", func(t *testing.T) {
op_e2e.InitParallel(t)
cfg := e2esys.DefaultSystemConfigForSoulGasToken(t, true, true)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

opGeth, err := NewOpGeth(t, ctx, &cfg)
require.NoError(t, err)
defer opGeth.Close()

// mint enough SoulGasToken with deposit tx, but no native balance
targetPK, _ := crypto.GenerateKey()
targetAccount := crypto.PubkeyToAddress(*targetPK.Public().(*ecdsa.PublicKey))

// fund targetAccount with 1 eth + 1wei
transferEthTx := types.NewTx(&types.DepositTx{
From: cfg.Secrets.Addresses().Bob,
To: &targetAccount,
Value: big.NewInt(params.Ether + params.Wei),
Gas: 1000001,
})
// convert 1 eth to sgt for targetAccount
mintTx := types.NewTx(&types.DepositTx{
From: targetAccount,
To: &types.SoulGasTokenAddr,
Value: big.NewInt(params.Ether),
Gas: 1000001,
Data: SoulGasTokenABI.Methods["deposit"].ID,
})
_, err = opGeth.AddL2Block(ctx, transferEthTx, mintTx)
require.NoError(t, err)

receipt, err := opGeth.L2Client.TransactionReceipt(ctx, mintTx.Hash())
require.NoError(t, err)
assert.Equal(t, types.ReceiptStatusSuccessful, receipt.Status)

signer := types.LatestSigner(opGeth.L2ChainConfig)

tx := types.MustSignNewTx(targetPK, signer, &types.DynamicFeeTx{
ChainID: big.NewInt(int64(cfg.DeployConfig.L2ChainID)),
Nonce: 1,
GasTipCap: big.NewInt(100),
GasFeeCap: big.NewInt(100000),
Gas: 1_000_001,
To: &cfg.Secrets.Addresses().Alice,
Value: big.NewInt(params.Wei),
Data: nil,
})
_, err = opGeth.AddL2Block(ctx, tx)
require.NoError(t, err)
})

}

func TestPreFjord(t *testing.T) {
Expand Down

0 comments on commit 409ef8b

Please sign in to comment.