Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add datacopy contract test #2788

Merged
merged 10 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 88 additions & 52 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,52 @@ import (
"github.com/iotexproject/iotex-core/testutil"
)

// ExpectedBalance defines an account-balance pair
type ExpectedBalance struct {
Account string `json:"account"`
RawBalance string `json:"rawBalance"`
}
type (
// ExpectedBalance defines an account-balance pair
ExpectedBalance struct {
Account string `json:"account"`
RawBalance string `json:"rawBalance"`
}

// GenesisBlockHeight defines an genesis blockHeight
type GenesisBlockHeight struct {
IsBering bool `json:"isBering"`
IsIceland bool `json:"isIceland"`
}
ExpectedBlockInfo struct {
TxRootHash string `json:"txRootHash"`
StateRootHash string `json:"stateRootHash"`
ReceiptRootHash string `json:"receiptRootHash"`
}

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

Log struct {
Topics []string `json:"topics"`
Data string `json:"data"`
}

ExecutionConfig struct {
Comment string `json:"comment"`
ContractIndex int `json:"contractIndex"`
AppendContractAddress bool `json:"appendContractAddress"`
ContractIndexToAppend int `json:"contractIndexToAppend"`
ContractAddressToAppend string `json:"contractAddressToAppend"`
ReadOnly bool `json:"readOnly"`
RawPrivateKey string `json:"rawPrivateKey"`
RawByteCode string `json:"rawByteCode"`
RawAmount string `json:"rawAmount"`
RawGasLimit uint `json:"rawGasLimit"`
RawGasPrice string `json:"rawGasPrice"`
Failed bool `json:"failed"`
RawReturnValue string `json:"rawReturnValue"`
RawExpectedGasConsumed uint `json:"rawExpectedGasConsumed"`
ExpectedStatus uint64 `json:"expectedStatus"`
ExpectedBalances []ExpectedBalance `json:"expectedBalances"`
ExpectedLogs []Log `json:"expectedLogs"`
ExpectedErrorMsg string `json:"expectedErrorMsg"`
ExpectedBlockInfos ExpectedBlockInfo `json:"expectedBlockInfos"`
}
)

