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
use async_trait::async_trait;
use cnidarium::StateWrite;
use filament_chain::genesis::AppState;
use tendermint::abci::request;

use crate::component::ABCIComponent;

mod query;
mod state;

pub use query::Query;
pub use state::{AccountsRead, AccountsWrite};

pub struct Accounts {}

#[async_trait]
impl ABCIComponent for Accounts {
    async fn init_chain<S: StateWrite>(&self, state: &mut S, app_state: &AppState) {
        for allocation in &app_state.allocations {
            // FIXME(xla): ABCI does not allow for errors to be returned during chain
            // initialisation. Which leaves aborting the program as only alternative for now.
            state
                .create_account(allocation.address.clone())
                .await
                .unwrap();
        }
    }

    async fn begin_block<S: StateWrite>(&self, _state: &mut S, _begin_block: &request::BeginBlock) {
    }

    async fn end_block<S: StateWrite>(&self, _state: &mut S, _end_block: &request::EndBlock) {}
}

#[cfg(test)]
mod test {
    use cnidarium::{StateDelta, Storage};
    use filament_chain::{
        genesis::{Allocation, AppState},
        Address,
        Amount,
        ChainId,
        ChainParameters,
    };
    use filament_crypto::SigningKey;
    use rand::{thread_rng, Rng as _};
    use tempfile::tempdir;

    use super::Accounts;
    use crate::component::{accounts::AccountsRead, ABCIComponent as _};

    #[tokio::test]
    async fn init_chain_allocations() -> eyre::Result<()> {
        let dir = tempdir()?;
        let path = dir.into_path();
        let storage = Storage::load(path.clone(), vec![])
            .await
            .map_err(|e| eyre::eyre!(e))?;

        let amount = thread_rng().gen_range(0..16);
        let addresses: Vec<Address> = (0..amount)
            .map(|_| {
                let sk = SigningKey::new(thread_rng());
                Address::from(sk.verification_key())
            })
            .collect();
        let allocations = addresses
            .iter()
            .map(|address| Allocation {
                address: address.clone(),
                denom: "ufilament".to_string(),
                amount: Amount::from(1000),
            })
            .collect();

        // Run init chain on the Accounts component with a populated set of allocations.
        {
            let mut state = StateDelta::new(storage.latest_snapshot());

            let app_state = AppState {
                allocations,
                chain_parameters: ChainParameters {
                    chain_id: ChainId::try_from("inprocess-testnet".to_string())?,
                    epoch_duration: 1024,
                },
            };

            let accounts = Accounts {};
            accounts.init_chain(&mut state, &app_state).await;

            storage.commit(state).await.map_err(|e| eyre::eyre!(e))?;
        }

        // Assert that for every address in the genesis allocation there is an account in the
        // state.
        let state = StateDelta::new(storage.latest_snapshot());

        for addr in &addresses {
            assert!(state.account(addr).await?.is_some());
        }

        Ok(())
    }
}