Skip to content

Commit

Permalink
ethereum, mobile: nullable topics for filter criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Sep 25, 2017
1 parent a3da21c commit ad17590
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion interfaces.go
Expand Up @@ -146,7 +146,7 @@ type FilterQuery struct {
// {{}, {B}} matches any topic in first position, B in second position
// {{A}}, {B}} matches topic A in first position, B in second position
// {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
Topics [][]common.Hash
Topics [][]*common.Hash
}

// LogFilterer provides access to contract log events using a one-off query or continuous
Expand Down
55 changes: 55 additions & 0 deletions mobile/common.go
Expand Up @@ -128,6 +128,61 @@ func (h *Hashes) Append(hash *Hash) {
h.hashes = append(h.hashes, hash.hash)
}

// NullableHashes represents a slice of hashes that can have the null value too.
type NullableHashes struct{ hashes []*common.Hash }

// NewNullableHashes creates a slice of uninitialized NullableHashes.
func NewNullableHashes(size int) *NullableHashes {
return &NullableHashes{
hashes: make([]*common.Hash, size),
}
}

// NewNullableHashesEmpty creates an empty slice of NullableHashes values.
func NewNullableHashesEmpty() *NullableHashes {
return NewNullableHashes(0)
}

// Size returns the number of hashes in the slice.
func (h *NullableHashes) Size() int {
return len(h.hashes)
}

// Get returns the hash at the given index from the slice.
func (h *NullableHashes) Get(index int) (hash *Hash, _ error) {
if index < 0 || index >= len(h.hashes) {
return nil, errors.New("index out of bounds")
}
if h.hashes[index] == nil {
return nil, nil
}
return &Hash{*h.hashes[index]}, nil
}

// Set sets the Hash at the given index in the slice.
func (h *NullableHashes) Set(index int, hash *Hash) error {
if index < 0 || index >= len(h.hashes) {
return errors.New("index out of bounds")
}
if hash == nil {
h.hashes[index] = nil
return nil
}
copy := hash.hash
h.hashes[index] = &copy
return nil
}

// Append adds a new Hash element to the end of the slice.
func (h *NullableHashes) Append(hash *Hash) {
if hash == nil {
h.hashes = append(h.hashes, nil)
return
}
copy := hash.hash
h.hashes = append(h.hashes, &copy)
}

// Address represents the 20 byte address of an Ethereum account.
type Address struct {
address common.Address
Expand Down
12 changes: 6 additions & 6 deletions mobile/ethereum.go
Expand Up @@ -85,12 +85,12 @@ func (p *SyncProgress) GetPulledStates() int64 { return int64(p.progress.Pulled
func (p *SyncProgress) GetKnownStates() int64 { return int64(p.progress.KnownStates) }

// Topics is a set of topic lists to filter events with.
type Topics struct{ topics [][]common.Hash }
type Topics struct{ topics [][]*common.Hash }

// NewTopics creates a slice of uninitialized Topics.
func NewTopics(size int) *Topics {
return &Topics{
topics: make([][]common.Hash, size),
topics: make([][]*common.Hash, size),
}
}

Expand All @@ -105,15 +105,15 @@ func (t *Topics) Size() int {
}

// Get returns the topic list at the given index from the slice.
func (t *Topics) Get(index int) (hashes *Hashes, _ error) {
func (t *Topics) Get(index int) (hashes *NullableHashes, _ error) {
if index < 0 || index >= len(t.topics) {
return nil, errors.New("index out of bounds")
}
return &Hashes{t.topics[index]}, nil
return &NullableHashes{t.topics[index]}, nil
}

// Set sets the topic list at the given index in the slice.
func (t *Topics) Set(index int, topics *Hashes) error {
func (t *Topics) Set(index int, topics *NullableHashes) error {
if index < 0 || index >= len(t.topics) {
return errors.New("index out of bounds")
}
Expand All @@ -122,7 +122,7 @@ func (t *Topics) Set(index int, topics *Hashes) error {
}

// Append adds a new topic list to the end of the slice.
func (t *Topics) Append(topics *Hashes) {
func (t *Topics) Append(topics *NullableHashes) {
t.topics = append(t.topics, topics.hashes)
}

Expand Down

0 comments on commit ad17590

Please sign in to comment.