perf(all): use big.Int.Sign() to compare with 0 (#1969)

This commit is contained in:
Daniel Liu 2026-02-05 14:15:44 +08:00 committed by GitHub
parent a51eaa7200
commit 934c8d0679
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 15 additions and 15 deletions

View file

@ -390,7 +390,7 @@ func (x *XDPoS_v1) GetCurrentEpochSwitchBlock(blockNumber *big.Int) (uint64, uin
func (x *XDPoS_v1) GetPeriod() uint64 { return x.config.Period } func (x *XDPoS_v1) GetPeriod() uint64 { return x.config.Period }
func (x *XDPoS_v1) whoIsCreator(snap *SnapshotV1, header *types.Header) (common.Address, error) { func (x *XDPoS_v1) whoIsCreator(snap *SnapshotV1, header *types.Header) (common.Address, error) {
if header.Number.Uint64() == 0 { if header.Number.Sign() == 0 {
return common.Address{}, errors.New("don't take block 0") return common.Address{}, errors.New("don't take block 0")
} }
m, err := ecrecover(header, snap.sigcache) m, err := ecrecover(header, snap.sigcache)
@ -446,7 +446,7 @@ func (x *XDPoS_v1) yourTurn(chain consensus.ChainReader, parent *types.Header, s
pre := common.Address{} pre := common.Address{}
// masternode[0] has chance to create block 1 // masternode[0] has chance to create block 1
preIndex := -1 preIndex := -1
if parent.Number.Uint64() != 0 { if parent.Number.Sign() != 0 {
pre, err = x.whoIsCreator(snap, parent) pre, err = x.whoIsCreator(snap, parent)
if err != nil { if err != nil {
return 0, 0, 0, false, err return 0, 0, 0, false, err

View file

@ -36,7 +36,7 @@ func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types
} }
if isEpochSwitch { if isEpochSwitch {
log.Debug("[getEpochSwitchInfo] header is epoch switch", "hash", hash.Hex(), "number", h.Number.Uint64()) log.Debug("[getEpochSwitchInfo] header is epoch switch", "hash", hash.Hex(), "number", h.Number.Uint64())
if h.Number.Uint64() == 0 { if h.Number.Sign() == 0 {
log.Warn("[getEpochSwitchInfo] block 0, init epoch differently") log.Warn("[getEpochSwitchInfo] block 0, init epoch differently")
// handle genesis block differently as follows // handle genesis block differently as follows
masternodes := common.ExtractAddressFromBytes(h.Extra[32 : len(h.Extra)-65]) masternodes := common.ExtractAddressFromBytes(h.Extra[32 : len(h.Extra)-65])

View file

@ -62,7 +62,7 @@ func (x *XDPoS_v2) verifyHeader(chain consensus.ChainReader, header *types.Heade
// Ensure gas limit is consistent with parent // Ensure gas limit is consistent with parent
err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit) err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit)
if err != nil && parent.Number.Uint64() != 0 { // skip genesis block if err != nil && parent.Number.Sign() != 0 { // skip genesis block
return err return err
} }
// Ensure gas used is less than or equal to gas limit // Ensure gas used is less than or equal to gas limit

View file

@ -439,7 +439,7 @@ func testReorg(t *testing.T, first, second []int64, td int64, full bool) {
} }
} else { } else {
prev := blockchain.CurrentHeader() prev := blockchain.CurrentHeader()
for header := blockchain.GetHeaderByNumber(blockchain.CurrentHeader().Number.Uint64() - 1); header.Number.Uint64() != 0; prev, header = header, blockchain.GetHeaderByNumber(header.Number.Uint64()-1) { for header := blockchain.GetHeaderByNumber(blockchain.CurrentHeader().Number.Uint64() - 1); header.Number.Sign() != 0; prev, header = header, blockchain.GetHeaderByNumber(header.Number.Uint64()-1) {
if prev.ParentHash != header.Hash() { if prev.ParentHash != header.Hash() {
t.Errorf("parent header hash mismatch: have %x, want %x", prev.ParentHash, header.Hash()) t.Errorf("parent header hash mismatch: have %x, want %x", prev.ParentHash, header.Hash())
} }

View file

@ -22,7 +22,7 @@ func TestGFp2Invert(t *testing.T) {
inv.Invert(a, pool) inv.Invert(a, pool)
b := newGFp2(pool).Mul(inv, a, pool) b := newGFp2(pool).Mul(inv, a, pool)
if b.x.Int64() != 0 || b.y.Int64() != 1 { if b.x.Sign() != 0 || b.y.Cmp(big.NewInt(1)) != 0 {
t.Fatalf("bad result for a^-1*a: %s %s", b.x, b.y) t.Fatalf("bad result for a^-1*a: %s %s", b.x, b.y)
} }
@ -36,11 +36,11 @@ func TestGFp2Invert(t *testing.T) {
} }
func isZero(n *big.Int) bool { func isZero(n *big.Int) bool {
return new(big.Int).Mod(n, P).Int64() == 0 return new(big.Int).Mod(n, P).Sign() == 0
} }
func isOne(n *big.Int) bool { func isOne(n *big.Int) bool {
return new(big.Int).Mod(n, P).Int64() == 1 return new(big.Int).Mod(n, P).Cmp(big.NewInt(1)) == 0
} }
func TestGFp6Invert(t *testing.T) { func TestGFp6Invert(t *testing.T) {

View file

@ -363,10 +363,10 @@ func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []comm
var ret []*types.Log var ret []*types.Log
Logs: Logs:
for _, log := range logs { for _, log := range logs {
if fromBlock != nil && fromBlock.Int64() >= 0 && fromBlock.Uint64() > log.BlockNumber { if fromBlock != nil && fromBlock.Sign() >= 0 && fromBlock.Uint64() > log.BlockNumber {
continue continue
} }
if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber { if toBlock != nil && toBlock.Sign() >= 0 && toBlock.Uint64() < log.BlockNumber {
continue continue
} }

View file

@ -152,7 +152,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
lendingTxSub: nil, lendingTxSub: nil,
} }
// Figure out whether to allow fast sync or not // Figure out whether to allow fast sync or not
if mode == downloader.FastSync && blockchain.CurrentBlock().Number.Uint64() > 0 { if mode == downloader.FastSync && blockchain.CurrentBlock().Number.Sign() > 0 {
log.Warn("Blockchain not empty, fast sync disabled") log.Warn("Blockchain not empty, fast sync disabled")
mode = downloader.FullSync mode = downloader.FullSync
} }

View file

@ -184,7 +184,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
if atomic.LoadUint32(&pm.snapSync) == 1 { if atomic.LoadUint32(&pm.snapSync) == 1 {
// Fast sync was explicitly requested, and explicitly granted // Fast sync was explicitly requested, and explicitly granted
mode = downloader.FastSync mode = downloader.FastSync
} else if currentBlock.Number.Uint64() == 0 && pm.blockchain.CurrentSnapBlock().Number.Uint64() > 0 { } else if currentBlock.Number.Sign() == 0 && pm.blockchain.CurrentSnapBlock().Number.Sign() > 0 {
// The database seems empty as the current block is the genesis. Yet the fast // The database seems empty as the current block is the genesis. Yet the fast
// block is ahead, so fast sync was enabled for this node at a certain point. // block is ahead, so fast sync was enabled for this node at a certain point.
// The only scenario where this can happen is if the user manually (or via a // The only scenario where this can happen is if the user manually (or via a

View file

@ -763,7 +763,7 @@ func (api *BlockChainAPI) GetBlockFinalityByNumber(ctx context.Context, blockNum
// GetMasternodes returns masternodes set at the starting block of epoch of the given block // GetMasternodes returns masternodes set at the starting block of epoch of the given block
func (api *BlockChainAPI) GetMasternodes(ctx context.Context, b *types.Block) ([]common.Address, error) { func (api *BlockChainAPI) GetMasternodes(ctx context.Context, b *types.Block) ([]common.Address, error) {
var masternodes []common.Address var masternodes []common.Address
if b.Number().Int64() >= 0 { if b.Number().Sign() >= 0 {
curBlockNumber := b.Number().Uint64() curBlockNumber := b.Number().Uint64()
prevBlockNumber := curBlockNumber + (common.MergeSignRange - (curBlockNumber % common.MergeSignRange)) prevBlockNumber := curBlockNumber + (common.MergeSignRange - (curBlockNumber % common.MergeSignRange))
latestBlockNumber := api.b.CurrentBlock().Number.Uint64() latestBlockNumber := api.b.CurrentBlock().Number.Uint64()
@ -1499,7 +1499,7 @@ func (api *BlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Block, i
// findNearestSignedBlock finds the nearest checkpoint from input block // findNearestSignedBlock finds the nearest checkpoint from input block
func (api *BlockChainAPI) findNearestSignedBlock(ctx context.Context, b *types.Block) *types.Block { func (api *BlockChainAPI) findNearestSignedBlock(ctx context.Context, b *types.Block) *types.Block {
if b.Number().Int64() <= 0 { if b.Number().Sign() <= 0 {
return nil return nil
} }

View file

@ -286,7 +286,7 @@ func (t *BlockTest) validateImportedHeaders(cm *core.BlockChain, validBlocks []b
// block-by-block, so we can only validate imported headers after // block-by-block, so we can only validate imported headers after
// all blocks have been processed by BlockChain, as they may not // all blocks have been processed by BlockChain, as they may not
// be part of the longest chain until last block is imported. // be part of the longest chain until last block is imported.
for b := cm.CurrentBlock(); b != nil && b.Number.Uint64() != 0; b = cm.GetHeaderByHash(b.ParentHash) { for b := cm.CurrentBlock(); b != nil && b.Number.Sign() != 0; b = cm.GetHeaderByHash(b.ParentHash) {
if err := validateHeader(bmap[b.Hash()].BlockHeader, b); err != nil { if err := validateHeader(bmap[b.Hash()].BlockHeader, b); err != nil {
return fmt.Errorf("imported block header validation failed: %v", err) return fmt.Errorf("imported block header validation failed: %v", err)
} }