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

popm/wasm: add 'minerStatus' method #178

Merged
merged 1 commit into from
Jul 15, 2024
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
15 changes: 12 additions & 3 deletions service/popm/popm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"slices"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -117,8 +118,9 @@ type Miner struct {
// Prometheus
isRunning bool

bfgWg sync.WaitGroup
bfgCmdCh chan bfgCmd // commands to send to bfg
bfgWg sync.WaitGroup
bfgCmdCh chan bfgCmd // commands to send to bfg
bfgConnected atomic.Bool

mineNowCh chan struct{}

Expand Down Expand Up @@ -665,6 +667,10 @@ func (m *Miner) checkForKeystones(ctx context.Context) error {
return nil
}

func (m *Miner) Connected() bool {
return m.bfgConnected.Load()
}

func (m *Miner) running() bool {
m.mtx.Lock()
defer m.mtx.Unlock()
Expand Down Expand Up @@ -813,6 +819,9 @@ func (m *Miner) connectBFG(pctx context.Context) error {
rWSCh <- m.handleBFGWebsocketRead(ctx, conn)
}()

log.Debugf("Connected to BFG: %s", m.cfg.BFGWSURL)
m.bfgConnected.Store(true)

select {
case <-ctx.Done():
err = ctx.Err()
Expand All @@ -821,8 +830,8 @@ func (m *Miner) connectBFG(pctx context.Context) error {
cancel()

// Wait for exit
log.Debugf("Connected to BFG: %s", m.cfg.BFGWSURL)
m.bfgWg.Wait()
m.bfgConnected.Store(false)

return err
}
Expand Down
7 changes: 7 additions & 0 deletions web/packages/pop-miner/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
BitcoinUTXOsResult,
KeyResult,
L2KeystonesResult,
MinerStatusResult,
PingResult,
VersionResult,
} from '../types';
Expand Down Expand Up @@ -68,6 +69,12 @@ export const stopPoPMiner: typeof types.stopPoPMiner = () => {
});
};

export const minerStatus: typeof types.minerStatus = () => {
return dispatch({
method: 'minerStatus',
}) as Promise<MinerStatusResult>;
};

