Skip to content

Commit 01a2c58

Browse files
authored
Revert "rpc: change BlockNumber constant values to match ethclient (ethereum#27219)"
This reverts commit c27eb12.
1 parent a8c67d0 commit 01a2c58

File tree

6 files changed

+47
-54
lines changed

6 files changed

+47
-54
lines changed

eth/filters/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
5656

5757
// from, to block number
5858
var test1 FilterCriteria
59-
vector := fmt.Sprintf(`{"fromBlock":"%v","toBlock":"%v"}`, fromBlock, toBlock)
59+
vector := fmt.Sprintf(`{"fromBlock":"%#x","toBlock":"%#x"}`, fromBlock, toBlock)
6060
if err := json.Unmarshal([]byte(vector), &test1); err != nil {
6161
t.Fatal(err)
6262
}

eth/filters/filter.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
296296
// pendingLogs returns the logs matching the filter criteria within the pending block.
297297
func (f *Filter) pendingLogs() ([]*types.Log, error) {
298298
block, receipts := f.sys.backend.PendingBlockAndReceipts()
299-
if block == nil {
300-
return nil, errors.New("pending state not available")
301-
}
302299
if bloomFilter(block.Bloom(), f.addresses, f.topics) {
303300
var unfiltered []*types.Log
304301
for _, r := range receipts {

eth/filters/filter_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/ethereum/go-ethereum/core/types"
3030
"github.com/ethereum/go-ethereum/crypto"
3131
"github.com/ethereum/go-ethereum/params"
32-
"github.com/ethereum/go-ethereum/rpc"
3332
)
3433

3534
func makeReceipt(addr common.Address) *types.Receipt {
@@ -180,7 +179,7 @@ func TestFilters(t *testing.T) {
180179
// Set block 998 as Finalized (-3)
181180
rawdb.WriteFinalizedBlockHash(db, chain[998].Hash())
182181

183-
filter := sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
182+
filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
184183
logs, _ := filter.Logs(context.Background())
185184
if len(logs) != 4 {
186185
t.Error("expected 4 log, got", len(logs))
@@ -194,36 +193,34 @@ func TestFilters(t *testing.T) {
194193
sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}),
195194
[]common.Hash{hash3},
196195
}, {
197-
sys.NewRangeFilter(990, int64(rpc.LatestBlockNumber), []common.Address{addr}, [][]common.Hash{{hash3}}),
196+
sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}),
198197
[]common.Hash{hash3},
199198
}, {
200199
sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}),
201200
[]common.Hash{hash1, hash2},
202201
}, {
203-
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}),
202+
sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}),
204203
nil,
205204
}, {
206-
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil),
205+
sys.NewRangeFilter(0, -1, []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil),
207206
nil,
208207
}, {
209-
sys.NewRangeFilter(0, int64(rpc.LatestBlockNumber), nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}),
208+
sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}),
210209
nil,
211210
}, {
212-
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), []common.Hash{hash4},
211+
sys.NewRangeFilter(-1, -1, nil, nil), []common.Hash{hash4},
213212
}, {
214-
sys.NewRangeFilter(int64(rpc.FinalizedBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), []common.Hash{hash3, hash4},
213+
sys.NewRangeFilter(-3, -1, nil, nil), []common.Hash{hash3, hash4},
215214
}, {
216-
sys.NewRangeFilter(int64(rpc.FinalizedBlockNumber), int64(rpc.FinalizedBlockNumber), nil, nil), []common.Hash{hash3},
215+
sys.NewRangeFilter(-3, -3, nil, nil), []common.Hash{hash3},
217216
}, {
218-
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.FinalizedBlockNumber), nil, nil), nil,
217+
sys.NewRangeFilter(-1, -3, nil, nil), nil,
219218
}, {
220-
sys.NewRangeFilter(int64(rpc.SafeBlockNumber), int64(rpc.LatestBlockNumber), nil, nil), nil,
219+
sys.NewRangeFilter(-4, -1, nil, nil), nil,
221220
}, {
222-
sys.NewRangeFilter(int64(rpc.SafeBlockNumber), int64(rpc.SafeBlockNumber), nil, nil), nil,
221+
sys.NewRangeFilter(-4, -4, nil, nil), nil,
223222
}, {
224-
sys.NewRangeFilter(int64(rpc.LatestBlockNumber), int64(rpc.SafeBlockNumber), nil, nil), nil,
225-
}, {
226-
sys.NewRangeFilter(int64(rpc.PendingBlockNumber), int64(rpc.PendingBlockNumber), nil, nil), nil,
223+
sys.NewRangeFilter(-1, -4, nil, nil), nil,
227224
},
228225
} {
229226
logs, _ := tc.f.Logs(context.Background())

ethclient/ethclient.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,15 +592,19 @@ func toBlockNumArg(number *big.Int) string {
592592
if number == nil {
593593
return "latest"
594594
}
595-
if number.Sign() >= 0 {
596-
return hexutil.EncodeBig(number)
595+
pending := big.NewInt(-1)
596+
if number.Cmp(pending) == 0 {
597+
return "pending"
597598
}
598-
// It's negative.
599-
if number.IsInt64() {
600-
return rpc.BlockNumber(number.Int64()).String()
599+
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
600+
if number.Cmp(finalized) == 0 {
601+
return "finalized"
601602
}
602-
// It's negative and large, which is invalid.
603-
return fmt.Sprintf("<invalid %d>", number)
603+
safe := big.NewInt(int64(rpc.SafeBlockNumber))
604+
if number.Cmp(safe) == 0 {
605+
return "safe"
606+
}
607+
return hexutil.EncodeBig(number)
604608
}
605609

606610
func toCallArg(msg ethereum.CallMsg) interface{} {

ethclient/gethclient/gethclient.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package gethclient
2020
import (
2121
"context"
2222
"encoding/json"
23-
"fmt"
2423
"math/big"
2524
"runtime"
2625
"runtime/debug"
@@ -208,15 +207,19 @@ func toBlockNumArg(number *big.Int) string {
208207
if number == nil {
209208
return "latest"
210209
}
211-
if number.Sign() >= 0 {
212-
return hexutil.EncodeBig(number)
210+
pending := big.NewInt(-1)
211+
if number.Cmp(pending) == 0 {
212+
return "pending"
213213
}
214-
// It's negative.
215-
if number.IsInt64() {
216-
return rpc.BlockNumber(number.Int64()).String()
214+
finalized := big.NewInt(int64(rpc.FinalizedBlockNumber))
215+
if number.Cmp(finalized) == 0 {
216+
return "finalized"
217217
}
218-
// It's negative and large, which is invalid.
219-
return fmt.Sprintf("<invalid %d>", number)
218+
safe := big.NewInt(int64(rpc.SafeBlockNumber))
219+
if number.Cmp(safe) == 0 {
220+
return "safe"
221+
}
222+
return hexutil.EncodeBig(number)
220223
}
221224

222225
func toCallArg(msg ethereum.CallMsg) interface{} {

rpc/types.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ type BlockNumber int64
6565
const (
6666
SafeBlockNumber = BlockNumber(-4)
6767
FinalizedBlockNumber = BlockNumber(-3)
68-
LatestBlockNumber = BlockNumber(-2)
69-
PendingBlockNumber = BlockNumber(-1)
68+
PendingBlockNumber = BlockNumber(-2)
69+
LatestBlockNumber = BlockNumber(-1)
7070
EarliestBlockNumber = BlockNumber(0)
7171
)
7272

@@ -111,38 +111,30 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
111111
return nil
112112
}
113113

114-
// Int64 returns the block number as int64.
115-
func (bn BlockNumber) Int64() int64 {
116-
return (int64)(bn)
117-
}
118-
119114
// MarshalText implements encoding.TextMarshaler. It marshals:
120115
// - "safe", "finalized", "latest", "earliest" or "pending" as strings
121116
// - other numbers as hex
122117
func (bn BlockNumber) MarshalText() ([]byte, error) {
123-
return []byte(bn.String()), nil
124-
}
125-
126-
func (bn BlockNumber) String() string {
127118
switch bn {
128119
case EarliestBlockNumber:
129-
return "earliest"
120+
return []byte("earliest"), nil
130121
case LatestBlockNumber:
131-
return "latest"
122+
return []byte("latest"), nil
132123
case PendingBlockNumber:
133-
return "pending"
124+
return []byte("pending"), nil
134125
case FinalizedBlockNumber:
135-
return "finalized"
126+
return []byte("finalized"), nil
136127
case SafeBlockNumber:
137-
return "safe"
128+
return []byte("safe"), nil
138129
default:
139-
if bn < 0 {
140-
return fmt.Sprintf("<invalid %d>", bn)
141-
}
142-
return hexutil.Uint64(bn).String()
130+
return hexutil.Uint64(bn).MarshalText()
143131
}
144132
}
145133

134+
func (bn BlockNumber) Int64() int64 {
135+
return (int64)(bn)
136+
}
137+
146138
type BlockNumberOrHash struct {
147139
BlockNumber *BlockNumber `json:"blockNumber,omitempty"`
148140
BlockHash *common.Hash `json:"blockHash,omitempty"`

0 commit comments

Comments
 (0)