Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
ree
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Oct 6, 2023
1 parent 3d7705a commit b03859d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 132 deletions.
8 changes: 5 additions & 3 deletions cosmos/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/miner"

Expand All @@ -41,7 +42,7 @@ var emptyHash = common.Hash{}
// Miner implements the baseapp.TxSelector interface.
type Miner struct {
*miner.Miner
serializer evmtypes.TxSerializer
serializer evmtypes.TxSerializer[*engine.ExecutionPayloadEnvelope]
currentPayload *miner.Payload
}

Expand All @@ -53,7 +54,7 @@ func New(gm *miner.Miner) *Miner {
}

// Init sets the transaction serializer.
func (m *Miner) Init(serializer evmtypes.TxSerializer) {
func (m *Miner) Init(serializer evmtypes.TxSerializer[*engine.ExecutionPayloadEnvelope]) {
m.serializer = serializer
}

Expand Down Expand Up @@ -113,7 +114,8 @@ func (m *Miner) resolveEnvelope() []byte {
if m.currentPayload == nil {
return nil
}
bz, err := m.serializer.PayloadToSdkTxBytes(m.currentPayload.ResolveFull())
envelope := m.currentPayload.ResolveFull()
bz, err := m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cosmos/txpool/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type TxSubProvider interface {

// TxSerializer provides an interface to Serialize Geth Transactions to Bytes (via sdk.Tx).
type TxSerializer interface {
TxToSdkTx(signedTx *coretypes.Transaction) (sdk.Tx, error)
TxToSdkTxBytes(signedTx *coretypes.Transaction) ([]byte, error)
ToSdkTx(signedTx *coretypes.Transaction, gasLimit uint64) (sdk.Tx, error)
ToSdkTxBytes(signedTx *coretypes.Transaction, gasLimit uint64) ([]byte, error)
}

// TxBroadcaster provides an interface to broadcast TxBytes to the comet p2p layer.
Expand Down Expand Up @@ -163,7 +163,7 @@ func (h *handler) broadcastTransactions(txs coretypes.Transactions) {
h.logger.Debug("broadcasting transactions", "num_txs", len(txs))
for _, signedEthTx := range txs {
// Serialize the transaction to Bytes
txBytes, err := h.serializer.TxToSdkTxBytes(signedEthTx)
txBytes, err := h.serializer.ToSdkTxBytes(signedEthTx, signedEthTx.Gas())
if err != nil {
h.logger.Error("failed to serialize transaction", "err", err)
continue
Expand Down
82 changes: 42 additions & 40 deletions cosmos/txpool/mocks/tx_serializer.go

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

102 changes: 20 additions & 82 deletions cosmos/x/evm/types/tx_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,43 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"

"github.com/ethereum/go-ethereum/beacon/engine"

coretypes "pkg.berachain.dev/polaris/eth/core/types"
)

// TxSerializer provides an interface to serialize ethereum transactions
// to sdk.Tx's and bytes that can be used by CometBFT.
type TxSerializer interface {
TxToSdkTx(signedTx *coretypes.Transaction) (sdk.Tx, error)
TxToSdkTxBytes(signedTx *coretypes.Transaction) ([]byte, error)
PayloadToSdkTxBytes(payload *engine.ExecutionPayloadEnvelope) ([]byte, error)
PayloadToSdkTx(payload *engine.ExecutionPayloadEnvelope) (sdk.Tx, error)
type TxSerializer[I any] interface {
ToSdkTx(input I, gasLimit uint64) (sdk.Tx, error)
ToSdkTxBytes(input I, gasLimit uint64) ([]byte, error)
}

// serializer implements TxSerializer.
type serializer struct {
type serializer[I any, O sdk.Msg] struct {
txConfig client.TxConfig
wrapFn func(I) (O, error)
}

// NewSerializer returns a new instance of TxSerializer.
func NewSerializer(txConfig client.TxConfig) TxSerializer {
return &serializer{
func NewSerializer[I any, O sdk.Msg](
txConfig client.TxConfig, wrapFn func(I) (O, error),
) TxSerializer[I] {
return &serializer[I, O]{
txConfig: txConfig,
wrapFn: wrapFn,
}
}

// SerializeToBytes converts an Ethereum transaction to Cosmos formatted
// txBytes which allows for it to broadcast it to CometBFT.
func (s *serializer) PayloadToSdkTxBytes(
payload *engine.ExecutionPayloadEnvelope,
) ([]byte, error) {
// First, we convert the Ethereum transaction to a Cosmos transaction.
cosmosTx, err := s.PayloadToSdkTx(payload)
if err != nil {
return nil, err
}

// Then we use the clientCtx.TxConfig.TxEncoder() to encode the Cosmos transaction into bytes.
return s.txConfig.TxEncoder()(cosmosTx)
}

// PayloadToSdkTx converts an ExecutionPayloadEnvelope to an sdk.Tx.
func (s *serializer) PayloadToSdkTx(payload *engine.ExecutionPayloadEnvelope) (sdk.Tx, error) {
func (s *serializer[I, O]) ToSdkTx(input I, gasLimit uint64) (sdk.Tx, error) {
var err error
// TODO: do we really need to use extensions for anything? Since we
// are using the standard ante handler stuff I don't think we actually need to.
tx := s.txConfig.NewTxBuilder()

// Set the tx gas limit to the block gas limit in the payload
tx.SetGasLimit(payload.ExecutionPayload.GasLimit)
tx.SetGasLimit(gasLimit)

bz, err := payload.MarshalJSON()
wrapped, err := s.wrapFn(input)
if err != nil {
return nil, err
}

wp := &WrappedPayloadEnvelope{
Data: bz,
}

// Lastly, we set the signature. We can pull the sequence from the nonce of the ethereum tx.
if err = tx.SetSignatures(
signingtypes.SignatureV2{
Expand All @@ -104,59 +80,21 @@ func (s *serializer) PayloadToSdkTx(payload *engine.ExecutionPayloadEnvelope) (s
}

// Lastly, we inject the signed ethereum transaction as a message into the Cosmos Tx.
if err = tx.SetMsgs(wp); err != nil {
return nil, err
}

// Finally, we return the Cosmos Tx.
return tx.GetTx(), nil
}

// TxToSdkTx converts an ethereum transaction to a Cosmos native transaction.
func (s *serializer) TxToSdkTx(signedTx *coretypes.Transaction) (sdk.Tx, error) {
var err error
// are using the standard ante handler stuff I don't think we actually need to.
tx := s.txConfig.NewTxBuilder()

// Create the WrappedEthereumTransaction message.
wrappedEthTx, err := WrapTx(signedTx)
if err != nil {
return nil, err
}

// Lastly, we set the signature. We can pull the sequence from the nonce of the ethereum tx.
if err = tx.SetSignatures(
signingtypes.SignatureV2{
Sequence: signedTx.Nonce(),
Data: &signingtypes.SingleSignatureData{
// We retrieve the hash of the signed transaction from the ethereum transaction
// objects, as this was the bytes that were signed. We pass these into the
// SingleSignatureData as the SignModeHandler needs to know what data was signed
// over so that it can verify the signature in the ante handler.
Signature: []byte{0x0},
},
PubKey: &secp256k1.PubKey{Key: []byte{0x0}},
},
); err != nil {
return nil, err
}

// Lastly, we inject the signed ethereum transaction as a message into the Cosmos Tx.
if err = tx.SetMsgs(wrappedEthTx); err != nil {
if err = tx.SetMsgs(wrapped); err != nil {
return nil, err
}

// Finally, we return the Cosmos Tx.
return tx.GetTx(), nil
}

// SerializeToBytes converts an Ethereum transaction to Cosmos formatted txBytes which allows for
// it to broadcast it to CometBFT.
// func
func (s *serializer) TxToSdkTxBytes(signedTx *coretypes.Transaction) ([]byte, error) {
// SerializeToBytes converts an Ethereum transaction to Cosmos formatted
// txBytes which allows for it to broadcast it to CometBFT.
func (s *serializer[I, O]) ToSdkTxBytes(
input I, gasLimit uint64,
) ([]byte, error) {
// First, we convert the Ethereum transaction to a Cosmos transaction.
// func(I any) sdk.Tx, error)
cosmosTx, err := s.TxToSdkTx(signedTx) ///
cosmosTx, err := s.ToSdkTx(input, gasLimit)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions e2e/testapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,13 @@ func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICon
panic(err)
}

// Create a TxSerializer.
serializer := evmtypes.NewSerializer(apiSvr.ClientCtx.TxConfig)
// Create the Serializers.
txSerializer := evmtypes.NewSerializer(apiSvr.ClientCtx.TxConfig, evmtypes.WrapTx)
payloadSerializer := evmtypes.NewSerializer(apiSvr.ClientCtx.TxConfig, evmtypes.WrapPayload)

// Initialize services.
app.mm.Init(serializer)
app.mp.Init(app.Logger(), apiSvr.ClientCtx, serializer)
app.mm.Init(payloadSerializer)
app.mp.Init(app.Logger(), apiSvr.ClientCtx, txSerializer)

// Register services with Polaris.
app.EVMKeeper.RegisterServices(apiSvr.ClientCtx, []node.Lifecycle{
Expand Down

0 comments on commit b03859d

Please sign in to comment.