github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/config.go (about)

     1  package gossip
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/syndtr/goleveldb/leveldb/opt"
     9  	"github.com/unicornultrafoundation/go-helios/gossip/dagprocessor"
    10  	"github.com/unicornultrafoundation/go-helios/gossip/itemsfetcher"
    11  	"github.com/unicornultrafoundation/go-helios/native/dag"
    12  	"github.com/unicornultrafoundation/go-helios/native/idx"
    13  	"github.com/unicornultrafoundation/go-helios/utils/cachescale"
    14  
    15  	"github.com/unicornultrafoundation/go-u2u/eventcheck/heavycheck"
    16  	"github.com/unicornultrafoundation/go-u2u/gossip/evmstore"
    17  	"github.com/unicornultrafoundation/go-u2u/gossip/filters"
    18  	"github.com/unicornultrafoundation/go-u2u/gossip/gasprice"
    19  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockrecords/brprocessor"
    20  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockrecords/brstream/brstreamleecher"
    21  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockrecords/brstream/brstreamseeder"
    22  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockvotes/bvprocessor"
    23  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockvotes/bvstream/bvstreamleecher"
    24  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/blockvotes/bvstream/bvstreamseeder"
    25  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/dag/dagstream/dagstreamleecher"
    26  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/dag/dagstream/dagstreamseeder"
    27  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/epochpacks/epprocessor"
    28  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/epochpacks/epstream/epstreamleecher"
    29  	"github.com/unicornultrafoundation/go-u2u/gossip/protocols/epochpacks/epstream/epstreamseeder"
    30  )
    31  
    32  const nominalSize uint = 1
    33  
    34  type (
    35  	// ProtocolConfig is config for p2p protocol
    36  	ProtocolConfig struct {
    37  		// 0/M means "optimize only for throughput", N/0 means "optimize only for latency", N/M is a balanced mode
    38  
    39  		LatencyImportance    int
    40  		ThroughputImportance int
    41  
    42  		EventsSemaphoreLimit dag.Metric
    43  		BVsSemaphoreLimit    dag.Metric
    44  		MsgsSemaphoreLimit   dag.Metric
    45  		MsgsSemaphoreTimeout time.Duration
    46  
    47  		ProgressBroadcastPeriod time.Duration
    48  
    49  		DagProcessor dagprocessor.Config
    50  		BvProcessor  bvprocessor.Config
    51  		BrProcessor  brprocessor.Config
    52  		EpProcessor  epprocessor.Config
    53  
    54  		DagFetcher       itemsfetcher.Config
    55  		TxFetcher        itemsfetcher.Config
    56  		DagStreamLeecher dagstreamleecher.Config
    57  		DagStreamSeeder  dagstreamseeder.Config
    58  		BvStreamLeecher  bvstreamleecher.Config
    59  		BvStreamSeeder   bvstreamseeder.Config
    60  		BrStreamLeecher  brstreamleecher.Config
    61  		BrStreamSeeder   brstreamseeder.Config
    62  		EpStreamLeecher  epstreamleecher.Config
    63  		EpStreamSeeder   epstreamseeder.Config
    64  
    65  		MaxInitialTxHashesSend   int
    66  		MaxRandomTxHashesSend    int
    67  		RandomTxHashesSendPeriod time.Duration
    68  
    69  		PeerCache PeerCacheConfig
    70  	}
    71  
    72  	// Config for the gossip service.
    73  	Config struct {
    74  		FilterAPI filters.Config
    75  
    76  		// This can be set to list of enrtree:// URLs which will be queried for
    77  		// for nodes to connect to.
    78  		U2UDiscoveryURLs  []string
    79  		SnapDiscoveryURLs []string
    80  
    81  		AllowSnapsync bool
    82  
    83  		TxIndex bool // Whether to enable indexing transactions and receipts or not
    84  
    85  		// Protocol options
    86  		Protocol ProtocolConfig
    87  
    88  		HeavyCheck heavycheck.Config
    89  
    90  		// Gas Price Oracle options
    91  		GPO gasprice.Config
    92  
    93  		// RPCGasCap is the global gas cap for eth-call variants.
    94  		RPCGasCap uint64 `toml:",omitempty"`
    95  
    96  		// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
    97  		// send-transction variants. The unit is ether.
    98  		RPCTxFeeCap float64 `toml:",omitempty"`
    99  
   100  		// RPCTimeout is a global time limit for RPC methods execution.
   101  		RPCTimeout time.Duration
   102  
   103  		// allows only for EIP155 transactions.
   104  		AllowUnprotectedTxs bool
   105  
   106  		RPCBlockExt bool
   107  	}
   108  
   109  	StoreCacheConfig struct {
   110  		// Cache size for full events.
   111  		EventsNum  int
   112  		EventsSize uint
   113  		// Cache size for event IDs
   114  		EventsIDsNum int
   115  		// Cache size for full blocks.
   116  		BlocksNum  int
   117  		BlocksSize uint
   118  		// Cache size for history block/epoch states.
   119  		BlockEpochStateNum int
   120  
   121  		LlrBlockVotesIndexes int
   122  		LlrEpochVotesIndexes int
   123  	}
   124  
   125  	// StoreConfig is a config for store db.
   126  	StoreConfig struct {
   127  		Cache StoreCacheConfig
   128  		// EVM is EVM store config
   129  		EVM                 evmstore.StoreConfig
   130  		MaxNonFlushedSize   int
   131  		MaxNonFlushedPeriod time.Duration
   132  		TraceTransactions   bool
   133  	}
   134  )
   135  
   136  type PeerCacheConfig struct {
   137  	MaxKnownTxs    int // Maximum transactions hashes to keep in the known list (prevent DOS)
   138  	MaxKnownEvents int // Maximum event hashes to keep in the known list (prevent DOS)
   139  	// MaxQueuedItems is the maximum number of items to queue up before
   140  	// dropping broadcasts. This is a sensitive number as a transaction list might
   141  	// contain a single transaction, or thousands.
   142  	MaxQueuedItems idx.Event
   143  	MaxQueuedSize  uint64
   144  }
   145  
   146  // DefaultConfig returns the default configurations for the gossip service.
   147  func DefaultConfig(scale cachescale.Func) Config {
   148  	cfg := Config{
   149  		FilterAPI: filters.DefaultConfig(),
   150  
   151  		TxIndex: true,
   152  
   153  		HeavyCheck: heavycheck.DefaultConfig(),
   154  
   155  		Protocol: ProtocolConfig{
   156  			LatencyImportance:    60,
   157  			ThroughputImportance: 40,
   158  			MsgsSemaphoreLimit: dag.Metric{
   159  				Num:  scale.Events(1000),
   160  				Size: scale.U64(30 * opt.MiB),
   161  			},
   162  			EventsSemaphoreLimit: dag.Metric{
   163  				Num:  scale.Events(10000),
   164  				Size: scale.U64(30 * opt.MiB),
   165  			},
   166  			BVsSemaphoreLimit: dag.Metric{
   167  				Num:  scale.Events(5000),
   168  				Size: scale.U64(15 * opt.MiB),
   169  			},
   170  			MsgsSemaphoreTimeout:    10 * time.Second,
   171  			ProgressBroadcastPeriod: 10 * time.Second,
   172  
   173  			DagProcessor: dagprocessor.DefaultConfig(scale),
   174  			BvProcessor:  bvprocessor.DefaultConfig(scale),
   175  			BrProcessor:  brprocessor.DefaultConfig(scale),
   176  			EpProcessor:  epprocessor.DefaultConfig(scale),
   177  			DagFetcher: itemsfetcher.Config{
   178  				ForgetTimeout:       1 * time.Minute,
   179  				ArriveTimeout:       1000 * time.Millisecond,
   180  				GatherSlack:         100 * time.Millisecond,
   181  				HashLimit:           20000,
   182  				MaxBatch:            scale.I(512),
   183  				MaxQueuedBatches:    scale.I(32),
   184  				MaxParallelRequests: 192,
   185  			},
   186  			TxFetcher: itemsfetcher.Config{
   187  				ForgetTimeout:       1 * time.Minute,
   188  				ArriveTimeout:       1000 * time.Millisecond,
   189  				GatherSlack:         100 * time.Millisecond,
   190  				HashLimit:           10000,
   191  				MaxBatch:            scale.I(512),
   192  				MaxQueuedBatches:    scale.I(32),
   193  				MaxParallelRequests: 64,
   194  			},
   195  			DagStreamLeecher:         dagstreamleecher.DefaultConfig(),
   196  			DagStreamSeeder:          dagstreamseeder.DefaultConfig(scale),
   197  			BvStreamLeecher:          bvstreamleecher.DefaultConfig(),
   198  			BvStreamSeeder:           bvstreamseeder.DefaultConfig(scale),
   199  			BrStreamLeecher:          brstreamleecher.DefaultConfig(),
   200  			BrStreamSeeder:           brstreamseeder.DefaultConfig(scale),
   201  			EpStreamLeecher:          epstreamleecher.DefaultConfig(),
   202  			EpStreamSeeder:           epstreamseeder.DefaultConfig(scale),
   203  			MaxInitialTxHashesSend:   20000,
   204  			MaxRandomTxHashesSend:    128,
   205  			RandomTxHashesSendPeriod: 20 * time.Second,
   206  			PeerCache:                DefaultPeerCacheConfig(scale),
   207  		},
   208  
   209  		GPO: gasprice.Config{
   210  			MaxGasPrice:      gasprice.DefaultMaxGasPrice,
   211  			MinGasPrice:      new(big.Int),
   212  			MinGasTip:        new(big.Int),
   213  			DefaultCertainty: 0.5 * gasprice.DecimalUnit,
   214  		},
   215  
   216  		RPCBlockExt: true,
   217  
   218  		RPCGasCap:   50000000,
   219  		RPCTxFeeCap: 100, // 100 U2U
   220  		RPCTimeout:  5 * time.Second,
   221  	}
   222  	sessionCfg := cfg.Protocol.DagStreamLeecher.Session
   223  	cfg.Protocol.DagProcessor.EventsBufferLimit.Num = idx.Event(sessionCfg.ParallelChunksDownload)*
   224  		idx.Event(sessionCfg.DefaultChunkItemsNum) + softLimitItems
   225  	cfg.Protocol.DagProcessor.EventsBufferLimit.Size = uint64(sessionCfg.ParallelChunksDownload)*sessionCfg.DefaultChunkItemsSize + 8*opt.MiB
   226  	cfg.Protocol.DagStreamLeecher.MaxSessionRestart = 4 * time.Minute
   227  	cfg.Protocol.DagFetcher.ArriveTimeout = 4 * time.Second
   228  	cfg.Protocol.DagFetcher.HashLimit = 10000
   229  	cfg.Protocol.TxFetcher.HashLimit = 10000
   230  
   231  	return cfg
   232  }
   233  
   234  func (c *Config) Validate() error {
   235  	p := c.Protocol
   236  	defaultChunkSize := dag.Metric{idx.Event(p.DagStreamLeecher.Session.DefaultChunkItemsNum), p.DagStreamLeecher.Session.DefaultChunkItemsSize}
   237  	if defaultChunkSize.Num > hardLimitItems-1 {
   238  		return fmt.Errorf("DefaultChunkSize.Num has to be at not greater than %d", hardLimitItems-1)
   239  	}
   240  	if defaultChunkSize.Size > protocolMaxMsgSize/2 {
   241  		return fmt.Errorf("DefaultChunkSize.Num has to be at not greater than %d", protocolMaxMsgSize/2)
   242  	}
   243  	if p.EventsSemaphoreLimit.Num < 2*defaultChunkSize.Num ||
   244  		p.EventsSemaphoreLimit.Size < 2*defaultChunkSize.Size {
   245  		return fmt.Errorf("EventsSemaphoreLimit has to be at least 2 times greater than %s (DefaultChunkSize)", defaultChunkSize.String())
   246  	}
   247  	if p.EventsSemaphoreLimit.Num < 2*p.DagProcessor.EventsBufferLimit.Num ||
   248  		p.EventsSemaphoreLimit.Size < 2*p.DagProcessor.EventsBufferLimit.Size {
   249  		return fmt.Errorf("EventsSemaphoreLimit has to be at least 2 times greater than %s (EventsBufferLimit)", p.DagProcessor.EventsBufferLimit.String())
   250  	}
   251  	if p.EventsSemaphoreLimit.Size < 2*protocolMaxMsgSize {
   252  		return fmt.Errorf("EventsSemaphoreLimit.Size has to be at least %d", 2*protocolMaxMsgSize)
   253  	}
   254  	if p.MsgsSemaphoreLimit.Size < protocolMaxMsgSize {
   255  		return fmt.Errorf("MsgsSemaphoreLimit.Size has to be at least %d", protocolMaxMsgSize)
   256  	}
   257  	if p.DagProcessor.EventsBufferLimit.Size < protocolMaxMsgSize {
   258  		return fmt.Errorf("EventsBufferLimit.Size has to be at least %d", protocolMaxMsgSize)
   259  	}
   260  
   261  	return nil
   262  }
   263  
   264  // DefaultStoreConfig for product.
   265  func DefaultStoreConfig(scale cachescale.Func) StoreConfig {
   266  	return StoreConfig{
   267  		Cache: StoreCacheConfig{
   268  			EventsNum:            scale.I(5000),
   269  			EventsSize:           scale.U(6 * opt.MiB),
   270  			EventsIDsNum:         scale.I(100000),
   271  			BlocksNum:            scale.I(5000),
   272  			BlocksSize:           scale.U(512 * opt.KiB),
   273  			BlockEpochStateNum:   scale.I(8),
   274  			LlrBlockVotesIndexes: scale.I(100),
   275  			LlrEpochVotesIndexes: scale.I(5),
   276  		},
   277  		EVM:                 evmstore.DefaultStoreConfig(scale),
   278  		MaxNonFlushedSize:   21*opt.MiB + scale.I(2*opt.MiB),
   279  		MaxNonFlushedPeriod: 30 * time.Minute,
   280  	}
   281  }
   282  
   283  // LiteStoreConfig is for tests or inmemory.
   284  func LiteStoreConfig() StoreConfig {
   285  	return DefaultStoreConfig(cachescale.Ratio{Base: 10, Target: 1})
   286  }
   287  
   288  func DefaultPeerCacheConfig(scale cachescale.Func) PeerCacheConfig {
   289  	return PeerCacheConfig{
   290  		MaxKnownTxs:    24576*3/4 + scale.I(24576/4),
   291  		MaxKnownEvents: 24576*3/4 + scale.I(24576/4),
   292  		MaxQueuedItems: 4096*3/4 + scale.Events(4096/4),
   293  		MaxQueuedSize:  protocolMaxMsgSize*3/4 + 1024 + scale.U64(protocolMaxMsgSize/4),
   294  	}
   295  }