From cf31fa986cdea3dfb3edb2a7e49be0f9ff1038ec Mon Sep 17 00:00:00 2001 From: rustfix <771054535@qq.com> Date: Sun, 6 Jul 2025 13:42:25 +0800 Subject: [PATCH] refactor: use the built-in max/min to simplify the code Signed-off-by: rustfix <771054535@qq.com> --- core/block_validator.go | 10 ++-------- core/bloombits/matcher.go | 10 ++-------- core/chain_indexer.go | 9 +++------ core/state/access_events.go | 5 +---- core/txindexer.go | 5 +---- core/txpool/blobpool/evictheap.go | 10 ++-------- core/vm/common.go | 10 ++-------- core/vm/contracts_test.go | 5 +---- 8 files changed, 14 insertions(+), 50 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index 49e58c8cb6..ca47ccd425 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -212,17 +212,11 @@ func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 { } // If we're outside our allowed gas range, we try to hone towards them if limit < desiredLimit { - limit = parentGasLimit + delta - if limit > desiredLimit { - limit = desiredLimit - } + limit = min(parentGasLimit+delta, desiredLimit) return limit } if limit > desiredLimit { - limit = parentGasLimit - delta - if limit < desiredLimit { - limit = desiredLimit - } + limit = max(parentGasLimit-delta, desiredLimit) } return limit } diff --git a/core/bloombits/matcher.go b/core/bloombits/matcher.go index 4c15c50ad9..79f04f4c2c 100644 --- a/core/bloombits/matcher.go +++ b/core/bloombits/matcher.go @@ -182,14 +182,8 @@ func (m *Matcher) Start(ctx context.Context, begin, end uint64, results chan uin // Calculate the first and last blocks of the section sectionStart := res.section * m.sectionSize - first := sectionStart - if begin > first { - first = begin - } - last := sectionStart + m.sectionSize - 1 - if end < last { - last = end - } + first := max(begin, sectionStart) + last := min(end, sectionStart+m.sectionSize-1) // Iterate over all the blocks in the section and return the matching ones for i := first; i <= last; i++ { // Skip the entire byte if no matches are found inside (and we're processing an entire byte!) diff --git a/core/chain_indexer.go b/core/chain_indexer.go index 2865daa1ff..aef4467b90 100644 --- a/core/chain_indexer.go +++ b/core/chain_indexer.go @@ -450,12 +450,9 @@ func (c *ChainIndexer) AddChildIndexer(indexer *ChainIndexer) { c.children = append(c.children, indexer) // Cascade any pending updates to new children too - sections := c.storedSections - if c.knownSections < sections { - // if a section is "stored" but not "known" then it is a checkpoint without - // available chain data so we should not cascade it yet - sections = c.knownSections - } + // if a section is "stored" but not "known" then it is a checkpoint without + // available chain data so we should not cascade it yet + sections := min(c.knownSections, c.storedSections) if sections > 0 { indexer.newHead(sections*c.sectionSize-1, false) } diff --git a/core/state/access_events.go b/core/state/access_events.go index b745c383b1..3424ae0a79 100644 --- a/core/state/access_events.go +++ b/core/state/access_events.go @@ -251,10 +251,7 @@ func (ae *AccessEvents) CodeChunksRangeGas(contractAddr common.Address, startPC, return 0 } - endPC := startPC + size - if endPC > codeLen { - endPC = codeLen - } + endPC := min(startPC+size, codeLen) if endPC > 0 { endPC -= 1 // endPC is the last bytecode that will be touched. } diff --git a/core/txindexer.go b/core/txindexer.go index effb0499d0..18ebbbdc85 100644 --- a/core/txindexer.go +++ b/core/txindexer.go @@ -101,10 +101,7 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don // It can happen when chain is rewound to a historical point which // is even lower than the indexes tail, recap the indexing target // to new head to avoid reading non-existent block bodies. - end := *tail - if end > head+1 { - end = head + 1 - } + end := min(*tail, head+1) rawdb.IndexTransactions(indexer.db, 0, end, stop, true) } return diff --git a/core/txpool/blobpool/evictheap.go b/core/txpool/blobpool/evictheap.go index 5e285e6c53..c1374f4807 100644 --- a/core/txpool/blobpool/evictheap.go +++ b/core/txpool/blobpool/evictheap.go @@ -95,14 +95,8 @@ func (h *evictHeap) Less(i, j int) bool { lastI := txsI[len(txsI)-1] lastJ := txsJ[len(txsJ)-1] - prioI := evictionPriority(h.basefeeJumps, lastI.evictionExecFeeJumps, h.blobfeeJumps, lastI.evictionBlobFeeJumps) - if prioI > 0 { - prioI = 0 - } - prioJ := evictionPriority(h.basefeeJumps, lastJ.evictionExecFeeJumps, h.blobfeeJumps, lastJ.evictionBlobFeeJumps) - if prioJ > 0 { - prioJ = 0 - } + prioI := min(evictionPriority(h.basefeeJumps, lastI.evictionExecFeeJumps, h.blobfeeJumps, lastI.evictionBlobFeeJumps), 0) + prioJ := min(evictionPriority(h.basefeeJumps, lastJ.evictionExecFeeJumps, h.blobfeeJumps, lastJ.evictionBlobFeeJumps), 0) if prioI == prioJ { return lastI.evictionExecTip.Lt(lastJ.evictionExecTip) } diff --git a/core/vm/common.go b/core/vm/common.go index 658803b820..8f039b39ec 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -57,10 +57,7 @@ func getData(data []byte, start uint64, size uint64) []byte { if start > length { start = length } - end := start + size - if end > length { - end = length - } + end := min(start+size, length) return common.RightPadBytes(data[start:end], int(size)) } @@ -69,10 +66,7 @@ func getDataAndAdjustedBounds(data []byte, start uint64, size uint64) (codeCopyP if start > length { start = length } - end := start + size - if end > length { - end = length - } + end := min(start+size, length) return common.RightPadBytes(data[start:end], int(size)), start, end - start } diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index e77ab82468..2da6a08027 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -173,10 +173,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { res, _, err = RunPrecompiledContract(p, data, reqGas, nil) } bench.StopTimer() - elapsed := uint64(time.Since(start)) - if elapsed < 1 { - elapsed = 1 - } + elapsed := max(uint64(time.Since(start)), 1) gasUsed := reqGas * uint64(bench.N) bench.ReportMetric(float64(reqGas), "gas/op") // Keep it as uint64, multiply 100 to get two digit float later