Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EthHash compatible type for subscription and filter IDs #9808

Merged
merged 5 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions api/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,11 @@ func init() {
ethFeeHistoryReward := [][]api.EthBigInt{}
addExample(&ethFeeHistoryReward)

addExample(api.EthFilterID("c5564560217c43e4bc0484df655e9019"))
addExample(api.EthSubscriptionID("b62df77831484129adf6682332ad0725"))
filterid, _ := api.EthHashFromHex("0x5CbEeC012345673f25E309Cc264f240bb0664031")
addExample(api.EthFilterID(filterid))

subid, _ := api.EthHashFromHex("0x5CbEeCF99d3fDB301234567c264f240bb0664031")
addExample(api.EthSubscriptionID(subid))

pstring := func(s string) *string { return &s }
addExample(&api.EthFilterSpec{
Expand Down
5 changes: 2 additions & 3 deletions api/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,10 @@ type EthFeeHistory struct {
Reward *[][]EthBigInt `json:"reward,omitempty"`
}

// An opaque identifier generated by the Lotus node to refer to an installed filter.
type EthFilterID string
type EthFilterID EthHash

// An opaque identifier generated by the Lotus node to refer to an active subscription.
type EthSubscriptionID string
type EthSubscriptionID EthHash

type EthFilterSpec struct {
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
Expand Down
2 changes: 1 addition & 1 deletion api/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestEthFilterResultMarshalJSON(t *testing.T) {
BlockHash: hash2,
BlockNumber: 53,
Topics: []EthBytes{hash1[:]},
Data: hash1[:],
Data: EthBytes(hash1[:]),
Address: addr,
}
logjson, err := json.Marshal(log)
Expand Down
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
19 changes: 9 additions & 10 deletions chain/events/filter/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

"github.com/google/uuid"
"github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
Expand All @@ -24,7 +23,7 @@ import (
const indexed uint8 = 0x01

type EventFilter struct {
id string
id types.FilterID
minHeight abi.ChainEpoch // minimum epoch to apply filter or -1 if no minimum
maxHeight abi.ChainEpoch // maximum epoch to apply filter or -1 if no maximum
tipsetCid cid.Cid
Expand All @@ -51,7 +50,7 @@ type CollectedEvent struct {
MsgCid cid.Cid // cid of message that produced event
}

func (f *EventFilter) ID() string {
func (f *EventFilter) ID() types.FilterID {
return f.id
}

Expand Down Expand Up @@ -290,7 +289,7 @@ type EventFilterManager struct {
EventIndex *EventIndex

mu sync.Mutex // guards mutations to filters
filters map[string]*EventFilter
filters map[types.FilterID]*EventFilter
currentHeight abi.ChainEpoch
}

Expand Down Expand Up @@ -365,13 +364,13 @@ func (m *EventFilterManager) Install(ctx context.Context, minHeight, maxHeight a
return nil, xerrors.Errorf("historic event index disabled")
}

id, err := uuid.NewRandom()
id, err := newFilterID()
if err != nil {
return nil, xerrors.Errorf("new uuid: %w", err)
return nil, xerrors.Errorf("new filter id: %w", err)
}

f := &EventFilter{
id: id.String(),
id: id,
minHeight: minHeight,
maxHeight: maxHeight,
tipsetCid: tipsetCid,
Expand All @@ -389,15 +388,15 @@ func (m *EventFilterManager) Install(ctx context.Context, minHeight, maxHeight a

m.mu.Lock()
if m.filters == nil {
m.filters = make(map[string]*EventFilter)
m.filters = make(map[types.FilterID]*EventFilter)
}
m.filters[id.String()] = f
m.filters[id] = f
m.mu.Unlock()

return f, nil
}

func (m *EventFilterManager) Remove(ctx context.Context, id string) error {
func (m *EventFilterManager) Remove(ctx context.Context, id types.FilterID) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, found := m.filters[id]; !found {
Expand Down
19 changes: 9 additions & 10 deletions chain/events/filter/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sync"
"time"

"github.com/google/uuid"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

Expand All @@ -14,7 +13,7 @@ import (
)

type MemPoolFilter struct {
id string
id types.FilterID
maxResults int // maximum number of results to collect, 0 is unlimited
ch chan<- interface{}

Expand All @@ -25,7 +24,7 @@ type MemPoolFilter struct {

var _ Filter = (*MemPoolFilter)(nil)

func (f *MemPoolFilter) ID() string {
func (f *MemPoolFilter) ID() types.FilterID {
return f.id
}

Expand Down Expand Up @@ -79,7 +78,7 @@ type MemPoolFilterManager struct {
MaxFilterResults int

mu sync.Mutex // guards mutations to filters
filters map[string]*MemPoolFilter
filters map[types.FilterID]*MemPoolFilter
}

func (m *MemPoolFilterManager) WaitForMpoolUpdates(ctx context.Context, ch <-chan api.MpoolUpdate) {
Expand Down Expand Up @@ -113,27 +112,27 @@ func (m *MemPoolFilterManager) processUpdate(ctx context.Context, u api.MpoolUpd
}

func (m *MemPoolFilterManager) Install(ctx context.Context) (*MemPoolFilter, error) {
id, err := uuid.NewRandom()
id, err := newFilterID()
if err != nil {
return nil, xerrors.Errorf("new uuid: %w", err)
return nil, xerrors.Errorf("new filter id: %w", err)
}

f := &MemPoolFilter{
id: id.String(),
id: id,
maxResults: m.MaxFilterResults,
}

m.mu.Lock()
if m.filters == nil {
m.filters = make(map[string]*MemPoolFilter)
m.filters = make(map[types.FilterID]*MemPoolFilter)
}
m.filters[id.String()] = f
m.filters[id] = f
m.mu.Unlock()

return f, nil
}

func (m *MemPoolFilterManager) Remove(ctx context.Context, id string) error {
func (m *MemPoolFilterManager) Remove(ctx context.Context, id types.FilterID) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, found := m.filters[id]; !found {
Expand Down
29 changes: 22 additions & 7 deletions chain/events/filter/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import (
"errors"
"sync"
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/filecoin-project/lotus/chain/types"
)

type Filter interface {
ID() string
ID() types.FilterID
LastTaken() time.Time
SetSubChannel(chan<- interface{})
ClearSubChannel()
}

type FilterStore interface {
Add(context.Context, Filter) error
Get(context.Context, string) (Filter, error)
Remove(context.Context, string) error
Get(context.Context, types.FilterID) (Filter, error)
Remove(context.Context, types.FilterID) error
NotTakenSince(when time.Time) []Filter // returns a list of filters that have not had their collected results taken
}

Expand All @@ -27,18 +32,28 @@ var (
ErrMaximumNumberOfFilters = errors.New("maximum number of filters registered")
)

func newFilterID() (types.FilterID, error) {
rawid, err := uuid.NewRandom()
if err != nil {
return types.FilterID{}, xerrors.Errorf("new uuid: %w", err)
}
id := types.FilterID{}
copy(id[:], rawid[:]) // uuid is 16 bytes, the last 16 bytes are zeroed
return id, nil
}

type memFilterStore struct {
max int
mu sync.Mutex
filters map[string]Filter
filters map[types.FilterID]Filter
}

var _ FilterStore = (*memFilterStore)(nil)

func NewMemFilterStore(maxFilters int) FilterStore {
return &memFilterStore{
max: maxFilters,
filters: make(map[string]Filter),
filters: make(map[types.FilterID]Filter),
}
}

Expand All @@ -57,7 +72,7 @@ func (m *memFilterStore) Add(_ context.Context, f Filter) error {
return nil
}

func (m *memFilterStore) Get(_ context.Context, id string) (Filter, error) {
func (m *memFilterStore) Get(_ context.Context, id types.FilterID) (Filter, error) {
m.mu.Lock()
f, found := m.filters[id]
m.mu.Unlock()
Expand All @@ -67,7 +82,7 @@ func (m *memFilterStore) Get(_ context.Context, id string) (Filter, error) {
return f, nil
}

func (m *memFilterStore) Remove(_ context.Context, id string) error {
func (m *memFilterStore) Remove(_ context.Context, id types.FilterID) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand Down
19 changes: 9 additions & 10 deletions chain/events/filter/tipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"sync"
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/filecoin-project/lotus/chain/types"
)

type TipSetFilter struct {
id string
id types.FilterID
maxResults int // maximum number of results to collect, 0 is unlimited
ch chan<- interface{}

Expand All @@ -23,7 +22,7 @@ type TipSetFilter struct {

var _ Filter = (*TipSetFilter)(nil)

func (f *TipSetFilter) ID() string {
func (f *TipSetFilter) ID() types.FilterID {
return f.id
}

Expand Down Expand Up @@ -77,7 +76,7 @@ type TipSetFilterManager struct {
MaxFilterResults int

mu sync.Mutex // guards mutations to filters
filters map[string]*TipSetFilter
filters map[types.FilterID]*TipSetFilter
}

func (m *TipSetFilterManager) Apply(ctx context.Context, from, to *types.TipSet) error {
Expand All @@ -100,27 +99,27 @@ func (m *TipSetFilterManager) Revert(ctx context.Context, from, to *types.TipSet
}

func (m *TipSetFilterManager) Install(ctx context.Context) (*TipSetFilter, error) {
id, err := uuid.NewRandom()
id, err := newFilterID()
if err != nil {
return nil, xerrors.Errorf("new uuid: %w", err)
return nil, xerrors.Errorf("new filter id: %w", err)
}

f := &TipSetFilter{
id: id.String(),
id: id,
maxResults: m.MaxFilterResults,
}

m.mu.Lock()
if m.filters == nil {
m.filters = make(map[string]*TipSetFilter)
m.filters = make(map[types.FilterID]*TipSetFilter)
}
m.filters[id.String()] = f
m.filters[id] = f
m.mu.Unlock()

return f, nil
}

func (m *TipSetFilterManager) Remove(ctx context.Context, id string) error {
func (m *TipSetFilterManager) Remove(ctx context.Context, id types.FilterID) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, found := m.filters[id]; !found {
Expand Down
2 changes: 2 additions & 0 deletions chain/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ type EventEntry struct {
// Any DAG-CBOR encodeable type.
Value []byte
}

type FilterID [32]byte // compatible with EthHash
Loading