-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscribe to events with multiple accounts and deduplicate (#1627)
- Loading branch information
Showing
7 changed files
with
197 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tools/walletextension/subscriptions/deduplication_circular_buffer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package subscriptions | ||
|
||
import "github.com/ethereum/go-ethereum/common" | ||
|
||
// LogKey uniquely represents a log (consists of BlockHash, TxHash, and Index) | ||
type LogKey struct { | ||
BlockHash common.Hash // Not necessary, but can be helpful in edge case of block reorg. | ||
TxHash common.Hash | ||
Index uint | ||
} | ||
|
||
// CircularBuffer is a data structure that uses a single, fixed-size buffer as if it was connected end-to-end. | ||
type CircularBuffer struct { | ||
data []LogKey | ||
size int | ||
end int | ||
} | ||
|
||
// NewCircularBuffer initializes a new CircularBuffer of the given size. | ||
func NewCircularBuffer(size int) *CircularBuffer { | ||
return &CircularBuffer{ | ||
data: make([]LogKey, size), | ||
size: size, | ||
end: 0, | ||
} | ||
} | ||
|
||
// Push adds a new LogKey to the end of the buffer. If the buffer is full, | ||
// it overwrites the oldest data with the new LogKey. | ||
func (cb *CircularBuffer) Push(key LogKey) { | ||
cb.data[cb.end] = key | ||
cb.end = (cb.end + 1) % cb.size | ||
} | ||
|
||
// Contains checks if the given LogKey exists in the buffer | ||
func (cb *CircularBuffer) Contains(key LogKey) bool { | ||
for _, item := range cb.data { | ||
if item == key { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.