bor filter apis

This commit is contained in:
Arpit Temani 2022-08-07 00:24:07 +05:30
parent 062ea8b505
commit 95891a878a
3 changed files with 17 additions and 17 deletions

View file

@ -337,8 +337,7 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
return nil, errors.New("No chain config found. Proper PublicFilterAPI initialization required")
}
// get sprint from bor config
sprint := api.chainConfig.Bor.Sprint
borConfig := api.chainConfig.Bor
var filter *Filter
var borLogsFilter *BorBlockLogsFilter
@ -347,7 +346,7 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
filter = NewBlockFilter(api.backend, *crit.BlockHash, crit.Addresses, crit.Topics)
// Block bor filter
if api.borLogs {
borLogsFilter = NewBorBlockLogsFilter(api.backend, sprint, *crit.BlockHash, crit.Addresses, crit.Topics)
borLogsFilter = NewBorBlockLogsFilter(api.backend, borConfig, *crit.BlockHash, crit.Addresses, crit.Topics)
}
} else {
// Convert the RPC block numbers into internal representations
@ -363,7 +362,7 @@ func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([
filter = NewRangeFilter(api.backend, begin, end, crit.Addresses, crit.Topics)
// Block bor filter
if api.borLogs {
borLogsFilter = NewBorBlockLogsRangeFilter(api.backend, sprint, begin, end, crit.Addresses, crit.Topics)
borLogsFilter = NewBorBlockLogsRangeFilter(api.backend, borConfig, begin, end, crit.Addresses, crit.Topics)
}
}

View file

@ -23,12 +23,12 @@ func (api *PublicFilterAPI) GetBorBlockLogs(ctx context.Context, crit FilterCrit
}
// get sprint from bor config
sprint := api.chainConfig.Bor.Sprint
borConfig := api.chainConfig.Bor
var filter *BorBlockLogsFilter
if crit.BlockHash != nil {
// Block filter requested, construct a single-shot filter
filter = NewBorBlockLogsFilter(api.backend, sprint, *crit.BlockHash, crit.Addresses, crit.Topics)
filter = NewBorBlockLogsFilter(api.backend, borConfig, *crit.BlockHash, crit.Addresses, crit.Topics)
} else {
// Convert the RPC block numbers into internal representations
begin := rpc.LatestBlockNumber.Int64()
@ -40,7 +40,7 @@ func (api *PublicFilterAPI) GetBorBlockLogs(ctx context.Context, crit FilterCrit
end = crit.ToBlock.Int64()
}
// Construct the range filter
filter = NewBorBlockLogsRangeFilter(api.backend, sprint, begin, end, crit.Addresses, crit.Topics)
filter = NewBorBlockLogsRangeFilter(api.backend, borConfig, begin, end, crit.Addresses, crit.Topics)
}
// Run the filter and return all the logs

View file

@ -22,13 +22,14 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)
// BorBlockLogsFilter can be used to retrieve and filter logs.
type BorBlockLogsFilter struct {
backend Backend
sprint uint64
backend Backend
borConfig *params.BorConfig
db ethdb.Database
addresses []common.Address
@ -40,9 +41,9 @@ type BorBlockLogsFilter struct {
// NewBorBlockLogsRangeFilter creates a new filter which uses a bloom filter on blocks to
// figure out whether a particular block is interesting or not.
func NewBorBlockLogsRangeFilter(backend Backend, sprint uint64, begin, end int64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
func NewBorBlockLogsRangeFilter(backend Backend, borConfig *params.BorConfig, begin, end int64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
// Create a generic filter and convert it into a range filter
filter := newBorBlockLogsFilter(backend, sprint, addresses, topics)
filter := newBorBlockLogsFilter(backend, borConfig, addresses, topics)
filter.begin = begin
filter.end = end
@ -51,19 +52,19 @@ func NewBorBlockLogsRangeFilter(backend Backend, sprint uint64, begin, end int64
// NewBorBlockLogsFilter creates a new filter which directly inspects the contents of
// a block to figure out whether it is interesting or not.
func NewBorBlockLogsFilter(backend Backend, sprint uint64, block common.Hash, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
func NewBorBlockLogsFilter(backend Backend, borConfig *params.BorConfig, block common.Hash, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
// Create a generic filter and convert it into a block filter
filter := newBorBlockLogsFilter(backend, sprint, addresses, topics)
filter := newBorBlockLogsFilter(backend, borConfig, addresses, topics)
filter.block = block
return filter
}
// 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.
func newBorBlockLogsFilter(backend Backend, sprint uint64, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
func newBorBlockLogsFilter(backend Backend, borConfig *params.BorConfig, addresses []common.Address, topics [][]common.Hash) *BorBlockLogsFilter {
return &BorBlockLogsFilter{
backend: backend,
sprint: sprint,
borConfig: borConfig,
addresses: addresses,
topics: topics,
db: backend.ChainDb(),
@ -94,7 +95,7 @@ func (f *BorBlockLogsFilter) Logs(ctx context.Context) ([]*types.Log, error) {
}
// adjust begin for sprint
f.begin = currentSprintEnd(f.sprint, f.begin)
f.begin = currentSprintEnd(f.borConfig.CalculateSprint(uint64(f.begin)), f.begin)
end := f.end
if f.end == -1 {
@ -146,5 +147,5 @@ func currentSprintEnd(sprint uint64, n int64) int64 {
return n
}
return n + 64 - m
return n + int64(sprint) - m
}