Skip to content

Commit

Permalink
refactor(transaction-pool): return tx indexes as integers (#483)
Browse files Browse the repository at this point in the history
* return tx indexes as integers

* fix compile error
  • Loading branch information
oXtxNt9U authored Mar 14, 2024
1 parent 4fe8a61 commit fa3e8be
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/source/contracts/p2p/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ export interface PostTransactionsRequest extends Request {
}

export interface PostTransactionsResponse extends Response {
accept: string[];
accept: number[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export type ProcessorError = {
};

export type ProcessorResult = {
accept: string[];
broadcast: string[];
invalid: string[];
excess: string[];
errors?: { [id: string]: ProcessorError };
accept: number[];
broadcast: number[];
invalid: number[];
excess: number[];
errors?: { [index: string]: ProcessorError };
};

export interface ProcessorExtension {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Contracts } from "@mainsail/contracts";

let accept: string[] = [];
let broadcast: string[] = [];
let invalid: string[] = [];
let excess: string[] = [];
let accept: number[] = [];
let broadcast: number[] = [];
let invalid: number[] = [];
let excess: number[] = [];
let errors: { [id: string]: Contracts.TransactionPool.ProcessorError } | undefined;

export const setProcessorState = (state: any): void => {
Expand All @@ -17,10 +17,10 @@ export const setProcessorState = (state: any): void => {

class TransactionPoolProcessorMock implements Partial<Contracts.TransactionPool.Processor> {
public async process(data: Contracts.Crypto.TransactionJson[] | Buffer[]): Promise<{
accept: string[];
broadcast: string[];
invalid: string[];
excess: string[];
accept: number[];
broadcast: number[];
invalid: number[];
excess: number[];
errors?: { [id: string]: Contracts.TransactionPool.ProcessorError };
}> {
return {
Expand Down
16 changes: 8 additions & 8 deletions packages/transaction-pool/source/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe<{
spiedExtension1.calledTimes(2);
spiedBroadcaster.neverCalled();

assert.equal(result.accept, ["0", "1"]);
assert.equal(result.accept, [0, 1]);
assert.equal(result.broadcast, []);
assert.equal(result.invalid, []);
assert.equal(result.excess, []);
Expand Down Expand Up @@ -145,9 +145,9 @@ describe<{
spiedExtension1.calledOnce();
spiedBroadcaster.calledOnce();

assert.equal(result.accept, ["0"]);
assert.equal(result.broadcast, ["0"]);
assert.equal(result.invalid, ["1"]);
assert.equal(result.accept, [0]);
assert.equal(result.broadcast, [0]);
assert.equal(result.invalid, [1]);
assert.equal(result.excess, []);
assert.truthy(result.errors["1"]);
assert.equal(result.errors["1"].type, "ERR_LOW_FEE");
Expand Down Expand Up @@ -175,8 +175,8 @@ describe<{
spiedExtension1.calledTimes(2);
spiedBroadcaster.called();

assert.equal(result.accept, ["0", "1"]);
assert.equal(result.broadcast, ["0"]);
assert.equal(result.accept, [0, 1]);
assert.equal(result.broadcast, [0]);
assert.equal(result.invalid, []);
assert.equal(result.excess, []);
assert.undefined(result.errors);
Expand Down Expand Up @@ -220,8 +220,8 @@ describe<{

assert.equal(result.accept, []);
assert.equal(result.broadcast, []);
assert.equal(result.invalid, ["0"]);
assert.equal(result.excess, ["0"]);
assert.equal(result.invalid, [0]);
assert.equal(result.excess, [0]);
assert.truthy(result.errors["0"]);
assert.equal(result.errors["0"].type, "ERR_EXCEEDS_MAX_COUNT");
});
Expand Down
22 changes: 10 additions & 12 deletions packages/transaction-pool/source/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,39 @@ export class Processor implements Contracts.TransactionPool.Processor {
private readonly transactionFactory!: Contracts.Crypto.TransactionFactory;

public async process(data: Buffer[]): Promise<Contracts.TransactionPool.ProcessorResult> {
const accept: string[] = [];
const broadcast: string[] = [];
const invalid: string[] = [];
const excess: string[] = [];
let errors: { [id: string]: Contracts.TransactionPool.ProcessorError } | undefined;
const accept: number[] = [];
const broadcast: number[] = [];
const invalid: number[] = [];
const excess: number[] = [];
let errors: { [index: string]: Contracts.TransactionPool.ProcessorError } | undefined;

const broadcastTransactions: Contracts.Crypto.Transaction[] = [];

try {
for (const [index, transactionData] of data.entries()) {
const entryId = String(index);

try {
const transaction = await this.#getTransactionFromBuffer(transactionData);

await this.pool.addTransaction(transaction);
accept.push(entryId);
accept.push(index);

try {
await Promise.all(this.extensions.map((e) => e.throwIfCannotBroadcast(transaction)));
broadcastTransactions.push(transaction);
broadcast.push(entryId);
broadcast.push(index);
} catch {}
} catch (error) {
invalid.push(entryId);
invalid.push(index);

if (error instanceof Exceptions.PoolError) {
if (error.type === "ERR_EXCEEDS_MAX_COUNT") {
excess.push(entryId);
excess.push(index);
}

if (!errors) {
errors = {};
}
errors[entryId] = {
errors[index] = {
message: error.message,
type: error.type,
};
Expand Down

0 comments on commit fa3e8be

Please sign in to comment.