Skip to content

Commit

Permalink
test contracts for shanghai
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Nov 24, 2023
1 parent 44e8990 commit 6dfcec6
Show file tree
Hide file tree
Showing 60 changed files with 3,714 additions and 16 deletions.
130 changes: 114 additions & 16 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/mohae/deepcopy"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
Expand Down Expand Up @@ -65,9 +66,10 @@ type (

// GenesisBlockHeight defines an genesis blockHeight
GenesisBlockHeight struct {
IsBering bool `json:"isBering"`
IsIceland bool `json:"isIceland"`
IsLondon bool `json:"isLondon"`
IsBering bool `json:"isBering"`
IsIceland bool `json:"isIceland"`
IsLondon bool `json:"isLondon"`
IsShanghai bool `json:"isShanghai"`
}

Log struct {
Expand Down Expand Up @@ -293,7 +295,7 @@ func readExecution(
return sf.SimulateExecution(ctx, addr, exec)
}

func runExecutions(
func (sct *SmartContractTest) runExecutions(
bc blockchain.Blockchain,
sf factory.Factory,
dao blockdao.BlockDAO,
Expand Down Expand Up @@ -329,11 +331,14 @@ func runExecutions(
return nil, nil, err
}
builder := &action.EnvelopeBuilder{}
elp := builder.SetAction(exec).
builder.SetAction(exec).
SetNonce(exec.Nonce()).
SetGasLimit(ecfg.GasLimit()).
SetGasPrice(ecfg.GasPrice()).
Build()
SetGasPrice(ecfg.GasPrice())
if sct.InitGenesis.IsShanghai {
builder.SetChainID(bc.ChainID())
}
elp := builder.Build()
selp, err := action.Sign(elp, ecfg.PrivateKey())
if err != nil {
return nil, nil, err
Expand All @@ -347,7 +352,7 @@ func runExecutions(
}
hashes = append(hashes, selpHash)
}
blk, err := bc.MintNewBlock(testutil.TimestampNow())
blk, err := bc.MintNewBlock(time.Unix(1234567890, 0))
if err != nil {
return nil, nil, err
}
Expand All @@ -369,7 +374,6 @@ func runExecutions(
hex.EncodeToString(stateRootHash[:]),
hex.EncodeToString(receiptRootHash[:]),
}

return receipts, blkInfo, nil
}

Expand Down Expand Up @@ -414,6 +418,13 @@ func (sct *SmartContractTest) prepareBlockchain(
cfg.Genesis.Blockchain.NewfoundlandBlockHeight = 0
cfg.Genesis.Blockchain.OkhotskBlockHeight = 0
}
if sct.InitGenesis.IsShanghai {
// Shanghai is enabled at Sumatra height
cfg.Genesis.Blockchain.PalauBlockHeight = 0
cfg.Genesis.Blockchain.QuebecBlockHeight = 0
cfg.Genesis.Blockchain.RedseaBlockHeight = 0
cfg.Genesis.Blockchain.SumatraBlockHeight = 0
}
for _, expectedBalance := range sct.InitBalances {
cfg.Genesis.InitBalanceMap[expectedBalance.Account] = expectedBalance.Balance().String()
}
Expand Down Expand Up @@ -479,7 +490,7 @@ func (sct *SmartContractTest) deployContracts(
if contract.AppendContractAddress {
contract.ContractAddressToAppend = contractAddresses[contract.ContractIndexToAppend]
}
receipts, _, err := runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&contract}, []string{action.EmptyAddress})
receipts, _, err := sct.runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&contract}, []string{action.EmptyAddress})
r.NoError(err)
r.Equal(1, len(receipts))
receipt := receipts[0]
Expand Down Expand Up @@ -519,7 +530,7 @@ func (sct *SmartContractTest) deployContracts(
func (sct *SmartContractTest) run(r *require.Assertions) {
// prepare blockchain
ctx := context.Background()
cfg := config.Default
cfg := deepcopy.Copy(config.Default).(config.Config)
cfg.Chain.ProducerPrivKey = identityset.PrivateKey(28).HexString()
cfg.Chain.EnableTrielessStateDB = false
bc, sf, dao, ap := sct.prepareBlockchain(ctx, cfg, r)
Expand Down Expand Up @@ -554,7 +565,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
}
} else {
var receipts []*action.Receipt
receipts, blkInfo, err = runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&exec}, []string{contractAddr})
receipts, blkInfo, err = sct.runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&exec}, []string{contractAddr})
r.NoError(err)
r.Equal(1, len(receipts))
receipt = receipts[0]
Expand Down Expand Up @@ -1185,6 +1196,93 @@ func TestLondonEVM(t *testing.T) {
t.Run("CVE-2021-39137-attack-replay", func(t *testing.T) {
NewSmartContractTest(t, "testdata-london/CVE-2021-39137-attack-replay.json")
})
t.Run("difficulty", func(t *testing.T) {
NewSmartContractTest(t, "testdata-london/difficulty.json")
})
}

