Skip to content

Commit

Permalink
[action] add access list tx (#4391)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Sep 13, 2024
1 parent 798fe51 commit 7a8a9fe
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 67 deletions.
12 changes: 12 additions & 0 deletions action/actctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ func (act *AbstractAction) convertToTx() TxCommonWithProto {
tx.gasPrice.Set(act.gasPrice)
}
return &tx
case AccessListTxType:
tx := AccessListTx{
chainID: act.chainID,
nonce: act.nonce,
gasLimit: act.gasLimit,
gasPrice: &big.Int{},
accessList: act.accessList,
}
if act.gasPrice != nil {
tx.gasPrice.Set(act.gasPrice)
}
return &tx
default:
panic(fmt.Sprintf("unsupported action version = %d", act.version))
}
Expand Down
2 changes: 2 additions & 0 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ func (elp *envelope) loadProtoTxCommon(pbAct *iotextypes.ActionCore) error {
switch pbAct.Version {
case LegacyTxType:
elp.common, err = fromProtoLegacyTx(pbAct)
case AccessListTxType:
elp.common, err = fromProtoAccessListTx(pbAct)
default:
panic(fmt.Sprintf("unsupported action version = %d", pbAct.Version))
}
Expand Down
38 changes: 0 additions & 38 deletions action/evm_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
package action

import (
"encoding/hex"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

Expand All @@ -23,41 +20,6 @@ type (
}
)

func toAccessListProto(list types.AccessList) []*iotextypes.AccessTuple {
if len(list) == 0 {
return nil
}
proto := make([]*iotextypes.AccessTuple, len(list))
for i, v := range list {
proto[i] = &iotextypes.AccessTuple{}
proto[i].Address = hex.EncodeToString(v.Address.Bytes())
if numKey := len(v.StorageKeys); numKey > 0 {
proto[i].StorageKeys = make([]string, numKey)
for j, key := range v.StorageKeys {
proto[i].StorageKeys[j] = hex.EncodeToString(key.Bytes())
}
}
}
return proto
}

func fromAccessListProto(list []*iotextypes.AccessTuple) types.AccessList {
if len(list) == 0 {
return nil
}
accessList := make(types.AccessList, len(list))
for i, v := range list {
accessList[i].Address = common.HexToAddress(v.Address)
if numKey := len(v.StorageKeys); numKey > 0 {
accessList[i].StorageKeys = make([]common.Hash, numKey)
for j, key := range v.StorageKeys {
accessList[i].StorageKeys[j] = common.HexToHash(key)
}
}
}
return accessList
}

// EffectiveGas returns the effective gas
func EffectiveGasTip(tx TxDynamicGas, baseFee *big.Int) (*big.Int, error) {
tip := tx.GasTipCap()
Expand Down
4 changes: 2 additions & 2 deletions action/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func TestExecutionAccessList(t *testing.T) {
identityset.Address(29).String(),
big.NewInt(20),
[]byte("test"))
elp := (&EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(1000000)).
SetAccessList(v.list).SetGasLimit(100).SetAction(ex).Build()
elp := (&EnvelopeBuilder{}).SetVersion(AccessListTxType).SetNonce(1).SetAccessList(v.list).
SetGasPrice(big.NewInt(1000000)).SetGasLimit(100).SetAction(ex).Build()
require.NoError(ex1.LoadProto(ex.Proto()))
require.Equal(ex, ex1)
gas, err := elp.IntrinsicGas()
Expand Down
27 changes: 19 additions & 8 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,13 @@ func readExecution(
return nil, nil, err
}
exec := action.NewExecution(contractAddr, ecfg.Amount(), ecfg.ByteCode())
elp := (&action.EnvelopeBuilder{}).SetNonce(state.PendingNonce()).SetGasPrice(ecfg.GasPrice()).
SetGasLimit(ecfg.GasLimit()).SetAccessList(ecfg.AccessList()).SetAction(exec).Build()
builder := (&action.EnvelopeBuilder{}).SetGasPrice(ecfg.GasPrice()).SetGasLimit(ecfg.GasLimit()).
SetNonce(state.PendingNonce()).SetAction(exec)
if len(ecfg.AccessList()) > 0 {
println("read acl")
builder.SetVersion(action.AccessListTxType).SetAccessList(ecfg.AccessList())
}
elp := builder.Build()
addr := ecfg.PrivateKey().PublicKey().Address()
if addr == nil {
return nil, nil, errors.New("failed to get address")
Expand Down Expand Up @@ -320,15 +325,15 @@ func (sct *SmartContractTest) runExecutions(
ecfg.Amount(),
ecfg.ByteCode(),
)
builder := &action.EnvelopeBuilder{}
builder.SetAction(exec).
SetNonce(nonce).
SetGasLimit(ecfg.GasLimit()).
SetGasPrice(ecfg.GasPrice()).
SetAccessList(ecfg.AccessList())
builder := (&action.EnvelopeBuilder{}).SetGasLimit(ecfg.GasLimit()).SetGasPrice(ecfg.GasPrice()).
SetNonce(nonce).SetAction(exec)
if sct.InitGenesis.IsShanghai {
builder.SetChainID(bc.ChainID())
}
if len(ecfg.AccessList()) > 0 {
println("write acl")
builder.SetVersion(action.AccessListTxType).SetAccessList(ecfg.AccessList())
}
elp := builder.Build()
selp, err := action.Sign(elp, ecfg.PrivateKey())
if err != nil {
Expand Down Expand Up @@ -1196,6 +1201,9 @@ func TestLondonEVM(t *testing.T) {
t.Run("datacopy", func(t *testing.T) {
NewSmartContractTest(t, "testdata-london/datacopy.json")
})
t.Run("datacopy-with-accesslist", func(t *testing.T) {
NewSmartContractTest(t, "testdata-london/datacopy-accesslist.json")
})
t.Run("CVE-2021-39137-attack-replay", func(t *testing.T) {
NewSmartContractTest(t, "testdata-london/CVE-2021-39137-attack-replay.json")
})
Expand Down Expand Up @@ -1229,6 +1237,9 @@ func TestShanghaiEVM(t *testing.T) {
t.Run("datacopy", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/datacopy.json")
})
t.Run("datacopy-with-accesslist", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/datacopy-accesslist.json")
})
t.Run("f.value", func(t *testing.T) {
NewSmartContractTest(t, "testdata-shanghai/f.value.json")
})
Expand Down
52 changes: 52 additions & 0 deletions action/protocol/execution/testdata-london/datacopy-accesslist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"initGenesis": {
"isBering" : true,
"isIceland" : true,
"isLondon" : true
},
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b5061026d806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063574c807814610030575b600080fd5b61003861003a565b005b604080516003808252818301909252600091602082018180368337019050509050601160f81b81600081518110610073576100736101e7565b60200101906001600160f81b031916908160001a905350602260f81b816001815181106100a2576100a26101e7565b60200101906001600160f81b031916908160001a905350603360f81b816002815181106100d1576100d16101e7565b60200101906001600160f81b031916908160001a9053508051604080516003808252818301909252600091602082018180368337019050509050600082602185018460208701600462010000fa9050826000602084013e61013182610137565b50505050565b805161014a90600090602084019061014e565b5050565b82805461015a906101fd565b90600052602060002090601f01602090048101928261017c57600085556101c2565b82601f1061019557805160ff19168380011785556101c2565b828001600101855582156101c2579182015b828111156101c25782518255916020019190600101906101a7565b506101ce9291506101d2565b5090565b5b808211156101ce57600081556001016101d3565b634e487b7160e01b600052603260045260246000fd5b600181811c9082168061021157607f821691505b60208210810361023157634e487b7160e01b600052602260045260246000fd5b5091905056fea2646970667358221220cb76f887319bd2bf08ab8098cd53e02dcf9d2c8b311ffdae7d73f2c4b7e2442264736f6c634300080e0033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 199671,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy datacopy contract"
}],
"executions": [{
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "574c8078",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [
{
"address": "0x1234567890abcdef1234567890abcdef12345678",
"storageKeys": [
"0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
"0x1234567890123456789012345678901234567890123456789012345678901234"
]
},
{
"address": "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
"storageKeys": [
"0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
]
}
],
"rawExpectedGasConsumed": 44220,
"expectedStatus": 1,
"comment": "the data of return is [0x11, 0x22, 0x33]",
"expectedBlockInfos" : {
"txRootHash" : "31da3e807a653dd42dc74d6d5a3dde2239b4c41859de6c65e044376a1020f9ce",
"stateRootHash" : "3595fffd9f80f99887e3108d84f3558e2691ab9dc1c73f6279e3da5735d02afa",
"receiptRootHash" : "6f9c949c391a25e324555d1592924d49d91d8883d8511f8db45fbae8b0bcde99"
}
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"initGenesis": {
"isBering" : true,
"isIceland" : true,
"isLondon" : true,
"isShanghai" : true
},
"initBalances": [{
"account": "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
"rawBalance": "1000000000000000000000000000"
}],
"deployments": [{
"rawByteCode": "608060405234801561001057600080fd5b5061026d806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063574c807814610030575b600080fd5b61003861003a565b005b604080516003808252818301909252600091602082018180368337019050509050601160f81b81600081518110610073576100736101e7565b60200101906001600160f81b031916908160001a905350602260f81b816001815181106100a2576100a26101e7565b60200101906001600160f81b031916908160001a905350603360f81b816002815181106100d1576100d16101e7565b60200101906001600160f81b031916908160001a9053508051604080516003808252818301909252600091602082018180368337019050509050600082602185018460208701600462010000fa9050826000602084013e61013182610137565b50505050565b805161014a90600090602084019061014e565b5050565b82805461015a906101fd565b90600052602060002090601f01602090048101928261017c57600085556101c2565b82601f1061019557805160ff19168380011785556101c2565b828001600101855582156101c2579182015b828111156101c25782518255916020019190600101906101a7565b506101ce9291506101d2565b5090565b5b808211156101ce57600081556001016101d3565b634e487b7160e01b600052603260045260246000fd5b600181811c9082168061021157607f821691505b60208210810361023157634e487b7160e01b600052602260045260246000fd5b5091905056fea2646970667358221220cb76f887319bd2bf08ab8098cd53e02dcf9d2c8b311ffdae7d73f2c4b7e2442264736f6c634300080e0033",
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawAmount": "0",
"rawGasLimit": 5000000,
"rawGasPrice": "0",
"rawExpectedGasConsumed": 199671,
"expectedStatus": 1,
"expectedBalances": [],
"comment": "deploy datacopy contract"
}],
"executions": [{
"rawPrivateKey": "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1",
"rawByteCode": "574c8078",
"rawAmount": "0",
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [
{
"address": "0x1234567890abcdef1234567890abcdef12345678",
"storageKeys": [
"0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
"0x1234567890123456789012345678901234567890123456789012345678901234"
]
},
{
"address": "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
"storageKeys": [
"0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
]
}
],
"rawExpectedGasConsumed": 44220,
"expectedStatus": 1,
"comment": "the data of return is [0x11, 0x22, 0x33]",
"expectedBlockInfos" : {
"txRootHash" : "6a61ad22579c0548af74a0c8ae6239b3950df05a6e69f758f566cb85111275fd",
"stateRootHash" : "3595fffd9f80f99887e3108d84f3558e2691ab9dc1c73f6279e3da5735d02afa",
"receiptRootHash" : "03b68bde2535bd4a933bea0a861d1756e31e7cda47be9bb1d126277d4c8c4c3f"
}
}]
}
Loading

0 comments on commit 7a8a9fe

Please sign in to comment.