psibase 0.23.0

Library and command-line tool for interacting with psibase networks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#![allow(non_snake_case)]

use crate::{
    AccountNumber, Hex, MethodNumber, Pack, TimePointSec, TimePointUSec, ToKey, ToSchema, Unpack,
    WasmConfigRow,
};
use async_graphql::{InputObject, SimpleObject, Union};
use serde::{Deserialize, Serialize};

// TODO: move
pub type Checksum256 = Hex<[u8; 32]>;

pub type BlockNum = u32;
pub type BlockTime = TimePointUSec;

/// A synchronous call
///
/// An Action represents a synchronous call between services.
/// It is the argument to [crate::native_raw::call] and can be fetched
/// using [crate::native_raw::getCurrentAction].
///
/// Transactions also contains actions requested by the
/// transaction authorizers.
#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
    PartialEq,
    Eq,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "ActionInput")]
pub struct Action {
    /// Account sending the action
    pub sender: AccountNumber,

    /// Service to execute the action
    pub service: AccountNumber,

    /// Service method to execute
    pub method: MethodNumber,

    /// Data for the method
    pub rawData: Hex<Vec<u8>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToKey, ToSchema, Serialize)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct SharedAction<'a> {
    pub sender: AccountNumber,
    pub service: AccountNumber,
    pub method: MethodNumber,
    pub rawData: Hex<&'a [u8]>,
}

/// The genesis action is the first action of the first transaction of
/// the first block. The action struct's fields are ignored, except
/// rawData, which contains this struct.
#[derive(Debug, Clone, Default, Pack, Unpack, ToKey, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct GenesisActionData {
    pub memo: String,
    pub services: Vec<GenesisService>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToKey, ToSchema, Serialize)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct SharedGenesisActionData<'a> {
    pub memo: String,
    #[serde(borrow)]
    pub services: Vec<SharedGenesisService<'a>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToKey, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct GenesisService {
    pub service: AccountNumber,
    pub flags: u64,
    pub vmType: u8,
    pub vmVersion: u8,
    pub code: Hex<Vec<u8>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToKey, ToSchema, Serialize)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct SharedGenesisService<'a> {
    pub service: AccountNumber,
    pub flags: u64,
    pub vmType: u8,
    pub vmVersion: u8,
    pub code: Hex<&'a [u8]>,
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
    PartialEq,
    Eq,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "ClaimInput")]
pub struct Claim {
    pub service: AccountNumber,
    pub rawData: Hex<Vec<u8>>,
}

/// Rules for TAPOS:
/// * Reference block's number must be either:
///    * One of the most-recent 128 blocks. For this case, refBlockIndex = blockNum & 0x7f
///    * A multiple of 8192. For this case, refBlockIndex = (blockNum >> 13) | 0x80
/// * refBlockSuffix = last 4 bytes of the block ID, bit-casted to u32     .
///
/// Transact maintains block suffixes for:
/// * The most-recent 128 blocks. This allows transactions to depend on other recent transactions.
/// * The most-recent 128 blocks which have block numbers which are a multiple of 8192. This gives
///   users which sign transactions offline plenty of time to do so.
///
/// If the blocks are all 1 second apart, then the second case allows up to 12 days to sign and execute
/// a transaction. If the blocks have variable length, then the amount of available time will vary with
/// network activity. If we assume a max sustained block rate of 4 per sec, then this still allows 3 days.
///
/// A transaction will be rejected if:
/// * It is expired.
/// * It arrives earlier than (expired - maxTrxLifetime). maxTrxLifetime
///   is defined in Transact.cpp and may be changed in the future.
/// * It references a block that isn't on the current fork, or a block which
///   is too old. For best results, use the most-recent irreversible block which
///   meets the criteria.
#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
    PartialEq,
    Eq,
)]
#[fracpack(definition_will_not_change, fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "TaposInput")]
pub struct Tapos {
    pub expiration: TimePointSec,
    pub refBlockSuffix: u32,
    pub flags: u16,
    pub refBlockIndex: u8,
}

impl Tapos {
    pub const DO_NOT_BROADCAST_FLAG: u16 = 1 << 0;
    pub const VALID_FLAGS: u16 = 0x0001;
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
    PartialEq,
    Eq,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "TransactionInput")]