export const ping: typeof types.ping = () => {
return dispatch({
method: 'ping',
Expand Down
1 change: 1 addition & 0 deletions web/packages/pop-miner/src/browser/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Method =
| 'bitcoinAddressToScriptHash'
| 'startPoPMiner'
| 'stopPoPMiner'
| 'minerStatus'
| 'ping'
| 'l2Keystones'
| 'bitcoinBalance'
Expand Down
38 changes: 29 additions & 9 deletions web/packages/pop-miner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,56 +250,56 @@ export type EventType =
* An event that has been dispatched.
*/
export type Event = {
type: EventType;
readonly type: EventType;
};

/**
* Dispatched when the PoP miner has stopped.
*/
export type EventMinerStart = Event & {
type: 'minerStart';
readonly type: 'minerStart';
};

/**
* Dispatched when the PoP miner has exited.
*/
export type EventMinerStop = Event & {
type: 'minerStop';
readonly type: 'minerStop';

/**
* The error that caused the PoP miner to exit, or null.
*/
error?: Error;
readonly error?: Error;
};

/**
* Dispatched when the PoP miner begins mining a keystone.
*/
export type EventMineKeystone = Event & {
type: 'mineKeystone';
readonly type: 'mineKeystone';

/**
* The keystone to be mined.
*/
keystone: L2Keystone;
readonly keystone: L2Keystone;
};

/**
* Dispatched when the PoP miner broadcasts a PoP transaction to the Bitcoin
* network.
*/
export type EventTransactionBroadcast = Event & {
type: 'transactionBroadcast';
readonly type: 'transactionBroadcast';

/**
* The keystone that was mined.
*/
keystone: L2Keystone;
readonly keystone: L2Keystone;

/**
* The hash of the Bitcoin transaction.
*/
txHash: string;
readonly txHash: string;
};

/**
Expand Down Expand Up @@ -379,6 +379,26 @@ export declare function startPoPMiner(args: MinerStartArgs): Promise<void>;
*/
export declare function stopPoPMiner(): Promise<void>;

/**
* @see minerStatus
*/
export type MinerStatusResult = {
/**
* Whether the PoP miner is currently running.
*/
readonly running: boolean;

/**
* Whether the PoP miner is currently connected to a BFG server.
*/
readonly connected: boolean;
};

/**
* Returns information about the status of the PoP miner.
*/
export declare function minerStatus(): Promise<MinerStatusResult>;

/**
* @see ping
*/
Expand Down
12 changes: 12 additions & 0 deletions web/popminer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
MethodBitcoinAddressToScriptHash Method = "bitcoinAddressToScriptHash" // Bitcoin address to script hash
MethodStartPoPMiner Method = "startPoPMiner" // Start PoP Miner
MethodStopPoPMiner Method = "stopPoPMiner" // Stop PoP Miner
MethodMinerStatus Method = "minerStatus" // PoP Miner status

// The following can only be dispatched after the PoP Miner is running.
MethodPing Method = "ping" // Ping BFG
Expand Down Expand Up @@ -139,6 +140,17 @@ type BitcoinAddressToScriptHashResult struct {
ScriptHash string `json:"scriptHash"`
}

// MinerStatusResult contains information about the status of the PoP miner.
// Returned by MethodMinerStatus.
type MinerStatusResult struct {
// Running is whether the PoP miner is running.
Running bool `json:"running"`

// Connecting is whether the PoP miner is currently connected to a BFG
// server.
Connected bool `json:"connected"`
}

// PingResult contains information when pinging the BFG server.
// Returned by MethodPing.
type PingResult struct {
Expand Down
17 changes: 17 additions & 0 deletions web/popminer/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ var handlers = map[Method]*Dispatch{
MethodStopPoPMiner: {
Handler: stopPopMiner,
},
MethodMinerStatus: {
Handler: minerStatus,
},

// The following can only be dispatched after the PoP Miner is running.
MethodPing: {
Expand Down Expand Up @@ -441,6 +444,20 @@ func stopPopMiner(_ js.Value, _ []js.Value) (any, error) {
return js.Null(), nil
}

func minerStatus(_ js.Value, _ []js.Value) (any, error) {
log.Tracef("minerStatus")
defer log.Tracef("minerStatus exit")

var status MinerStatusResult
miner, err := runningMiner()
if err == nil {
status.Running = true
status.Connected = miner.Connected()
}

return status, nil
}

func ping(_ js.Value, _ []js.Value) (any, error) {
log.Tracef("ping")
defer log.Tracef("ping exit")
Expand Down
1 change: 0 additions & 1 deletion web/popminer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type JSMarshaler interface {
func jsValueOf(x any) js.Value {
v, err := jsValueSafe(x)
if err != nil {
log.Debugf("jsValueOf: attempting reflection for %T: %v", x, err)
return jsReflectValueOf(reflect.ValueOf(x))
}
return v
Expand Down
5 changes: 5 additions & 0 deletions web/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<pre class="StopPopMinerShow"></pre>
</div>

<div>
<button id="minerStatusButton">Miner Status</button>
<pre class="minerStatusDisplay"></pre>
</div>

<!-- connected commands go here, run after StartPopMinerButton -->
<hr>
<p>The following commands require a live connection (after StartPopMinerButton)</p>
Expand Down
19 changes: 19 additions & 0 deletions web/www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ StopPopMinerButton.addEventListener('click', () => {
StopPopMiner();
});

// miner status
const minerStatusDisplay = document.querySelector('.minerStatusDisplay');

async function minerStatus() {
try {
const result = await dispatch({
method: 'minerStatus',
});
minerStatusDisplay.innerText = JSON.stringify(result, null, 2);
} catch (err) {
minerStatusDisplay.innerText = 'Promise rejected: \n' + JSON.stringify(err, null, 2);
console.error('Caught exception', err);
}
}

minerStatusButton.addEventListener('click', () => {
minerStatus();
});

// ping
const PingShow = document.querySelector('.PingShow');

Expand Down