rpc: change BlockNumber constant values to match ethclient #27219 (#992)

This commit is contained in:
Daniel Liu 2025-04-28 10:59:34 +08:00 committed by GitHub
parent 3ae8b8a99d
commit b628212d8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 80 additions and 65 deletions

View file

@ -56,7 +56,7 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
// from, to block number
var test1 FilterCriteria
vector := fmt.Sprintf(`{"fromBlock":"%#x","toBlock":"%#x"}`, fromBlock, toBlock)
vector := fmt.Sprintf(`{"fromBlock":"%v","toBlock":"%v"}`, fromBlock, toBlock)
if err := json.Unmarshal([]byte(vector), &test1); err != nil {
t.Fatal(err)
}

View file

@ -288,6 +288,9 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
// pendingLogs returns the logs matching the filter criteria within the pending block.
func (f *Filter) pendingLogs() ([]*types.Log, error) {
block, receipts := f.sys.backend.PendingBlockAndReceipts()
if block == nil {
return nil, errors.New("pending state not available")
}
if bloomFilter(block.Bloom(), f.addresses, f.topics) {
var unfiltered []*types.Log
for _, r := range receipts {

View file

@ -19,6 +19,7 @@ package filters
import (
"context"
"math/big"
"reflect"
"testing"
"github.com/XinFinOrg/XDPoSChain/common"
@ -28,6 +29,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rpc"
)
func makeReceipt(addr common.Address) *types.Receipt {
@ -157,58 +159,49 @@ func TestFilters(t *testing.T) {
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
}
filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
filter := sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
logs, _ := filter.Logs(context.Background())
if len(logs) != 4 {
t.Error("expected 4 log, got", len(logs))
}
filter = sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}})
logs, _ = filter.Logs(context.Background())
if len(logs) != 1 {
t.Error("expected 1 log, got", len(logs))
}
if len(logs) > 0 && logs[0].Topics[0] != hash3 {
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
}
filter = sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}})
logs, _ = filter.Logs(context.Background())
if len(logs) != 1 {
t.Error("expected 1 log, got", len(logs))
}
if len(logs) > 0 && logs[0].Topics[0] != hash3 {
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
}
filter = sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}})
logs, _ = filter.Logs(context.Background())
if len(logs) != 2 {
t.Error("expected 2 log, got", len(logs))
}
failHash := common.BytesToHash([]byte("fail"))
filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}})
logs, _ = filter.Logs(context.Background())
if len(logs) != 0 {
t.Error("expected 0 log, got", len(logs))
}
failAddr := common.BytesToAddress([]byte("failmenow"))
filter = sys.NewRangeFilter(0, -1, []common.Address{failAddr}, nil)
logs, _ = filter.Logs(context.Background())
if len(logs) != 0 {
t.Error("expected 0 log, got", len(logs))
}
filter = sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{failHash}, {hash1}})
logs, _ = filter.Logs(context.Background())
if len(logs) != 0 {
t.Error("expected 0 log, got", len(logs))
for i, tc := range []struct {
f *Filter
wantHashes []common.Hash
}{
{
sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}),
[]common.Hash{hash3},
}, {
sys.NewRangeFilter(990, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash3}}),
[]common.Hash{hash3},
}, {
sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}),
[]common.Hash{hash1, hash2},
}, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}),
nil,
}, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil),
nil,
}, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}),
nil,
},
} {
logs, _ := tc.f.Logs(context.Background())
var haveHashes []common.Hash
for _, l := range logs {
haveHashes = append(haveHashes, l.Topics[0])
}
if have, want := len(haveHashes), len(tc.wantHashes); have != want {
t.Fatalf("test %d, have %d logs, want %d", i, have, want)
}
if len(haveHashes) == 0 {
continue
}
if !reflect.DeepEqual(tc.wantHashes, haveHashes) {
t.Fatalf("test %d, have %v want %v", i, haveHashes, tc.wantHashes)
}
}
}

View file

@ -534,11 +534,15 @@ func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
}
return hexutil.EncodeBig(number)
// It's negative.
if number.IsInt64() {
return rpc.BlockNumber(number.Int64()).String()
}
// It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
}
// SendOrderTransaction injects a signed transaction into the pending pool for execution.

View file

@ -19,6 +19,7 @@ package gethclient
import (
"context"
"fmt"
"math/big"
"runtime"
"runtime/debug"
@ -183,11 +184,15 @@ func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
}
return hexutil.EncodeBig(number)
// It's negative.
if number.IsInt64() {
return rpc.BlockNumber(number.Int64()).String()
}
// It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
}
func toCallArg(msg ethereum.CallMsg) interface{} {

View file

@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"strings"
@ -64,10 +65,11 @@ type EpochNumber int64
const (
CommittedBlockNumber = BlockNumber(-3)
PendingBlockNumber = BlockNumber(-2)
LatestBlockNumber = BlockNumber(-1)
LatestBlockNumber = BlockNumber(-2)
PendingBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0)
LatestEpochNumber = EpochNumber(-1)
LatestEpochNumber = EpochNumber(-1)
)
// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
@ -108,6 +110,7 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
return nil
}
// Int64 returns the block number as int64.
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}
@ -116,17 +119,24 @@ func (bn BlockNumber) Int64() int64 {
// - "finalized", "latest", "earliest" or "pending" as strings
// - other numbers as hex
func (bn BlockNumber) MarshalText() ([]byte, error) {
return []byte(bn.String()), nil
}
func (bn BlockNumber) String() string {
switch bn {
case EarliestBlockNumber:
return []byte("earliest"), nil
return "earliest"
case LatestBlockNumber:
return []byte("latest"), nil
return "latest"
case PendingBlockNumber:
return []byte("pending"), nil
return "pending"
case CommittedBlockNumber:
return []byte("finalized"), nil
return "committed"
default:
return hexutil.Uint64(bn).MarshalText()
if bn < 0 {
return fmt.Sprintf("<invalid %d>", bn)
}
return hexutil.Uint64(bn).String()
}
}