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

feature(ironfish): Add RPC route for chain:broadcast #3742

Merged
merged 5 commits into from
Apr 10, 2023
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
34 changes: 34 additions & 0 deletions ironfish-cli/src/commands/chain/broadcast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { CliUx } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'

export class BroadcastCommand extends IronfishCommand {
static description = `Broadcast a transaction to the network`

static flags = {
...RemoteFlags,
}

static args = [
{
name: 'transaction',
required: true,
description: 'The transaction in hex encoding',
},
]

async start(): Promise<void> {
const { args } = await this.parse(BroadcastCommand)
const transaction = args.transaction as string

CliUx.ux.action.start(`Broadcasting transaction`)
const client = await this.sdk.connectRpc()
const response = await client.broadcastTransaction({ transaction })
if (response.content) {
CliUx.ux.action.stop(`Transaction broadcasted: ${response.content.hash}`)
}
}
}
2 changes: 1 addition & 1 deletion ironfish/src/network/peerNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export class PeerNetwork {
* The full transaction will be sent to a subset of sqrt(num_peers)
* and the rest of the peers will receive the transaction hash
*/
private broadcastTransaction(transaction: Transaction): void {
public broadcastTransaction(transaction: Transaction): void {
const hash = transaction.hash()

const peersToSendToArray = ArrayUtils.shuffle([
Expand Down
11 changes: 11 additions & 0 deletions ironfish/src/rpc/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ApiNamespace,
BlockTemplateStreamRequest,
BlockTemplateStreamResponse,
BroadcastTransactionRequest,
BroadcastTransactionResponse,
CreateAccountRequest,
CreateAccountResponse,
GetAccountNotesStreamRequest,
Expand Down Expand Up @@ -633,4 +635,13 @@ export abstract class RpcClient {
params,
).waitForEnd()
}

async broadcastTransaction(
params: BroadcastTransactionRequest,
): Promise<RpcResponse<BroadcastTransactionResponse>> {
return this.request<BroadcastTransactionResponse>(
`${ApiNamespace.chain}/broadcastTransaction`,
params,
).waitForEnd()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Route chain/broadcastTransaction should broadcast a transaction": [
{
"version": 1,
"id": "fc87bf56-f69f-4c5a-9ba9-cc8786b8c80f",
"name": "test",
"spendingKey": "27616b2d313a67a92884ebba87d8d7be12df3259712726322ffb1fde58cf2076",
"viewKey": "856f2fbade361a274f6e3a79e689b8b18791a11e2ddba27c7b73396395537a28080bc8a25ff9a3a9ceb9c597f6ba6773cda7c5fde7175ca4086d8a4bf55dc871",
"incomingViewKey": "0a9ff2c66d5a1c408a10c6059bd5ec59b7a7269e1a1148a014ab7ace12986103",
"outgoingViewKey": "6c4c299a8014960177a557245fd92e87efcf109ac05788c8d1ebf32ccdbcfe3f",
"publicAddress": "0e9c7b4c6bbf6240658a34f15960ffb0e8e7d8643c81c219c97afa6b50d8278b",
"createdAt": null
},
{
"type": "Buffer",
"data": "base64:AQAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzKiP////8AAAAArxKGEva5q6RkDSu6BaWRJ4DReIqiA0C8fer2MaRESdi07TdMQN3J2egShdQS5r5L8WDxHGPbfmKdEqWdlOFhfGPXMZet7fiXHNkcozLhE7C3SRIbtY0T6Xqu5+YK+ExlRZlvOIl9Pd51me/6gQogtgEWy+MyyiH+SnpDTCBfVb4Q8/lU+t9XdTQe0yIpAYhm3qR49BVhqROPOIythmha14hK0P91aIzqfGj6TIQ6d46TfHZ4tB0R5ElkMVlAAoMF9OsJWaed6tfWsQqCjlb3gQPsiP9bgwSYqb29u3WamIGtt1iKGD2+7zqNffpjQng+/M16d9Ib7lV+v9pVsl9qMZ6ez0VQUC44/R0ri6oK7IZKJvzjk3QvO5ii9bRsNhtqYk37RyZmCTYRh1CoDIaGM0/c8c4pTTbSCDA7TISVECy/4BAocImO6u2KDelK9ONvNgbDbCoFsIQv40znDqw3VLNoOdJ4s4ubXGGHQnVsSFkx5ypX7a3B/QfZyidesRcenvtyaH3i0Mlt38CeIkTcLIgc+3QO2fozB6rgw2gUhLU1cYvCX/U0al8H8GrVUZD4BQtVvzkVTD7tuqgvjLsS3llUYzBacUX1Ojies4F8tDu5HFcr5ipMqklyb24gRmlzaCBub3RlIGVuY3J5cHRpb24gbWluZXIga2V5MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwaH8q3Kv6l3Ci26OtYIUfaCtBMKQqaocUDbotgnIN7cciTVtJa4291ZfzIKgNOAL1A+Ae6tGK5gfnAyNKc85iBQ=="
}
]
}
32 changes: 32 additions & 0 deletions ironfish/src/rpc/routes/chain/broadcastTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { useMinersTxFixture } from '../../../testUtilities/fixtures'
import { createRouteTest } from '../../../testUtilities/routeTest'

describe('Route chain/broadcastTransaction', () => {
const routeTest = createRouteTest()

it('should broadcast a transaction', async () => {
const transaction = await useMinersTxFixture(routeTest.wallet)

const broadcastSpy = jest.spyOn(routeTest.peerNetwork, 'broadcastTransaction')

const response = await routeTest.client.broadcastTransaction({
transaction: transaction.serialize().toString('hex'),
})

expect(response.status).toBe(200)
holahula marked this conversation as resolved.
Show resolved Hide resolved
expect(response.content?.hash).toEqual(transaction.hash().toString('hex'))
expect(broadcastSpy).toHaveBeenCalled()
})

it("should return an error if the transaction won't deserialize", async () => {
await expect(
routeTest.client.broadcastTransaction({
transaction: '0xdeadbeef',
}),
).rejects.toThrow('Out of bounds read')
})
})
50 changes: 50 additions & 0 deletions ironfish/src/rpc/routes/chain/broadcastTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import * as yup from 'yup'
import { Transaction } from '../../../primitives'
import { ValidationError } from '../../adapters'
import { ApiNamespace, router } from '../router'

export type BroadcastTransactionRequest = {
transaction: string
}

export type BroadcastTransactionResponse = {
hash: string
}

export const BroadcastTransactionRequestSchema: yup.ObjectSchema<BroadcastTransactionRequest> =
yup
.object({
transaction: yup.string().defined(),
})
.defined()

export const BroadcastTransactionResponseSchema: yup.ObjectSchema<BroadcastTransactionResponse> =
yup
.object({
hash: yup.string().defined(),
})
.defined()

router.register<typeof BroadcastTransactionRequestSchema, BroadcastTransactionResponse>(
`${ApiNamespace.chain}/broadcastTransaction`,
BroadcastTransactionRequestSchema,
(request, node): void => {
const data = Buffer.from(request.data.transaction, 'hex')
const transaction = new Transaction(data)

const verify = node.chain.verifier.verifyCreatedTransaction(transaction)
holahula marked this conversation as resolved.
Show resolved Hide resolved
if (!verify.valid) {
throw new ValidationError(`Invalid transaction, reason: ${String(verify.reason)}`)
}

node.peerNetwork.broadcastTransaction(transaction)

request.end({
hash: transaction.hash().toString('hex'),
})
},
)
1 change: 1 addition & 0 deletions ironfish/src/rpc/routes/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

export * from './broadcastTransaction'
export * from './estimateFeeRate'
export * from './estimateFeeRates'
export * from './exportChain'
Expand Down