Propagation

Automatically propagate transactions with Mevlink

Now that we've added Mevlink to the Eth Backend, we can modify eth/api_backend.go so that any SendTx() calls automatically get propagated to the Mevlink service.

Imports

First we have to add a couple imports:

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"

SendTx()

Then in the SendTx definition we can add a call to b.eth.mlstream.EmitTransaction(). SendTx should look like the following:

func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
	go func() {
		rawTx, err := rlp.EncodeToBytes(signedTx)
		if err == nil {
			b.eth.mlstream.EmitTransaction(rawTx)
		} else {
			log.Error("error encoding tx for mevlink", "err", err)
		}
	}()
	return b.eth.txPool.AddLocal(signedTx)
}

eth/api_backend.go modifications

// eth/api_backend.go

// ...

import (
	"context"
	"errors"
	"math/big"
	"time"

	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/consensus"
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/bloombits"
	"github.com/ethereum/go-ethereum/core/rawdb"
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/txpool"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/core/vm"
	"github.com/ethereum/go-ethereum/eth/gasprice"
	"github.com/ethereum/go-ethereum/eth/tracers"
	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/event"
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/miner"
	"github.com/ethereum/go-ethereum/params"
	"github.com/ethereum/go-ethereum/rlp"
	"github.com/ethereum/go-ethereum/rpc"
)

// ...

func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
	go func() {
		rawTx, err := rlp.EncodeToBytes(signedTx)
		if err == nil {
			b.eth.mlstream.EmitTransaction(rawTx)
		} else {
			log.Error("error encoding tx for mevlink", "err", err)
		}
		b.eth.handler.BroadcastTransactions([]*types.Transaction{signedTx}, true)
	}()
	return b.eth.txPool.AddLocal(signedTx)
}

// ...

Last updated