Skip to content

Commit

Permalink
Adds bundle marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruteri committed Nov 9, 2023
1 parent 26ac6f9 commit 585d52e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/types/suave_structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion core/vm/contracts_suave_marshaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Check failure on line 251 in core/vm/contracts_suave_marshaling.go

View workflow job for this annotation

GitHub Actions / Lint and test

unnecessary conversion (unconvert)
RevertingHashes: revertingHashes,
})
if err != nil {
return nil, err
}
Expand Down
71 changes: 71 additions & 0 deletions core/vm/contracts_suave_marshaling_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"encoding/json"
"math/big"
"testing"

Expand Down Expand Up @@ -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])
}
16 changes: 4 additions & 12 deletions core/vm/contracts_suave_runtime_adapter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions suave/artifacts/addresses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions suave/e2e/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion suave/gen/suave_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ functions:
- name: output1
type: bytes
- name: unmarshalBundle
address: "0x0000000000000000000000000000000030300010"
address: "0x0000000000000000000000000000000030300011"
input:
- name: bundle
type: bytes
Expand Down
6 changes: 3 additions & 3 deletions suave/sol/libraries/Suave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -215,7 +215,7 @@ library Suave {
revert PeekerReverted(MARSHAL_BUNDLE, data);
}

return abi.decode(data, (bytes));
return data;
}

function newBid(
Expand Down
6 changes: 3 additions & 3 deletions suave/sol/libraries/SuaveForge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 585d52e

Please sign in to comment.