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

Add chain/addTransaction for signed transaction #3716

Closed
wants to merge 6 commits into from
Closed
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
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
55 changes: 55 additions & 0 deletions ironfish/src/rpc/routes/chain/addTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* 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 { ApiNamespace, router } from '../router'

export type ChainAddTransactionRequest = {
transaction: string
}

export type ChainAddTransactionResponse = {
success: boolean
reason?: string
}

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

export const ChainAddTransactionResponseSchema: yup.ObjectSchema<ChainAddTransactionResponse> =
yup
.object({
success: yup.boolean().defined(),
reason: yup.string().optional(),
})
.defined()

router.register<typeof ChainAddTransactionRequestSchema, ChainAddTransactionResponse>(
`${ApiNamespace.chain}/addTransaction`,
ChainAddTransactionRequestSchema,
async (request, node): Promise<void> => {
const buffer = Buffer.from(request.data.transaction, 'hex')
const transaction = new Transaction(buffer)

// Some verification

const firstVerify = await node.chain.verifier.verifyNewTransaction(transaction)
if (!firstVerify.valid) {
request.end({ success: false, reason: JSON.stringify(firstVerify.reason) })
}

const secondVerify = node.memPool.acceptTransaction(transaction)
if (!secondVerify) {
request.end({ success: false, reason: 'Mempool rejected' })
}

node.peerNetwork.broadcastTransaction(transaction)

request.end({ success: true })
},
)
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 @@ -17,3 +17,4 @@ export * from './getConsensusParameters'
export * from './getAsset'
export * from './getNetworkInfo'
export * from './getNoteWitness'
export * from './addTransaction'