mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 19:00:46 +00:00
This commit is contained in:
parent
3ae8b8a99d
commit
b628212d8e
6 changed files with 80 additions and 65 deletions
|
|
@ -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":"%#x","toBlock":"%#x"}`, fromBlock, toBlock)
|
vector := fmt.Sprintf(`{"fromBlock":"%v","toBlock":"%v"}`, 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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.
|
// 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 {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ package filters
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/common"
|
"github.com/XinFinOrg/XDPoSChain/common"
|
||||||
|
|
@ -28,6 +29,7 @@ import (
|
||||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
"github.com/XinFinOrg/XDPoSChain/crypto"
|
"github.com/XinFinOrg/XDPoSChain/crypto"
|
||||||
"github.com/XinFinOrg/XDPoSChain/params"
|
"github.com/XinFinOrg/XDPoSChain/params"
|
||||||
|
"github.com/XinFinOrg/XDPoSChain/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeReceipt(addr common.Address) *types.Receipt {
|
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])
|
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())
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
filter = sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}})
|
for i, tc := range []struct {
|
||||||
logs, _ = filter.Logs(context.Background())
|
f *Filter
|
||||||
if len(logs) != 1 {
|
wantHashes []common.Hash
|
||||||
t.Error("expected 1 log, got", len(logs))
|
}{
|
||||||
|
{
|
||||||
|
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 len(logs) > 0 && logs[0].Topics[0] != hash3 {
|
if have, want := len(haveHashes), len(tc.wantHashes); have != want {
|
||||||
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
|
t.Fatalf("test %d, have %d logs, want %d", i, have, want)
|
||||||
}
|
}
|
||||||
|
if len(haveHashes) == 0 {
|
||||||
filter = sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}})
|
continue
|
||||||
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 {
|
if !reflect.DeepEqual(tc.wantHashes, haveHashes) {
|
||||||
t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
|
t.Fatalf("test %d, have %v want %v", i, haveHashes, tc.wantHashes)
|
||||||
}
|
}
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -534,11 +534,15 @@ func toBlockNumArg(number *big.Int) string {
|
||||||
if number == nil {
|
if number == nil {
|
||||||
return "latest"
|
return "latest"
|
||||||
}
|
}
|
||||||
pending := big.NewInt(-1)
|
if number.Sign() >= 0 {
|
||||||
if number.Cmp(pending) == 0 {
|
|
||||||
return "pending"
|
|
||||||
}
|
|
||||||
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.
|
// SendOrderTransaction injects a signed transaction into the pending pool for execution.
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ package gethclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
@ -183,11 +184,15 @@ func toBlockNumArg(number *big.Int) string {
|
||||||
if number == nil {
|
if number == nil {
|
||||||
return "latest"
|
return "latest"
|
||||||
}
|
}
|
||||||
pending := big.NewInt(-1)
|
if number.Sign() >= 0 {
|
||||||
if number.Cmp(pending) == 0 {
|
|
||||||
return "pending"
|
|
||||||
}
|
|
||||||
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{} {
|
||||||
|
|
|
||||||
24
rpc/types.go
24
rpc/types.go
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -64,9 +65,10 @@ type EpochNumber int64
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CommittedBlockNumber = BlockNumber(-3)
|
CommittedBlockNumber = BlockNumber(-3)
|
||||||
PendingBlockNumber = BlockNumber(-2)
|
LatestBlockNumber = BlockNumber(-2)
|
||||||
LatestBlockNumber = BlockNumber(-1)
|
PendingBlockNumber = BlockNumber(-1)
|
||||||
EarliestBlockNumber = BlockNumber(0)
|
EarliestBlockNumber = BlockNumber(0)
|
||||||
|
|
||||||
LatestEpochNumber = EpochNumber(-1)
|
LatestEpochNumber = EpochNumber(-1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -108,6 +110,7 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int64 returns the block number as int64.
|
||||||
func (bn BlockNumber) Int64() int64 {
|
func (bn BlockNumber) Int64() int64 {
|
||||||
return (int64)(bn)
|
return (int64)(bn)
|
||||||
}
|
}
|
||||||
|
|
@ -116,17 +119,24 @@ func (bn BlockNumber) Int64() int64 {
|
||||||
// - "finalized", "latest", "earliest" or "pending" as strings
|
// - "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 []byte("earliest"), nil
|
return "earliest"
|
||||||
case LatestBlockNumber:
|
case LatestBlockNumber:
|
||||||
return []byte("latest"), nil
|
return "latest"
|
||||||
case PendingBlockNumber:
|
case PendingBlockNumber:
|
||||||
return []byte("pending"), nil
|
return "pending"
|
||||||
case CommittedBlockNumber:
|
case CommittedBlockNumber:
|
||||||
return []byte("finalized"), nil
|
return "committed"
|
||||||
default:
|
default:
|
||||||
return hexutil.Uint64(bn).MarshalText()
|
if bn < 0 {
|
||||||
|
return fmt.Sprintf("<invalid %d>", bn)
|
||||||
|
}
|
||||||
|
return hexutil.Uint64(bn).String()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue