casper-node 2.0.3

The Casper blockchain node
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use async_trait::async_trait;
use casper_storage::{
    data_access_layer::{
        balance::BalanceHandling,
        tagged_values::{TaggedValuesRequest, TaggedValuesResult, TaggedValuesSelection},
        BalanceRequest, BalanceResult, ProofHandling, TotalSupplyRequest, TotalSupplyResult,
    },
    global_state::state::StateProvider,
};
use casper_types::{
    account::AccountHash, bytesrepr::Bytes, testing::TestRng, EraId, ExecutionInfo, FeeHandling,
    KeyTag, PricingHandling, PricingMode, PublicKey, RefundHandling, SecretKey, TimeDiff,
    Transaction, TransactionHash, TransactionRuntimeParams, U512,
};
use once_cell::sync::OnceCell;
use std::{collections::BTreeMap, sync::Arc, time::Duration};

use crate::{
    reactor::main_reactor::tests::{
        configs_override::ConfigsOverride,
        fixture::TestFixture,
        transactions::{
            BalanceAmount, ALICE_PUBLIC_KEY, ALICE_SECRET_KEY, BOB_PUBLIC_KEY, BOB_SECRET_KEY,
        },
        ERA_ONE, ONE_MIN, TEN_SECS,
    },
    types::transaction::transaction_v1_builder::TransactionV1Builder,
};

pub(crate) struct TestStateSnapshot {
    pub(crate) exec_infos: BTreeMap<TransactionHash, ExecutionInfo>,
    pub(crate) balances: BTreeMap<AccountHash, BalanceAmount>,
    pub(crate) total_supply: U512,
}

/// This defines the condition
/// a network should achieve after setup and start
/// before we can proceed with transaction injection
#[derive(Clone, Debug)]
pub(crate) enum RunUntilCondition {
    /// Runs the network until all nodes reach the given completed block height.
    BlockHeight { block_height: u64, within: Duration },
    /// Runs the network until all nodes' consensus components reach the given era.
    ConsensusInEra { era_id: EraId, within: Duration },
}

impl RunUntilCondition {
    async fn run_until(&self, fixture: &mut TestFixture) -> Result<(), TestScenarioError> {
        match self {
            RunUntilCondition::BlockHeight {
                block_height,
                within,
            } => {
                fixture
                    .try_run_until_block_height(*block_height, *within)
                    .await
            }
            RunUntilCondition::ConsensusInEra { era_id, within } => {
                fixture.try_until_consensus_in_era(*era_id, *within).await
            }
        }
        .map_err(|_| TestScenarioError::NetworkDidNotStabilize)
    }
}

#[derive(Debug)]
pub(crate) enum TestScenarioError {
    UnexpectedState,
    NetworkDidNotStabilize,
    CannotSetBeforeState,
}

struct ScenarioDataInstance {
    fixture: TestFixture,
    block_height: u64,
}

impl ScenarioDataInstance {
    pub(crate) async fn inject_transaction(&mut self, txn: Transaction) {
        self.fixture.inject_transaction(txn).await
    }

    pub(crate) async fn run_until_executed_transaction(
        &mut self,
        txn_hash: &TransactionHash,
        within: Duration,
    ) {
        self.fixture
            .run_until_executed_transaction(txn_hash, within)
            .await
    }
}

#[async_trait]
pub(crate) trait Assertion: Send + Sync {
    async fn assert(&self, snapshots_at_heights: BTreeMap<u64, TestStateSnapshot>);
}

#[derive(Debug, Clone, Eq, PartialEq)]
enum TestScenarioState {
    PreSetup,
    PreRun,
    Running,
}

pub(crate) struct TestScenario {
    state: TestScenarioState,
    data: ScenarioDataInstance,
    initial_run_until: RunUntilCondition,
    exec_infos: BTreeMap<TransactionHash, ExecutionInfo>,
    state_before_test: OnceCell<TestStateSnapshot>,
}

impl TestScenario {
    pub(crate) async fn setup(&mut self) -> Result<(), TestScenarioError> {
        if self.state != TestScenarioState::PreSetup {
            return Err(TestScenarioError::UnexpectedState);
        }
        self.run_until(self.initial_run_until.clone()).await?;
        self.state_before_test
            .set(self.get_current_state().await)
            .map_err(|_| TestScenarioError::CannotSetBeforeState)?;
        self.state = TestScenarioState::PreRun;
        Ok(())
    }

    pub(crate) async fn run(
        &mut self,
        to_inject: Vec<Transaction>,
    ) -> Result<Vec<ExecutionInfo>, TestScenarioError> {
        if self.state == TestScenarioState::PreSetup {
            return Err(TestScenarioError::UnexpectedState);
        }
        let mut to_ret = vec![];
        for transaction in &to_inject {
            let hash = transaction.hash();
            self.data.inject_transaction(transaction.clone()).await;
            self.data
                .run_until_executed_transaction(&hash, TEN_SECS)
                .await;
            let (_node_id, runner) = self.data.fixture.network.nodes().iter().next().unwrap();
            let exec_info = runner
                .main_reactor()
                .storage()
                .read_execution_info(hash)
                .expect("Expected transaction to be included in a block.");
            let transaction_block_height = exec_info.block_height;
            if transaction_block_height > self.data.block_height {
                self.data.block_height = transaction_block_height;
            }
            to_ret.push(exec_info.clone());
            self.exec_infos.insert(hash, exec_info);
        }
        self.state = TestScenarioState::Running;
        Ok(to_ret)
    }

    pub(crate) async fn run_until(
        &mut self,
        run_until: RunUntilCondition,
    ) -> Result<(), TestScenarioError> {
        run_until.run_until(&mut self.data.fixture).await
    }

    pub(crate) fn chain_name(&self) -> String {
        self.data.fixture.chainspec.network_config.name.clone()
    }

    pub(crate) async fn assert<T: Assertion>(&mut self, assertion: T) {
        if self.state_before_test.get().is_none() {
            panic!("TestScenario not in state eligible to do assertions");
        }
        let max_block_height = self.data.fixture.highest_complete_block().height();
        let mut snapshots = BTreeMap::new();
        for i in 0..=max_block_height {
            snapshots.insert(i, self.get_state_at_height(i).await);
        }
        assertion.assert(snapshots).await
    }

    async fn get_state_at_height(&self, block_height: u64) -> TestStateSnapshot {
        let all_accounts = self.get_all_accounts(block_height).await;
        let mut balances = BTreeMap::new();
        for account_hash in all_accounts {
            let balance_amount = self.get_balance_amount(account_hash, block_height).await;
            balances.insert(account_hash, balance_amount);
        }

        let total_supply = self.get_total_supply(block_height).await;
        let exec_infos: BTreeMap<TransactionHash, ExecutionInfo> = self
            .exec_infos
            .iter()
            .filter_map(|(k, v)| {
                if v.block_height <= block_height {
                    Some((*k, v.clone()))
                } else {
                    None
                }
            })
            .collect();

        TestStateSnapshot {
            exec_infos,
            balances,
            total_supply,
        }
    }

    async fn get_current_state(&self) -> TestStateSnapshot {
        let block = self.data.fixture.highest_complete_block();
        let block_height = block.height();
        self.get_state_at_height(block_height).await
    }

    async fn get_all_accounts(&self, block_height: u64) -> Vec<AccountHash> {
        let fixture = &self.data.fixture;
        let (_node_id, runner) = fixture.network.nodes().iter().next().unwrap();
        let block_header = runner
            .main_reactor()
            .storage()
            .read_block_header_by_height(block_height, true)
            .expect("failure to read block header")
            .unwrap();
        let state_hash = *block_header.state_root_hash();
        let request =
            TaggedValuesRequest::new(state_hash, TaggedValuesSelection::All(KeyTag::Account));
        match runner
            .main_reactor()
            .contract_runtime()
            .data_access_layer()
            .tagged_values(request)
        {
            TaggedValuesResult::Success { values, .. } => values
                .iter()
                .filter_map(|el| el.as_account().map(|el| el.account_hash()))
                .collect(),
            _ => panic!("Couldn't get all account hashes"),
        }
    }

    pub(crate) fn get_balance(
        &self,
        account_hash: AccountHash,
        block_height: Option<u64>,
        get_total: bool,
    ) -> BalanceResult {
        let fixture = &self.data.fixture;
        let (_node_id, runner) = fixture.network.nodes().iter().next().unwrap();
        let protocol_version = fixture.chainspec.protocol_version();
        let block_height = block_height.unwrap_or(
            runner
                .main_reactor()
                .storage()
                .highest_complete_block_height()
                .expect("missing highest completed block"),
        );
        let block_header = runner
            .main_reactor()
            .storage()
            .read_block_header_by_height(block_height, true)
            .expect("failure to read block header")
            .unwrap();
        let state_hash = *block_header.state_root_hash();
        let balance_handling = if get_total {
            BalanceHandling::Total
        } else {
            BalanceHandling::Available
        };
        runner
            .main_reactor()
            .contract_runtime()
            .data_access_layer()
            .balance(BalanceRequest::from_account_hash(
                state_hash,
                protocol_version,
                account_hash,
                balance_handling,
                ProofHandling::NoProofs,
            ))
    }

    async fn get_balance_amount(
        &self,
        account_hash: AccountHash,
        block_height: u64,
    ) -> BalanceAmount {
        let block_height = Some(block_height);

        let total = self
            .get_balance(account_hash, block_height, true)
            .total_balance()
            .copied()
            .unwrap_or(U512::zero());
        let available = self
            .get_balance(account_hash, block_height, false)
            .available_balance()
            .copied()
            .unwrap_or(U512::zero());
        BalanceAmount { available, total }
    }

    async fn get_total_supply(&self, block_height: u64) -> U512 {
        let fixture = &self.data.fixture;
        let (_node_id, runner) = fixture.network.nodes().iter().next().unwrap();
        let protocol_version = fixture.chainspec.protocol_version();
        let state_hash = *runner
            .main_reactor()
            .storage()
            .read_block_header_by_height(block_height, true)
            .expect("failure to read block header")
            .unwrap()
            .state_root_hash();

        let total_supply_req = TotalSupplyRequest::new(state_hash, protocol_version);
        let result = runner
            .main_reactor()
            .contract_runtime()
            .data_access_layer()
            .total_supply(total_supply_req);

        if let TotalSupplyResult::Success { total_supply } = result {
            total_supply
        } else {
            panic!("Can't get total supply")
        }
    }

    pub(crate) fn mint_const_transfer_cost(&self) -> u32 {
        self.data
            .fixture
            .chainspec
            .system_costs_config
            .mint_costs()
            .transfer
    }

    pub(crate) fn native_transfer_minimum_motes(&self) -> u64 {
        self.data
            .fixture
            .chainspec
            .transaction_config
            .native_transfer_minimum_motes
    }

    pub(crate) fn get_gas_limit_for_lane(&self, lane_id: u8) -> Option<u64> {
        self.data
            .fixture
            .chainspec
            .transaction_config
            .transaction_v1_config
            .get_lane_by_id(lane_id)
            .map(|el| el.max_transaction_gas_limit)
    }

    pub(crate) fn get_block_height(&self) -> u64 {
        self.data.block_height
    }
}

type StakesType = Option<(Vec<Arc<SecretKey>>, BTreeMap<PublicKey, U512>)>;

#[derive(Default)]
pub(crate) struct TestScenarioBuilder {
    maybe_stakes_setup: StakesType,
    maybe_pricing_handling: Option<PricingHandling>,
    maybe_initial_run_until: Option<RunUntilCondition>,
    maybe_refund_handling: Option<RefundHandling>,
    maybe_fee_handling: Option<FeeHandling>,
    maybe_balance_hold_interval_override: Option<TimeDiff>,
    maybe_minimum_era_height: Option<u64>,
}

impl TestScenarioBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub async fn build(self, rng: &mut TestRng) -> TestScenario {
        let TestScenarioBuilder {
            maybe_stakes_setup,
            maybe_pricing_handling,
            maybe_initial_run_until,
            maybe_refund_handling,
            maybe_fee_handling,
            maybe_balance_hold_interval_override,
            maybe_minimum_era_height,
        } = self;
        let (secret_keys, stakes) = maybe_stakes_setup.unwrap_or({
            let stakes: BTreeMap<PublicKey, U512> = vec![
                (ALICE_PUBLIC_KEY.clone(), U512::from(u128::MAX)), /* Node 0 is effectively
                                                                    * guaranteed to be the
                                                                    * proposer. */
                (BOB_PUBLIC_KEY.clone(), U512::from(1)),
            ]
            .into_iter()
            .collect();
            let secret_keys = vec![ALICE_SECRET_KEY.clone(), BOB_SECRET_KEY.clone()];
            (secret_keys, stakes)
        });

        let pricing_handling = maybe_pricing_handling.unwrap_or(PricingHandling::Fixed);
        let initial_run_until =
            maybe_initial_run_until.unwrap_or(RunUntilCondition::ConsensusInEra {
                era_id: ERA_ONE,
                within: ONE_MIN,
            });
        let config = ConfigsOverride::default().with_pricing_handling(pricing_handling);
        let config = if let Some(refund_handling) = maybe_refund_handling {
            config.with_refund_handling(refund_handling)
        } else {
            config
        };
        let config = if let Some(fee_handling) = maybe_fee_handling {
            config.with_fee_handling(fee_handling)
        } else {
            config
        };
        let config =
            if let Some(balance_hold_interval_override) = maybe_balance_hold_interval_override {
                config.with_balance_hold_interval(balance_hold_interval_override)
            } else {
                config
            };
        let config = if let Some(minimum_era_height) = maybe_minimum_era_height {
            config.with_minimum_era_height(minimum_era_height)
        } else {
            config
        };
        let child_rng = rng.create_child();
        let fixture =
            TestFixture::new_with_keys(child_rng, secret_keys, stakes, Some(config)).await;
        let data = ScenarioDataInstance {
            fixture,
            block_height: 0_u64,
        };

        TestScenario {
            state: TestScenarioState::PreSetup,
            data,
            initial_run_until,
            exec_infos: BTreeMap::new(),
            state_before_test: OnceCell::new(),
        }
    }

    /// Sets refund handling config option.
    pub fn with_refund_handling(mut self, refund_handling: RefundHandling) -> Self {
        self.maybe_refund_handling = Some(refund_handling);
        self
    }

    pub(crate) fn with_fee_handling(mut self, fee_handling: FeeHandling) -> Self {
        self.maybe_fee_handling = Some(fee_handling);
        self
    }

    pub(crate) fn with_balance_hold_interval(mut self, balance_hold_interval: TimeDiff) -> Self {
        self.maybe_balance_hold_interval_override = Some(balance_hold_interval);
        self
    }

    pub(crate) fn with_minimum_era_height(mut self, minimum_era_height: u64) -> Self {
        self.maybe_minimum_era_height = Some(minimum_era_height);
        self
    }
}

pub(super) fn build_wasm_transction(
    chain_name: String,
    from: &SecretKey,
    pricing: PricingMode,
) -> Transaction {
    //These bytes are intentionally so large - this way they fall into "WASM_LARGE" category in the
    // local chainspec Alternatively we could change the chainspec to have a different limits
    // for the wasm categories, but that would require aligning all tests that use local
    // chainspec
    let module_bytes = Bytes::from(vec![1; 172_033]);
    Transaction::from(
        TransactionV1Builder::new_session(
            false,
            module_bytes,
            TransactionRuntimeParams::VmCasperV1,
        )
        .with_chain_name(chain_name)
        .with_pricing_mode(pricing)
        .with_initiator_addr(PublicKey::from(from))
        .build()
        .unwrap(),
    )
}