mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-13 15:33:47 +00:00
chg: use sprint from genesis
This commit is contained in:
parent
7cea144782
commit
be23d02c9e
4 changed files with 46 additions and 13 deletions
|
|
@ -297,6 +297,13 @@ func (s *Ethereum) APIs() []rpc.API {
|
||||||
// Append any APIs exposed explicitly by the consensus engine
|
// Append any APIs exposed explicitly by the consensus engine
|
||||||
apis = append(apis, s.engine.APIs(s.BlockChain())...)
|
apis = append(apis, s.engine.APIs(s.BlockChain())...)
|
||||||
|
|
||||||
|
// BOR change starts
|
||||||
|
// set genesis to public filter api
|
||||||
|
publicFilterAPI := filters.NewPublicFilterAPI(s.APIBackend, false)
|
||||||
|
// avoiding constructor changed by introducing new method to set genesis
|
||||||
|
publicFilterAPI.SetChainConfig(s.blockchain.Config())
|
||||||
|
// BOR change ends
|
||||||
|
|
||||||
// Append all the local APIs and return
|
// Append all the local APIs and return
|
||||||
return append(apis, []rpc.API{
|
return append(apis, []rpc.API{
|
||||||
{
|
{
|
||||||
|
|
@ -322,7 +329,7 @@ func (s *Ethereum) APIs() []rpc.API {
|
||||||
}, {
|
}, {
|
||||||
Namespace: "eth",
|
Namespace: "eth",
|
||||||
Version: "1.0",
|
Version: "1.0",
|
||||||
Service: filters.NewPublicFilterAPI(s.APIBackend, false),
|
Service: publicFilterAPI, // BOR related change
|
||||||
Public: true,
|
Public: true,
|
||||||
}, {
|
}, {
|
||||||
Namespace: "admin",
|
Namespace: "admin",
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -59,6 +60,8 @@ type PublicFilterAPI struct {
|
||||||
events *EventSystem
|
events *EventSystem
|
||||||
filtersMu sync.Mutex
|
filtersMu sync.Mutex
|
||||||
filters map[rpc.ID]*filter
|
filters map[rpc.ID]*filter
|
||||||
|
|
||||||
|
chainConfig *params.ChainConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPublicFilterAPI returns a new PublicFilterAPI instance.
|
// NewPublicFilterAPI returns a new PublicFilterAPI instance.
|
||||||
|
|
@ -324,13 +327,20 @@ func (api *PublicFilterAPI) NewFilter(crit FilterCriteria) (rpc.ID, error) {
|
||||||
//
|
//
|
||||||
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
|
||||||
func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*types.Log, error) {
|
func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*types.Log, error) {
|
||||||
|
if api.chainConfig == nil {
|
||||||
|
return nil, errors.New("No chain config found. Proper PublicFilterAPI initialization required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// get sprint from bor config
|
||||||
|
sprint := api.chainConfig.Bor.Sprint
|
||||||
|
|
||||||
var filter *Filter
|
var filter *Filter
|
||||||
var borLogsFilter *BorBlockLogsFilter
|
var borLogsFilter *BorBlockLogsFilter
|
||||||
if crit.BlockHash != nil {
|
if crit.BlockHash != nil {
|
||||||
// Block filter requested, construct a single-shot filter
|
// Block filter requested, construct a single-shot filter
|
||||||
filter = NewBlockFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
|
filter = NewBlockFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
|
||||||
// Block bor filter
|
// Block bor filter
|
||||||
borLogsFilter = NewBorBlockLogsFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
|
borLogsFilter = NewBorBlockLogsFilter(api.backend, sprint, *crit.BlockHash, crit.Addresses, crit.Topics)
|
||||||
} else {
|
} else {
|
||||||
// Convert the RPC block numbers into internal representations
|
// Convert the RPC block numbers into internal representations
|
||||||
begin := rpc.LatestBlockNumber.Int64()
|
begin := rpc.LatestBlockNumber.Int64()
|
||||||
|
|
@ -344,7 +354,7 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
|
||||||
// Construct the range filter
|
// Construct the range filter
|
||||||
filter = NewRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
|
filter = NewRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
|
||||||
// Block bor filter
|
// Block bor filter
|
||||||
borLogsFilter = NewBorBlockLogsRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
|
borLogsFilter = NewBorBlockLogsRangeFilter(api.backend, sprint, begin, end, crit.Addresses, crit.Topics)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the filter and return all the logs
|
// Run the filter and return all the logs
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,32 @@ package filters
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
ethereum "github.com/ethereum/go-ethereum"
|
ethereum "github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetChainConfig sets chain config
|
||||||
|
func (api *PublicFilterAPI) SetChainConfig(chainConfig *params.ChainConfig) {
|
||||||
|
api.chainConfig = chainConfig
|
||||||
|
}
|
||||||
|
|
||||||
func (api *PublicFilterAPI) GetBorBlockLogs(ctx context.Context, crit FilterCriteria) ([]*types.Log, error) {
|
func (api *PublicFilterAPI) GetBorBlockLogs(ctx context.Context, crit FilterCriteria) ([]*types.Log, error) {
|
||||||
|
if api.chainConfig == nil {
|
||||||
|
return nil, errors.New("No chain config found. Proper PublicFilterAPI initialization required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// get sprint from bor config
|
||||||
|
sprint := api.chainConfig.Bor.Sprint
|
||||||
|
|
||||||
var filter *BorBlockLogsFilter
|
var filter *BorBlockLogsFilter
|
||||||
if crit.BlockHash != nil {
|
if crit.BlockHash != nil {
|
||||||
// Block filter requested, construct a single-shot filter
|
// Block filter requested, construct a single-shot filter
|
||||||
filter = NewBorBlockLogsFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
|
filter = NewBorBlockLogsFilter(api.backend, sprint, *crit.BlockHash, crit.Addresses, crit.Topics)
|
||||||
} else {
|
} else {
|
||||||
// Convert the RPC block numbers into internal representations
|
// Convert the RPC block numbers into internal representations
|
||||||
begin := rpc.LatestBlockNumber.Int64()
|
begin := rpc.LatestBlockNumber.Int64()
|
||||||
|
|
@ -26,7 +40,7 @@ func (api *PublicFilterAPI) GetBorBlockLogs(ctx context.Context, crit FilterCrit
|
||||||
end = crit.ToBlock.Int64()
|
end = crit.ToBlock.Int64()
|
||||||
}
|
}
|
||||||
// Construct the range filter
|
// Construct the range filter
|
||||||
filter = NewBorBlockLogsRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
|
filter = NewBorBlockLogsRangeFilter(api.backend, sprint, begin, end, crit.Addresses, crit.Topics)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the filter and return all the logs
|
// Run the filter and return all the logs
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import (
|
||||||
// BorBlockLogsFilter can be used to retrieve and filter logs.
|
// BorBlockLogsFilter can be used to retrieve and filter logs.
|
||||||
type BorBlockLogsFilter struct {
|
type BorBlockLogsFilter struct {
|
||||||
backend Backend
|
backend Backend
|
||||||
|
sprint uint64
|
||||||
|
|
||||||
db ethdb.Database
|
db ethdb.Database
|
||||||
addresses []common.Address
|
addresses []common.Address
|
||||||
|
|
@ -40,9 +41,9 @@ type BorBlockLogsFilter struct {
|
||||||
|
|
||||||
// NewBorBlockLogsRangeFilter creates a new filter which uses a bloom filter on blocks to
|
// NewBorBlockLogsRangeFilter creates a new filter which uses a bloom filter on blocks to
|
||||||
// figure out whether a particular block is interesting or not.
|
// figure out whether a particular block is interesting or not.
|
||||||
func NewBorBlockLogsRangeFilter(backend Backend, begin, end int64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
func NewBorBlockLogsRangeFilter(backend Backend, sprint uint64, begin, end int64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
||||||
// Create a generic filter and convert it into a range filter
|
// Create a generic filter and convert it into a range filter
|
||||||
filter := newBorBlockLogsFilter(backend, addresses, topics)
|
filter := newBorBlockLogsFilter(backend, sprint, addresses, topics)
|
||||||
filter.begin = begin
|
filter.begin = begin
|
||||||
filter.end = end
|
filter.end = end
|
||||||
|
|
||||||
|
|
@ -51,18 +52,19 @@ func NewBorBlockLogsRangeFilter(backend Backend, begin, end int64, addresses []c
|
||||||
|
|
||||||
// NewBorBlockLogsFilter creates a new filter which directly inspects the contents of
|
// NewBorBlockLogsFilter creates a new filter which directly inspects the contents of
|
||||||
// a block to figure out whether it is interesting or not.
|
// a block to figure out whether it is interesting or not.
|
||||||
func NewBorBlockLogsFilter(backend Backend, block common.Hash, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
func NewBorBlockLogsFilter(backend Backend, sprint uint64, block common.Hash, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
||||||
// Create a generic filter and convert it into a block filter
|
// Create a generic filter and convert it into a block filter
|
||||||
filter := newBorBlockLogsFilter(backend, addresses, topics)
|
filter := newBorBlockLogsFilter(backend, sprint, addresses, topics)
|
||||||
filter.block = block
|
filter.block = block
|
||||||
return filter
|
return filter
|
||||||
}
|
}
|
||||||
|
|
||||||
// newBorBlockLogsFilter creates a generic filter that can either filter based on a block hash,
|
// newBorBlockLogsFilter creates a generic filter that can either filter based on a block hash,
|
||||||
// or based on range queries. The search criteria needs to be explicitly set.
|
// or based on range queries. The search criteria needs to be explicitly set.
|
||||||
func newBorBlockLogsFilter(backend Backend, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
func newBorBlockLogsFilter(backend Backend, sprint uint64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
|
||||||
return &BorBlockLogsFilter{
|
return &BorBlockLogsFilter{
|
||||||
backend: backend,
|
backend: backend,
|
||||||
|
sprint: sprint,
|
||||||
addresses: addresses,
|
addresses: addresses,
|
||||||
topics: topics,
|
topics: topics,
|
||||||
db: backend.ChainDb(),
|
db: backend.ChainDb(),
|
||||||
|
|
@ -97,7 +99,7 @@ func (f *BorBlockLogsFilter) Logs(ctx context.Context) ([]*types.Log, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjust begin for sprint
|
// adjust begin for sprint
|
||||||
f.begin = currentSprintEnd(f.begin)
|
f.begin = currentSprintEnd(f.sprint, f.begin)
|
||||||
|
|
||||||
end := f.end
|
end := f.end
|
||||||
if f.end == -1 {
|
if f.end == -1 {
|
||||||
|
|
@ -143,8 +145,8 @@ func (f *BorBlockLogsFilter) borBlockLogs(ctx context.Context, receipt *types.Re
|
||||||
return logs, nil
|
return logs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentSprintEnd(n int64) int64 {
|
func currentSprintEnd(sprint uint64, n int64) int64 {
|
||||||
m := n % 64
|
m := n % int64(sprint)
|
||||||
if m == 0 {
|
if m == 0 {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue