-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
popm/wasm: add events and clean up globals (#175)
- Add events to the `popm` package. - Add events to the WebAssembly PoP Miner and the `@hemilabs/pop-miner` package. - Rename `activeMiner()` to `runningMiner()`. - Add a `Service` type and use `svc` global variable (replaces `pm` and `pmMtx`). This allows us to have a global struct to store data in, without the need to use a lock to access the data. - Add 4 initial events: `minerStart`, `minerStop`, `mineKeystone`, and `transactionBroadcast`. Closes #150
- Loading branch information
1 parent
a237239
commit 9c4cea5
Showing
11 changed files
with
560 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 Hemi Labs, Inc. | ||
// Use of this source code is governed by the MIT License, | ||
// which can be found in the LICENSE file. | ||
|
||
package popm | ||
|
||
import ( | ||
"github.com/hemilabs/heminetwork/hemi" | ||
) | ||
|
||
// EventHandler is a function that can handle an event. | ||
type EventHandler = func(event EventType, data any) | ||
|
||
// EventType represents a type of event. | ||
type EventType int | ||
|
||
const ( | ||
// EventTypeMineKeystone is an event dispatched when a L2 keystone is being | ||
// mined. | ||
EventTypeMineKeystone EventType = iota + 1 | ||
|
||
// EventTypeTransactionBroadcast is an event dispatched when a Bitcoin | ||
// transaction has been broadcast to the network. | ||
EventTypeTransactionBroadcast | ||
) | ||
|
||
// EventMineKeystone is the data for EventTypeMineKeystone. | ||
type EventMineKeystone struct { | ||
Keystone *hemi.L2Keystone | ||
} | ||
|
||
// EventTransactionBroadcast is the data for EventTypeTransactionBroadcast. | ||
type EventTransactionBroadcast struct { | ||
Keystone *hemi.L2Keystone | ||
TxHash string | ||
} | ||
|
||
// RegisterEventHandler registers an event handler to receive all events | ||
// dispatched by the miner. The dispatched events can be filtered by EventType | ||
// when received. | ||
func (m *Miner) RegisterEventHandler(handler EventHandler) { | ||
m.eventHandlersMtx.Lock() | ||
defer m.eventHandlersMtx.Unlock() | ||
m.eventHandlers = append(m.eventHandlers, handler) | ||
} | ||
|
||
// dispatchEvent calls all registered event handlers with the given eventType | ||
// and data. It is recommended to call this function in a go routine to avoid | ||
// blocking operation while the event is being dispatched, as all event handlers | ||
// will be executed synchronously. | ||
func (m *Miner) dispatchEvent(eventType EventType, data any) { | ||
m.eventHandlersMtx.RLock() | ||
defer m.eventHandlersMtx.RUnlock() | ||
for _, handler := range m.eventHandlers { | ||
handler(eventType, data) | ||
} | ||
} |
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
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.