func TestShanghaiEVM(t *testing.T) {
t.Run("array-return", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/array-return.json")
})
t.Run("basefee", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/basefee.json")
})
t.Run("basic-token", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/basic-token.json")
})
t.Run("call-dynamic", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/call-dynamic.json")
})
t.Run("chainid-selfbalance", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/chainid-selfbalance.json")
})
t.Run("CVE-2021-39137-attack-replay", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/CVE-2021-39137-attack-replay.json")
})
t.Run("datacopy", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/datacopy.json")
})
t.Run("f.value", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/f.value.json")
})
t.Run("factory", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/factory.json")
})
t.Run("gas-test", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/gas-test.json")
})
t.Run("infiniteloop", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/infiniteloop.json")
})
t.Run("mapping-delete", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/mapping-delete.json")
})
t.Run("maxtime", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/maxtime.json")
})
t.Run("modifiers", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/modifiers.json")
})
t.Run("multisend", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/multisend.json")
})
t.Run("no-variable-length-returns", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/no-variable-length-returns.json")
})
t.Run("public-mapping", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/public-mapping.json")
})
t.Run("reentry-attack", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/reentry-attack.json")
})
t.Run("remove-from-array", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/remove-from-array.json")
})
t.Run("self-destruct", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/self-destruct.json")
})
t.Run("send-eth", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/send-eth.json")
})
t.Run("sha3", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/sha3.json")
})
t.Run("storage-test", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/storage-test.json")
})
t.Run("tail-recursion", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/tail-recursion.json")
})
t.Run("tuple", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/tuple.json")
})
t.Run("wireconnection", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/wireconnection.json")
})
t.Run("prevrandao", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/prevrandao.json")
})
}

