From 585d52ea3a5d4a1c04ba0c063d68dcfc95c2609c Mon Sep 17 00:00:00 2001 From: Mateusz Morusiewicz <11313015+Ruteri@users.noreply.github.com> Date: Thu, 9 Nov 2023 19:07:27 +0100 Subject: [PATCH] Adds bundle marshaling --- core/types/suave_structs.go | 2 +- core/vm/contracts_suave_marshaling.go | 16 ++++- core/vm/contracts_suave_marshaling_test.go | 71 ++++++++++++++++++++++ core/vm/contracts_suave_runtime_adapter.go | 16 ++--- suave/artifacts/addresses.go | 4 +- suave/e2e/workflow_test.go | 67 ++++++++++++++++++++ suave/gen/suave_spec.yaml | 2 +- suave/sol/libraries/Suave.sol | 6 +- suave/sol/libraries/SuaveForge.sol | 6 +- 9 files changed, 167 insertions(+), 23 deletions(-) diff --git a/core/types/suave_structs.go b/core/types/suave_structs.go index 3eeb054d5..cd845e5ad 100755 --- a/core/types/suave_structs.go +++ b/core/types/suave_structs.go @@ -1,5 +1,5 @@ // Code generated by suave/gen. DO NOT EDIT. -// Hash: 642b45869f08a20f4a179ca827b5de96c51ce2a2af2170a228530cb296f82272 +// Hash: 00b9c4dfc0f1adf82ecb877e71dde061759b2726572a1d88b1137e95b3c42248 package types import "github.com/ethereum/go-ethereum/common" diff --git a/core/vm/contracts_suave_marshaling.go b/core/vm/contracts_suave_marshaling.go index c2a4b1067..0af2ad986 100644 --- a/core/vm/contracts_suave_marshaling.go +++ b/core/vm/contracts_suave_marshaling.go @@ -236,7 +236,21 @@ func (p *marshalBundlePrecompile) Run(input []byte) ([]byte, error) { if err != nil { return nil, err } - bundleBytes, err := p.marshalBundle(unpackedArgs[0].(types.Bundle)) + unpackedBundle := unpackedArgs[0].(struct { + BlockNumber uint64 "json:\"blockNumber\"" + Txs [][]uint8 "json:\"txs\"" + RevertingHashes [][32]uint8 "json:\"revertingHashes\"" + }) + revertingHashes := make([]common.Hash, len(unpackedBundle.RevertingHashes)) + for i, rh := range unpackedBundle.RevertingHashes { + revertingHashes[i] = rh + } + + bundleBytes, err := p.marshalBundle(types.Bundle{ + BlockNumber: unpackedBundle.BlockNumber, + Txs: [][]byte(unpackedBundle.Txs), + RevertingHashes: revertingHashes, + }) if err != nil { return nil, err } diff --git a/core/vm/contracts_suave_marshaling_test.go b/core/vm/contracts_suave_marshaling_test.go index bff0c4a4a..b811afa0c 100644 --- a/core/vm/contracts_suave_marshaling_test.go +++ b/core/vm/contracts_suave_marshaling_test.go @@ -1,6 +1,7 @@ package vm import ( + "encoding/json" "math/big" "testing" @@ -85,3 +86,73 @@ func TestDecodeTransaction(t *testing.T) { require.Equal(t, uint64(10), recoveredTxArgs.Value) require.Equal(t, []byte{0x16}, recoveredTxArgs.Input) } + +func TestMarshalBundle(t *testing.T) { + pAbi := artifacts.SuaveAbi.Methods["marshalBundle"] + + tx := types.NewTransaction(15, common.Address{0x5, 0x4, 0x3, 0x2, 0x1, 0x0}, big.NewInt(10), 21000, big.NewInt(100), []byte{0x16}) + + txBytes, err := tx.MarshalBinary() + require.NoError(t, err) + + bundle := types.Bundle{ + BlockNumber: 100, + Txs: [][]byte{txBytes}, + RevertingHashes: []common.Hash{common.Hash{0x01}}, + } + + packedInput, err := pAbi.Inputs.Pack(bundle) + require.NoError(t, err) + outp, err := (&marshalBundlePrecompile{}).Run(packedInput) + require.NoError(t, err) + + recoveredBundle := &types.SBundle{} + require.NoError(t, json.Unmarshal(outp, &recoveredBundle)) + + require.Equal(t, 1, len(recoveredBundle.Txs)) + marshaledRecoveredTx, err := recoveredBundle.Txs[0].MarshalBinary() + require.NoError(t, err) + + require.Equal(t, txBytes, marshaledRecoveredTx) + require.Equal(t, big.NewInt(100), recoveredBundle.BlockNumber) + require.Equal(t, []common.Hash{common.Hash{0x01}}, recoveredBundle.RevertingHashes) +} + +func TestUnmarshalBundle(t *testing.T) { + pAbi := artifacts.SuaveAbi.Methods["unmarshalBundle"] + + tx := types.NewTransaction(15, common.Address{0x5, 0x4, 0x3, 0x2, 0x1, 0x0}, big.NewInt(10), 21000, big.NewInt(100), []byte{0x16}) + + txBytes, err := tx.MarshalBinary() + require.NoError(t, err) + + bundle := &types.SBundle{ + BlockNumber: big.NewInt(100), + Txs: types.Transactions{tx}, + RevertingHashes: []common.Hash{common.Hash{0x01}}, + } + + bundleBytes, err := json.Marshal(bundle) + require.NoError(t, err) + + packedInput, err := pAbi.Inputs.Pack(bundleBytes) + require.NoError(t, err) + + outp, err := (&unmarshalBundlePrecompile{}).Run(packedInput) + require.NoError(t, err) + + unpackedOutput, err := pAbi.Outputs.Unpack(outp) + require.NoError(t, err) + + recoveredBundle := unpackedOutput[0].(struct { + BlockNumber uint64 "json:\"blockNumber\"" + Txs [][]uint8 "json:\"txs\"" + RevertingHashes [][32]uint8 "json:\"revertingHashes\"" + }) + + require.Equal(t, uint64(100), recoveredBundle.BlockNumber) + require.Equal(t, 1, len(recoveredBundle.Txs)) + require.Equal(t, txBytes, recoveredBundle.Txs[0]) + require.Equal(t, 1, len(recoveredBundle.RevertingHashes)) + require.Equal(t, [32]uint8{1}, recoveredBundle.RevertingHashes[0]) +} diff --git a/core/vm/contracts_suave_runtime_adapter.go b/core/vm/contracts_suave_runtime_adapter.go index aa37e7f0d..4b01723f6 100644 --- a/core/vm/contracts_suave_runtime_adapter.go +++ b/core/vm/contracts_suave_runtime_adapter.go @@ -1,5 +1,5 @@ // Code generated by suave/gen. DO NOT EDIT. -// Hash: 642b45869f08a20f4a179ca827b5de96c51ce2a2af2170a228530cb296f82272 +// Hash: 00b9c4dfc0f1adf82ecb877e71dde061759b2726572a1d88b1137e95b3c42248 package vm import ( @@ -53,7 +53,7 @@ var ( simulateBundleAddr = common.HexToAddress("0x0000000000000000000000000000000042100000") submitBundleJsonRPCAddr = common.HexToAddress("0x0000000000000000000000000000000043000001") submitEthBlockBidToRelayAddr = common.HexToAddress("0x0000000000000000000000000000000042100002") - unmarshalBundleAddr = common.HexToAddress("0x0000000000000000000000000000000030300010") + unmarshalBundleAddr = common.HexToAddress("0x0000000000000000000000000000000030300011") ) var addrList = []common.Address{ @@ -352,11 +352,7 @@ func (b *SuaveRuntimeAdapter) encodeTransaction(input []byte) (res []byte, err e return } - result, err = artifacts.SuaveAbi.Methods["encodeTransaction"].Outputs.Pack(output1) - if err != nil { - err = errFailedToPackOutput - return - } + result = output1 return result, nil } @@ -544,11 +540,7 @@ func (b *SuaveRuntimeAdapter) marshalBundle(input []byte) (res []byte, err error return } - result, err = artifacts.SuaveAbi.Methods["marshalBundle"].Outputs.Pack(output1) - if err != nil { - err = errFailedToPackOutput - return - } + result = output1 return result, nil } diff --git a/suave/artifacts/addresses.go b/suave/artifacts/addresses.go index 96c3aa36b..ee29d1471 100644 --- a/suave/artifacts/addresses.go +++ b/suave/artifacts/addresses.go @@ -1,5 +1,5 @@ // Code generated by suave/gen. DO NOT EDIT. -// Hash: 642b45869f08a20f4a179ca827b5de96c51ce2a2af2170a228530cb296f82272 +// Hash: 00b9c4dfc0f1adf82ecb877e71dde061759b2726572a1d88b1137e95b3c42248 package artifacts import ( @@ -24,7 +24,7 @@ var ( simulateBundleAddr = common.HexToAddress("0x0000000000000000000000000000000042100000") submitBundleJsonRPCAddr = common.HexToAddress("0x0000000000000000000000000000000043000001") submitEthBlockBidToRelayAddr = common.HexToAddress("0x0000000000000000000000000000000042100002") - unmarshalBundleAddr = common.HexToAddress("0x0000000000000000000000000000000030300010") + unmarshalBundleAddr = common.HexToAddress("0x0000000000000000000000000000000030300011") ) var SuaveMethods = map[string]common.Address{ diff --git a/suave/e2e/workflow_test.go b/suave/e2e/workflow_test.go index 4fed6cd8a..960c147bc 100644 --- a/suave/e2e/workflow_test.go +++ b/suave/e2e/workflow_test.go @@ -200,6 +200,73 @@ func TestTxMarshaling(t *testing.T) { require.Equal(t, []byte{0x16}, recoveredTx.Data()) } +func TestBundleMarshaling(t *testing.T) { + fr := newFramework(t) + defer fr.Close() + + clt := fr.NewSDKClient() + + // marshalBundleAbi := artifacts.SuaveAbi.Methods["marshalBundle"] + marshalBundleAddr := artifacts.SuaveMethods["marshalBundle"] + unmarshalBundleAbi := artifacts.SuaveAbi.Methods["unmarshalBundle"] + unmarshalBundleAddr := artifacts.SuaveMethods["unmarshalBundle"] + + tx := types.NewTransaction(15, common.Address{0x5, 0x4, 0x3, 0x2, 0x1, 0x0}, big.NewInt(10), 21000, big.NewInt(100), []byte{0x16}) + + txBytes, err := tx.MarshalBinary() + require.NoError(t, err) + + bundle := &types.SBundle{ + BlockNumber: big.NewInt(100), + Txs: types.Transactions{tx}, + RevertingHashes: []common.Hash{common.Hash{0x01}}, + } + + bundleBytes, err := json.Marshal(bundle) + require.NoError(t, err) + + calldata, err := unmarshalBundleAbi.Inputs.Pack(bundleBytes) + require.NoError(t, err) + + decodedBundleReturn, err := clt.RPC().CallContract(context.TODO(), ethereum.CallMsg{ + To: &unmarshalBundleAddr, + Data: calldata, + }, nil) + require.NoError(t, err) + + unpackedDecodedBundleReturn, err := unmarshalBundleAbi.Outputs.Unpack(decodedBundleReturn) + require.NoError(t, err) + + recoveredDecodedBundle := unpackedDecodedBundleReturn[0].(struct { + BlockNumber uint64 "json:\"blockNumber\"" + Txs [][]uint8 "json:\"txs\"" + RevertingHashes [][32]uint8 "json:\"revertingHashes\"" + }) + + require.Equal(t, uint64(100), recoveredDecodedBundle.BlockNumber) + require.Equal(t, 1, len(recoveredDecodedBundle.Txs)) + require.Equal(t, txBytes, recoveredDecodedBundle.Txs[0]) + require.Equal(t, 1, len(recoveredDecodedBundle.RevertingHashes)) + require.Equal(t, [32]uint8{1}, recoveredDecodedBundle.RevertingHashes[0]) + + encodedBundleReturn, err := clt.RPC().CallContract(context.TODO(), ethereum.CallMsg{ + To: &marshalBundleAddr, + Data: decodedBundleReturn, + }, nil) + require.NoError(t, err) + + recoveredBundle := &types.SBundle{} + require.NoError(t, json.Unmarshal(encodedBundleReturn, &recoveredBundle)) + + require.Equal(t, 1, len(recoveredBundle.Txs)) + marshaledRecoveredTx, err := recoveredBundle.Txs[0].MarshalBinary() + require.NoError(t, err) + + require.Equal(t, txBytes, marshaledRecoveredTx) + require.Equal(t, big.NewInt(100), recoveredBundle.BlockNumber) + require.Equal(t, []common.Hash{common.Hash{0x01}}, recoveredBundle.RevertingHashes) +} + func TestMempool(t *testing.T) { // t.Fatal("not implemented") fr := newFramework(t) diff --git a/suave/gen/suave_spec.yaml b/suave/gen/suave_spec.yaml index 747c29688..3275a5c5b 100644 --- a/suave/gen/suave_spec.yaml +++ b/suave/gen/suave_spec.yaml @@ -181,7 +181,7 @@ functions: - name: output1 type: bytes - name: unmarshalBundle - address: "0x0000000000000000000000000000000030300010" + address: "0x0000000000000000000000000000000030300011" input: - name: bundle type: bytes diff --git a/suave/sol/libraries/Suave.sol b/suave/sol/libraries/Suave.sol index 02a891fb6..c130ee76a 100644 --- a/suave/sol/libraries/Suave.sol +++ b/suave/sol/libraries/Suave.sol @@ -99,7 +99,7 @@ library Suave { address public constant SUBMIT_ETH_BLOCK_BID_TO_RELAY = 0x0000000000000000000000000000000042100002; - address public constant UNMARSHAL_BUNDLE = 0x0000000000000000000000000000000030300010; + address public constant UNMARSHAL_BUNDLE = 0x0000000000000000000000000000000030300011; // Returns whether execution is off- or on-chain function isConfidential() internal view returns (bool b) { @@ -168,7 +168,7 @@ library Suave { revert PeekerReverted(ENCODE_TRANSACTION, data); } - return abi.decode(data, (bytes)); + return data; } function ethcall(address contractAddr, bytes memory input1) internal view returns (bytes memory) { @@ -215,7 +215,7 @@ library Suave { revert PeekerReverted(MARSHAL_BUNDLE, data); } - return abi.decode(data, (bytes)); + return data; } function newBid( diff --git a/suave/sol/libraries/SuaveForge.sol b/suave/sol/libraries/SuaveForge.sol index 2ecdb7355..39542d4ff 100644 --- a/suave/sol/libraries/SuaveForge.sol +++ b/suave/sol/libraries/SuaveForge.sol @@ -72,7 +72,7 @@ library SuaveForge { function encodeTransaction(Suave.TransactionArgs memory txn) internal view returns (bytes memory) { bytes memory data = forgeIt("0x0000000000000000000000000000000030300000", abi.encode(txn)); - return abi.decode(data, (bytes)); + return data; } function ethcall(address contractAddr, bytes memory input1) internal view returns (bytes memory) { @@ -102,7 +102,7 @@ library SuaveForge { function marshalBundle(Suave.Bundle memory bundle) internal view returns (bytes memory) { bytes memory data = forgeIt("0x0000000000000000000000000000000030300010", abi.encode(bundle)); - return abi.decode(data, (bytes)); + return data; } function newBid( @@ -156,7 +156,7 @@ library SuaveForge { } function unmarshalBundle(bytes memory bundle) internal view returns (Suave.Bundle memory) { - bytes memory data = forgeIt("0x0000000000000000000000000000000030300010", abi.encode(bundle)); + bytes memory data = forgeIt("0x0000000000000000000000000000000030300011", abi.encode(bundle)); return abi.decode(data, (Suave.Bundle)); }