2021-08-14 13:47:11 +00:00
package main
import (
"fmt"
"os"
"path"
2021-09-08 05:31:51 +00:00
"time"
2021-08-14 13:47:11 +00:00
"github.com/ledgerwatch/erigon-lib/direct"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
2021-09-03 04:19:35 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/grpcutil"
2021-08-14 13:47:11 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
proto_sentry "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
2021-09-13 07:58:25 +00:00
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
2021-08-17 08:52:55 +00:00
"github.com/ledgerwatch/erigon-lib/kv/remotedb"
2021-09-15 07:22:57 +00:00
"github.com/ledgerwatch/erigon-lib/kv/remotedbserver"
2021-08-14 13:47:11 +00:00
"github.com/ledgerwatch/erigon-lib/txpool"
2021-09-08 05:31:51 +00:00
"github.com/ledgerwatch/erigon-lib/txpool/txpooluitl"
2021-09-15 02:09:19 +00:00
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
2021-08-14 13:47:11 +00:00
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/common/paths"
2021-09-15 02:09:19 +00:00
"github.com/ledgerwatch/erigon/ethdb/privateapi"
2021-08-14 13:47:11 +00:00
"github.com/ledgerwatch/erigon/internal/debug"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
)
var (
sentryAddr [ ] string // Address of the sentry <host>:<port>
privateApiAddr string
2021-09-02 05:55:04 +00:00
txpoolApiAddr string
2021-08-14 13:47:11 +00:00
datadir string // Path to td working dir
TLSCertfile string
TLSCACert string
TLSKeyFile string
2021-09-29 07:48:19 +00:00
pendingPoolLimit int
baseFeePoolLimit int
queuedPoolLimit int
2021-10-07 12:02:18 +00:00
priceLimit uint64
2021-08-14 13:47:11 +00:00
)
func init ( ) {
utils . CobraFlags ( rootCmd , append ( debug . Flags , utils . MetricFlags ... ) )
rootCmd . Flags ( ) . StringSliceVar ( & sentryAddr , "sentry.api.addr" , [ ] string { "localhost:9091" } , "comma separated sentry addresses '<host>:<port>,<host>:<port>'" )
2021-09-02 05:55:04 +00:00
rootCmd . Flags ( ) . StringVar ( & privateApiAddr , "private.api.addr" , "localhost:9090" , "execution service <host>:<port>" )
rootCmd . Flags ( ) . StringVar ( & txpoolApiAddr , "txpool.api.addr" , "localhost:9094" , "txpool service <host>:<port>" )
2021-08-14 13:47:11 +00:00
rootCmd . Flags ( ) . StringVar ( & datadir , utils . DataDirFlag . Name , paths . DefaultDataDir ( ) , utils . DataDirFlag . Usage )
if err := rootCmd . MarkFlagDirname ( utils . DataDirFlag . Name ) ; err != nil {
panic ( err )
}
rootCmd . PersistentFlags ( ) . StringVar ( & TLSCertfile , "tls.cert" , "" , "certificate for client side TLS handshake" )
rootCmd . PersistentFlags ( ) . StringVar ( & TLSKeyFile , "tls.key" , "" , "key file for client side TLS handshake" )
rootCmd . PersistentFlags ( ) . StringVar ( & TLSCACert , "tls.cacert" , "" , "CA certificate for client side TLS handshake" )
2021-09-29 07:48:19 +00:00
rootCmd . PersistentFlags ( ) . IntVar ( & pendingPoolLimit , "txpool.globalslots" , txpool . DefaultConfig . PendingSubPoolLimit , "Maximum number of executable transaction slots for all accounts" )
2021-09-29 07:55:43 +00:00
rootCmd . PersistentFlags ( ) . IntVar ( & baseFeePoolLimit , "txpool.globalbasefeeeslots" , txpool . DefaultConfig . BaseFeeSubPoolLimit , "Maximum number of non-executable transactions where only not enough baseFee" )
2021-09-29 07:48:19 +00:00
rootCmd . PersistentFlags ( ) . IntVar ( & queuedPoolLimit , "txpool.globalqueue" , txpool . DefaultConfig . QueuedSubPoolLimit , "Maximum number of non-executable transaction slots for all accounts" )
2021-10-07 12:02:18 +00:00
rootCmd . PersistentFlags ( ) . Uint64Var ( & priceLimit , "txpool.pricelimit" , txpool . DefaultConfig . MinFeeCap , "Minimum gas price (fee cap) limit to enforce for acceptance into the pool" )
rootCmd . PersistentFlags ( ) . Uint64Var ( & priceLimit , "txpool.accountslots" , txpool . DefaultConfig . AccountSlots , "Minimum number of executable transaction slots guaranteed per account" )
2021-08-14 13:47:11 +00:00
}
var rootCmd = & cobra . Command {
Use : "sentry" ,
Short : "Run p2p sentry" ,
PersistentPreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
return debug . SetupCobra ( cmd )
} ,
PersistentPostRun : func ( cmd * cobra . Command , args [ ] string ) {
debug . Exit ( )
} ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2021-09-08 05:31:51 +00:00
ctx := cmd . Context ( )
2021-09-03 04:19:35 +00:00
creds , err := grpcutil . TLS ( TLSCACert , TLSCertfile , TLSKeyFile )
if err != nil {
return fmt . Errorf ( "could not connect to remoteKv: %w" , err )
}
coreConn , err := grpcutil . Connect ( creds , privateApiAddr )
2021-08-14 13:47:11 +00:00
if err != nil {
return fmt . Errorf ( "could not connect to remoteKv: %w" , err )
}
kvClient := remote . NewKVClient ( coreConn )
coreDB , err := remotedb . NewRemote ( gointerfaces . VersionFromProto ( remotedbserver . KvServiceAPIVersion ) , log . New ( ) , kvClient ) . Open ( )
if err != nil {
return fmt . Errorf ( "could not connect to remoteKv: %w" , err )
}
2021-08-27 04:51:33 +00:00
log . Info ( "TxPool started" , "db" , path . Join ( datadir , "txpool" ) )
2021-08-14 13:47:11 +00:00
2021-09-08 05:31:51 +00:00
sentryClients := make ( [ ] direct . SentryClient , len ( sentryAddr ) )
2021-08-14 13:47:11 +00:00
for i := range sentryAddr {
2021-09-03 04:19:35 +00:00
creds , err := grpcutil . TLS ( TLSCACert , TLSCertfile , TLSKeyFile )
if err != nil {
return fmt . Errorf ( "could not connect to sentry: %w" , err )
}
sentryConn , err := grpcutil . Connect ( creds , sentryAddr [ i ] )
2021-08-14 13:47:11 +00:00
if err != nil {
return fmt . Errorf ( "could not connect to sentry: %w" , err )
}
sentryClients [ i ] = direct . NewSentryClientRemote ( proto_sentry . NewSentryClient ( sentryConn ) )
}
2021-09-08 05:31:51 +00:00
cfg := txpool . DefaultConfig
cfg . DBDir = path . Join ( datadir , "txpool" )
2021-09-15 02:09:19 +00:00
cfg . LogEvery = 30 * time . Second
cfg . CommitEvery = 30 * time . Second
2021-09-29 07:48:19 +00:00
cfg . PendingSubPoolLimit = pendingPoolLimit
cfg . BaseFeeSubPoolLimit = baseFeePoolLimit
cfg . QueuedSubPoolLimit = queuedPoolLimit
2021-10-07 12:02:18 +00:00
cfg . MinFeeCap = priceLimit
2021-09-08 05:31:51 +00:00
2021-09-29 01:36:25 +00:00
cacheConfig := kvcache . DefaultCoherentConfig
2021-09-13 07:58:25 +00:00
cacheConfig . MetricsLabel = "txpool"
2021-08-21 15:04:24 +00:00
newTxs := make ( chan txpool . Hashes , 1024 )
2021-09-08 05:31:51 +00:00
defer close ( newTxs )
2021-09-13 07:58:25 +00:00
txPoolDB , txPool , fetch , send , txpoolGrpcServer , err := txpooluitl . AllComponents ( ctx , cfg ,
kvcache . New ( cacheConfig ) , newTxs , coreDB , sentryClients , kvClient )
2021-08-14 13:47:11 +00:00
if err != nil {
return err
}
2021-09-08 05:31:51 +00:00
fetch . ConnectCore ( )
fetch . ConnectSentries ( )
2021-08-14 13:47:11 +00:00
2021-09-03 04:19:35 +00:00
/ *
var ethashApi * ethash . API
2021-09-29 01:36:25 +00:00
sif casted , ok := backend . engine . ( * ethash . Ethash ) ; ok {
2021-09-03 04:19:35 +00:00
ethashApi = casted . APIs ( nil ) [ 1 ] . Service . ( * ethash . API )
}
* /
2021-09-15 02:09:19 +00:00
miningGrpcServer := privateapi . NewMiningServer ( cmd . Context ( ) , & rpcdaemontest . IsMiningMock { } , nil )
2021-09-03 04:19:35 +00:00
2021-09-15 02:09:19 +00:00
grpcServer , err := txpool . StartGrpc ( txpoolGrpcServer , miningGrpcServer , txpoolApiAddr , nil )
2021-09-02 05:55:04 +00:00
if err != nil {
return err
}
2021-09-08 05:31:51 +00:00
notifyMiner := func ( ) { }
txpool . MainLoop ( cmd . Context ( ) , txPoolDB , coreDB , txPool , newTxs , send , txpoolGrpcServer . NewSlotsStreams , notifyMiner )
2021-08-14 13:47:11 +00:00
2021-09-02 05:55:04 +00:00
grpcServer . GracefulStop ( )
2021-08-14 13:47:11 +00:00
return nil
} ,
}
func main ( ) {
ctx , cancel := utils . RootContext ( )
defer cancel ( )
if err := rootCmd . ExecuteContext ( ctx ) ; err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
}