diff --git a/cmd/devp2p/internal/ethtest/suite.go b/cmd/devp2p/internal/ethtest/suite.go index 8ebbe2a05d..416df8067c 100644 --- a/cmd/devp2p/internal/ethtest/suite.go +++ b/cmd/devp2p/internal/ethtest/suite.go @@ -377,7 +377,7 @@ func (s *Suite) TestGetBlockBodies(t *utesting.T) { // randBuf makes a random buffer size kilobytes large. func randBuf(size int) []byte { - buf := make([]byte, size*1024) + buf := make([]byte, size*common.Kilobytes) rand.Read(buf) return buf } diff --git a/cmd/evm/eofparse.go b/cmd/evm/eofparse.go index 9710735576..2897802b7b 100644 --- a/cmd/evm/eofparse.go +++ b/cmd/evm/eofparse.go @@ -26,6 +26,7 @@ import ( "path/filepath" "strings" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/log" "github.com/urfave/cli/v2" @@ -105,7 +106,7 @@ func eofParseAction(ctx *cli.Context) error { } // If neither are passed in, read input from stdin. scanner := bufio.NewScanner(os.Stdin) - scanner.Buffer(make([]byte, 1024*1024), 10*1024*1024) + scanner.Buffer(make([]byte, common.Megabytes), 10*common.Megabytes) for scanner.Scan() { l := strings.TrimSpace(scanner.Text()) if strings.HasPrefix(l, "#") || l == "" { @@ -197,7 +198,7 @@ func eofDumpAction(ctx *cli.Context) error { } // Otherwise read from stdin scanner := bufio.NewScanner(os.Stdin) - scanner.Buffer(make([]byte, 1024*1024), 10*1024*1024) + scanner.Buffer(make([]byte, common.Megabytes), 10*common.Megabytes) for scanner.Scan() { l := strings.TrimSpace(scanner.Text()) if strings.HasPrefix(l, "#") || l == "" { diff --git a/cmd/evm/internal/t8ntool/tx_iterator.go b/cmd/evm/internal/t8ntool/tx_iterator.go index 047626c56b..f572d62a6e 100644 --- a/cmd/evm/internal/t8ntool/tx_iterator.go +++ b/cmd/evm/internal/t8ntool/tx_iterator.go @@ -176,7 +176,7 @@ type rlpTxIterator struct { } func newRlpTxIterator(rlpData []byte) txIterator { - in := rlp.NewStream(bytes.NewBuffer(rlpData), 1024*1024) + in := rlp.NewStream(bytes.NewBuffer(rlpData), common.Megabytes) in.List() return &rlpTxIterator{in} } diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index c57a9a947d..c479d89b6f 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -351,8 +351,8 @@ func importChain(ctx *cli.Context) error { mem := new(runtime.MemStats) runtime.ReadMemStats(mem) - fmt.Printf("Object memory: %.3f MB current, %.3f MB peak\n", float64(mem.Alloc)/1024/1024, float64(peakMemAlloc.Load())/1024/1024) - fmt.Printf("System memory: %.3f MB current, %.3f MB peak\n", float64(mem.Sys)/1024/1024, float64(peakMemSys.Load())/1024/1024) + fmt.Printf("Object memory: %.3f MB current, %.3f MB peak\n", float64(mem.Alloc)/common.Megabytes, float64(peakMemAlloc.Load())/common.Megabytes) + fmt.Printf("System memory: %.3f MB current, %.3f MB peak\n", float64(mem.Sys)/common.Megabytes, float64(peakMemSys.Load())/common.Megabytes) fmt.Printf("Allocations: %.3f million\n", float64(mem.Mallocs)/1000000) fmt.Printf("GC pause: %v\n\n", time.Duration(mem.PauseTotalNs)) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index d34e15ebc0..1148aad545 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -94,7 +94,7 @@ func StartNode(ctx *cli.Context, stack *node.Node, isConsole bool) { minFreeDiskSpace = 2 * ctx.Int(CacheFlag.Name) * ctx.Int(CacheGCFlag.Name) / 100 } if minFreeDiskSpace > 0 { - go monitorFreeDiskSpace(sigc, stack.InstanceDir(), uint64(minFreeDiskSpace)*1024*1024) + go monitorFreeDiskSpace(sigc, stack.InstanceDir(), uint64(minFreeDiskSpace)*common.Megabytes) } shutdown := func() { @@ -523,7 +523,7 @@ func ImportPreimages(db ethdb.Database, fn string) error { } // Accumulate the preimages and flush when enough ws gathered preimages[crypto.Keccak256Hash(blob)] = common.CopyBytes(blob) - if len(preimages) > 1024 { + if len(preimages) > common.Kilobytes { rawdb.WritePreimages(db, preimages) preimages = make(map[common.Hash][]byte) } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f5fc94cebc..05639a880f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1587,11 +1587,11 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { // Cap the cache allowance and tune the garbage collector mem, err := gopsutil.VirtualMemory() if err == nil { - if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 { - log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024) - mem.Total = 2 * 1024 * 1024 * 1024 + if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*common.Gigabytes { + log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/common.Megabytes, "addressable", 2*common.Kilobytes) + mem.Total = 2 * common.Gigabytes } - allowance := int(mem.Total / 1024 / 1024 / 3) + allowance := int(mem.Total / common.Megabytes / 3) if cache := ctx.Int(CacheFlag.Name); cache > allowance { log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance) ctx.Set(CacheFlag.Name, strconv.Itoa(allowance)) diff --git a/common/bytes.go b/common/bytes.go index d1f5c6c995..c1d51a75bc 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -24,6 +24,13 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" ) +const ( + Kilobytes = 1024 + Megabytes = 1024 * Kilobytes + Gigabytes = 1024 * Megabytes + Terabytes = 1024 * Gigabytes +) + // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { diff --git a/common/size.go b/common/size.go index 097b6304a8..d713ab09b1 100644 --- a/common/size.go +++ b/common/size.go @@ -26,14 +26,14 @@ type StorageSize float64 // String implements the stringer interface. func (s StorageSize) String() string { - if s > 1099511627776 { - return fmt.Sprintf("%.2f TiB", s/1099511627776) - } else if s > 1073741824 { - return fmt.Sprintf("%.2f GiB", s/1073741824) - } else if s > 1048576 { - return fmt.Sprintf("%.2f MiB", s/1048576) - } else if s > 1024 { - return fmt.Sprintf("%.2f KiB", s/1024) + if s > Terabytes { + return fmt.Sprintf("%.2f TiB", s/Terabytes) + } else if s > Gigabytes { + return fmt.Sprintf("%.2f GiB", s/Gigabytes) + } else if s > Megabytes { + return fmt.Sprintf("%.2f MiB", s/Megabytes) + } else if s > Kilobytes { + return fmt.Sprintf("%.2f KiB", s/Kilobytes) } else { return fmt.Sprintf("%.2f B", s) } @@ -42,14 +42,14 @@ func (s StorageSize) String() string { // TerminalString implements log.TerminalStringer, formatting a string for console // output during logging. func (s StorageSize) TerminalString() string { - if s > 1099511627776 { - return fmt.Sprintf("%.2fTiB", s/1099511627776) - } else if s > 1073741824 { - return fmt.Sprintf("%.2fGiB", s/1073741824) - } else if s > 1048576 { - return fmt.Sprintf("%.2fMiB", s/1048576) - } else if s > 1024 { - return fmt.Sprintf("%.2fKiB", s/1024) + if s > Terabytes { + return fmt.Sprintf("%.2fTiB", s/Terabytes) + } else if s > Gigabytes { + return fmt.Sprintf("%.2fGiB", s/Gigabytes) + } else if s > Megabytes { + return fmt.Sprintf("%.2fMiB", s/Megabytes) + } else if s > Kilobytes { + return fmt.Sprintf("%.2fKiB", s/Kilobytes) } else { return fmt.Sprintf("%.2fB", s) } diff --git a/core/blockchain.go b/core/blockchain.go index 203dcd2693..8d756cb360 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -170,14 +170,14 @@ func (c *CacheConfig) triedbConfig(isVerkle bool) *triedb.Config { } if c.StateScheme == rawdb.HashScheme { config.HashDB = &hashdb.Config{ - CleanCacheSize: c.TrieCleanLimit * 1024 * 1024, + CleanCacheSize: c.TrieCleanLimit * common.Megabytes, } } if c.StateScheme == rawdb.PathScheme { config.PathDB = &pathdb.Config{ StateHistory: c.StateHistory, - CleanCacheSize: c.TrieCleanLimit * 1024 * 1024, - WriteBufferSize: c.TrieDirtyLimit * 1024 * 1024, + CleanCacheSize: c.TrieCleanLimit * common.Megabytes, + WriteBufferSize: c.TrieDirtyLimit * common.Megabytes, } } return config @@ -1518,9 +1518,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. // If we exceeded our memory allowance, flush matured singleton nodes to disk var ( _, nodes, imgs = bc.triedb.Size() // all memory is contained within the nodes return for hashdb - limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * 1024 * 1024 + limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * common.Megabytes ) - if nodes > limit || imgs > 4*1024*1024 { + if nodes > limit || imgs > 4*common.Megabytes { bc.triedb.Cap(limit - ethdb.IdealBatchSize) } // Find the next state trie we need to commit @@ -2022,7 +2022,7 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s // switch over to the new chain if the TD exceeded the current chain. // insertSideChain is only used pre-merge. func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, makeWitness bool) (*stateless.Witness, int, error) { - var current = bc.CurrentBlock() + current := bc.CurrentBlock() // The first sidechain block error is already verified to be ErrPrunedAncestor. // Since we don't import them here, we expect ErrUnknownAncestor for the remaining @@ -2100,7 +2100,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma // If memory use grew too large, import and continue. Sadly we need to discard // all raised events and logs from notifications since we're too heavy on the // memory here. - if len(blocks) >= 2048 || memory > 64*1024*1024 { + if len(blocks) >= 2048 || memory > 64*common.Megabytes { log.Info("Importing heavy sidechain segment", "blocks", len(blocks), "start", blocks[0].NumberU64(), "end", block.NumberU64()) if _, _, err := bc.insertChain(blocks, true, false); err != nil { return nil, 0, err diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 2f62d86e4b..d68441583a 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -324,7 +324,7 @@ func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValu return rlpHeaders } // read remaining from ancients, cap at 2M - data, err := db.AncientRange(ChainFreezerHeaderTable, i+1-count, count, 2*1024*1024) + data, err := db.AncientRange(ChainFreezerHeaderTable, i+1-count, count, 2*common.Megabytes) if err != nil { log.Error("Failed to read headers from freezer", "err", err) return rlpHeaders diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index 99b63df4dc..1cfe047dc5 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -21,13 +21,14 @@ import ( "math" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" "github.com/golang/snappy" ) // This is the maximum amount of data that will be buffered in memory // for a single freezer table batch. -const freezerBatchBufferLimit = 2 * 1024 * 1024 +const freezerBatchBufferLimit = 2 * common.Megabytes // freezerBatch is a write operation of multiple items on a freezer. type freezerBatch struct { diff --git a/core/state/database.go b/core/state/database.go index faf4954650..a4fcac2e24 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -37,7 +37,7 @@ const ( codeSizeCacheSize = 100000 // Cache size granted for caching clean code. - codeCacheSize = 64 * 1024 * 1024 + codeCacheSize = 64 * common.Megabytes // Number of address->curve point associations to keep. pointCacheSize = 4096 diff --git a/core/state/pruner/bloom.go b/core/state/pruner/bloom.go index dad2b5b2a8..9dcc32d70b 100644 --- a/core/state/pruner/bloom.go +++ b/core/state/pruner/bloom.go @@ -56,7 +56,7 @@ type stateBloom struct { // to the https://hur.st/bloomfilter/?n=600000000&p=&m=2048MB&k=4, the parameters // are picked so that the false-positive rate for mainnet is low enough. func newStateBloomWithSize(size uint64) (*stateBloom, error) { - bloom, err := bloomfilter.New(size*1024*1024*8, 4) + bloom, err := bloomfilter.New(size*common.Megabytes*8, 4) if err != nil { return nil, err } diff --git a/core/state/snapshot/difflayer.go b/core/state/snapshot/difflayer.go index 28957051d4..eb67272292 100644 --- a/core/state/snapshot/difflayer.go +++ b/core/state/snapshot/difflayer.go @@ -41,7 +41,7 @@ var ( // Note, bumping this up might drastically increase the size of the bloom // filters that's stored in every diff layer. Don't do that without fully // understanding all the implications. - aggregatorMemoryLimit = uint64(4 * 1024 * 1024) + aggregatorMemoryLimit = uint64(4 * common.Megabytes) // aggregatorItemLimit is an approximate number of items that will end up // in the aggregator layer before it's flushed out to disk. A plain account diff --git a/core/state/snapshot/generate.go b/core/state/snapshot/generate.go index 01fb55ea4c..7554781f28 100644 --- a/core/state/snapshot/generate.go +++ b/core/state/snapshot/generate.go @@ -71,7 +71,7 @@ func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *triedb.Database, cache diskdb: diskdb, triedb: triedb, root: root, - cache: fastcache.New(cache * 1024 * 1024), + cache: fastcache.New(cache * common.Megabytes), genMarker: genMarker, genPending: make(chan struct{}), genAbort: make(chan chan *generatorStats), diff --git a/core/state/snapshot/journal.go b/core/state/snapshot/journal.go index e4b396b990..ceebc01010 100644 --- a/core/state/snapshot/journal.go +++ b/core/state/snapshot/journal.go @@ -139,7 +139,7 @@ func loadSnapshot(diskdb ethdb.KeyValueStore, triedb *triedb.Database, root comm base := &diskLayer{ diskdb: diskdb, triedb: triedb, - cache: fastcache.New(cache * 1024 * 1024), + cache: fastcache.New(cache * common.Megabytes), root: baseRoot, } snapshot, generator, err := loadAndParseJournal(diskdb, base) diff --git a/core/state/snapshot/snapshot.go b/core/state/snapshot/snapshot.go index f0f6296433..ceaef5d37e 100644 --- a/core/state/snapshot/snapshot.go +++ b/core/state/snapshot/snapshot.go @@ -560,7 +560,7 @@ func diffToDisk(bottom *diffLayer) *diskLayer { // Ensure we don't write too much data blindly. It's ok to flush, the // root will go missing in case of a crash and we'll detect and regen // the snapshot. - if batch.ValueSize() > 64*1024*1024 { + if batch.ValueSize() > 64*common.Megabytes { if err := batch.Write(); err != nil { log.Crit("Failed to write state changes", "err", err) } @@ -595,7 +595,7 @@ func diffToDisk(bottom *diffLayer) *diskLayer { // Ensure we don't write too much data blindly. It's ok to flush, the // root will go missing in case of a crash and we'll detect and regen // the snapshot. - if batch.ValueSize() > 64*1024*1024 { + if batch.ValueSize() > 64*common.Megabytes { if err := batch.Write(); err != nil { log.Crit("Failed to write state changes", "err", err) } diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index 12a4133b40..12412d48e6 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -53,14 +53,14 @@ const ( // txAvgSize is an approximate byte size of a transaction metadata to avoid // tiny overflows causing all txs to move a shelf higher, wasting disk space. - txAvgSize = 4 * 1024 + txAvgSize = 4 * common.Kilobytes // txMaxSize is the maximum size a single transaction can have, outside // the included blobs. Since blob transactions are pulled instead of pushed, // and only a small metadata is kept in ram, the rest is on disk, there is // no critical limit that should be enforced. Still, capping it to some sane // limit can never hurt. - txMaxSize = 1024 * 1024 + txMaxSize = common.Megabytes // maxTxsPerAccount is the maximum number of blob transactions admitted from // a single account. The limit is enforced to minimize the DoS potential of diff --git a/core/txpool/blobpool/config.go b/core/txpool/blobpool/config.go index 1d180739cd..90ae4b8775 100644 --- a/core/txpool/blobpool/config.go +++ b/core/txpool/blobpool/config.go @@ -17,6 +17,7 @@ package blobpool import ( + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" ) @@ -30,8 +31,8 @@ type Config struct { // DefaultConfig contains the default configurations for the transaction pool. var DefaultConfig = Config{ Datadir: "blobpool", - Datacap: 10 * 1024 * 1024 * 1024 / 4, // TODO(karalabe): /4 handicap for rollout, gradually bump back up to 10GB - PriceBump: 100, // either have patience or be aggressive, no mushy ground + Datacap: 10 * common.Gigabytes / 4, // TODO(karalabe): /4 handicap for rollout, gradually bump back up to 10GB + PriceBump: 100, // either have patience or be aggressive, no mushy ground } // sanitize checks the provided user configurations and changes anything that's diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 7bf360ff65..a870c9639d 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -49,7 +49,7 @@ const ( // takes up based on its size. The slots are used as DoS protection, ensuring // that validating a new transaction remains a constant operation (in reality // O(maxslots), where max slots are 4 currently). - txSlotSize = 32 * 1024 + txSlotSize = 32 * common.Kilobytes // txMaxSize is the maximum size a single transaction can have. This field has // non-trivial consequences: larger transactions are significantly harder and diff --git a/eth/backend.go b/eth/backend.go index 6d1b6bae99..e547708f0c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -126,7 +126,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { } config.TrieDirtyCache = 0 } - log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*1024*1024, "dirty", common.StorageSize(config.TrieDirtyCache)*1024*1024) + log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*common.Megabytes, "dirty", common.StorageSize(config.TrieDirtyCache)*common.Megabytes) chainDb, err := stack.OpenDatabaseWithFreezer("chaindata", config.DatabaseCache, config.DatabaseHandles, config.DatabaseFreezer, "eth/db/chaindata/", false) if err != nil { diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 000ad97ca9..4919060dcd 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -42,10 +42,10 @@ const ( ) var ( - blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download - blockCacheInitialItems = 2048 // Initial number of blocks to start fetching, before we know the sizes of the blocks - blockCacheMemory = 256 * 1024 * 1024 // Maximum amount of memory to use for block caching - blockCacheSizeWeight = 0.1 // Multiplier to approximate the average block size based on past ones + blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download + blockCacheInitialItems = 2048 // Initial number of blocks to start fetching, before we know the sizes of the blocks + blockCacheMemory = 256 * common.Megabytes // Maximum amount of memory to use for block caching + blockCacheSizeWeight = 0.1 // Multiplier to approximate the average block size based on past ones ) var ( @@ -400,7 +400,8 @@ func (q *queue) ReserveReceipts(p *peerConnection, count int) (*fetchRequest, bo // progress - whether any progress was made // throttle - if the caller should throttle for a while func (q *queue) reserveHeaders(p *peerConnection, count int, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque[int64, *types.Header], - pendPool map[string]*fetchRequest, kind uint) (*fetchRequest, bool, bool) { + pendPool map[string]*fetchRequest, kind uint, +) (*fetchRequest, bool, bool) { // Short circuit if the pool has been depleted, or if the peer's already // downloading something (sanity check not to corrupt state) if taskQueue.Empty() { @@ -658,7 +659,8 @@ func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque[int64, *types.Header], pendPool map[string]*fetchRequest, reqTimer *metrics.Timer, resInMeter, resDropMeter *metrics.Meter, results int, validate func(index int, header *types.Header) error, - reconstruct func(index int, result *fetchResult)) (int, error) { + reconstruct func(index int, result *fetchResult), +) (int, error) { // Short circuit if the data was never requested request := pendPool[id] if request == nil { diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go index f2a3cb0292..09eff1e970 100644 --- a/eth/protocols/eth/handler.go +++ b/eth/protocols/eth/handler.go @@ -33,7 +33,7 @@ import ( const ( // softResponseLimit is the target maximum size of replies to data retrievals. - softResponseLimit = 2 * 1024 * 1024 + softResponseLimit = 2 * common.Megabytes // maxHeadersServe is the maximum number of block headers to serve. This number // is there to limit the number of disk lookups. diff --git a/eth/protocols/eth/protocol.go b/eth/protocols/eth/protocol.go index aeef4330ff..a1098f3875 100644 --- a/eth/protocols/eth/protocol.go +++ b/eth/protocols/eth/protocol.go @@ -46,7 +46,7 @@ var ProtocolVersions = []uint{ETH68} var protocolLengths = map[uint]uint64{ETH68: 17} // maxMessageSize is the maximum cap on the size of a protocol message. -const maxMessageSize = 10 * 1024 * 1024 +const maxMessageSize = 10 * common.Megabytes const ( StatusMsg = 0x00 diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index 924aff7ac9..6011bc302c 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -35,7 +35,7 @@ import ( const ( // softResponseLimit is the target maximum size of replies to data retrievals. - softResponseLimit = 2 * 1024 * 1024 + softResponseLimit = 2 * common.Megabytes // maxCodeLookups is the maximum number of bytecodes to serve. This number is // there to limit the number of disk lookups. diff --git a/eth/protocols/snap/protocol.go b/eth/protocols/snap/protocol.go index 0db206b081..bbb7382e64 100644 --- a/eth/protocols/snap/protocol.go +++ b/eth/protocols/snap/protocol.go @@ -43,7 +43,7 @@ var ProtocolVersions = []uint{SNAP1} var protocolLengths = map[uint]uint64{SNAP1: 8} // maxMessageSize is the maximum cap on the size of a protocol message. -const maxMessageSize = 10 * 1024 * 1024 +const maxMessageSize = 10 * common.Megabytes const ( GetAccountRangeMsg = 0x00 diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 9e079f540f..8973b8ff1d 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -48,12 +48,12 @@ const ( // minRequestSize is the minimum number of bytes to request from a remote peer. // This number is used as the low cap for account and storage range requests. // Bytecode and trienode are limited inherently by item count (1). - minRequestSize = 64 * 1024 + minRequestSize = 64 * common.Kilobytes // maxRequestSize is the maximum number of bytes to request from a remote peer. // This number is used as the high cap for account and storage range requests. // Bytecode and trienode are limited more explicitly by the caps below. - maxRequestSize = 512 * 1024 + maxRequestSize = 512 * common.Kilobytes // maxCodeRequestCount is the maximum number of bytecode blobs to request in a // single query. If this number is too low, we're not filling responses fully @@ -63,7 +63,7 @@ const ( // Deployed bytecodes are currently capped at 24KB, so the minimum request // size should be maxRequestSize / 24K. Assuming that most contracts do not // come close to that, requesting 4x should be a good approximation. - maxCodeRequestCount = maxRequestSize / (24 * 1024) * 4 + maxCodeRequestCount = maxRequestSize / (24 * common.Kilobytes) * 4 // maxTrieRequestCount is the maximum number of trie node blobs to request in // a single query. If this number is too low, we're not filling responses fully @@ -95,7 +95,7 @@ const ( trienodeHealThrottleDecrease = 1.25 // batchSizeThreshold is the maximum size allowed for gentrie batch. - batchSizeThreshold = 8 * 1024 * 1024 + batchSizeThreshold = 8 * common.Megabytes ) var ( diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 3cb86c80e2..249af2b671 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -61,7 +61,7 @@ const ( // on top of memory. // For non-archive nodes, this limit _will_ be overblown, as disk-backed tries // will only be found every ~15K blocks or so. - defaultTracechainMemLimit = common.StorageSize(500 * 1024 * 1024) + defaultTracechainMemLimit = common.StorageSize(500 * common.Megabytes) // maximumPendingTraceStates is the maximum number of states allowed waiting // for tracing. The creation of trace state will be paused if the unused diff --git a/eth/tracers/internal/util.go b/eth/tracers/internal/util.go index 88a9f5db44..d7feb68360 100644 --- a/eth/tracers/internal/util.go +++ b/eth/tracers/internal/util.go @@ -20,11 +20,12 @@ import ( "errors" "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" ) const ( - memoryPadLimit = 1024 * 1024 + memoryPadLimit = common.Megabytes ) // GetMemoryCopyPadded returns offset + size as a new slice. diff --git a/ethdb/batch.go b/ethdb/batch.go index 541f40c838..7b0f14f49d 100644 --- a/ethdb/batch.go +++ b/ethdb/batch.go @@ -16,9 +16,11 @@ package ethdb +import "github.com/ethereum/go-ethereum/common" + // IdealBatchSize defines the size of the data batches should ideally add in one // write. -const IdealBatchSize = 100 * 1024 +const IdealBatchSize = 100 * common.Kilobytes // Batch is a write-only database that commits changes to its host database // when Write is called. A batch cannot be used concurrently. diff --git a/ethdb/pebble/pebble.go b/ethdb/pebble/pebble.go index 969e67af5a..4be4819e09 100644 --- a/ethdb/pebble/pebble.go +++ b/ethdb/pebble/pebble.go @@ -153,7 +153,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e handles = minHandles } logger := log.New("database", file) - logger.Info("Allocated cache and file handles", "cache", common.StorageSize(cache*1024*1024), "handles", handles) + logger.Info("Allocated cache and file handles", "cache", common.StorageSize(cache*common.Megabytes), "handles", handles) // The max memtable size is limited by the uint32 offsets stored in // internal/arenaskl.node, DeferredBatchOp, and flushableBatchEntry. @@ -170,7 +170,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e // Two memory tables is configured which is identical to leveldb, // including a frozen memory table and another live one. memTableLimit := 2 - memTableSize := cache * 1024 * 1024 / 2 / memTableLimit + memTableSize := cache * common.Megabytes / 2 / memTableLimit // The memory table size is currently capped at maxMemTableSize-1 due to a // known bug in the pebble where maxMemTableSize is not recognized as a @@ -191,7 +191,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e // Pebble has a single combined cache area and the write // buffers are taken from this too. Assign all available // memory allowance for cache. - Cache: pebble.NewCache(int64(cache * 1024 * 1024)), + Cache: pebble.NewCache(int64(cache * common.Megabytes)), MaxOpenFiles: handles, // The size of memory table(as well as the write buffer). @@ -212,13 +212,13 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e // Per-level options. Options for at least one level must be specified. The // options for the last level are used for all subsequent levels. Levels: []pebble.LevelOptions{ - {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 4 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 8 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 16 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 32 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 64 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, - {TargetFileSize: 128 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 2 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 4 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 8 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 16 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 32 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 64 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, + {TargetFileSize: 128 * common.Megabytes, FilterPolicy: bloom.FilterPolicy(10)}, }, ReadOnly: readonly, EventListener: &pebble.EventListener{ diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go index 0090a7d4c1..eb9ab5f45e 100644 --- a/ethstats/ethstats.go +++ b/ethstats/ethstats.go @@ -56,7 +56,7 @@ const ( // chainHeadChanSize is the size of channel listening to ChainHeadEvent. chainHeadChanSize = 10 - messageSizeLimit = 15 * 1024 * 1024 + messageSizeLimit = 15 * common.Megabytes ) // backend encompasses the bare-minimum functionality needed for ethstats reporting diff --git a/internal/era/e2store/e2store.go b/internal/era/e2store/e2store.go index b0d43bf55a..53f2b32401 100644 --- a/internal/era/e2store/e2store.go +++ b/internal/era/e2store/e2store.go @@ -21,11 +21,13 @@ import ( "errors" "fmt" "io" + + "github.com/ethereum/go-ethereum/common" ) const ( headerSize = 8 - valueSizeLimit = 1024 * 1024 * 50 + valueSizeLimit = 50 * common.Megabytes ) // Entry is a variable-length-data record in an e2store. diff --git a/rpc/http.go b/rpc/http.go index f4b99429ef..56f766effa 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -30,10 +30,12 @@ import ( "strconv" "sync" "time" + + "github.com/ethereum/go-ethereum/common" ) const ( - defaultBodyLimit = 5 * 1024 * 1024 + defaultBodyLimit = 5 * common.Megabytes contentType = "application/json" ) diff --git a/rpc/websocket.go b/rpc/websocket.go index 9f67caf859..87210a255d 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -28,6 +28,7 @@ import ( "time" mapset "github.com/deckarep/golang-set/v2" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" "github.com/gorilla/websocket" ) @@ -38,7 +39,7 @@ const ( wsPingInterval = 30 * time.Second wsPingWriteTimeout = 5 * time.Second wsPongTimeout = 30 * time.Second - wsDefaultReadLimit = 32 * 1024 * 1024 + wsDefaultReadLimit = 32 * common.Megabytes ) var wsBufferPool = new(sync.Pool) diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index d48850c102..4003c8ef56 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -36,20 +36,20 @@ import ( const ( // defaultCleanSize is the default memory allowance of clean cache. - defaultCleanSize = 16 * 1024 * 1024 + defaultCleanSize = 16 * common.Megabytes // maxBufferSize is the maximum memory allowance of node buffer. // Too large buffer will cause the system to pause for a long // time when write happens. Also, the largest batch that pebble can // support is 4GB, node will panic if batch size exceeds this limit. - maxBufferSize = 256 * 1024 * 1024 + maxBufferSize = 256 * common.Megabytes // defaultBufferSize is the default memory allowance of node buffer // that aggregates the writes from above until it's flushed into the // disk. It's meant to be used once the initial sync is finished. // Do not increase the buffer size arbitrarily, otherwise the system // pause time will increase when the database writes happen. - defaultBufferSize = 64 * 1024 * 1024 + defaultBufferSize = 64 * common.Megabytes ) var ( diff --git a/triedb/preimages.go b/triedb/preimages.go index a5384910f7..c13201af67 100644 --- a/triedb/preimages.go +++ b/triedb/preimages.go @@ -74,7 +74,7 @@ func (store *preimageStore) commit(force bool) error { store.lock.Lock() defer store.lock.Unlock() - if store.preimagesSize <= 4*1024*1024 && !force { + if store.preimagesSize <= 4*common.Megabytes && !force { return nil } batch := store.disk.NewBatch()