tigerbeetle-node 0.4.3 → 0.5.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/src/index.ts CHANGED
@@ -130,7 +130,6 @@ export enum CommitTransferError {
130
130
  transfer_not_found,
131
131
  transfer_not_two_phase_commit,
132
132
  transfer_expired,
133
- already_auto_committed,
134
133
  already_committed,
135
134
  already_committed_but_accepted,
136
135
  already_committed_but_rejected,
@@ -151,16 +150,18 @@ export type CommitTransfersError = {
151
150
  }
152
151
 
153
152
  export type AccountID = bigint // u128
153
+ export type TransferID = bigint // u128
154
154
 
155
- export type Event = Account | Transfer | Commit | AccountID
156
- export type Result = CreateAccountsError | CreateTransfersError | CommitTransfersError | Account
155
+ export type Event = Account | Transfer | Commit | AccountID | TransferID
156
+ export type Result = CreateAccountsError | CreateTransfersError | CommitTransfersError | Account | Transfer
157
157
  export type ResultCallback = (error: undefined | Error, results: Result[]) => void
158
158
 
159
159
  export enum Operation {
160
160
  CREATE_ACCOUNT = 3,
161
161
  CREATE_TRANSFER,
162
162
  COMMIT_TRANSFER,
163
- ACCOUNT_LOOKUP
163
+ ACCOUNT_LOOKUP,
164
+ TRANSFER_LOOKUP
164
165
  }
165
166
 
166
167
  export interface Client {
@@ -168,6 +169,7 @@ export interface Client {
168
169
  createTransfers: (batch: Transfer[]) => Promise<CreateTransfersError[]>
169
170
  commitTransfers: (batch: Commit[]) => Promise<CommitTransfersError[]>
170
171
  lookupAccounts: (batch: AccountID[]) => Promise<Account[]>
172
+ lookupTransfers: (batch: TransferID[]) => Promise<Transfer[]>
171
173
  request: (operation: Operation, batch: Event[], callback: ResultCallback) => void
172
174
  rawRequest: (operation: Operation, rawBatch: Buffer, callback: ResultCallback) => void
173
175
  destroy: () => void
@@ -237,6 +239,7 @@ export function createClient (args: InitArgs): Client {
237
239
  const callback = (error: undefined | Error, results: CreateAccountsError[]) => {
238
240
  if (error) {
239
241
  reject(error)
242
+ return
240
243
  }
241
244
  resolve(results)
242
245
  }
@@ -263,6 +266,7 @@ export function createClient (args: InitArgs): Client {
263
266
  const callback = (error: undefined | Error, results: CreateTransfersError[]) => {
264
267
  if (error) {
265
268
  reject(error)
269
+ return
266
270
  }
267
271
  resolve(results)
268
272
  }
@@ -289,6 +293,7 @@ export function createClient (args: InitArgs): Client {
289
293
  const callback = (error: undefined | Error, results: CommitTransfersError[]) => {
290
294
  if (error) {
291
295
  reject(error)
296
+ return
292
297
  }
293
298
  resolve(results)
294
299
  }
@@ -306,6 +311,7 @@ export function createClient (args: InitArgs): Client {
306
311
  const callback = (error: undefined | Error, results: Account[]) => {
307
312
  if (error) {
308
313
  reject(error)
314
+ return
309
315
  }
310
316
  resolve(results)
311
317
  }
@@ -318,6 +324,24 @@ export function createClient (args: InitArgs): Client {
318
324
  })
319
325
  }
320
326
 
327
+ const lookupTransfers = async (batch: TransferID[]): Promise<Transfer[]> => {
328
+ return new Promise((resolve, reject) => {
329
+ const callback = (error: undefined | Error, results: Transfer[]) => {
330
+ if (error) {
331
+ reject(error)
332
+ return
333
+ }
334
+ resolve(results)
335
+ }
336
+
337
+ try {
338
+ binding.request(context, Operation.TRANSFER_LOOKUP, batch, callback)
339
+ } catch (error) {
340
+ reject(error)
341
+ }
342
+ })
343
+ }
344
+
321
345
  const destroy = (): void => {
322
346
  binding.deinit(context)
323
347
  if (_interval){
@@ -331,6 +355,7 @@ export function createClient (args: InitArgs): Client {
331
355
  createTransfers,
332
356
  commitTransfers,
333
357
  lookupAccounts,
358
+ lookupTransfers,
334
359
  request,
335
360
  rawRequest,
336
361
  destroy
package/src/node.zig CHANGED
@@ -213,7 +213,7 @@ fn decode_from_object(comptime T: type, env: c.napi_env, object: c.napi_value) !
213
213
  .credits_accepted = try translate.u64_from_object(env, object, "credits_accepted"),
214
214
  .timestamp = try validate_timestamp(env, object),
215
215
  },
216
- u128 => try translate.u128_from_value(env, object, "Account lookup"),
216
+ u128 => try translate.u128_from_value(env, object, "lookup"),
217
217
  else => unreachable,
218
218
  };
219
219
  }
@@ -229,6 +229,7 @@ pub fn decode_events(
229
229
  .create_transfers => try decode_events_from_array(env, array, Transfer, output),
230
230
  .commit_transfers => try decode_events_from_array(env, array, Commit, output),
231
231
  .lookup_accounts => try decode_events_from_array(env, array, u128, output),
232
+ .lookup_transfers => try decode_events_from_array(env, array, u128, output),
232
233
  else => unreachable,
233
234
  };
234
235
  }
@@ -307,7 +308,7 @@ fn encode_napi_results_array(
307
308
  );
308
309
  }
309
310
  },
310
- else => {
311
+ Account => {
311
312
  var i: u32 = 0;
312
313
  while (i < results.len) : (i += 1) {
313
314
  const result = results[i];
@@ -413,6 +414,105 @@ fn encode_napi_results_array(
413
414
  );
414
415
  }
415
416
  },
417
+ Transfer => {
418
+ var i: u32 = 0;
419
+ while (i < results.len) : (i += 1) {
420
+ const result = results[i];
421
+ const napi_object = try translate.create_object(
422
+ env,
423
+ "Failed to create transfer lookup result object.",
424
+ );
425
+
426
+ try translate.u128_into_object(
427
+ env,
428
+ napi_object,
429
+ "id",
430
+ result.id,
431
+ "Failed to set property \"id\" of transfer lookup result.",
432
+ );
433
+
434
+ try translate.u128_into_object(
435
+ env,
436
+ napi_object,
437
+ "debit_account_id",
438
+ result.debit_account_id,
439
+ "Failed to set property \"debit_account_id\" of transfer lookup result.",
440
+ );
441
+
442
+ try translate.u128_into_object(
443
+ env,
444
+ napi_object,
445
+ "credit_account_id",
446
+ result.credit_account_id,
447
+ "Failed to set property \"credit_account_id\" of transfer lookup result.",
448
+ );
449
+
450
+ try translate.u128_into_object(
451
+ env,
452
+ napi_object,
453
+ "user_data",
454
+ result.user_data,
455
+ "Failed to set property \"user_data\" of transfer lookup result.",
456
+ );
457
+
458
+ try translate.byte_slice_into_object(
459
+ env,
460
+ napi_object,
461
+ "reserved",
462
+ &result.reserved,
463
+ "Failed to set property \"reserved\" of transfer lookup result.",
464
+ );
465
+
466
+ try translate.u64_into_object(
467
+ env,
468
+ napi_object,
469
+ "timeout",
470
+ result.timeout,
471
+ "Failed to set property \"timeout\" of transfer lookup result.",
472
+ );
473
+
474
+ try translate.u32_into_object(
475
+ env,
476
+ napi_object,
477
+ "code",
478
+ @intCast(u32, result.code),
479
+ "Failed to set property \"code\" of transfer lookup result.",
480
+ );
481
+
482
+ try translate.u32_into_object(
483
+ env,
484
+ napi_object,
485
+ "flags",
486
+ @bitCast(u32, result.flags),
487
+ "Failed to set property \"flags\" of transfer lookup result.",
488
+ );
489
+
490
+ try translate.u64_into_object(
491
+ env,
492
+ napi_object,
493
+ "amount",
494
+ result.amount,
495
+ "Failed to set property \"amount\" of transfer lookup result.",
496
+ );
497
+
498
+ try translate.u64_into_object(
499
+ env,
500
+ napi_object,
501
+ "timestamp",
502
+ result.timestamp,
503
+ "Failed to set property \"timestamp\" of transfer lookup result.",
504
+ );
505
+
506
+ try translate.set_array_element(
507
+ env,
508
+ napi_array,
509
+ i,
510
+ napi_object,
511
+ "Failed to set element in results array.",
512
+ );
513
+ }
514
+ },
515
+ else => unreachable,
416
516
  }
417
517
 
418
518
  return napi_array;
@@ -610,6 +710,7 @@ fn on_result(user_data: u128, operation: Operation, results: Client.Error![]cons
610
710
  value,
611
711
  ) catch return,
612
712
  .lookup_accounts => encode_napi_results_array(Account, env, value) catch return,
713
+ .lookup_transfers => encode_napi_results_array(Transfer, env, value) catch return,
613
714
  };
614
715
 
615
716
  argv[0] = globals.napi_undefined;
package/src/test.ts CHANGED
@@ -176,6 +176,20 @@ test('can create a two-phase transfer', async (): Promise<void> => {
176
176
  assert.strictEqual(accounts[1].credits_reserved, 0n)
177
177
  assert.strictEqual(accounts[1].debits_accepted, 100n)
178
178
  assert.strictEqual(accounts[1].debits_reserved, 50n)
179
+
180
+ // Lookup the transfer
181
+ const transfers = await client.lookupTransfers([transfer.id])
182
+ assert.strictEqual(transfers.length, 1)
183
+ assert.strictEqual(transfers[0].id, 1n)
184
+ assert.strictEqual(transfers[0].debit_account_id, accountB.id)
185
+ assert.strictEqual(transfers[0].credit_account_id, accountA.id)
186
+ assert.strictEqual(transfers[0].user_data, 0n)
187
+ assert.notStrictEqual(transfers[0].reserved, Zeroed32Bytes)
188
+ assert.strictEqual(transfers[0].timeout > 0, true)
189
+ assert.strictEqual(transfers[0].code, 1)
190
+ assert.strictEqual(transfers[0].flags, 2)
191
+ assert.strictEqual(transfers[0].amount, 50n)
192
+ assert.strictEqual(transfers[0].timestamp > 0, true)
179
193
  })
180
194
 
181
195
  test('can commit a two-phase transfer', async (): Promise<void> => {
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
  set -e
3
- scripts/install_zig.sh 0.8.0
3
+ scripts/install_zig.sh 0.8.1
4
4
  echo "Building TigerBeetle..."
5
5
  zig/zig build -Dcpu=baseline -Drelease-safe
6
6
  mv zig-out/bin/tigerbeetle .
@@ -0,0 +1,109 @@
1
+ @echo off
2
+
3
+ set DEFAULT_RELEASE=0.8.1
4
+
5
+ :: Determine the Zig build:
6
+ if "%~1"=="" (
7
+ set ZIG_RELEASE=%DEFAULT_RELEASE%
8
+ ) else if "%~1"=="latest" (
9
+ set ZIG_RELEASE=builds
10
+ ) else (
11
+ set ZIG_RELEASE=%~1
12
+ )
13
+
14
+ :: Checks format of release version.
15
+ echo.%ZIG_RELEASE% | findstr /b /r /c:"builds" /c:"^[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*">nul || (echo.Unexpected release format. && exit 1)
16
+
17
+ set ZIG_OS=windows
18
+ set ZIG_ARCH=x86_64
19
+
20
+ set ZIG_TARGET=zig-%ZIG_OS%-%ZIG_ARCH%
21
+
22
+ :: Determine the build, split the JSON line on whitespace and extract the 2nd field:
23
+ for /f "tokens=2" %%a in ('curl --silent https://ziglang.org/download/index.json ^| findstr %ZIG_TARGET% ^| findstr %ZIG_RELEASE%' ) do (
24
+ set ZIG_URL=%%a
25
+ )
26
+
27
+ :: Then remove quotes and commas:
28
+ for /f %%b in ("%ZIG_URL:,=%") do (
29
+ set ZIG_URL=%%~b
30
+ )
31
+
32
+ :: Checks the ZIG_URL variable follows the expected format.
33
+ echo.%ZIG_URL% | findstr /b /r /c:"https://ziglang.org/builds/" /c:"https://ziglang.org/download/%ZIG_RELEASE%">nul || (echo.Unexpected release URL format. && exit 1)
34
+
35
+ if "%ZIG_RELEASE%"=="builds" (
36
+ echo Installing Zig latest build...
37
+ ) else (
38
+ echo Installing Zig %ZIG_RELEASE% release build...
39
+ )
40
+
41
+ :: Using variable modifiers to determine the directory and filename from the URL:
42
+ :: %~ni Expands %i to a file name only and %~xi Expands %i to a file name extension only.
43
+ for /f %%i in ("%ZIG_URL%") do (
44
+ set ZIG_DIRECTORY=%%~ni
45
+ set ZIG_TARBALL=%%~nxi
46
+ )
47
+
48
+ :: Checks the ZIG_DIRECTORY variable follows the expected format.
49
+ echo.%ZIG_DIRECTORY% | findstr /b /r /c:"zig-win64-" /c:"zig-windows-x86_64-">nul || (echo.Unexpected zip directory name format. && exit 1)
50
+
51
+ :: Making sure we download to the same output document, without wget adding "-1" etc. if the file was previously partially downloaded:
52
+ if exist %ZIG_TARBALL% (
53
+ del /q %ZIG_TARBALL%
54
+ if exist %ZIG_TARBALL% (
55
+ echo Failed to delete %ZIG_TARBALL%.
56
+ exit 1
57
+ )
58
+ )
59
+
60
+ echo Downloading %ZIG_URL%...
61
+ curl --silent --progress-bar --output %ZIG_TARBALL% %ZIG_URL%
62
+ if not exist %ZIG_TARBALL% (
63
+ echo Failed to download Zig zip file.
64
+ exit 1
65
+ )
66
+
67
+ :: Replace any existing Zig installation so that we can install or upgrade:
68
+ echo Removing any existing 'zig' and %ZIG_DIRECTORY% folders before extracting.
69
+ if exist zig\ (
70
+ rd /s /q zig\
71
+ :: Ensure the directory has been deleted.
72
+ if exist zig\ (
73
+ echo The ‘zig’ directory could not be deleted.
74
+ exit 1
75
+ )
76
+ )
77
+
78
+ if exist %ZIG_DIRECTORY%\ (
79
+ rd /s /q %ZIG_DIRECTORY%
80
+ :: Ensure the directory has been deleted.
81
+ if exist %ZIG_DIRECTORY% (
82
+ echo The %ZIG_DIRECTORY% directory could not be deleted.
83
+ exit 1
84
+ )
85
+ )
86
+
87
+ :: Extract and then remove the downloaded tarball:
88
+ echo Extracting %ZIG_TARBALL%...
89
+ powershell -Command "Expand-Archive %ZIG_TARBALL% -DestinationPath ."
90
+ if not exist %ZIG_TARBALL% (
91
+ echo Failed to extract zip file.
92
+ exit 1
93
+ )
94
+
95
+ echo Installing %ZIG_DIRECTORY% to 'zig' in current working directory...
96
+ ren %ZIG_DIRECTORY% zig
97
+ if exist %ZIG_DIRECTORY% (
98
+ echo Failed to rename %ZIG_DIRECTORY% to zig.
99
+ exit 1
100
+ )
101
+
102
+ :: Removes the zip file
103
+ del /q %ZIG_TARBALL%
104
+ if exist %ZIG_TARBALL% (
105
+ echo Failed to delete %ZIG_TARBALL% file.
106
+ exit 1
107
+ )
108
+
109
+ echo "Congratulations, you have successfully installed Zig version %ZIG_RELEASE%. Enjoy!"
@@ -0,0 +1,48 @@
1
+ :: Installs Zig if needed and runs the VOPR
2
+ @echo off
3
+
4
+ :: Install Zig if a zig folder does not already exist:
5
+ if not exist zig\ (
6
+ :: Installs the latest version of Zig
7
+ call scripts\install_zig.bat
8
+ :: Checks that the Zig folder now exists
9
+ if not exist zig\ (
10
+ echo The Zig installation failed.
11
+ exit 1
12
+ )
13
+ echo Running the TigerBeetle VOPR for the first time...
14
+ echo Visit https://www.tigerbeetle.com
15
+ )
16
+
17
+ :: If a seed is provided as an argument then replay the seed, otherwise test 1,000 seeds:
18
+ if not "%~1"=="" (
19
+ :: Build in fast ReleaseSafe mode if required, useful where you don't need debug logging:
20
+ if "%~2"=="-OReleaseSafe" (
21
+ echo Replaying seed %~1 in ReleaseSafe mode...
22
+ call zig\zig run src\simulator.zig -OReleaseSafe -- %~1
23
+ if not %ERRORLEVEL%==0 (
24
+ echo Cannot replay the %~1 seed using the VOPR.
25
+ exit 1
26
+ )
27
+ ) else (
28
+ echo Replaying seed %~1 in Debug mode with full debug logging enabled...
29
+ call zig\zig run src\simulator.zig -ODebug -- %~1
30
+ if not %ERRORLEVEL%==0 (
31
+ echo Cannot run the VOPR.
32
+ exit 1
33
+ )
34
+ )
35
+ ) else (
36
+ call zig\zig build-exe src\simulator.zig -OReleaseSafe
37
+ if not %ERRORLEVEL%==0 (
38
+ echo Cannot run the VOPR.
39
+ exit 1
40
+ )
41
+ for %%i in (1,1,1000) do (
42
+ call simulator
43
+ if not %ERRORLEVEL%==0 (
44
+ echo Cannot run a seed using the VOPR.
45
+ exit 1
46
+ )
47
+ )
48
+ )
@@ -223,7 +223,7 @@ const TimedQueue = struct {
223
223
 
224
224
  const now = std.time.milliTimestamp();
225
225
  self.start = now;
226
- if (self.batches.peek_ptr()) |starting_batch| {
226
+ if (self.batches.head_ptr()) |starting_batch| {
227
227
  log.debug("sending first batch...", .{});
228
228
  self.batch_start = now;
229
229
  var message = self.client.get_message() orelse {
@@ -285,7 +285,7 @@ const TimedQueue = struct {
285
285
  else => unreachable,
286
286
  }
287
287
 
288
- if (self.batches.peek_ptr()) |next_batch| {
288
+ if (self.batches.head_ptr()) |next_batch| {
289
289
  var message = self.client.get_message() orelse {
290
290
  @panic("Client message pool has been exhausted.");
291
291
  };
@@ -81,6 +81,14 @@ pub const Demo = struct {
81
81
  print_results(Account, results);
82
82
  }
83
83
 
84
+ pub fn on_lookup_transfers(
85
+ user_data: u128,
86
+ operation: StateMachine.Operation,
87
+ results: Client.Error![]const u8,
88
+ ) void {
89
+ print_results(Transfer, results);
90
+ }
91
+
84
92
  pub fn on_create_transfers(
85
93
  user_data: u128,
86
94
  operation: StateMachine.Operation,
@@ -0,0 +1,8 @@
1
+ usingnamespace @import("tigerbeetle.zig");
2
+ usingnamespace @import("demo.zig");
3
+
4
+ pub fn main() !void {
5
+ const ids = [_]u128{ 1000, 1001, 1002 };
6
+
7
+ try Demo.request(.lookup_transfers, ids, Demo.on_lookup_transfers);
8
+ }
@@ -1216,7 +1216,7 @@ test "accept/connect/send/receive" {
1216
1216
  completion,
1217
1217
  self.client,
1218
1218
  &self.send_buf,
1219
- os.MSG_NOSIGNAL,
1219
+ if (std.Target.current.os.tag == .linux) os.MSG_NOSIGNAL else 0,
1220
1220
  );
1221
1221
  }
1222
1222
 
@@ -1241,7 +1241,7 @@ test "accept/connect/send/receive" {
1241
1241
  completion,
1242
1242
  self.accepted_sock,
1243
1243
  &self.recv_buf,
1244
- os.MSG_NOSIGNAL,
1244
+ if (std.Target.current.os.tag == .linux) os.MSG_NOSIGNAL else 0,
1245
1245
  );
1246
1246
  }
1247
1247
 
@@ -105,6 +105,7 @@ pub const IO = struct {
105
105
 
106
106
  for (events[0..new_events]) |event| {
107
107
  const completion = @intToPtr(*Completion, event.udata);
108
+ completion.next = null;
108
109
  self.completed.push(completion);
109
110
  }
110
111
  }
@@ -405,6 +406,7 @@ pub const IO = struct {
405
406
  InputOutput,
406
407
  NoSpaceLeft,
407
408
  ReadOnlyFileSystem,
409
+ AccessDenied,
408
410
  } || os.UnexpectedError;
409
411
 
410
412
  pub fn fsync(
@@ -455,6 +457,8 @@ pub const IO = struct {
455
457
  NoSpaceLeft,
456
458
  NotDir,
457
459
  FileLocksNotSupported,
460
+ BadPathName,
461
+ InvalidUtf8,
458
462
  WouldBlock,
459
463
  } || os.UnexpectedError;
460
464
 
@@ -919,7 +919,7 @@ fn MessageBusImpl(comptime process_type: ProcessType) type {
919
919
  assert(connection.peer == .client or connection.peer == .replica);
920
920
  assert(connection.state == .connected);
921
921
  assert(connection.fd != -1);
922
- const message = connection.send_queue.peek() orelse return;
922
+ const message = connection.send_queue.head() orelse return;
923
923
  assert(!connection.send_submitted);
924
924
  connection.send_submitted = true;
925
925
  bus.io.send(
@@ -949,9 +949,9 @@ fn MessageBusImpl(comptime process_type: ProcessType) type {
949
949
  connection.terminate(bus, .shutdown);
950
950
  return;
951
951
  };
952
- assert(connection.send_progress <= connection.send_queue.peek_ptr().?.*.header.size);
952
+ assert(connection.send_progress <= connection.send_queue.head().?.header.size);
953
953
  // If the message has been fully sent, move on to the next one.
954
- if (connection.send_progress == connection.send_queue.peek_ptr().?.*.header.size) {
954
+ if (connection.send_progress == connection.send_queue.head().?.header.size) {
955
955
  connection.send_progress = 0;
956
956
  const message = connection.send_queue.pop().?;
957
957
  bus.unref(message);