pub struct Transaction {
    pub tapos: Tapos,
    pub actions: Vec<Action>,
    pub claims: Vec<Claim>,
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
    PartialEq,
    Eq,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "SignedTransactionInput")]
pub struct SignedTransaction {
    // Contains a packed `Transaction`. TODO: shared_view_ptr
    pub transaction: Hex<Vec<u8>>,
    pub proofs: Vec<Hex<Vec<u8>>>,
}

type TermNum = u32;

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
#[graphql(input_name = "ProducerInput")]
pub struct Producer {
    pub name: AccountNumber,
    pub auth: Claim,
}

#[derive(
    Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize, SimpleObject, InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct CftConsensus {
    pub producers: Vec<Producer>,
}

#[derive(
    Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize, SimpleObject, InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct BftConsensus {
    pub producers: Vec<Producer>,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize, Union)]
#[fracpack(fracpack_mod = "fracpack")]
pub enum ConsensusData {
    CFT(CftConsensus),
    BFT(BftConsensus),
}

impl ConsensusData {
    pub fn producers(&self) -> &Vec<Producer> {
        match self {
            ConsensusData::CFT(cft) => &cft.producers,
            ConsensusData::BFT(bft) => &bft.producers,
        }
    }
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[fracpack(definition_will_not_change)]
#[to_key(psibase_mod = "crate")]
pub struct ConsensusBytes {
    pub consensusVariantIdx: u8,
    pub consensusData: Vec<u8>,
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct BlockHeaderAuthAccount {
    pub codeNum: AccountNumber,
    pub codeHash: Checksum256,
    pub vmType: u8,
    pub vmVersion: u8,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct Consensus {
    pub data: ConsensusData,
    pub services: Vec<BlockHeaderAuthAccount>,
    pub wasmConfig: WasmConfigRow,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct PendingConsensus {
    pub consensus: Consensus,
    pub blockNum: BlockNum,
}

#[derive(Debug, Clone, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct JointConsensus {
    pub current: Consensus,
    pub next: Option<PendingConsensus>,
}

#[derive(
    Debug,
    Clone,
    Default,
    Pack,
    Unpack,
    ToKey,
    ToSchema,
    Serialize,
    Deserialize,
    SimpleObject,
    InputObject,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
pub struct BlockHeaderCode {
    pub vmType: u8,
    pub vmVersion: u8,
    pub code: Vec<u8>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct BlockHeader {
    pub previous: Checksum256,
    pub blockNum: BlockNum,
    pub time: BlockTime,
    pub producer: AccountNumber,
    pub term: TermNum,
    pub commitNum: BlockNum,

    // Holds a sha256 of the current JointConsensus
    consensusState: Checksum256,

    // Holds a merkle root of the transactions in the block.
    // This does not depend on execution, so that it can be
    // verified early. The leaves of the tree have type
    // TransactionInfo.
    trxMerkleRoot: Checksum256,

    // The merkle root of events generated while processing the block.
    // The leaves have type EventInfo.
    eventMerkleRoot: Checksum256,

    // If newConsensus is set, activates joint consensus on
    // this block. Joint consensus must not be active already.
    // Joint consensus ends after this block becomes irreversible.
    newConsensus: Option<Consensus>,

    // This contains the code for authServices
    // It MUST contain all code that was added in this block
    // It MUST NOT contain code that is not in authServices
    // It MAY contain code that is unchanged from the previous block
    // authCode MUST NOT be included when calculating a block hash.
    authCode: Option<Vec<BlockHeaderCode>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct Block {
    pub header: BlockHeader,
    pub transactions: Vec<SignedTransaction>,
    pub subjectiveData: Vec<Hex<Vec<u8>>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct SignedBlock {
    pub block: Block,
    pub signature: Hex<Vec<u8>>,
}

#[derive(Debug, Clone, Default, Pack, Unpack, ToSchema, Serialize, Deserialize)]
#[fracpack(fracpack_mod = "fracpack")]
pub struct BlockInfo {
    pub header: BlockHeader,
    pub blockId: Checksum256,
}