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

This reverts commit c27eb1239b.
This commit is contained in:
devopsbo3 2023-11-10 12:27:53 -06:00 committed by GitHub
parent a8c67d066d
commit 01a2c585b5
6 changed files with 47 additions and 54 deletions

View file

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

View file

@ -296,9 +296,6 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
// pendingLogs returns the logs matching the filter criteria within the pending block. // pendingLogs returns the logs matching the filter criteria within the pending block.
func (f *Filter) pendingLogs() ([]*types.Log, error) { func (f *Filter) pendingLogs() ([]*types.Log, error) {
block, receipts := f.sys.backend.PendingBlockAndReceipts() 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) { if bloomFilter(block.Bloom(), f.addresses, f.topics) {
var unfiltered []*types.Log var unfiltered []*types.Log
for _, r := range receipts { for _, r := range receipts {

View file

@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
) )
func makeReceipt(addr common.Address) *types.Receipt { func makeReceipt(addr common.Address) *types.Receipt {
@ -180,7 +179,7 @@ func TestFilters(t *testing.T) {
// Set block 998 as Finalized (-3) // Set block 998 as Finalized (-3)
rawdb.WriteFinalizedBlockHash(db, chain[998].Hash()) rawdb.WriteFinalizedBlockHash(db, chain[998].Hash())
filter := sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}) filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
logs, _ := filter.Logs(context.Background()) logs, _ := filter.Logs(context.Background())
if len(logs) != 4 { if len(logs) != 4 {
t.Error("expected 4 log, got", len(logs)) t.Error("expected 4 log, got", len(logs))
@ -194,36 +193,34 @@ func TestFilters(t *testing.T) {
sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}), sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}),
[]common.Hash{hash3}, []common.Hash{hash3},
}, { }, {
sys.NewRangeFilter(990, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash3}}), sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}),
[]common.Hash{hash3}, []common.Hash{hash3},
}, { }, {
sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}), sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}),
[]common.Hash{hash1, hash2}, []common.Hash{hash1, hash2},
}, { }, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}), sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}),
nil, nil,
}, { }, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil), sys.NewRangeFilter(0, -1, []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil),
nil, nil,
}, { }, {
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}), sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}),
nil, nil,
}, { }, {
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), []common.Hash{hash4}, sys.NewRangeFilter(-1, -1, nil, nil), []common.Hash{hash4},
}, { }, {
sys.NewRangeFilter(int64(rpc.FinalizedBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), []common.Hash{hash3, hash4}, sys.NewRangeFilter(-3, -1, nil, nil), []common.Hash{hash3, hash4},
}, { }, {
sys.NewRangeFilter(int64(rpc.FinalizedBlockNumber), int64(rpc.FinalizedBlockNumber), nil, nil), []common.Hash{hash3}, sys.NewRangeFilter(-3, -3, nil, nil), []common.Hash{hash3},
}, { }, {
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.FinalizedBlockNumber), nil, nil), nil, sys.NewRangeFilter(-1, -3, nil, nil), nil,
}, { }, {
sys.NewRangeFilter(int64(rpc.SafeBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), nil, sys.NewRangeFilter(-4, -1, nil, nil), nil,
}, { }, {
sys.NewRangeFilter(int64(rpc.SafeBlockNumber), int64(rpc.SafeBlockNumber), nil, nil), nil, sys.NewRangeFilter(-4, -4, nil, nil), nil,
}, { }, {
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.SafeBlockNumber), nil, nil), nil, sys.NewRangeFilter(-1, -4, nil, nil), nil,
}, {
sys.NewRangeFilter(int64(rpc.PendingBlockNumber), int64(rpc.PendingBlockNumber), nil, nil), nil,
}, },
} { } {
logs, _ := tc.f.Logs(context.Background()) logs, _ := tc.f.Logs(context.Background())

View file

@ -592,15 +592,19 @@ func toBlockNumArg(number *big.Int) string {
if number == nil { if number == nil {
return "latest" return "latest"
} }
if number.Sign() >= 0 { pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
}
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
if number.Cmp(finalized) == 0 {
return "finalized"
}
safe := big.NewInt(int64(rpc.SafeBlockNumber))
if number.Cmp(safe) == 0 {
return "safe"
}
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{} { func toCallArg(msg ethereum.CallMsg) interface{} {

View file

@ -20,7 +20,6 @@ package gethclient
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"math/big" "math/big"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
@ -208,15 +207,19 @@ func toBlockNumArg(number *big.Int) string {
if number == nil { if number == nil {
return "latest" return "latest"
} }
if number.Sign() >= 0 { pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
}
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
if number.Cmp(finalized) == 0 {
return "finalized"
}
safe := big.NewInt(int64(rpc.SafeBlockNumber))
if number.Cmp(safe) == 0 {
return "safe"
}
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{} { func toCallArg(msg ethereum.CallMsg) interface{} {

View file

@ -65,8 +65,8 @@ type BlockNumber int64
const ( const (
SafeBlockNumber = BlockNumber(-4) SafeBlockNumber = BlockNumber(-4)
FinalizedBlockNumber = BlockNumber(-3) FinalizedBlockNumber = BlockNumber(-3)
LatestBlockNumber = BlockNumber(-2) PendingBlockNumber = BlockNumber(-2)
PendingBlockNumber = BlockNumber(-1) LatestBlockNumber = BlockNumber(-1)
EarliestBlockNumber = BlockNumber(0) EarliestBlockNumber = BlockNumber(0)
) )
@ -111,38 +111,30 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// Int64 returns the block number as int64.
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}
// MarshalText implements encoding.TextMarshaler. It marshals: // MarshalText implements encoding.TextMarshaler. It marshals:
// - "safe", "finalized", "latest", "earliest" or "pending" as strings // - "safe", "finalized", "latest", "earliest" or "pending" as strings
// - other numbers as hex // - other numbers as hex
func (bn BlockNumber) MarshalText() ([]byte, error) { func (bn BlockNumber) MarshalText() ([]byte, error) {
return []byte(bn.String()), nil
}
func (bn BlockNumber) String() string {
switch bn { switch bn {
case EarliestBlockNumber: case EarliestBlockNumber:
return "earliest" return []byte("earliest"), nil
case LatestBlockNumber: case LatestBlockNumber:
return "latest" return []byte("latest"), nil
case PendingBlockNumber: case PendingBlockNumber:
return "pending" return []byte("pending"), nil
case FinalizedBlockNumber: case FinalizedBlockNumber:
return "finalized" return []byte("finalized"), nil
case SafeBlockNumber: case SafeBlockNumber:
return "safe" return []byte("safe"), nil
default: default:
if bn < 0 { return hexutil.Uint64(bn).MarshalText()
return fmt.Sprintf("<invalid %d>", bn)
}
return hexutil.Uint64(bn).String()
} }
} }
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}
type BlockNumberOrHash struct { type BlockNumberOrHash struct {
BlockNumber *BlockNumber `json:"blockNumber,omitempty"` BlockNumber *BlockNumber `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"` BlockHash *common.Hash `json:"blockHash,omitempty"`