Skip to content

Commit

Permalink
Add Ironfish Stratum V3 (post-fishhash) to reference pool (#4713)
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh authored Feb 9, 2024
1 parent 5da0af2 commit 5737865
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ironfish/src/fileStores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export class Config<
poolMaxConnectionsPerIp: 0,
poolLarkWebhook: '',
poolXnSize: 2,
poolSupportedVersions: [1, 2],
poolSupportedVersions: [2, 3],
jsonLogs: false,
feeEstimatorMaxBlockHistory: DEFAULT_FEE_ESTIMATOR_MAX_BLOCK_HISTORY,
feeEstimatorPercentileSlow: DEFAULT_FEE_ESTIMATOR_PERCENTILE_SLOW,
Expand Down
30 changes: 27 additions & 3 deletions ironfish/src/mining/stratum/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export type MiningSubmitMessageV2 = {
graffiti: string
}

export type MiningSubmitMessageV3 = {
miningRequestId: number
randomness: string
}

export type MiningSubscribedMessageV1 = {
clientId: number
graffiti: string
Expand All @@ -53,7 +58,12 @@ export type MiningSubscribedMessageV2 = {
xn: string
}

export type MiningSubmittedMessageV2 = {
export type MiningSubscribedMessageV3 = {
clientId: number
xn: string
}

export type MiningSubmittedMessage = {
id: number
result: boolean
message?: string
Expand Down Expand Up @@ -128,7 +138,14 @@ export const MiningSubscribedMessageSchemaV1: yup.ObjectSchema<MiningSubscribedM
})
.required()

export const MiningSubscribedMessageV2Schema: yup.ObjectSchema<MiningSubscribedMessageV2> = yup
export const MiningSubscribedMessageSchemaV2: yup.ObjectSchema<MiningSubscribedMessageV2> = yup
.object({
clientId: yup.number().required(),
xn: yup.string().required(),
})
.required()

export const MiningSubscribedMessageSchemaV3: yup.ObjectSchema<MiningSubscribedMessageV3> = yup
.object({
clientId: yup.number().required(),
xn: yup.string().required(),
Expand All @@ -141,7 +158,7 @@ export const MiningSetTargetSchema: yup.ObjectSchema<MiningSetTargetMessage> = y
})
.required()

export const MiningSubmittedSchemaV2: yup.ObjectSchema<MiningSubmittedMessageV2> = yup
export const MiningSubmittedSchema: yup.ObjectSchema<MiningSubmittedMessage> = yup
.object({
id: yup.number().required(),
result: yup.bool().required(),
Expand Down Expand Up @@ -184,6 +201,13 @@ export const MiningSubmitSchemaV2: yup.ObjectSchema<MiningSubmitMessageV2> = yup
})
.required()

export const MiningSubmitSchemaV3: yup.ObjectSchema<MiningSubmitMessageV3> = yup
.object({
miningRequestId: yup.number().required(),
randomness: yup.string().required(),
})
.required()

export const MiningGetStatusSchema: yup.ObjectSchema<MiningGetStatusMessage> = yup
.object({
publicAddress: yup.string().optional(),
Expand Down
65 changes: 60 additions & 5 deletions ironfish/src/mining/stratum/stratumServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
MiningStatusMessage,
MiningSubmitSchemaV1,
MiningSubmitSchemaV2,
MiningSubmittedMessageV2,
MiningSubmitSchemaV3,
MiningSubmittedMessage,
MiningSubscribedMessageV1,
MiningSubscribedMessageV2,
MiningSubscribeSchema,
Expand All @@ -33,8 +34,6 @@ import {
import { StratumPeers } from './stratumPeers'
import { StratumServerClient } from './stratumServerClient'

const SUPPORTED_VERSIONS = [1, 2]

export class StratumServer {
readonly pool: MiningPool
readonly config: Config
Expand Down Expand Up @@ -64,7 +63,7 @@ export class StratumServer {
this.config = options.config
this.logger = options.logger

this.supportedVersions = SUPPORTED_VERSIONS
this.supportedVersions = options.config.get('poolSupportedVersions')

this.clients = new Map()
this.nextMinerId = 1
Expand Down Expand Up @@ -287,6 +286,22 @@ export class StratumServer {
agent: body.result.agent,
}

this.send(client.socket, 'mining.subscribed', {
clientId: client.id,
xn,
})
} else if (body.result.version === 3) {
const xnHexSize = 2 * this.config.get('poolXnSize')
const xn = idHex.slice(-xnHexSize).padStart(xnHexSize, '0')

client.subscription = {
version: 3,
publicAddress: body.result.publicAddress,
xn,
name: body.result.name,
agent: body.result.agent,
}

this.send(client.socket, 'mining.subscribed', {
clientId: client.id,
xn,
Expand Down Expand Up @@ -354,6 +369,46 @@ export class StratumServer {
graffiti,
)

if (error) {
this.send(client.socket, 'mining.submitted', {
id: message.id,
result: false,
message: error,
})
} else {
this.send(client.socket, 'mining.submitted', {
id: message.id,
result: true,
})
}
} else if (client.subscription?.version === 3) {
const body = await YupUtils.tryValidate(MiningSubmitSchemaV3, message.body)

if (body.error) {
this.peers.ban(client, {
message: body.error.message,
})
return
}

const { randomness, miningRequestId } = body.result

if (!randomness.startsWith(client.subscription.xn)) {
this.send(client.socket, 'mining.submitted', {
id: message.id,
result: false,
message: 'invalid leading xnonce in randomness',
})
return
}

const { error } = await this.pool.submitWork(
client,
miningRequestId,
randomness,
GraffitiUtils.fromString(`${this.pool.name}`).toString('hex'),
)

if (error) {
this.send(client.socket, 'mining.submitted', {
id: message.id,
Expand Down Expand Up @@ -481,7 +536,7 @@ export class StratumServer {
send(socket: net.Socket, method: 'mining.set_target', body: MiningSetTargetMessage): void
send(socket: net.Socket, method: 'mining.subscribed', body: MiningSubscribedMessageV1): void
send(socket: net.Socket, method: 'mining.subscribed', body: MiningSubscribedMessageV2): void
send(socket: net.Socket, method: 'mining.submitted', body: MiningSubmittedMessageV2): void
send(socket: net.Socket, method: 'mining.submitted', body: MiningSubmittedMessage): void
send(socket: net.Socket, method: 'mining.wait_for_work'): void
send(socket: net.Socket, method: 'mining.status', body: MiningStatusMessage): void
send(socket: net.Socket, method: string, body?: unknown): void {
Expand Down
10 changes: 9 additions & 1 deletion ironfish/src/mining/stratum/stratumServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ type V2Subscription = {
xn: string
}

type V3Subscription = {
version: 3
publicAddress: string
name?: string
agent?: string
xn: string
}

export class StratumServerClient {
id: number
socket: net.Socket
connected: boolean
remoteAddress: string
subscription: V1Subscription | V2Subscription | null = null
subscription: V1Subscription | V2Subscription | V3Subscription | null = null
messageBuffer: MessageBuffer

private constructor(options: { socket: net.Socket; id: number }) {
Expand Down

0 comments on commit 5737865

Please sign in to comment.