tigerbeetle-node 0.10.0 → 0.11.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 +302 -101
- package/dist/index.d.ts +70 -72
- package/dist/index.js +70 -72
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/scripts/download_node_headers.sh +14 -7
- package/src/index.ts +6 -10
- package/src/node.zig +6 -3
- package/src/tigerbeetle/scripts/benchmark.sh +4 -4
- package/src/tigerbeetle/scripts/confirm_image.sh +44 -0
- package/src/tigerbeetle/scripts/fuzz_loop.sh +15 -0
- package/src/tigerbeetle/scripts/fuzz_unique_errors.sh +7 -0
- package/src/tigerbeetle/scripts/install.sh +19 -4
- package/src/tigerbeetle/scripts/install_zig.bat +5 -1
- package/src/tigerbeetle/scripts/install_zig.sh +24 -14
- package/src/tigerbeetle/scripts/pre-commit.sh +9 -0
- package/src/tigerbeetle/scripts/shellcheck.sh +5 -0
- package/src/tigerbeetle/scripts/tests_on_alpine.sh +10 -0
- package/src/tigerbeetle/scripts/tests_on_ubuntu.sh +14 -0
- package/src/tigerbeetle/src/benchmark.zig +4 -2
- package/src/tigerbeetle/src/benchmark_array_search.zig +3 -3
- package/src/tigerbeetle/src/c/tb_client/thread.zig +8 -9
- package/src/tigerbeetle/src/c/tb_client.h +100 -80
- package/src/tigerbeetle/src/c/tb_client.zig +4 -1
- package/src/tigerbeetle/src/cli.zig +1 -1
- package/src/tigerbeetle/src/config.zig +48 -16
- package/src/tigerbeetle/src/demo.zig +3 -1
- package/src/tigerbeetle/src/eytzinger_benchmark.zig +3 -3
- package/src/tigerbeetle/src/io/linux.zig +1 -1
- package/src/tigerbeetle/src/lsm/README.md +214 -0
- package/src/tigerbeetle/src/lsm/binary_search.zig +137 -10
- package/src/tigerbeetle/src/lsm/bloom_filter.zig +43 -0
- package/src/tigerbeetle/src/lsm/compaction.zig +352 -398
- package/src/tigerbeetle/src/lsm/composite_key.zig +2 -0
- package/src/tigerbeetle/src/lsm/eytzinger.zig +1 -1
- package/src/tigerbeetle/src/lsm/forest.zig +21 -447
- package/src/tigerbeetle/src/lsm/forest_fuzz.zig +412 -0
- package/src/tigerbeetle/src/lsm/grid.zig +145 -69
- package/src/tigerbeetle/src/lsm/groove.zig +196 -133
- package/src/tigerbeetle/src/lsm/k_way_merge.zig +40 -18
- package/src/tigerbeetle/src/lsm/level_iterator.zig +28 -9
- package/src/tigerbeetle/src/lsm/manifest.zig +81 -181
- package/src/tigerbeetle/src/lsm/manifest_level.zig +210 -454
- package/src/tigerbeetle/src/lsm/manifest_log.zig +77 -28
- package/src/tigerbeetle/src/lsm/posted_groove.zig +64 -76
- package/src/tigerbeetle/src/lsm/segmented_array.zig +561 -241
- package/src/tigerbeetle/src/lsm/segmented_array_benchmark.zig +148 -0
- package/src/tigerbeetle/src/lsm/segmented_array_fuzz.zig +9 -0
- package/src/tigerbeetle/src/lsm/set_associative_cache.zig +62 -12
- package/src/tigerbeetle/src/lsm/table.zig +83 -48
- package/src/tigerbeetle/src/lsm/table_immutable.zig +30 -23
- package/src/tigerbeetle/src/lsm/table_iterator.zig +25 -14
- package/src/tigerbeetle/src/lsm/table_mutable.zig +63 -12
- package/src/tigerbeetle/src/lsm/test.zig +49 -55
- package/src/tigerbeetle/src/lsm/tree.zig +407 -402
- package/src/tigerbeetle/src/lsm/tree_fuzz.zig +457 -0
- package/src/tigerbeetle/src/main.zig +28 -6
- package/src/tigerbeetle/src/message_bus.zig +2 -2
- package/src/tigerbeetle/src/message_pool.zig +14 -17
- package/src/tigerbeetle/src/simulator.zig +145 -112
- package/src/tigerbeetle/src/state_machine.zig +338 -228
- package/src/tigerbeetle/src/static_allocator.zig +65 -0
- package/src/tigerbeetle/src/storage.zig +3 -7
- package/src/tigerbeetle/src/test/accounting/auditor.zig +577 -0
- package/src/tigerbeetle/src/test/accounting/workload.zig +819 -0
- package/src/tigerbeetle/src/test/cluster.zig +18 -48
- package/src/tigerbeetle/src/test/conductor.zig +365 -0
- package/src/tigerbeetle/src/test/fuzz.zig +121 -0
- package/src/tigerbeetle/src/test/id.zig +89 -0
- package/src/tigerbeetle/src/test/priority_queue.zig +645 -0
- package/src/tigerbeetle/src/test/state_checker.zig +93 -69
- package/src/tigerbeetle/src/test/state_machine.zig +11 -35
- package/src/tigerbeetle/src/test/storage.zig +29 -8
- package/src/tigerbeetle/src/tigerbeetle.zig +14 -16
- package/src/tigerbeetle/src/unit_tests.zig +7 -0
- package/src/tigerbeetle/src/vopr.zig +494 -0
- package/src/tigerbeetle/src/vopr_hub/README.md +58 -0
- package/src/tigerbeetle/src/vopr_hub/SETUP.md +199 -0
- package/src/tigerbeetle/src/vopr_hub/go.mod +3 -0
- package/src/tigerbeetle/src/vopr_hub/main.go +1022 -0
- package/src/tigerbeetle/src/vopr_hub/scheduler/go.mod +3 -0
- package/src/tigerbeetle/src/vopr_hub/scheduler/main.go +403 -0
- package/src/tigerbeetle/src/vsr/client.zig +13 -0
- package/src/tigerbeetle/src/vsr/journal.zig +16 -13
- package/src/tigerbeetle/src/vsr/replica.zig +924 -491
- package/src/tigerbeetle/src/vsr/superblock.zig +55 -37
- package/src/tigerbeetle/src/vsr/superblock_client_table.zig +7 -10
- package/src/tigerbeetle/src/vsr/superblock_free_set.zig +2 -2
- package/src/tigerbeetle/src/vsr/superblock_manifest.zig +18 -3
- package/src/tigerbeetle/src/vsr.zig +75 -55
- package/src/tigerbeetle/scripts/vopr.bat +0 -48
- package/src/tigerbeetle/scripts/vopr.sh +0 -33
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# tigerbeetle-node
|
|
2
|
-
[TigerBeetle](https://github.com/
|
|
2
|
+
[TigerBeetle](https://github.com/tigerbeetledb/tigerbeetle) client for Node.js.
|
|
3
3
|
|
|
4
4
|
## Installation
|
|
5
5
|
|
|
@@ -9,6 +9,16 @@ Install the `tigerbeetle-node` module to your current working directory:
|
|
|
9
9
|
npm install tigerbeetle-node
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
+
If you run into issues, check out the distribution-specific install
|
|
13
|
+
steps that are run in CI to test support:
|
|
14
|
+
|
|
15
|
+
* [Alpine](./scripts/test_install_on_alpine.sh)
|
|
16
|
+
* [Amazon Linux](./scripts/test_install_on_amazonlinux.sh)
|
|
17
|
+
* [Debian](./scripts/test_install_on_debian.sh)
|
|
18
|
+
* [Fedora](./scripts/test_install_on_fedora.sh)
|
|
19
|
+
* [Ubuntu](./scripts/test_install_on_ubuntu.sh)
|
|
20
|
+
* [RHEL](./scripts/test_install_on_rhelubi.sh)
|
|
21
|
+
|
|
12
22
|
### Prerequisites
|
|
13
23
|
|
|
14
24
|
* NodeJS >= `14.0.0`. _(If the correct version is not installed, an installation error will occur)_
|
|
@@ -24,140 +34,282 @@ For the moment, only one client can be instantiated globally per process.
|
|
|
24
34
|
Future releases will allow multiple client instantiations.
|
|
25
35
|
|
|
26
36
|
```js
|
|
27
|
-
|
|
37
|
+
const {
|
|
38
|
+
createClient,
|
|
39
|
+
CreateAccountError,
|
|
40
|
+
CreateTransferError,
|
|
41
|
+
AccountFlags,
|
|
42
|
+
TransferFlags,
|
|
43
|
+
} = require('tigerbeetle-node');
|
|
28
44
|
|
|
29
45
|
const client = createClient({
|
|
30
46
|
cluster_id: 0,
|
|
31
47
|
replica_addresses: ['3001', '3002', '3003']
|
|
32
|
-
})
|
|
48
|
+
});
|
|
33
49
|
```
|
|
34
50
|
|
|
35
|
-
One of the ways TigerBeetle achieves its performance is through
|
|
36
|
-
This is reflected in the below function interfaces where
|
|
51
|
+
One of the ways TigerBeetle achieves its performance is through
|
|
52
|
+
batching. This is reflected in the below function interfaces where
|
|
53
|
+
each one takes in an array of items.
|
|
54
|
+
|
|
55
|
+
### Sidenote: `BigInt`
|
|
56
|
+
|
|
57
|
+
TigerBeetle uses 64-bit integers for many fields while JavaScript's
|
|
58
|
+
builtin `Number` maximum value is 2^53-1. The `n` suffix in JavaScript
|
|
59
|
+
means the value is a BigInt. This is useful for literal numbers. If
|
|
60
|
+
you already have a `Number` variable though, you can call the `BigInt`
|
|
61
|
+
constructor to get a `BigInt` from it. For example, `1n` is the same
|
|
62
|
+
as `BigInt(1)`.
|
|
63
|
+
|
|
64
|
+
## Creating Accounts: `client.createAccounts`
|
|
37
65
|
|
|
38
|
-
|
|
66
|
+
See details for account fields in the [Accounts
|
|
67
|
+
reference](https://docs.tigerbeetle.com/reference/accounts).
|
|
39
68
|
|
|
40
69
|
```js
|
|
41
70
|
const account = {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
71
|
+
id: 137n, // u128
|
|
72
|
+
user_data: 0n, // u128, opaque third-party identifier to link this account to an external entity:
|
|
73
|
+
reserved: Buffer.alloc(48, 0), // [48]u8
|
|
74
|
+
ledger: 1, // u32, ledger value
|
|
75
|
+
code: 718, // u16, a chart of accounts code describing the type of account (e.g. clearing, settlement)
|
|
76
|
+
flags: 0, // u16
|
|
77
|
+
debits_pending: 0n, // u64
|
|
78
|
+
debits_posted: 0n, // u64
|
|
79
|
+
credits_pending: 0n, // u64
|
|
80
|
+
credits_posted: 0n, // u64
|
|
81
|
+
timestamp: 0n, // u64, Reserved: This will be set by the server.
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const errors = await client.createAccounts([account]);
|
|
85
|
+
if (errors.length) {
|
|
86
|
+
// Grab a human-readable message from the response
|
|
87
|
+
console.log(CreateAccountError[errors[0].code]);
|
|
53
88
|
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Account Flags
|
|
92
|
+
|
|
93
|
+
The account `flags` value is a bitfield. See details for these flags in
|
|
94
|
+
the [Accounts
|
|
95
|
+
reference](https://docs.tigerbeetle.com/reference/accounts#flags).
|
|
54
96
|
|
|
55
|
-
|
|
97
|
+
To toggle behavior for an account, combine enum values stored in the
|
|
98
|
+
`AccountFlags` object (in TypeScript it is an actual enum) with
|
|
99
|
+
bitwise-or:
|
|
100
|
+
|
|
101
|
+
* `AccountFlags.linked`
|
|
102
|
+
* `AccountFlags.debits_must_not_exceed_credits`
|
|
103
|
+
* `AccountFlags.credits_must_not_exceed_credits`
|
|
104
|
+
|
|
105
|
+
For example, to link `account0` and `account1`, where `account0`
|
|
106
|
+
additionally has the `debits_must_not_exceed_credits` constraint:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const account0 = { ... account values ... };
|
|
110
|
+
const account1 = { ... account values ... };
|
|
111
|
+
account0.flags = AccountFlags.linked | AccountFlags.debits_must_not_exceed_credits;
|
|
112
|
+
// Create the account
|
|
113
|
+
const errors = client.createAccounts([account0, account1]);
|
|
56
114
|
```
|
|
57
|
-
|
|
115
|
+
|
|
116
|
+
### Response and Errors
|
|
117
|
+
|
|
118
|
+
The response is an empty array if all accounts were created
|
|
119
|
+
successfully. If the response is non-empty, each object in the
|
|
120
|
+
response array contains error information for an account that
|
|
121
|
+
failed. The error object contains an error code and the index of the
|
|
122
|
+
account in the request batch.
|
|
123
|
+
|
|
58
124
|
```js
|
|
59
|
-
|
|
125
|
+
const errors = await client.createAccounts([account1, account2, account3]);
|
|
60
126
|
|
|
61
|
-
|
|
62
|
-
|
|
127
|
+
// errors = [{ index: 1, code: 1 }];
|
|
128
|
+
for (const error of errors) {
|
|
63
129
|
switch (error.code) {
|
|
64
|
-
case CreateAccountError.exists:
|
|
65
|
-
console.error(`Batch
|
|
66
|
-
|
|
130
|
+
case CreateAccountError.exists:
|
|
131
|
+
console.error(`Batch account at ${error.index} already exists.`);
|
|
132
|
+
break;
|
|
133
|
+
default:
|
|
134
|
+
console.error(`Batch account at ${error.index} failed to create: ${CreateAccountError[error.code]}.`);
|
|
67
135
|
}
|
|
136
|
+
}
|
|
68
137
|
```
|
|
69
|
-
The example above shows that the event in index 1 failed with error 1. This means that `account1` and `account3` were created successfully but not `account2`.
|
|
70
138
|
|
|
71
|
-
The
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
| `linked` | `debits_must_not_exceed_credits` | `credits_must_not_exceed_debits` |
|
|
139
|
+
The example above shows that the account in index 1 failed with
|
|
140
|
+
error 1. This error here means that `account1` and `account3` were
|
|
141
|
+
created successfully. But `account2` was not created.
|
|
75
142
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
debits_must_not_exceed_credits = (1 << 1),
|
|
81
|
-
credits_must_not_exceed_debits = (1 << 2)
|
|
82
|
-
}
|
|
143
|
+
To handle errors you can either 1) exactly match error codes returned
|
|
144
|
+
from `client.createAccounts` with enum values in the
|
|
145
|
+
`CreateAccountError` object, or you can 2) look up the error code in
|
|
146
|
+
the `CreateAccountError` object for a human-readable string.
|
|
83
147
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
148
|
+
## Account Lookup: `client.lookupAccounts`
|
|
149
|
+
|
|
150
|
+
Account lookup is batched, like account creation. Pass in all `id`s to
|
|
151
|
+
fetch, and matched accounts are returned.
|
|
87
152
|
|
|
88
|
-
|
|
153
|
+
If no account matches an `id`, no object is returned for that
|
|
154
|
+
account. So the order of accounts in the response is not necessarily
|
|
155
|
+
the same as the order of `id`s in the request. You can refer to the
|
|
156
|
+
`id` field in the response to distinguish accounts.
|
|
89
157
|
|
|
90
|
-
The `id` of the account is used for lookups. Only matched accounts are returned.
|
|
91
158
|
```js
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
### Creating a Transfer
|
|
159
|
+
// account 137n exists, 138n does not
|
|
160
|
+
const accounts = await client.lookupAccounts([137n, 138n]);
|
|
161
|
+
/* console.log(accounts);
|
|
162
|
+
* [{
|
|
163
|
+
* id: 137n,
|
|
164
|
+
* user_data: 0n,
|
|
165
|
+
* reserved: Buffer,
|
|
166
|
+
* ledger: 1,
|
|
167
|
+
* code: 718,
|
|
168
|
+
* flags: 0,
|
|
169
|
+
* debits_pending: 0n,
|
|
170
|
+
* debits_posted: 0n,
|
|
171
|
+
* credits_pending: 0n,
|
|
172
|
+
* credits_posted: 0n,
|
|
173
|
+
* timestamp: 1623062009212508993n,
|
|
174
|
+
* }]
|
|
175
|
+
*/
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Creating Transfers: `client.createTransfers`
|
|
113
179
|
|
|
114
180
|
This creates a journal entry between two accounts.
|
|
181
|
+
|
|
182
|
+
See details for transfer fields in the [Transfers
|
|
183
|
+
reference](https://docs.tigerbeetle.com/reference/transfers).
|
|
184
|
+
|
|
115
185
|
```js
|
|
116
186
|
const transfer = {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
187
|
+
id: 1n, // u128
|
|
188
|
+
pending_id: 0n, // u128
|
|
189
|
+
// Double-entry accounting:
|
|
190
|
+
debit_account_id: 1n, // u128
|
|
191
|
+
credit_account_id: 2n, // u128
|
|
192
|
+
// Opaque third-party identifier to link this transfer to an external entity:
|
|
193
|
+
user_data: 0n, // u128
|
|
194
|
+
reserved: 0n, // u128
|
|
195
|
+
// Timeout applicable for a pending/2-phase transfer:
|
|
196
|
+
timeout: 0n, // u64, in nano-seconds.
|
|
197
|
+
// Collection of accounts usually grouped by the currency:
|
|
198
|
+
// You can't transfer money between accounts with different ledgers:
|
|
199
|
+
ledger: 1, // u32, ledger for transfer (e.g. currency).
|
|
200
|
+
// Chart of accounts code describing the reason for the transfer:
|
|
201
|
+
code: 720, // u16, (e.g. deposit, settlement)
|
|
202
|
+
flags: 0, // u16
|
|
203
|
+
amount: 10n, // u64
|
|
204
|
+
timestamp: 0n, //u64, Reserved: This will be set by the server.
|
|
205
|
+
};
|
|
206
|
+
const errors = await client.createTransfers([transfer]);
|
|
207
|
+
for (const error of errors) {
|
|
208
|
+
switch (error.code) {
|
|
209
|
+
default:
|
|
210
|
+
console.error(`Batch transfer at ${error.index} failed to create: ${CreateAccountError[error.code]}.`);
|
|
211
|
+
}
|
|
134
212
|
}
|
|
135
|
-
const errors = await client.createTransfers([transfer])
|
|
136
213
|
```
|
|
137
|
-
Two-phase transfers are supported natively by toggling the appropriate flag. TigerBeetle will then adjust the `credits_pending` and `debits_pending` fields of the appropriate accounts. A corresponding commit transfer then needs to be sent to accept or reject the transfer.
|
|
138
214
|
|
|
139
|
-
|
|
215
|
+
### Response and Errors
|
|
216
|
+
|
|
217
|
+
The response is an empty array if all transfers were created
|
|
218
|
+
successfully. If the response is non-empty, each object in the
|
|
219
|
+
response array contains error information for an transfer that
|
|
220
|
+
failed. The error object contains an error code and the index of the
|
|
221
|
+
transfer in the request batch.
|
|
222
|
+
|
|
140
223
|
```js
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
224
|
+
const errors = await client.createTransfers([transfer1, transfer2, transfer3]);
|
|
225
|
+
|
|
226
|
+
// errors = [{ index: 1, code: 1 }];
|
|
227
|
+
for (const error of errors) {
|
|
228
|
+
switch (error.code) {
|
|
229
|
+
case CreateTransferError.exists:
|
|
230
|
+
console.error(`Batch transfer at ${error.index} already exists.`);
|
|
231
|
+
break;
|
|
232
|
+
default:
|
|
233
|
+
console.error(`Batch transfer at ${error.index} failed to create: ${CreateTransferError[error.code]}.`);
|
|
146
234
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
The example above shows that the transfer in index 1 failed with
|
|
239
|
+
error 1. This error here means that `transfer1` and `transfer3` were
|
|
240
|
+
created successfully. But `transfer2` was not created.
|
|
241
|
+
|
|
242
|
+
To handle errors you can either 1) exactly match error codes returned
|
|
243
|
+
from `client.createTransfers` with enum values in the
|
|
244
|
+
`CreateTransferError` object, or you can 2) look up the error code in
|
|
245
|
+
the `CreateTransferError` object for a human-readable string.
|
|
246
|
+
|
|
247
|
+
### Batching
|
|
248
|
+
|
|
249
|
+
TigerBeetle performance is maximized when you batch inserts. The
|
|
250
|
+
client does not do this automatically for you. So, for example, you
|
|
251
|
+
can insert 1 million transfers one at a time like so:
|
|
252
|
+
|
|
253
|
+
```js
|
|
254
|
+
for (let i = 0; i < 1_000_000; i++) {
|
|
255
|
+
const errors = client.createTransfers(transfers[i]);
|
|
256
|
+
// error handling omitted
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
But the insert rate will be a fraction of potential. Instead, always
|
|
261
|
+
batch what you can. The maximum batch size is set in the TigerBeetle
|
|
262
|
+
server. The default is 8191.
|
|
263
|
+
|
|
264
|
+
```js
|
|
265
|
+
const BATCH_SIZE = 8191;
|
|
266
|
+
for (let i = 0; i < 1_000_000; i += BATCH_SIZE) {
|
|
267
|
+
const errors = client.createTransfers(transfers.slice(i, Math.min(transfers.length, BATCH_SIZE)));
|
|
268
|
+
// error handling omitted
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Transfer Flags
|
|
273
|
+
|
|
274
|
+
The transfer `flags` value is a bitfield. See details for these flags in
|
|
275
|
+
the [Transfers
|
|
276
|
+
reference](https://docs.tigerbeetle.com/reference/transfers#flags).
|
|
151
277
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
278
|
+
To toggle behavior for a transfer, combine enum values stored in the
|
|
279
|
+
`TransferFlags` object (in TypeScript it is an actual enum) with
|
|
280
|
+
bitwise-or:
|
|
281
|
+
|
|
282
|
+
* `TransferFlags.linked`
|
|
283
|
+
* `TransferFlags.pending`
|
|
284
|
+
* `TransferFlags.post_pending_transfer`
|
|
285
|
+
* `TransferFlags.void_pending_transfer`
|
|
286
|
+
|
|
287
|
+
For example, to link `transfer0` and `transfer1`:
|
|
288
|
+
|
|
289
|
+
```js
|
|
290
|
+
const transfer0 = { ... transfer values ... };
|
|
291
|
+
const transfer1 = { ... transfer values ... };
|
|
292
|
+
transfer0.flags = TransferFlags.linked;
|
|
293
|
+
// Create the transfer
|
|
294
|
+
const errors = client.createTransfers([transfer0, transfer1]);
|
|
156
295
|
```
|
|
157
296
|
|
|
158
|
-
|
|
297
|
+
#### Two-Phase Transfers
|
|
298
|
+
|
|
299
|
+
Two-phase transfers are supported natively by toggling the appropriate
|
|
300
|
+
flag. TigerBeetle will then adjust the `credits_pending` and
|
|
301
|
+
`debits_pending` fields of the appropriate accounts. A corresponding
|
|
302
|
+
post pending transfer then needs to be sent to post or void the
|
|
303
|
+
transfer.
|
|
304
|
+
|
|
305
|
+
##### Post a Pending Transfer
|
|
306
|
+
|
|
307
|
+
With `transfer.flags == TransferFlags.post_pending_transfer`,
|
|
308
|
+
TigerBeetle will post the transfer. TigerBeetle will atomically roll
|
|
309
|
+
back the changes to `debits_pending` and `credits_pending` of the
|
|
310
|
+
appropriate accounts and apply them to the `debits_posted` and
|
|
311
|
+
`credits_posted` balances.
|
|
159
312
|
|
|
160
|
-
With `flags = post_pending_transfer`, TigerBeetle will accept the transfer. TigerBeetle will atomically rollback the changes to `debits_pending` and `credits_pending` of the appropriate accounts and apply them to the `debits_posted` and `credits_posted` balances.
|
|
161
313
|
```js
|
|
162
314
|
const post = {
|
|
163
315
|
id: 2n, // u128, must correspond to the transfer id
|
|
@@ -168,9 +320,58 @@ const post = {
|
|
|
168
320
|
const errors = await client.createTransfers([post])
|
|
169
321
|
```
|
|
170
322
|
|
|
171
|
-
|
|
323
|
+
## Transfer Lookup: `client.lookupTransfers`
|
|
324
|
+
|
|
325
|
+
NOTE: While transfer lookup exists, it is not a flexible query API. We
|
|
326
|
+
are developing query APIs and there will be new methods for querying
|
|
327
|
+
transfers in the future.
|
|
328
|
+
|
|
329
|
+
Transfer lookup is batched, like transfer creation. Pass in all `id`s to
|
|
330
|
+
fetch, and matched transfers are returned.
|
|
331
|
+
|
|
332
|
+
If no transfer matches an `id`, no object is returned for that
|
|
333
|
+
transfer. So the order of transfers in the response is not necessarily
|
|
334
|
+
the same as the order of `id`s in the request. You can refer to the
|
|
335
|
+
`id` field in the response to distinguish transfers.
|
|
336
|
+
|
|
337
|
+
```js
|
|
338
|
+
// transfer 1n exists, 2n does not
|
|
339
|
+
const transfers = await client.lookupTransfers([1n, 2n]);
|
|
340
|
+
/* console.log(transfers);
|
|
341
|
+
* [{
|
|
342
|
+
* id: 1n,
|
|
343
|
+
* pending_id: 0n,
|
|
344
|
+
* debit_account_id: 1n,
|
|
345
|
+
* credit_account_id: 2n,
|
|
346
|
+
* user_data: 0n,
|
|
347
|
+
* reserved: 0n,
|
|
348
|
+
* timeout: 0n,
|
|
349
|
+
* ledger: 1,
|
|
350
|
+
* code: 720,
|
|
351
|
+
* flags: 0,
|
|
352
|
+
* amount: 10n,
|
|
353
|
+
* timestamp: 1623062009212508993n,
|
|
354
|
+
* }]
|
|
355
|
+
*/
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Linked Events
|
|
359
|
+
|
|
360
|
+
When the `linked` flag is specified for the `createAccount` or
|
|
361
|
+
`createTransfer` event, it links an event with the next event in the
|
|
362
|
+
batch, to create a chain of events, of arbitrary length, which all
|
|
363
|
+
succeed or fail together. The tail of a chain is denoted by the first
|
|
364
|
+
event without this flag. The last event in a batch may therefore never
|
|
365
|
+
have the `linked` flag set as this would leave a chain
|
|
366
|
+
open-ended. Multiple chains or individual events may coexist within a
|
|
367
|
+
batch to succeed or fail independently.
|
|
172
368
|
|
|
173
|
-
|
|
369
|
+
Events within a chain are executed within order, or are rolled back on
|
|
370
|
+
error, so that the effect of each event in the chain is visible to the
|
|
371
|
+
next, and so that the chain is either visible or invisible as a unit
|
|
372
|
+
to subsequent events after the chain. The event that was the first to
|
|
373
|
+
break the chain will have a unique error result. Other events in the
|
|
374
|
+
chain will have their error result set to `linked_event_failed`.
|
|
174
375
|
|
|
175
376
|
```js
|
|
176
377
|
let batch = []
|
|
@@ -218,7 +419,7 @@ const errors = await client.createTransfers(batch)
|
|
|
218
419
|
To get up and running when cloning the repo:
|
|
219
420
|
|
|
220
421
|
```shell
|
|
221
|
-
git clone --recurse-submodules https://github.com/
|
|
422
|
+
git clone --recurse-submodules https://github.com/tigerbeetledb/tigerbeetle-node.git
|
|
222
423
|
cd tigerbeetle-node/
|
|
223
424
|
npm install --include dev # This will automatically install and build everything you need.
|
|
224
425
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -24,26 +24,23 @@ export declare enum AccountFlags {
|
|
|
24
24
|
}
|
|
25
25
|
export declare enum CreateAccountError {
|
|
26
26
|
linked_event_failed = 1,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
exists_with_different_credits_pending = 19,
|
|
45
|
-
exists_with_different_credits_posted = 20,
|
|
46
|
-
exists = 21
|
|
27
|
+
linked_event_chain_open = 2,
|
|
28
|
+
reserved_flag = 3,
|
|
29
|
+
reserved_field = 4,
|
|
30
|
+
id_must_not_be_zero = 5,
|
|
31
|
+
id_must_not_be_int_max = 6,
|
|
32
|
+
ledger_must_not_be_zero = 7,
|
|
33
|
+
code_must_not_be_zero = 8,
|
|
34
|
+
debits_pending_must_be_zero = 9,
|
|
35
|
+
debits_posted_must_be_zero = 10,
|
|
36
|
+
credits_pending_must_be_zero = 11,
|
|
37
|
+
credits_posted_must_be_zero = 12,
|
|
38
|
+
mutually_exclusive_flags = 13,
|
|
39
|
+
exists_with_different_flags = 14,
|
|
40
|
+
exists_with_different_user_data = 15,
|
|
41
|
+
exists_with_different_ledger = 16,
|
|
42
|
+
exists_with_different_code = 17,
|
|
43
|
+
exists = 18
|
|
47
44
|
}
|
|
48
45
|
export declare type CreateAccountsError = {
|
|
49
46
|
index: number;
|
|
@@ -71,58 +68,59 @@ export declare enum TransferFlags {
|
|
|
71
68
|
}
|
|
72
69
|
export declare enum CreateTransferError {
|
|
73
70
|
linked_event_failed = 1,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
71
|
+
linked_event_chain_open = 2,
|
|
72
|
+
reserved_flag = 3,
|
|
73
|
+
reserved_field = 4,
|
|
74
|
+
id_must_not_be_zero = 5,
|
|
75
|
+
id_must_not_be_int_max = 6,
|
|
76
|
+
debit_account_id_must_not_be_zero = 7,
|
|
77
|
+
debit_account_id_must_not_be_int_max = 8,
|
|
78
|
+
credit_account_id_must_not_be_zero = 9,
|
|
79
|
+
credit_account_id_must_not_be_int_max = 10,
|
|
80
|
+
accounts_must_be_different = 11,
|
|
81
|
+
pending_id_must_be_zero = 12,
|
|
82
|
+
pending_transfer_must_timeout = 13,
|
|
83
|
+
ledger_must_not_be_zero = 14,
|
|
84
|
+
code_must_not_be_zero = 15,
|
|
85
|
+
amount_must_not_be_zero = 16,
|
|
86
|
+
debit_account_not_found = 17,
|
|
87
|
+
credit_account_not_found = 18,
|
|
88
|
+
accounts_must_have_the_same_ledger = 19,
|
|
89
|
+
transfer_must_have_the_same_ledger_as_accounts = 20,
|
|
90
|
+
exists_with_different_flags = 21,
|
|
91
|
+
exists_with_different_debit_account_id = 22,
|
|
92
|
+
exists_with_different_credit_account_id = 23,
|
|
93
|
+
exists_with_different_user_data = 24,
|
|
94
|
+
exists_with_different_pending_id = 25,
|
|
95
|
+
exists_with_different_timeout = 26,
|
|
96
|
+
exists_with_different_code = 27,
|
|
97
|
+
exists_with_different_amount = 28,
|
|
98
|
+
exists = 29,
|
|
99
|
+
overflows_debits_pending = 30,
|
|
100
|
+
overflows_credits_pending = 31,
|
|
101
|
+
overflows_debits_posted = 32,
|
|
102
|
+
overflows_credits_posted = 33,
|
|
103
|
+
overflows_debits = 34,
|
|
104
|
+
overflows_credits = 35,
|
|
105
|
+
exceeds_credits = 36,
|
|
106
|
+
exceeds_debits = 37,
|
|
107
|
+
cannot_post_and_void_pending_transfer = 38,
|
|
108
|
+
pending_transfer_cannot_post_or_void_another = 39,
|
|
109
|
+
timeout_reserved_for_pending_transfer = 40,
|
|
110
|
+
pending_id_must_not_be_zero = 41,
|
|
111
|
+
pending_id_must_not_be_int_max = 42,
|
|
112
|
+
pending_id_must_be_different = 43,
|
|
113
|
+
pending_transfer_not_found = 44,
|
|
114
|
+
pending_transfer_not_pending = 45,
|
|
115
|
+
pending_transfer_has_different_debit_account_id = 46,
|
|
116
|
+
pending_transfer_has_different_credit_account_id = 47,
|
|
117
|
+
pending_transfer_has_different_ledger = 48,
|
|
118
|
+
pending_transfer_has_different_code = 49,
|
|
119
|
+
exceeds_pending_transfer_amount = 50,
|
|
120
|
+
pending_transfer_has_different_amount = 51,
|
|
121
|
+
pending_transfer_already_posted = 52,
|
|
122
|
+
pending_transfer_already_voided = 53,
|
|
123
|
+
pending_transfer_expired = 54
|
|
126
124
|
}
|
|
127
125
|
export declare type CreateTransfersError = {
|
|
128
126
|
index: number;
|