func benchmarkHotContractWithFactory(b *testing.B, async bool) {
Expand Down Expand Up @@ -1225,7 +1323,7 @@ func benchmarkHotContractWithFactory(b *testing.B, async bool) {
contractAddr := contractAddresses[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
receipts, _, err := runExecutions(
receipts, _, err := sct.runExecutions(
bc, sf, dao, ap, []*ExecutionConfig{
{
RawPrivateKey: "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
Expand Down Expand Up @@ -1256,7 +1354,7 @@ func benchmarkHotContractWithFactory(b *testing.B, async bool) {
})
contractAddrs = append(contractAddrs, contractAddr)
}
receipts, _, err = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
receipts, _, err = sct.runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
r.NoError(err)
for _, receipt := range receipts {
r.Equal(uint64(1), receipt.Status)
Expand Down Expand Up @@ -1302,7 +1400,7 @@ func benchmarkHotContractWithStateDB(b *testing.B, cachedStateDBOption bool) {
contractAddr := contractAddresses[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
receipts, _, err := runExecutions(
receipts, _, err := sct.runExecutions(
bc, sf, dao, ap, []*ExecutionConfig{
{
RawPrivateKey: "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
Expand Down Expand Up @@ -1333,7 +1431,7 @@ func benchmarkHotContractWithStateDB(b *testing.B, cachedStateDBOption bool) {
})
contractAddrs = append(contractAddrs, contractAddr)
}
receipts, _, err = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
receipts, _, err = sct.runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
r.NoError(err)
for _, receipt := range receipts {
r.Equal(uint64(1), receipt.Status)
Expand Down
35 changes: 35 additions & 0 deletions action/protocol/execution/testdata-london/difficulty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"initGenesis": {
"isBering" : true,
"isIceland" : true,
"isLondon" : true
},
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b5060b58061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063b7a21ecf14602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600044905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea2646970667358221220bfe775ccea0b6731cd2cafbedc8751178a489fc8e8432068bd0745e52908bed764736f6c63430008110033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 67487,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy contract with difficulty opcode"
}],
"executions": [{
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "b7a21ecf",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [],
"rawExpectedGasConsumed": 10714,
"expectedStatus": 1,
"comment": "call diffi()",
"readOnly": true,
"rawReturnValue": "0000000000000000000000000000000000000000000000000000000000000032"
}]
}
12 changes: 12 additions & 0 deletions action/protocol/execution/testdata-london/difficulty.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.17;

contract Difficulty {

function diffi() public view returns (uint256 r) {
assembly {
r := difficulty()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"initGenesis": {
"isBering" : true,
"isIceland" : true,
"isLondon" : true,
"isShanghai" : true
},
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "0000000000000000000000008eae784e072e961f76948a785b62c9a950fb17ae62c9a950fb17ae00000000000000000000000000000000000000000000000000",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 16400,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy attack contract(https://etherscan.io/address/0x8eae784e072e961f76948a785b62c9a950fb17ae)"
}],
"executions": [{
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "3034526020600760203460045afa602034343e604034f3",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [],
"rawExpectedGasConsumed": 12300,
"expectedStatus": 1,
"comment": "launch attack(https://etherscan.io/tx/0x1cb6fb36633d270edefc04d048145b4298e67b8aa82a9e5ec4aa1435dd770ce4)",
"expectedBlockInfos" : {
"txRootHash" : "2fe919aaaf4bd8cb41c30b6c076c3f63401b8f4e97d10c094cd5d780c1b9bd8a",
"stateRootHash" : "431a30093ca6213a048ac891ee1d5d194641f5eb7736c50df20bbb587f1b4192",
"receiptRootHash" : "a957881177b1e3c04bd4cbda648e06eb628497dd0fc55ab118b307875ba8bde3"
}
}]
}
46 changes: 46 additions & 0 deletions action/protocol/execution/testdata-shanghai/array-return.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"initGenesis": {
"isBering" : true,
"isIceland" : true,
"isLondon" : true,
"isShanghai" : true
},
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b50610115806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c298557814602d575b600080fd5b60336047565b604051603e9190609d565b60405180910390f35b60606000805480602002602001604051908101604052809291908181526020018280548015609357602002820191906000526020600020905b8154815260200190600101908083116080575b5050505050905090565b6020808252825182820181905260009190848201906040850190845b8181101560d35783518352928401929184019160010160b9565b5090969550505050505056fea2646970667358221220003153ac66045f6e4649a560a665c439bafefa3a32ad368e45a214ceeb75fdbe64736f6c634300080e0033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 96405,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy array-return contract A"
},{
"rawByteCode": "608060405234801561001057600080fd5b50610212806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063febb0f7e14610030575b600080fd5b61003861004e565b60405161004591906100c4565b60405180910390f35b60008054604080516318530aaf60e31b815290516060936001600160a01b039093169263c298557892600480820193918290030181865afa158015610097573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100bf919081019061011e565b905090565b6020808252825182820181905260009190848201906040850190845b818110156100fc578351835292840192918401916001016100e0565b50909695505050505050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561013157600080fd5b825167ffffffffffffffff8082111561014957600080fd5b818501915085601f83011261015d57600080fd5b81518181111561016f5761016f610108565b8060051b604051601f19603f8301168101818110858211171561019457610194610108565b6040529182528482019250838101850191888311156101b257600080fd5b938501935b828510156101d0578451845293850193928501926101b7565b9897505050505050505056fea2646970667358221220e2e424aa63507945438677689f578de59a4ff358c3b0332310e7341459ee099164736f6c634300080e0033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 172353,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy array-return contract B"
}],
"executions": [{
"readOnly": true,
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "febb0f7e",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [],
"rawExpectedGasConsumed": 10504,
"expectedStatus": 106,
"failed": true,
"comment": "call bar"
}]
}
Loading

0 comments on commit 6dfcec6

Please sign in to comment.