func (eb *ExpectedBalance) Balance() *big.Int {
balance, ok := new(big.Int).SetString(eb.RawBalance, 10)
Expand All @@ -78,32 +113,6 @@ func readCode(sr protocol.StateReader, addr []byte) ([]byte, error) {
return c[:], err
}

type Log struct {
Topics []string `json:"topics"`
Data string `json:"data"`
}

type ExecutionConfig struct {
Comment string `json:"comment"`
ContractIndex int `json:"contractIndex"`
AppendContractAddress bool `json:"appendContractAddress"`
ContractIndexToAppend int `json:"contractIndexToAppend"`
ContractAddressToAppend string `json:"contractAddressToAppend"`
ReadOnly bool `json:"readOnly"`
RawPrivateKey string `json:"rawPrivateKey"`
RawByteCode string `json:"rawByteCode"`
RawAmount string `json:"rawAmount"`
RawGasLimit uint `json:"rawGasLimit"`
RawGasPrice string `json:"rawGasPrice"`
Failed bool `json:"failed"`
RawReturnValue string `json:"rawReturnValue"`
RawExpectedGasConsumed uint `json:"rawExpectedGasConsumed"`
ExpectedStatus uint64 `json:"expectedStatus"`
ExpectedBalances []ExpectedBalance `json:"expectedBalances"`
ExpectedLogs []Log `json:"expectedLogs"`
ExpectedErrorMsg string `json:"expectedErrorMsg"`
}

func (cfg *ExecutionConfig) PrivateKey() crypto.PrivateKey {
priKey, err := crypto.HexStringToPrivateKey(cfg.RawPrivateKey)
if err != nil {
Expand Down Expand Up @@ -258,7 +267,7 @@ func runExecutions(
ap actpool.ActPool,
ecfgs []*ExecutionConfig,
contractAddrs []string,
) ([]*action.Receipt, error) {
) ([]*action.Receipt, error, *ExpectedBlockInfo) {
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
nonces := map[string]uint64{}
hashes := []hash.Hash256{}
for i, ecfg := range ecfgs {
Expand All @@ -269,7 +278,7 @@ func runExecutions(
if nonce, ok = nonces[executor]; !ok {
state, err := accountutil.AccountState(sf, executor)
if err != nil {
return nil, err
return nil, err, nil
}
nonce = state.Nonce
}
Expand All @@ -284,7 +293,7 @@ func runExecutions(
ecfg.ByteCode(),
)
if err != nil {
return nil, err
return nil, err, nil
}
builder := &action.EnvelopeBuilder{}
elp := builder.SetAction(exec).
Expand All @@ -294,34 +303,41 @@ func runExecutions(
Build()
selp, err := action.Sign(elp, ecfg.PrivateKey())
if err != nil {
return nil, err
return nil, err, nil
}
if err := ap.Add(context.Background(), selp); err != nil {
return nil, err
return nil, err, nil
}
selpHash, err := selp.Hash()
if err != nil {
return nil, err
return nil, err, nil
}
hashes = append(hashes, selpHash)
}
blk, err := bc.MintNewBlock(testutil.TimestampNow())
if err != nil {
return nil, err
return nil, err, nil
}

if err := bc.CommitBlock(blk); err != nil {
return nil, err
return nil, err, nil
}
receipts := []*action.Receipt{}
for _, hash := range hashes {
receipt, err := dao.GetReceiptByActionHash(hash, blk.Height())
if err != nil {
return nil, err
return nil, err, nil
}
receipts = append(receipts, receipt)
}
stateRootHash, txRootHash, receiptRootHash := blk.DeltaStateDigest(), blk.TxRoot(), blk.ReceiptRoot()
blkInfo := &ExpectedBlockInfo{
hex.EncodeToString(txRootHash[:]),
hex.EncodeToString(stateRootHash[:]),
hex.EncodeToString(receiptRootHash[:]),
}

return receipts, nil
return receipts, nil, blkInfo
}

func (sct *SmartContractTest) prepareBlockchain(
Expand Down Expand Up @@ -414,7 +430,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, _ := 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 @@ -455,6 +471,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
// prepare blockchain
ctx := context.Background()
cfg := config.Default
cfg.Chain.ProducerPrivKey = identityset.PrivateKey(28).HexString()
cfg.Chain.EnableTrielessStateDB = false
bc, sf, dao, ap := sct.prepareBlockchain(ctx, cfg, r)
defer func() {
Expand All @@ -475,6 +492,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
}
var retval []byte
var receipt *action.Receipt
var blkInfo *ExpectedBlockInfo
var err error
if exec.ReadOnly {
retval, receipt, err = readExecution(bc, sf, dao, ap, &exec, contractAddr)
Expand All @@ -486,7 +504,8 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
r.Equal(expected, retval)
}
} else {
receipts, err := runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&exec}, []string{contractAddr})
var receipts []*action.Receipt
receipts, err, blkInfo = runExecutions(bc, sf, dao, ap, []*ExecutionConfig{&exec}, []string{contractAddr})
r.NoError(err)
r.Equal(1, len(receipts))
receipt = receipts[0]
Expand All @@ -506,6 +525,11 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
if exec.ExpectedGasConsumed() != 0 {
r.Equal(exec.ExpectedGasConsumed(), receipt.GasConsumed, i)
}
if exec.ExpectedBlockInfos != (ExpectedBlockInfo{}) {
r.Equal(exec.ExpectedBlockInfos.ReceiptRootHash, blkInfo.ReceiptRootHash)
r.Equal(exec.ExpectedBlockInfos.TxRootHash, blkInfo.TxRootHash)
r.Equal(exec.ExpectedBlockInfos.StateRootHash, blkInfo.StateRootHash)
}
for _, expectedBalance := range exec.ExpectedBalances {
account := expectedBalance.Account
if account == "" {
Expand Down Expand Up @@ -872,6 +896,15 @@ func TestProtocol_Handle(t *testing.T) {
t.Run("self-destruct", func(t *testing.T) {
NewSmartContractTest(t, "testdata/self-destruct.json")
})
// datacopy
t.Run("datacopy", func(t *testing.T) {
NewSmartContractTest(t, "testdata/datacopy.json")
})
// this test replay CVE-2021-39137 attack, see attack details
// at https://github.com/ethereum/go-ethereum/blob/master/docs/postmortems/2021-08-22-split-postmortem.md
t.Run("CVE-2021-39137-attack-replay", func(t *testing.T) {
NewSmartContractTest(t, "testdata/CVE-2021-39137-attack-replay.json")
})
}

func TestMaxTime(t *testing.T) {
Expand Down Expand Up @@ -956,6 +989,9 @@ func TestIstanbulEVM(t *testing.T) {
t.Run("self-destruct", func(t *testing.T) {
NewSmartContractTest(t, "testdata-istanbul/self-destruct.json")
})
t.Run("datacopy", func(t *testing.T) {
NewSmartContractTest(t, "testdata-istanbul/datacopy.json")
})
}

func benchmarkHotContractWithFactory(b *testing.B, async bool) {
Expand Down Expand Up @@ -996,7 +1032,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, _ := runExecutions(
bc, sf, dao, ap, []*ExecutionConfig{
{
RawPrivateKey: "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
Expand Down Expand Up @@ -1027,7 +1063,7 @@ func benchmarkHotContractWithFactory(b *testing.B, async bool) {
})
contractAddrs = append(contractAddrs, contractAddr)
}
receipts, err = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
receipts, err, _ = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
r.NoError(err)
for _, receipt := range receipts {
r.Equal(uint64(1), receipt.Status)
Expand Down Expand Up @@ -1073,7 +1109,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, _ := runExecutions(
bc, sf, dao, ap, []*ExecutionConfig{
{
RawPrivateKey: "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
Expand Down Expand Up @@ -1104,7 +1140,7 @@ func benchmarkHotContractWithStateDB(b *testing.B, cachedStateDBOption bool) {
})
contractAddrs = append(contractAddrs, contractAddr)
}
receipts, err = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
receipts, err, _ = runExecutions(bc, sf, dao, ap, ecfgs, contractAddrs)
r.NoError(err)
for _, receipt := range receipts {
r.Equal(uint64(1), receipt.Status)
Expand Down
28 changes: 28 additions & 0 deletions action/protocol/execution/testdata-istanbul/datacopy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b50610347806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063574c807814610030575b600080fd5b61003861004e565b6040516100459190610231565b60405180910390f35b60606000600367ffffffffffffffff81111561006d5761006c6102d1565b5b6040519080825280601f01601f19166020018201604052801561009f5781602001600182028036833780820191505090505b509050601160f81b816000815181106100bb576100ba6102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350602260f81b81600181518110610103576101026102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350603360f81b8160028151811061014b5761014a6102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060008151905060008167ffffffffffffffff81111561019c5761019b6102d1565b5b6040519080825280601f01601f1916602001820160405280156101ce5781602001600182028036833780820191505090505b509050600082602185018460208701600462010000fa9050826000602084013e8194505050505090565b600061020382610253565b61020d818561025e565b935061021d81856020860161026f565b61022681610300565b840191505092915050565b6000602082019050818103600083015261024b81846101f8565b905092915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561028d578082015181840152602081019050610272565b8381111561029c576000848401525b50505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f830116905091905056fea2646970667358221220ab496a0fa0ea6ff6e54fb6a9d37878e9e5361d6e3f640ab58d2ddee31465ede864736f6c63430008070033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 265114,
"expectedBalances": [],
"comment": "deploy datacopy contract"
}],
"executions": [{
"readOnly": true,
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "574c8078",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 12481,
"hasReturnValue": true,
"rawReturnValue": "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000031122330000000000000000000000000000000000000000000000000000000000",
"comment": "the data of return is [0x11, 0x22, 0x33]"
}]
}
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions action/protocol/execution/testdata-istanbul/datacopy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pragma solidity ^0.8.3;

contract Datacopy {
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
function dataCopy() public view returns (bytes memory) {
bytes memory arr = new bytes(3);
arr[0] = 0x11;
arr[1] = 0x22;
arr[2] = 0x33;
uint length = arr.length;
bytes memory result = new bytes(length);
bool ret;
assembly {
// Call precompiled contract to copy data
ret :=staticcall(0x10000, 0x04, add(arr, 0x20), length, add(arr, 0x21), length)
// copy returnData into result
returndatacopy(add(result, 0x20), 0x00, length)
}
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
return result;
}
}
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "0000000000000000000000008eae784e072e961f76948a785b62c9a950fb17ae62c9a950fb17ae00000000000000000000000000000000000000000000000000",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 16400,
"expectedBalances": [],
"comment": "deploy attack contract(https://etherscan.io/address/0x8eae784e072e961f76948a785b62c9a950fb17ae)"
}],
"executions": [{
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "3034526020600760203460045afa602034343e604034f3",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 12300,
"comment": "launch attack(https://etherscan.io/tx/0x1cb6fb36633d270edefc04d048145b4298e67b8aa82a9e5ec4aa1435dd770ce4)",
"expectedBlockInfos" : {
"txRootHash" : "a59ead74a3870e9b5ca352c5f59108df402ca203ef2109799fe2d8e1da49c83d",
"stateRootHash" : "ed9bd589ee5ab5660a3d5d863bbeea13020a0aacab18e8655a626beaf9a54713",
"receiptRootHash" : "3285579efa8521fbf95829b868ff5d37632c4feac6167e9ab2dc4961004c9272"
}
}]
}
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 28 additions & 0 deletions action/protocol/execution/testdata/datacopy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b50610347806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063574c807814610030575b600080fd5b61003861004e565b6040516100459190610231565b60405180910390f35b60606000600367ffffffffffffffff81111561006d5761006c6102d1565b5b6040519080825280601f01601f19166020018201604052801561009f5781602001600182028036833780820191505090505b509050601160f81b816000815181106100bb576100ba6102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350602260f81b81600181518110610103576101026102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350603360f81b8160028151811061014b5761014a6102a2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060008151905060008167ffffffffffffffff81111561019c5761019b6102d1565b5b6040519080825280601f01601f1916602001820160405280156101ce5781602001600182028036833780820191505090505b509050600082602185018460208701600462010000fa9050826000602084013e8194505050505090565b600061020382610253565b61020d818561025e565b935061021d81856020860161026f565b61022681610300565b840191505092915050565b6000602082019050818103600083015261024b81846101f8565b905092915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561028d578082015181840152602081019050610272565b8381111561029c576000848401525b50505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f830116905091905056fea2646970667358221220ab496a0fa0ea6ff6e54fb6a9d37878e9e5361d6e3f640ab58d2ddee31465ede864736f6c63430008070033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 265114,
"expectedBalances": [],
"comment": "deploy datacopy contract"
}],
"executions": [{
"readOnly": true,
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "574c8078",
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 12481,
"hasReturnValue": true,
"rawReturnValue": "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000031122330000000000000000000000000000000000000000000000000000000000",
"comment": "the data of return is [0x11, 0x22, 0x33]"
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
}]
}
Liuhaai marked this conversation as resolved.
Show resolved Hide resolved
Loading