@midnightntwrk/wallet-sdk-capabilities 4.0.0-rc.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/liveness/livenessReads.js +11 -10
- package/dist/liveness/livenessService.js +4 -5
- package/dist/proving/index.d.ts +1 -0
- package/dist/proving/index.js +1 -0
- package/dist/proving/provingService.d.ts +16 -49
- package/dist/proving/provingService.js +1 -89
- package/dist/proving/v8ProvingService.d.ts +34 -7
- package/dist/proving/v8ProvingService.js +36 -6
- package/dist/proving/v9ProvingService.d.ts +108 -0
- package/dist/proving/v9ProvingService.js +146 -0
- package/dist/proving/versionedProving.d.ts +22 -4
- package/dist/proving/versionedProving.js +28 -2
- package/dist/signatures/v8Signatures.d.ts +1 -1
- package/dist/simulation/index.d.ts +4 -6
- package/dist/simulation/index.js +4 -7
- package/dist/simulation/v8/Simulator.d.ts +1 -3
- package/dist/simulation/v8/Simulator.js +1 -3
- package/dist/simulation/v8/SimulatorState.d.ts +1 -10
- package/dist/simulation/v8/SimulatorState.js +1 -9
- package/dist/simulation/v8/index.d.ts +1 -1
- package/dist/simulation/v8/index.js +1 -1
- package/dist/simulation/v9/Simulator.d.ts +0 -2
- package/dist/simulation/v9/Simulator.js +0 -2
- package/dist/simulation/v9/SimulatorState.d.ts +0 -9
- package/dist/simulation/v9/SimulatorState.js +0 -8
- package/dist/simulation/v9/index.d.ts +1 -1
- package/dist/simulation/v9/index.js +1 -1
- package/dist/submission/submissionService.d.ts +57 -9
- package/dist/submission/submissionService.js +61 -15
- package/dist/transactionLifecycle/SnapshotFormat.d.ts +51 -0
- package/dist/transactionLifecycle/SnapshotFormat.js +120 -0
- package/dist/transactionLifecycle/index.d.ts +4 -0
- package/dist/{pendingTransactions → transactionLifecycle}/index.js +4 -2
- package/dist/transactionLifecycle/lifecycle.d.ts +402 -0
- package/dist/transactionLifecycle/lifecycle.js +407 -0
- package/dist/transactionLifecycle/lifecycleService.d.ts +448 -0
- package/dist/transactionLifecycle/lifecycleService.js +350 -0
- package/dist/transactionLifecycle/transactionTraits.d.ts +106 -0
- package/dist/transactionLifecycle/transactionTraits.js +53 -0
- package/dist/validation/blockData.d.ts +23 -4
- package/dist/validation/blockData.js +34 -10
- package/dist/validation/index.d.ts +1 -0
- package/dist/validation/index.js +1 -0
- package/dist/validation/promiseBoundary.d.ts +10 -0
- package/dist/validation/promiseBoundary.js +30 -0
- package/dist/validation/v8ValidationService.d.ts +6 -5
- package/dist/validation/v8ValidationService.js +11 -4
- package/dist/validation/v9ValidationService.d.ts +30 -0
- package/dist/validation/v9ValidationService.js +61 -0
- package/dist/validation/validationService.d.ts +54 -32
- package/dist/validation/validationService.js +29 -49
- package/dist/validation/versionedValidation.d.ts +5 -3
- package/dist/validation/versionedValidation.js +5 -3
- package/package.json +12 -12
- package/dist/pendingTransactions/index.d.ts +0 -2
- package/dist/pendingTransactions/pendingTransactions.d.ts +0 -266
- package/dist/pendingTransactions/pendingTransactions.js +0 -302
- package/dist/pendingTransactions/pendingTransactionsService.d.ts +0 -108
- package/dist/pendingTransactions/pendingTransactionsService.js +0 -186
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of MIDNIGHT-WALLET-SDK.
|
|
3
|
+
* Copyright (C) Midnight Foundation
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* You may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
import { TransactionStatus } from '@midnightntwrk/wallet-sdk-indexer-client';
|
|
16
|
+
import { HttpQueryClient } from '@midnightntwrk/wallet-sdk-indexer-client/effect';
|
|
17
|
+
import { EitherOps, ObservableOps } from '@midnightntwrk/wallet-sdk-utilities';
|
|
18
|
+
import { Array as Arr, Clock, DateTime, DefaultServices, Duration, Effect, Exit, Option, pipe, Scope, Stream, SubscriptionRef, } from 'effect';
|
|
19
|
+
import * as Lifecycle from './lifecycle.js';
|
|
20
|
+
import { traitForVersion, } from './transactionTraits.js';
|
|
21
|
+
/**
|
|
22
|
+
* The lifecycle records in a ref, moved by the pure transitions in `lifecycle.ts` with the clock's time.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const service = new TransactionLifecycleServiceEffectImpl(traits);
|
|
26
|
+
* yield * service.balanced({ tx: erased, identifiers, intentHashes, inputs, ttl, protocolVersion });
|
|
27
|
+
*/
|
|
28
|
+
export class TransactionLifecycleServiceEffectImpl {
|
|
29
|
+
#state;
|
|
30
|
+
#txTraits;
|
|
31
|
+
/**
|
|
32
|
+
* Builds a service holding the records a previous one stored.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const service = yield * TransactionLifecycleServiceEffectImpl.restore(stored, traits);
|
|
36
|
+
*
|
|
37
|
+
* @param data What `Lifecycle.serialize` wrote.
|
|
38
|
+
* @param txTraits The traits each record's transaction is read with, chosen by its version.
|
|
39
|
+
* @returns The service, or why the payload could not be read.
|
|
40
|
+
*/
|
|
41
|
+
static restore(data, txTraits) {
|
|
42
|
+
return pipe(Lifecycle.deserialize(data, txTraits), EitherOps.toEffect, Effect.map((state) => new TransactionLifecycleServiceEffectImpl(txTraits, state)));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Builds a service, empty or holding `initialState`.
|
|
46
|
+
*
|
|
47
|
+
* @param txTraits The trait each record's transaction is read with, chosen by the version it was built for.
|
|
48
|
+
* @param initialState The records to begin with; none when left out.
|
|
49
|
+
*/
|
|
50
|
+
constructor(txTraits, initialState) {
|
|
51
|
+
this.#txTraits = txTraits;
|
|
52
|
+
// Built synchronously so the service can be constructed outside an Effect, as the facade does.
|
|
53
|
+
this.#state = SubscriptionRef.make(initialState ?? Lifecycle.empty()).pipe(Effect.runSync);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The records, now and each time they move.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* Every write publishes, whether or not it moved anything, so consecutive identical values are dropped. A transition
|
|
60
|
+
* that moves nothing returns the very state it was given, so comparing by identity never hides a real move.
|
|
61
|
+
* @returns A stream that starts with the records as they stand.
|
|
62
|
+
*/
|
|
63
|
+
state() {
|
|
64
|
+
return Stream.concat(Stream.fromEffect(SubscriptionRef.get(this.#state)), this.#state.changes).pipe(Stream.changesWith((a, b) => a === b));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Polls the indexer and sweeps once per tick.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Each tick asks the indexer first and sweeps after, so an inclusion found on the tick a TTL passes is recorded
|
|
71
|
+
* before the TTL could end the record: the inclusion happened within the TTL.
|
|
72
|
+
*
|
|
73
|
+
* The ticks are drained within the provided scope, so closing that scope stops the poller.
|
|
74
|
+
* @param ticks When to poll.
|
|
75
|
+
* @returns An effect that runs until the provided scope closes.
|
|
76
|
+
*/
|
|
77
|
+
startPolling(ticks) {
|
|
78
|
+
return ticks.pipe(Stream.runForEachScoped(() => pipe(this.poll(), Effect.andThen(this.sweep()))));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Records a transaction as balanced, at the clock's time. See `Lifecycle.balanced` for which records it replaces.
|
|
82
|
+
*
|
|
83
|
+
* @param facts The balanced transaction, with its proofs erased, and what the record keeps of it.
|
|
84
|
+
* @returns An effect that applies the move.
|
|
85
|
+
*/
|
|
86
|
+
balanced(facts) {
|
|
87
|
+
return this.moveNow((state, now) => Lifecycle.balanced(state, facts, now));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Records a transaction as finalized, at the clock's time. See `Lifecycle.finalized` for which records it folds.
|
|
91
|
+
*
|
|
92
|
+
* @param facts The finalized transaction and what the record keeps of it.
|
|
93
|
+
* @returns An effect that applies the move.
|
|
94
|
+
*/
|
|
95
|
+
finalized(facts) {
|
|
96
|
+
return this.moveNow((state, now) => Lifecycle.finalized(state, facts, now));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Records a transaction as submitted, at the clock's time, which becomes its submission time.
|
|
100
|
+
*
|
|
101
|
+
* @param facts The submitted transaction and what the record keeps of it.
|
|
102
|
+
* @returns An effect that applies the move.
|
|
103
|
+
*/
|
|
104
|
+
submitted(facts) {
|
|
105
|
+
return this.moveNow((state, now) => Lifecycle.submitted(state, facts, now));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Records that the transaction holding any of `identifiers` was reverted.
|
|
109
|
+
*
|
|
110
|
+
* @param identifiers The reverted transaction's identifiers.
|
|
111
|
+
* @returns An effect that applies the move.
|
|
112
|
+
*/
|
|
113
|
+
reverted(identifiers) {
|
|
114
|
+
return SubscriptionRef.update(this.#state, (state) => Lifecycle.reverted(state, identifiers));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Gives up on every live transaction built for an epoch the chain has left by `chainNow`.
|
|
118
|
+
*
|
|
119
|
+
* @param chainNow The protocol version the chain has reached.
|
|
120
|
+
* @returns An effect that applies the move.
|
|
121
|
+
*/
|
|
122
|
+
orphanBeyond(chainNow) {
|
|
123
|
+
return SubscriptionRef.update(this.#state, (state) => Lifecycle.orphanBeyond(state, this.#txTraits, chainNow));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Records that a block included the transaction holding any of `identifiers`: confirmed on a success, rejected with
|
|
127
|
+
* the verdict on a failure or partial success.
|
|
128
|
+
*
|
|
129
|
+
* @param identifiers The included transaction's identifiers.
|
|
130
|
+
* @param verdict What the chain made of it.
|
|
131
|
+
* @returns An effect that applies the move.
|
|
132
|
+
*/
|
|
133
|
+
included(identifiers, verdict) {
|
|
134
|
+
return SubscriptionRef.update(this.#state, (state) => verdict.status === 'SUCCESS'
|
|
135
|
+
? Lifecycle.confirmed(state, identifiers)
|
|
136
|
+
: Lifecycle.rejected(state, identifiers, verdict));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The records as they stand, written in the stored format with the traits the service was built with.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const stored = yield * service.serializeState();
|
|
143
|
+
*
|
|
144
|
+
* @returns An effect that gives the store, in the current format version.
|
|
145
|
+
*/
|
|
146
|
+
serializeState() {
|
|
147
|
+
return pipe(SubscriptionRef.get(this.#state), Effect.map((state) => Lifecycle.serialize(state, this.#txTraits)));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Ends what has passed its TTL, then drops it, as two writes: a record that ends because its TTL passed is due to be
|
|
151
|
+
* dropped at once, so publishing between them is the only way a caller ever sees that end.
|
|
152
|
+
*/
|
|
153
|
+
sweep() {
|
|
154
|
+
return pipe(this.moveNow((state, now) => Lifecycle.expire(state, now)), Effect.andThen(this.moveNow((state, now) => Lifecycle.dropFinished(state, now))));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Asks the indexer about every live record that holds its transaction, whichever live state it is in, so a
|
|
158
|
+
* transaction submitted outside the facade is confirmed too. A record without its transaction has nothing to ask by.
|
|
159
|
+
*/
|
|
160
|
+
poll() {
|
|
161
|
+
return pipe(SubscriptionRef.get(this.#state), Effect.map((state) => Arr.filterMap(state.records, (record) => Lifecycle.isLive(record)
|
|
162
|
+
? Option.all({ tx: record.tx, trait: traitForVersion(this.#txTraits, record.protocolVersion) }).pipe(Option.map((asked) => ({ ...asked, identifiers: record.identifiers })))
|
|
163
|
+
: Option.none())), Effect.flatMap((asked) => Effect.forEach(asked, ({ tx, trait, identifiers }) => pipe(verdictOf(tx, trait), Effect.flatMap(Option.match({
|
|
164
|
+
onNone: () => Effect.void,
|
|
165
|
+
onSome: (verdict) => this.included(identifiers, verdict),
|
|
166
|
+
}))))), Effect.asVoid);
|
|
167
|
+
}
|
|
168
|
+
moveNow(move) {
|
|
169
|
+
return SubscriptionRef.updateEffect(this.#state, (state) => Effect.map(DateTime.now, (now) => move(state, now)));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* What the indexer reports for `tx`: the result of a regular transaction carrying every one of its identifiers, or
|
|
174
|
+
* nothing while it has none. A query that fails reports nothing, so the next tick asks again.
|
|
175
|
+
*/
|
|
176
|
+
const verdictOf = (tx, trait) => pipe(TransactionStatus, Effect.flatMap((query) => query({ transactionId: trait.firstId(tx) })), Effect.map((result) => Arr.findFirst(result.transactions, (reported) =>
|
|
177
|
+
// Only a regular transaction carries the identifiers and result this needs, and the wallet only submits
|
|
178
|
+
// regular ones; every other variant, including ones a later schema adds, is ignored.
|
|
179
|
+
reported.__typename === 'RegularTransaction' && trait.areAllTxIdsIncluded(tx, reported.identifiers)
|
|
180
|
+
? verdictFrom(reported.transactionResult)
|
|
181
|
+
: Option.none())), Effect.catchAll((error) => pipe(Effect.logWarning(error, 'Could not ask the indexer about a transaction; asking again next tick'), Effect.as(Option.none()))));
|
|
182
|
+
/** A final result as a verdict, segments stripped to what the chain decided; any other status is no verdict yet. */
|
|
183
|
+
const verdictFrom = (result) => {
|
|
184
|
+
const segments = (result.segments ?? []).map(({ id, success }) => ({ id, success }));
|
|
185
|
+
return result.status === 'SUCCESS'
|
|
186
|
+
? Option.some({ status: 'SUCCESS' })
|
|
187
|
+
: result.status === 'FAILURE' || result.status === 'PARTIAL_SUCCESS'
|
|
188
|
+
? Option.some({ status: result.status, segments })
|
|
189
|
+
: Option.none();
|
|
190
|
+
};
|
|
191
|
+
/** The Effect clock that reads `wallClock`, sleeping as the system clock does. */
|
|
192
|
+
const effectClockOf = (wallClock) => {
|
|
193
|
+
const system = Clock.make();
|
|
194
|
+
const millis = () => wallClock.now().getTime();
|
|
195
|
+
return {
|
|
196
|
+
[Clock.ClockTypeId]: Clock.ClockTypeId,
|
|
197
|
+
unsafeCurrentTimeMillis: millis,
|
|
198
|
+
currentTimeMillis: Effect.sync(millis),
|
|
199
|
+
unsafeCurrentTimeNanos: () => BigInt(millis()) * 1000000n,
|
|
200
|
+
currentTimeNanos: Effect.sync(() => BigInt(millis()) * 1000000n),
|
|
201
|
+
sleep: (duration) => system.sleep(duration),
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Runs a {@link TransactionLifecycleServiceEffectImpl}, polling the configured indexer once a second between `start` and
|
|
206
|
+
* `stop`.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const service = await TransactionLifecycleServiceImpl.init({ txTraits, configuration });
|
|
210
|
+
* await service.start();
|
|
211
|
+
*/
|
|
212
|
+
export class TransactionLifecycleServiceImpl {
|
|
213
|
+
/**
|
|
214
|
+
* Builds a service, empty or holding `initialState`.
|
|
215
|
+
*
|
|
216
|
+
* @param initParams The traits records are read with, the indexer to poll, and any records to begin with.
|
|
217
|
+
* @returns The service, not yet polling.
|
|
218
|
+
*/
|
|
219
|
+
static init(initParams) {
|
|
220
|
+
return pipe(Scope.make(), Effect.map((scope) => new TransactionLifecycleServiceImpl(new TransactionLifecycleServiceEffectImpl(initParams.txTraits, initParams.initialState), scope, initParams.configuration, pipe(Option.fromNullable(initParams.clock), Option.map(effectClockOf), Option.getOrElse(Clock.make)))), Effect.runPromise);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Builds a service holding the records a previous one stored.
|
|
224
|
+
*
|
|
225
|
+
* @param data What `Lifecycle.serialize` wrote.
|
|
226
|
+
* @param txTraits The traits each record's transaction is read with.
|
|
227
|
+
* @param configuration The indexer to poll.
|
|
228
|
+
* @param clock What every move and sweep reads the time from; the system clock when left out.
|
|
229
|
+
* @returns The service, not yet polling; rejects with why the payload could not be read.
|
|
230
|
+
*/
|
|
231
|
+
static restore(data, txTraits, configuration, clock) {
|
|
232
|
+
return pipe(Lifecycle.deserialize(data, txTraits), EitherOps.toEffect, Effect.flatMap((initialState) => Effect.promise(() => TransactionLifecycleServiceImpl.init({ txTraits, initialState, configuration, ...(clock ? { clock } : {}) }))), Effect.runPromise);
|
|
233
|
+
}
|
|
234
|
+
#effectService;
|
|
235
|
+
#scope;
|
|
236
|
+
#configuration;
|
|
237
|
+
#clock;
|
|
238
|
+
/**
|
|
239
|
+
* Wraps an Effect service. Prefer {@link TransactionLifecycleServiceImpl.init} or
|
|
240
|
+
* {@link TransactionLifecycleServiceImpl.restore}, which build the scope and clock for you.
|
|
241
|
+
*
|
|
242
|
+
* @param effectService The service whose records this one holds.
|
|
243
|
+
* @param scope The scope polling runs in; {@link stop} closes it.
|
|
244
|
+
* @param configuration The indexer to poll.
|
|
245
|
+
* @param clock What every move and sweep reads the time from.
|
|
246
|
+
*/
|
|
247
|
+
constructor(effectService, scope, configuration, clock) {
|
|
248
|
+
this.#effectService = effectService;
|
|
249
|
+
this.#scope = scope;
|
|
250
|
+
this.#configuration = configuration;
|
|
251
|
+
this.#clock = clock;
|
|
252
|
+
}
|
|
253
|
+
/** Runs one move against the service's clock, so every record is stamped with the time its caller keeps. */
|
|
254
|
+
#run(move) {
|
|
255
|
+
return Effect.runPromise(Effect.withClock(move, this.#clock));
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Starts polling the configured indexer once a second, in the background, until {@link stop}.
|
|
259
|
+
*
|
|
260
|
+
* @returns Resolves once polling has started.
|
|
261
|
+
*/
|
|
262
|
+
start() {
|
|
263
|
+
return this.#effectService
|
|
264
|
+
.startPolling(Stream.tick(Duration.seconds(1)))
|
|
265
|
+
.pipe(Effect.withClock(this.#clock), Effect.provide(HttpQueryClient.layer({ url: this.#configuration.indexerClientConnection.indexerHttpUrl })), Effect.provideService(Scope.Scope, this.#scope), Effect.provide(DefaultServices.liveServices), Effect.runFork, () => Promise.resolve());
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Stops polling by closing the scope it runs in. No indexer query and no sweep runs after it resolves; the records
|
|
269
|
+
* are kept, so they can still be read and saved.
|
|
270
|
+
*
|
|
271
|
+
* @returns Resolves once polling has stopped.
|
|
272
|
+
*/
|
|
273
|
+
stop() {
|
|
274
|
+
return pipe(Scope.close(this.#scope, Exit.succeed(undefined)), Effect.runPromise);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* The records, now and each time they move.
|
|
278
|
+
*
|
|
279
|
+
* @returns An observable that starts with the records as they stand.
|
|
280
|
+
*/
|
|
281
|
+
state() {
|
|
282
|
+
return this.#effectService.state().pipe(ObservableOps.fromStream);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Records a transaction as balanced, at the service's clock time.
|
|
286
|
+
*
|
|
287
|
+
* @param facts The balanced transaction, with its proofs erased, and what the record keeps of it.
|
|
288
|
+
* @returns Resolves once the move is applied.
|
|
289
|
+
*/
|
|
290
|
+
balanced(facts) {
|
|
291
|
+
return this.#run(this.#effectService.balanced(facts));
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Records a transaction as finalized, at the service's clock time.
|
|
295
|
+
*
|
|
296
|
+
* @param facts The finalized transaction and what the record keeps of it.
|
|
297
|
+
* @returns Resolves once the move is applied.
|
|
298
|
+
*/
|
|
299
|
+
finalized(facts) {
|
|
300
|
+
return this.#run(this.#effectService.finalized(facts));
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Records a transaction as submitted, at the service's clock time, which becomes its submission time.
|
|
304
|
+
*
|
|
305
|
+
* @param facts The submitted transaction and what the record keeps of it.
|
|
306
|
+
* @returns Resolves once the move is applied.
|
|
307
|
+
*/
|
|
308
|
+
submitted(facts) {
|
|
309
|
+
return this.#run(this.#effectService.submitted(facts));
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Records that the transaction holding any of `identifiers` was reverted.
|
|
313
|
+
*
|
|
314
|
+
* @param identifiers The reverted transaction's identifiers.
|
|
315
|
+
* @returns Resolves once the move is applied.
|
|
316
|
+
*/
|
|
317
|
+
reverted(identifiers) {
|
|
318
|
+
return this.#run(this.#effectService.reverted(identifiers));
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Gives up on every live transaction built for an epoch the chain has left by `chainNow`.
|
|
322
|
+
*
|
|
323
|
+
* @param chainNow The protocol version the chain has reached.
|
|
324
|
+
* @returns Resolves once the move is applied.
|
|
325
|
+
*/
|
|
326
|
+
orphanBeyond(chainNow) {
|
|
327
|
+
return this.#run(this.#effectService.orphanBeyond(chainNow));
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Records that a block included the transaction holding any of `identifiers`, with the chain's `verdict`.
|
|
331
|
+
*
|
|
332
|
+
* @param identifiers The included transaction's identifiers.
|
|
333
|
+
* @param verdict What the chain made of it.
|
|
334
|
+
* @returns Resolves once the move is applied.
|
|
335
|
+
*/
|
|
336
|
+
included(identifiers, verdict) {
|
|
337
|
+
return this.#run(this.#effectService.included(identifiers, verdict));
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Writes the records as they stand, with the traits the service was built with, for {@link restore} to read back.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* const stored = await facade.transactionLifecycleService.serializeState();
|
|
344
|
+
*
|
|
345
|
+
* @returns The store, in the current format version.
|
|
346
|
+
*/
|
|
347
|
+
serializeState() {
|
|
348
|
+
return Effect.runPromise(this.#effectService.serializeState());
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ProtocolVersion } from '@midnightntwrk/wallet-sdk-abstractions';
|
|
2
|
+
import { type DateTime, Option } from 'effect';
|
|
3
|
+
/**
|
|
4
|
+
* Ids of the coins a transaction spends, per wallet: unshielded coins as `intentHash#outputNo`, shielded coins by
|
|
5
|
+
* nullifier.
|
|
6
|
+
*/
|
|
7
|
+
export type Inputs = Readonly<{
|
|
8
|
+
unshielded: readonly string[];
|
|
9
|
+
shielded?: readonly string[];
|
|
10
|
+
}>;
|
|
11
|
+
export type TransactionTrait<TTransaction> = {
|
|
12
|
+
ids: (tx: TTransaction) => readonly string[];
|
|
13
|
+
firstId: (tx: TTransaction) => string;
|
|
14
|
+
areAllTxIdsIncluded: (tx: TTransaction, txIds: readonly string[]) => boolean;
|
|
15
|
+
/** The hash of each intent the transaction carries: the value the ledger stamps on the coins it creates. */
|
|
16
|
+
intentHashes: (tx: TTransaction) => readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* Every coin the transaction spends, whoever owns it: unshielded coins as `intentHash#outputNo`, shielded coins by
|
|
19
|
+
* nullifier, with `shielded` absent when it spends none.
|
|
20
|
+
*/
|
|
21
|
+
inputs: (tx: TTransaction) => Inputs;
|
|
22
|
+
/** The instant after which the transaction can no longer be included, for one created at `createdAt`. */
|
|
23
|
+
ttl: (tx: TTransaction, createdAt: DateTime.Utc) => Date;
|
|
24
|
+
serialize: (tx: TTransaction) => Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* What the bytes alone do not say about the transaction, stored beside them and handed back to `deserialize`; left
|
|
27
|
+
* out by a trait whose transactions are their bytes.
|
|
28
|
+
*/
|
|
29
|
+
formOf?: (tx: TTransaction) => string;
|
|
30
|
+
/** Reads the bytes `serialize` wrote, with the form `formOf` named when they were written, if it named one. */
|
|
31
|
+
deserialize: (serialized: Uint8Array, form?: string) => TTransaction;
|
|
32
|
+
isTx: (tx: unknown) => tx is TTransaction;
|
|
33
|
+
};
|
|
34
|
+
export type HasTransactionTrait<TTransaction> = {
|
|
35
|
+
txTrait: TransactionTrait<TTransaction>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The transaction traits a wallet reads its transactions with, keyed by the protocol version range each one serves.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* A transaction's bytes, identifiers and TTL are only meaningful under the ledger version it was authored against, so
|
|
42
|
+
* which trait applies is a property of the transaction rather than of the wallet holding it. Keeping that in the
|
|
43
|
+
* shared {@link ProtocolVersion.Registry} means the boundary between two traits is the same boundary variant selection
|
|
44
|
+
* and codec selection use.
|
|
45
|
+
*/
|
|
46
|
+
export type VersionedTransactionTrait<TTransaction> = ProtocolVersion.Registry<TransactionTrait<TTransaction>>;
|
|
47
|
+
export type HasVersionedTransactionTrait<TTransaction> = {
|
|
48
|
+
txTraits: VersionedTransactionTrait<TTransaction>;
|
|
49
|
+
};
|
|
50
|
+
/** Registers one trait for every protocol version: the shape a wallet that speaks a single ledger version has. */
|
|
51
|
+
export declare const singleTrait: <TTransaction>(trait: TransactionTrait<TTransaction>) => VersionedTransactionTrait<TTransaction>;
|
|
52
|
+
/** The oldest registered trait: what an envelope that carries no version stamp is read with. */
|
|
53
|
+
export declare const headTrait: <TTransaction>(traits: VersionedTransactionTrait<TTransaction>) => Option.Option<TransactionTrait<TTransaction>>;
|
|
54
|
+
/**
|
|
55
|
+
* The trait registered for a protocol version, or the oldest trait when nothing says which version applies.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* Falling back to the oldest trait is the same convention snapshot restore uses for an envelope written before versions
|
|
59
|
+
* were recorded: the only envelopes without a stamp are ones written before stamping existed, and those necessarily
|
|
60
|
+
* predate every version boundary since.
|
|
61
|
+
* @param traits The registered traits.
|
|
62
|
+
* @param protocolVersion The version the transaction was authored for, when one is known.
|
|
63
|
+
* @returns The trait to read with, or `Option.none()` when the version falls outside every registered range.
|
|
64
|
+
*/
|
|
65
|
+
export declare const traitForVersion: <TTransaction>(traits: VersionedTransactionTrait<TTransaction>, protocolVersion: Option.Option<ProtocolVersion.ProtocolVersion>) => Option.Option<TransactionTrait<TTransaction>>;
|
|
66
|
+
/** The registered trait that recognises a transaction object as its own, if any does. */
|
|
67
|
+
export declare const recognisingTrait: <TTransaction>(traits: VersionedTransactionTrait<TTransaction>, tx: unknown) => Option.Option<TransactionTrait<TTransaction>>;
|
|
68
|
+
/**
|
|
69
|
+
* The trait to read an incoming transaction with: the one that recognises it, or the oldest as a last resort.
|
|
70
|
+
*
|
|
71
|
+
* @param traits The registered traits.
|
|
72
|
+
* @param tx The transaction to place.
|
|
73
|
+
* @returns The trait that owns `tx`, the oldest trait when none claims it, or `Option.none()` when nothing is
|
|
74
|
+
* registered at all.
|
|
75
|
+
*/
|
|
76
|
+
export declare const traitForTx: <TTransaction>(traits: VersionedTransactionTrait<TTransaction>, tx: TTransaction) => Option.Option<TransactionTrait<TTransaction>>;
|
|
77
|
+
export type FailedTransactionResult = Readonly<{
|
|
78
|
+
segments: ReadonlyArray<{
|
|
79
|
+
id: number;
|
|
80
|
+
success: boolean;
|
|
81
|
+
}>;
|
|
82
|
+
status: 'PARTIAL_SUCCESS' | 'FAILURE';
|
|
83
|
+
}>;
|
|
84
|
+
export type SuccessTransactionResult = Readonly<{
|
|
85
|
+
segments: ReadonlyArray<{
|
|
86
|
+
id: number;
|
|
87
|
+
success: boolean;
|
|
88
|
+
}>;
|
|
89
|
+
status: 'SUCCESS';
|
|
90
|
+
}>;
|
|
91
|
+
/**
|
|
92
|
+
* The verdict on a transaction a protocol upgrade left behind.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* Deliberately not one of the indexer's statuses: the chain never reported anything about this transaction and never
|
|
96
|
+
* will, because bytes authored under the previous protocol version cannot be included under the new one. Saying so in
|
|
97
|
+
* its own arm keeps "the node rejected this" and "this can no longer be submitted at all" distinguishable.
|
|
98
|
+
*/
|
|
99
|
+
export type OrphanedByForkResult = Readonly<{
|
|
100
|
+
status: 'ORPHANED_BY_FORK';
|
|
101
|
+
/** The protocol version the transaction was authored for. */
|
|
102
|
+
authoredFor: ProtocolVersion.ProtocolVersion;
|
|
103
|
+
/** The protocol version the chain had reached when the wallet gave up on it. */
|
|
104
|
+
chainNow: ProtocolVersion.ProtocolVersion;
|
|
105
|
+
}>;
|
|
106
|
+
export type TransactionResult = FailedTransactionResult | SuccessTransactionResult | OrphanedByForkResult;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of MIDNIGHT-WALLET-SDK.
|
|
3
|
+
* Copyright (C) Midnight Foundation
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* You may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
import { ProtocolVersion } from '@midnightntwrk/wallet-sdk-abstractions';
|
|
16
|
+
import { Option } from 'effect';
|
|
17
|
+
/** Registers one trait for every protocol version: the shape a wallet that speaks a single ledger version has. */
|
|
18
|
+
export const singleTrait = (trait) => ({
|
|
19
|
+
entries: [
|
|
20
|
+
{
|
|
21
|
+
range: ProtocolVersion.makeRange(ProtocolVersion.MinSupportedVersion, ProtocolVersion.MaxSupportedVersion),
|
|
22
|
+
value: trait,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
/** The oldest registered trait: what an envelope that carries no version stamp is read with. */
|
|
27
|
+
export const headTrait = (traits) => Option.fromNullable(traits.entries.at(0)).pipe(Option.map((entry) => entry.value));
|
|
28
|
+
/**
|
|
29
|
+
* The trait registered for a protocol version, or the oldest trait when nothing says which version applies.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Falling back to the oldest trait is the same convention snapshot restore uses for an envelope written before versions
|
|
33
|
+
* were recorded: the only envelopes without a stamp are ones written before stamping existed, and those necessarily
|
|
34
|
+
* predate every version boundary since.
|
|
35
|
+
* @param traits The registered traits.
|
|
36
|
+
* @param protocolVersion The version the transaction was authored for, when one is known.
|
|
37
|
+
* @returns The trait to read with, or `Option.none()` when the version falls outside every registered range.
|
|
38
|
+
*/
|
|
39
|
+
export const traitForVersion = (traits, protocolVersion) => Option.match(protocolVersion, {
|
|
40
|
+
onNone: () => headTrait(traits),
|
|
41
|
+
onSome: (version) => ProtocolVersion.select(traits, version),
|
|
42
|
+
});
|
|
43
|
+
/** The registered trait that recognises a transaction object as its own, if any does. */
|
|
44
|
+
export const recognisingTrait = (traits, tx) => Option.fromNullable(traits.entries.find((entry) => entry.value.isTx(tx))).pipe(Option.map((entry) => entry.value));
|
|
45
|
+
/**
|
|
46
|
+
* The trait to read an incoming transaction with: the one that recognises it, or the oldest as a last resort.
|
|
47
|
+
*
|
|
48
|
+
* @param traits The registered traits.
|
|
49
|
+
* @param tx The transaction to place.
|
|
50
|
+
* @returns The trait that owns `tx`, the oldest trait when none claims it, or `Option.none()` when nothing is
|
|
51
|
+
* registered at all.
|
|
52
|
+
*/
|
|
53
|
+
export const traitForTx = (traits, tx) => Option.orElse(recognisingTrait(traits, tx), () => headTrait(traits));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ProtocolVersion } from '@midnightntwrk/wallet-sdk-abstractions';
|
|
2
|
-
import { Either } from 'effect';
|
|
2
|
+
import { Either, Option } from 'effect';
|
|
3
3
|
import { LedgerParametersCodec } from '../codecs/index.js';
|
|
4
|
-
import {
|
|
4
|
+
import { V9 } from '../simulation/index.js';
|
|
5
5
|
import type { AnyLedgerParameters, BlockData } from './validationService.js';
|
|
6
6
|
export type BlockDataFetcher = () => Promise<BlockData<AnyLedgerParameters>>;
|
|
7
7
|
/**
|
|
@@ -46,6 +46,25 @@ export type WireBlock = Readonly<{
|
|
|
46
46
|
* @returns The block data, or the typed reason its parameters could not be read.
|
|
47
47
|
*/
|
|
48
48
|
export declare const blockDataFrom: (codecs: LedgerParametersCodec.LedgerParametersCodecs<AnyLedgerParameters>, block: WireBlock) => Either.Either<BlockData<AnyLedgerParameters>, LedgerParametersCodec.LedgerParametersCodecError>;
|
|
49
|
+
/**
|
|
50
|
+
* Reads block data whose parameters arrived untyped back as one ledger version's.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* A wallet that priced a fee hands back the block it priced against with its parameters as `unknown`, because they are
|
|
54
|
+
* the pricing variant's own object and its API names no ledger version. Validation needs them typed, so this asks the
|
|
55
|
+
* object itself which ledger version made it; parameters of neither are refused rather than passed on, and the caller
|
|
56
|
+
* validates against a block it fetches.
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const { blockData } = await dustWallet.balanceTransactions([transaction], ttl);
|
|
60
|
+
* // Some with the parameters typed as either ledger version's; none when they are neither.
|
|
61
|
+
* const priced = withLedgerParameters(blockData);
|
|
62
|
+
* ```;
|
|
63
|
+
*
|
|
64
|
+
* @param blockData The block data, with parameters of unknown type.
|
|
65
|
+
* @returns The same block data with its parameters typed, or nothing when they are neither ledger version's.
|
|
66
|
+
*/
|
|
67
|
+
export declare const withLedgerParameters: (blockData: BlockData<unknown>) => Option.Option<BlockData<AnyLedgerParameters>>;
|
|
49
68
|
/**
|
|
50
69
|
* Builds a `BlockDataFetcher` that queries the indexer over HTTP for the latest block.
|
|
51
70
|
*
|
|
@@ -53,7 +72,7 @@ export declare const blockDataFrom: (codecs: LedgerParametersCodec.LedgerParamet
|
|
|
53
72
|
*/
|
|
54
73
|
export declare const makeDefaultBlockDataFetcher: (config: DefaultBlockDataFetcherConfiguration) => BlockDataFetcher;
|
|
55
74
|
/**
|
|
56
|
-
* Builds a `BlockDataFetcher` backed by a {@link Simulator}. Returns the simulator's latest block, using its
|
|
75
|
+
* Builds a `BlockDataFetcher` backed by a {@link V9.Simulator}. Returns the simulator's latest block, using its
|
|
57
76
|
* `currentTime` for the timestamp (so fast-forwarded simulator clocks are respected).
|
|
58
77
|
*/
|
|
59
|
-
export declare const
|
|
78
|
+
export declare const makeV9SimulatorBlockDataFetcher: (simulator: V9.Simulator) => BlockDataFetcher;
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
|
-
import
|
|
14
|
-
import
|
|
13
|
+
import * as ledgerV8 from '@midnightntwrk/ledger-v8';
|
|
14
|
+
import * as ledgerV9 from '@midnightntwrk/ledger-v9';
|
|
15
15
|
import { ProtocolVersion } from '@midnightntwrk/wallet-sdk-abstractions';
|
|
16
16
|
import { BlockHash } from '@midnightntwrk/wallet-sdk-indexer-client';
|
|
17
17
|
import { HttpQueryClient } from '@midnightntwrk/wallet-sdk-indexer-client/effect';
|
|
18
|
-
import { Effect, Either, pipe } from 'effect';
|
|
18
|
+
import { Effect, Either, Option, pipe } from 'effect';
|
|
19
19
|
import { LedgerParametersCodec } from '../codecs/index.js';
|
|
20
|
-
import {
|
|
20
|
+
import { V9 } from '../simulation/index.js';
|
|
21
21
|
/**
|
|
22
22
|
* The ledger parameters codecs validation reads blocks with, split at the version the chain forks at.
|
|
23
23
|
*
|
|
@@ -37,18 +37,18 @@ export const defaultLedgerParametersCodecs = (forkVersion) => Either.getOrThrow(
|
|
|
37
37
|
? [
|
|
38
38
|
{
|
|
39
39
|
sinceVersion: ProtocolVersion.MinSupportedVersion,
|
|
40
|
-
codec: LedgerParametersCodec.fromDeserializer((bytes) =>
|
|
40
|
+
codec: LedgerParametersCodec.fromDeserializer((bytes) => ledgerV8.LedgerParameters.deserialize(bytes)),
|
|
41
41
|
},
|
|
42
42
|
{
|
|
43
43
|
sinceVersion: forkVersion,
|
|
44
|
-
codec: LedgerParametersCodec.fromDeserializer((bytes) => LedgerParameters.deserialize(bytes)),
|
|
44
|
+
codec: LedgerParametersCodec.fromDeserializer((bytes) => ledgerV9.LedgerParameters.deserialize(bytes)),
|
|
45
45
|
},
|
|
46
46
|
]
|
|
47
47
|
: // A chain whose boundary is at or below the minimum supported version has no ledger-v8 epoch to register for.
|
|
48
48
|
[
|
|
49
49
|
{
|
|
50
50
|
sinceVersion: ProtocolVersion.MinSupportedVersion,
|
|
51
|
-
codec: LedgerParametersCodec.fromDeserializer((bytes) => LedgerParameters.deserialize(bytes)),
|
|
51
|
+
codec: LedgerParametersCodec.fromDeserializer((bytes) => ledgerV9.LedgerParameters.deserialize(bytes)),
|
|
52
52
|
},
|
|
53
53
|
]));
|
|
54
54
|
/**
|
|
@@ -66,6 +66,30 @@ export const blockDataFrom = (codecs, block) => pipe(LedgerParametersCodec.decod
|
|
|
66
66
|
ledgerParameters,
|
|
67
67
|
timestamp: new Date(block.timestamp),
|
|
68
68
|
})));
|
|
69
|
+
/**
|
|
70
|
+
* Reads block data whose parameters arrived untyped back as one ledger version's.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* A wallet that priced a fee hands back the block it priced against with its parameters as `unknown`, because they are
|
|
74
|
+
* the pricing variant's own object and its API names no ledger version. Validation needs them typed, so this asks the
|
|
75
|
+
* object itself which ledger version made it; parameters of neither are refused rather than passed on, and the caller
|
|
76
|
+
* validates against a block it fetches.
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const { blockData } = await dustWallet.balanceTransactions([transaction], ttl);
|
|
80
|
+
* // Some with the parameters typed as either ledger version's; none when they are neither.
|
|
81
|
+
* const priced = withLedgerParameters(blockData);
|
|
82
|
+
* ```;
|
|
83
|
+
*
|
|
84
|
+
* @param blockData The block data, with parameters of unknown type.
|
|
85
|
+
* @returns The same block data with its parameters typed, or nothing when they are neither ledger version's.
|
|
86
|
+
*/
|
|
87
|
+
export const withLedgerParameters = (blockData) => {
|
|
88
|
+
const { ledgerParameters } = blockData;
|
|
89
|
+
return ledgerParameters instanceof ledgerV9.LedgerParameters || ledgerParameters instanceof ledgerV8.LedgerParameters
|
|
90
|
+
? Option.some({ ...blockData, ledgerParameters })
|
|
91
|
+
: Option.none();
|
|
92
|
+
};
|
|
69
93
|
/**
|
|
70
94
|
* Builds a `BlockDataFetcher` that queries the indexer over HTTP for the latest block.
|
|
71
95
|
*
|
|
@@ -84,13 +108,13 @@ export const makeDefaultBlockDataFetcher = (config) => {
|
|
|
84
108
|
}).pipe(Effect.provide(HttpQueryClient.layer({ url })), Effect.scoped));
|
|
85
109
|
};
|
|
86
110
|
/**
|
|
87
|
-
* Builds a `BlockDataFetcher` backed by a {@link Simulator}. Returns the simulator's latest block, using its
|
|
111
|
+
* Builds a `BlockDataFetcher` backed by a {@link V9.Simulator}. Returns the simulator's latest block, using its
|
|
88
112
|
* `currentTime` for the timestamp (so fast-forwarded simulator clocks are respected).
|
|
89
113
|
*/
|
|
90
|
-
export const
|
|
114
|
+
export const makeV9SimulatorBlockDataFetcher = (simulator) => {
|
|
91
115
|
return () => Effect.runPromise(Effect.gen(function* () {
|
|
92
116
|
const state = yield* simulator.getLatestState();
|
|
93
|
-
const lastBlock = getLastBlock(state);
|
|
117
|
+
const lastBlock = V9.getLastBlock(state);
|
|
94
118
|
if (!lastBlock)
|
|
95
119
|
throw new Error('Simulator has not produced any block yet.');
|
|
96
120
|
return {
|
package/dist/validation/index.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* Rejects a promise with the typed failure itself rather than the fiber wrapper around it, so a caller can `catch` the
|
|
4
|
+
* error class the signature names.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* `E extends Error` is what makes that rejection legitimate rather than a thrown bare value, and every failure on this
|
|
8
|
+
* surface is a `Data.TaggedError`, which is one.
|
|
9
|
+
*/
|
|
10
|
+
export declare const runPromiseThrowingFailure: <A, E extends Error>(effect: Effect.Effect<A, E>) => Promise<A>;
|