Skip to content

Commit

Permalink
Features and fixes needed for contract deployment test (#13)
Browse files Browse the repository at this point in the history
* add and remove tx from geth mempool so forge can deploy a contract

* pass metro addr and port by flag

* fixes from pr feedback

* formatting
  • Loading branch information
steezeburger committed Apr 20, 2023
1 parent e19690c commit c2d33eb
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ var (
utils.GRPCPortFlag,
}

metroFlags = []cli.Flag{
utils.MetroGRPCHostFlag,
utils.MetroGRPCPortFlag,
}

metricsFlags = []cli.Flag{
utils.MetricsEnabledFlag,
utils.MetricsEnabledExpensiveFlag,
Expand Down Expand Up @@ -248,6 +253,7 @@ func init() {
app.Flags = flags.Merge(
nodeFlags,
rpcFlags,
metroFlags,
consoleFlags,
debug.Flags,
metricsFlags,
Expand Down
22 changes: 22 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,20 @@ var (
Category: flags.APICategory,
}

// Metro grpc address and port overrides
MetroGRPCHostFlag = &cli.StringFlag{
Name: "metro.addr",
Usage: "Metro gRPC server listening interface",
Value: ethconfig.Defaults.MetroGRPCHost,
Category: flags.APICategory,
}
MetroGRPCPortFlag = &cli.IntFlag{
Name: "metro.port",
Usage: "Metro gRPC server listening port",
Value: ethconfig.Defaults.MetroGRPCPort,
Category: flags.APICategory,
}

// Network Settings
MaxPeersFlag = &cli.IntFlag{
Name: "maxpeers",
Expand Down Expand Up @@ -1891,6 +1905,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
}

// metro
if ctx.IsSet(MetroGRPCHostFlag.Name) {
cfg.MetroGRPCHost = ctx.String(MetroGRPCHostFlag.Name)
}
if ctx.IsSet(MetroGRPCPortFlag.Name) {
cfg.MetroGRPCPort = ctx.Int(MetroGRPCPortFlag.Name)
}

// Override any default configs for hard coded networks.
switch {
case ctx.Bool(MainnetFlag.Name):
Expand Down
8 changes: 8 additions & 0 deletions core/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,14 @@ func (pool *TxPool) AddRemotesSync(txs []*types.Transaction) []error {
return pool.addTxs(txs, false, true)
}

// Remove a single transaction from the mempool.
func (pool *TxPool) RemoveTx(hash common.Hash) {
pool.mu.Lock()
defer pool.mu.Unlock()

pool.removeTx(hash, false)
}

// This is like AddRemotes with a single transaction, but waits for pool reorganization. Tests use this method.
func (pool *TxPool) addRemoteSync(tx *types.Transaction) error {
errs := pool.AddRemotesSync([]*types.Transaction{tx})
Expand Down
5 changes: 5 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package eth
import (
"context"
"errors"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -351,6 +352,10 @@ func (b *EthAPIBackend) UnprotectedAllowed() bool {
return b.allowUnprotectedTxs
}

func (b *EthAPIBackend) MetroGRPCEndpoint() string {
return fmt.Sprintf("%s:%d", b.eth.config.MetroGRPCHost, b.eth.config.MetroGRPCPort)
}

func (b *EthAPIBackend) RPCGasCap() uint64 {
return b.eth.config.RPCGasCap
}
Expand Down
8 changes: 8 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ var Defaults = Config{
RPCEVMTimeout: 5 * time.Second,
GPO: FullNodeGPO,
RPCTxFeeCap: 1, // 1 ether

// Metro GRPC
MetroGRPCHost: "localhost",
MetroGRPCPort: 9090,
}

func init() {
Expand Down Expand Up @@ -207,6 +211,10 @@ type Config struct {

// OverrideShanghai (TODO: remove after the fork)
OverrideShanghai *uint64 `toml:",omitempty"`

// Metro GRPC Host and Port
MetroGRPCHost string `toml:",omitempty"`
MetroGRPCPort int `toml:",omitempty"`
}

// CreateConsensusEngine creates a consensus engine for the given chain configuration.
Expand Down
5 changes: 5 additions & 0 deletions grpc/execution/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (s *ExecutionServiceServer) DoBlock(ctx context.Context, req *executionv1.D
return nil, fmt.Errorf("failed to insert block into blockchain (n=%d)", n)
}

// remove txs from original mempool
for _, tx := range block.Transactions() {
s.eth.TxPool().RemoveTx(tx.Hash())
}

newForkChoice := &engine.ForkchoiceStateV1{
HeadBlockHash: block.Hash(),
SafeBlockHash: block.Hash(),
Expand Down
9 changes: 8 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1694,12 +1694,19 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
}

// save transaction in geth mempool as well, so things like forge can look it up
if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err
}

// send to metro instead of eth mempool
txBytes, err := tx.MarshalBinary()
if err != nil {
return common.Hash{}, err
}
if err := submitMetroTransaction(txBytes); err != nil {

metroAPI := NewMetroAPI(b.MetroGRPCEndpoint())
if err := metroAPI.SubmitTransaction(txBytes); err != nil {
return common.Hash{}, err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ type Backend interface {
SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
BloomStatus() (uint64, uint64)
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)

// Metro GRPC endpoint
MetroGRPCEndpoint() string
}

func GetAPIs(apiBackend Backend) []rpc.API {
Expand Down
14 changes: 12 additions & 2 deletions internal/ethapi/metro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ package ethapi

import (
metrotx "github.com/astriaorg/metro-transactions/tx"
"github.com/ethereum/go-ethereum/log"
)

const secondaryChainID = "ethereum"

func submitMetroTransaction(tx []byte) error {
return metrotx.BuildAndSendSecondaryTransaction(metrotx.DefaultGRPCEndpoint, secondaryChainID, tx)
type MetroAPI struct {
endpoint string
}

func NewMetroAPI(endpoint string) *MetroAPI {
log.Info("NewMetroAPI", "endpoint", endpoint)
return &MetroAPI{endpoint: endpoint}
}

func (api *MetroAPI) SubmitTransaction(tx []byte) error {
return metrotx.BuildAndSendSecondaryTransaction(api.endpoint, secondaryChainID, tx)
}
1 change: 1 addition & 0 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func (b *backendMock) FeeHistory(ctx context.Context, blockCount uint64, lastBlo
func (b *backendMock) ChainDb() ethdb.Database { return nil }
func (b *backendMock) AccountManager() *accounts.Manager { return nil }
func (b *backendMock) ExtRPCEnabled() bool { return false }
func (b *backendMock) MetroGRPCEndpoint() string { return "" }
func (b *backendMock) RPCGasCap() uint64 { return 0 }
func (b *backendMock) RPCEVMTimeout() time.Duration { return time.Second }
func (b *backendMock) RPCTxFeeCap() float64 { return 0 }
Expand Down
5 changes: 5 additions & 0 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package les
import (
"context"
"errors"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -292,6 +293,10 @@ func (b *LesApiBackend) UnprotectedAllowed() bool {
return b.allowUnprotectedTxs
}

func (b *LesApiBackend) MetroGRPCEndpoint() string {
return fmt.Sprintf("%s:%d", b.eth.config.MetroGRPCHost, b.eth.config.MetroGRPCPort)
}

func (b *LesApiBackend) RPCGasCap() uint64 {
return b.eth.config.RPCGasCap
}
Expand Down

0 comments on commit c2d33eb

Please sign in to comment.