Skip to content

Commit eb8c39c

Browse files
committed
adjusted DI for TxStatusWaiter
1 parent 43a4c49 commit eb8c39c

File tree

7 files changed

+62
-42
lines changed

7 files changed

+62
-42
lines changed

packages/sequencer/src/protocol/baselayer/MinaBaseLayer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SequencerModule,
1414
} from "../../sequencer/builder/SequencerModule";
1515
import { MinaTransactionSender } from "../../settlement/transactions/MinaTransactionSender";
16+
import { TxStatusWaiter } from "../../settlement/transactions/TxStatusWaiter";
1617
import { DefaultOutgoingMessageAdapter } from "../../settlement/messages/outgoing/DefaultOutgoingMessageAdapter";
1718

1819
import { BaseLayer } from "./BaseLayer";
@@ -93,6 +94,10 @@ export class MinaBaseLayer
9394
useClass: MinaTransactionSender,
9495
},
9596

97+
TxStatusWaiter: {
98+
useClass: TxStatusWaiter,
99+
},
100+
96101
L1TransactionDispatcherConfig: {
97102
useValue: {
98103
...DEFAULT_L1_TRANSACTION_DISPATCHER_CONFIG,

packages/sequencer/src/protocol/baselayer/network-utils/LightnetUtils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ export class LightnetUtils implements MinaNetworkUtils {
105105

106106
tx.sign([faucetDonor]);
107107

108-
await this.transactionSender.signProveAndSendTransaction(
109-
tx,
110-
[faucetDonorPublicKey],
111-
"included"
112-
);
108+
await this.transactionSender.proveAndSendTransaction(tx, "included");
113109

114110
log.provable.info(
115111
`Funded account ${receiver.toBase58()} with ${fundingAmount / 1e9} MINA`

packages/sequencer/src/protocol/baselayer/network-utils/LocalBlockchainUtils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ export class LocalBlockchainUtils implements MinaNetworkUtils {
8787

8888
tx.sign([faucetDonor]);
8989

90-
await this.transactionSender.signProveAndSendTransaction(
91-
tx,
92-
[faucetDonorPublicKey],
93-
"included"
94-
);
90+
await this.transactionSender.proveAndSendTransaction(tx, "included");
9591

9692
log.provable.info(
9793
`Funded account ${receiver.toBase58()} with ${fundingAmount / 1e9} MINA`

packages/sequencer/src/settlement/transactions/L1TransactionDispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class L1TransactionDispatcher {
3535
@inject("L1TransactionRetryStrategy")
3636
private readonly retryStrategy: L1TransactionRetryStrategy,
3737
@inject("SettlementSigner") private readonly signer: MinaSigner,
38-
private readonly waiter: TxStatusWaiter,
38+
@inject("TxStatusWaiter") private readonly waiter: TxStatusWaiter,
3939
@inject("L1TransactionDispatcherConfig")
4040
private readonly config: Required<DispatcherConfig>,
4141
@inject("BaseLayer")

packages/sequencer/src/settlement/transactions/MinaTransactionSender.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class MinaTransactionSender implements Closeable {
3434
@inject("SettlementSigner") private readonly signer: MinaSigner,
3535
@inject("FeeStrategy") private readonly feeStrategy: FeeStrategy,
3636
private readonly dispatcher: L1TransactionDispatcher,
37-
private readonly waiter: TxStatusWaiter
37+
@inject("TxStatusWaiter") private readonly waiter: TxStatusWaiter
3838
) {
3939
this.dispatcher.start();
4040
}
@@ -90,6 +90,9 @@ export class MinaTransactionSender implements Closeable {
9090
queuedAt: now,
9191
nextActionAt: now,
9292
});
93+
log.info(
94+
`MinaTransactionSender: queued transaction ${txnId} for sender ${sender} nonce ${nonceNum}`
95+
);
9396

9497
this.dispatcher.requestDispatch(sender);
9598

packages/sequencer/src/settlement/transactions/TxStatusWaiter.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { inject, injectable } from "tsyringe";
22
import {
33
EventsRecord,
44
ReplayingSingleUseEventEmitter,
5+
log,
56
} from "@proto-kit/common";
67

78
import {
@@ -79,6 +80,9 @@ export class TxStatusWaiter {
7980
): Promise<void> {
8081
const initial = await this.pendingStorage.findById(txId);
8182
if (!initial) {
83+
log.info(
84+
`TxStatusWaiter: waitFor(${desiredStatus}) unknown txId=${txId}`
85+
);
8286
throw new Error(`Unknown pending L1 transaction id ${txId}`);
8387
}
8488
if (initial.status === "failed") {
@@ -90,24 +94,21 @@ export class TxStatusWaiter {
9094

9195
const emitter = this.getEmitter(txId);
9296
const eventPromise = new Promise<void>((resolve, reject) => {
93-
emitter.on(desiredStatus, () => resolve());
97+
emitter.on(desiredStatus, () => {
98+
log.info(
99+
`TxStatusWaiter: waitFor(${desiredStatus}) resolved by event txId=${txId}`
100+
);
101+
resolve();
102+
});
94103
emitter.on("failed", ({ error }) => {
104+
log.info(
105+
`TxStatusWaiter: waitFor(${desiredStatus}) rejected by failed event txId=${txId}`,
106+
error
107+
);
95108
reject(error instanceof Error ? error : new Error(String(error)));
96109
});
97110
});
98111

99-
// if the status change happened between the first DB read and listener registration
100-
const afterSubscribe = await this.pendingStorage.findById(txId);
101-
if (!afterSubscribe) {
102-
throw new Error(`Unknown pending L1 transaction id ${txId}`);
103-
}
104-
if (afterSubscribe.status === "failed") {
105-
throw TxStatusWaiter.toError(afterSubscribe);
106-
}
107-
if (TxStatusWaiter.isSatisfied(afterSubscribe, desiredStatus)) {
108-
return;
109-
}
110-
111112
if (options.timeoutMs !== undefined) {
112113
const timeoutPromise = new Promise<void>((_, reject) => {
113114
const t = setTimeout(() => {

packages/sequencer/test/settlement/MinaTransactionSender.test.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type SenderFixture = {
3838
retryStrategy: any;
3939
};
4040

41+
type BaseLayerStub = {
42+
isLocalBlockChain: () => boolean;
43+
config: { network: { type: "local" | "remote" | "lightnet" } };
44+
};
45+
4146
function makeSender(
4247
pendingStorage: PendingL1TransactionStorage,
4348
dispatcherConfig: {
@@ -47,6 +52,10 @@ function makeSender(
4752
} = {
4853
pollIntervalMs: 5,
4954
statusCheckIntervalMs: 5,
55+
},
56+
baseLayerStub: BaseLayerStub = {
57+
isLocalBlockChain: () => true,
58+
config: { network: { type: "local" } },
5059
}
5160
): SenderFixture {
5261
const flowCreator = {
@@ -73,8 +82,6 @@ function makeSender(
7382
applyTransaction: jest.fn(async () => undefined),
7483
} as any;
7584

76-
const baseLayer = { config: { network: { type: "local" } } } as any;
77-
7885
const retryStrategy = {
7986
shouldRetry: jest.fn(async () => true),
8087
prepareRetryTransaction: jest.fn(async (record: any) => record.transaction),
@@ -89,13 +96,14 @@ function makeSender(
8996
retryStrategy as any,
9097
signer as any,
9198
waiter,
92-
dispatcherConfig
99+
dispatcherConfig,
100+
baseLayerStub as any
93101
);
94102
const sender = new MinaTransactionSender(
95103
flowCreator,
96104
provingTask,
97105
simulator,
98-
baseLayer,
106+
baseLayerStub as any,
99107
pendingStorage as any,
100108
signer,
101109
feeStrategy,
@@ -134,7 +142,6 @@ describe("MinaTransactionSender (unit)", () => {
134142
const pendingStorage: PendingL1TransactionStorage =
135143
new InMemoryPendingL1TransactionStorage();
136144
const { sender } = makeSender(pendingStorage);
137-
checkZkappTransactionStatus.mockResolvedValue({ success: false });
138145

139146
const { tx } = makeTx({ senderBase58: "S", nonce: 0, hash: "H1" });
140147

@@ -159,7 +166,6 @@ describe("MinaTransactionSender (unit)", () => {
159166
const pendingStorage: PendingL1TransactionStorage =
160167
new InMemoryPendingL1TransactionStorage();
161168
const { sender } = makeSender(pendingStorage);
162-
checkZkappTransactionStatus.mockResolvedValueOnce({ success: true });
163169

164170
const { tx } = makeTx({ senderBase58: "S", nonce: 0, hash: "H2" });
165171
try {
@@ -178,7 +184,6 @@ describe("MinaTransactionSender (unit)", () => {
178184
const pendingStorage: PendingL1TransactionStorage =
179185
new InMemoryPendingL1TransactionStorage();
180186
const { sender } = makeSender(pendingStorage);
181-
checkZkappTransactionStatus.mockResolvedValue({ success: false });
182187

183188
const tx0 = makeTx({ senderBase58: "S", nonce: 0, hash: "H0" });
184189
const tx1 = makeTx({ senderBase58: "S", nonce: 1, hash: "H1" });
@@ -202,11 +207,18 @@ describe("MinaTransactionSender (unit)", () => {
202207
it("should retry a transaction if first attempt fails", async () => {
203208
const pendingStorage: PendingL1TransactionStorage =
204209
new InMemoryPendingL1TransactionStorage();
205-
const { sender } = makeSender(pendingStorage, {
206-
pollIntervalMs: 5,
207-
statusCheckIntervalMs: 0,
208-
inclusionTimeoutMs: 0,
209-
});
210+
const { sender } = makeSender(
211+
pendingStorage,
212+
{
213+
pollIntervalMs: 5,
214+
statusCheckIntervalMs: 0,
215+
inclusionTimeoutMs: 0,
216+
},
217+
{
218+
isLocalBlockChain: () => false,
219+
config: { network: { type: "remote" } },
220+
}
221+
);
210222

211223
const { tx } = makeTx({ senderBase58: "S", nonce: 0, hash: "H-R1" });
212224

@@ -238,11 +250,18 @@ describe("MinaTransactionSender (unit)", () => {
238250
it("should stop retrying when shouldRetry returns false", async () => {
239251
const pendingStorage: PendingL1TransactionStorage =
240252
new InMemoryPendingL1TransactionStorage();
241-
const { sender, retryStrategy } = makeSender(pendingStorage, {
242-
pollIntervalMs: 5,
243-
statusCheckIntervalMs: 0,
244-
inclusionTimeoutMs: 0,
245-
});
253+
const { sender, retryStrategy } = makeSender(
254+
pendingStorage,
255+
{
256+
pollIntervalMs: 5,
257+
statusCheckIntervalMs: 0,
258+
inclusionTimeoutMs: 0,
259+
},
260+
{
261+
isLocalBlockChain: () => false,
262+
config: { network: { type: "remote" } },
263+
}
264+
);
246265

247266
// Force "no retry"
248267
retryStrategy.shouldRetry = jest.fn(async () => false);

0 commit comments

Comments
 (0)