Skip to main content

amadeus_runtime/consensus/
consensus_apply.rs

1// Consensus application environment and entry processing
2use crate::Result;
3use amadeus_utils::rocksdb::{BoundColumnFamily, MultiThreaded, RocksDb, Transaction, TransactionDB};
4use amadeus_utils::{Hash, PublicKey, Signature};
5use std::collections::HashMap;
6use std::sync::Arc;
7
8use crate::consensus::consensus_muts;
9
10pub struct ApplyEnv<'db> {
11    pub caller_env: CallerEnv,
12    pub cf: Arc<BoundColumnFamily<'db>>,
13    pub txn: Transaction<'db, TransactionDB<MultiThreaded>>,
14    pub muts_final: Vec<consensus_muts::Mutation>,
15    pub muts_final_rev: Vec<consensus_muts::Mutation>,
16    pub muts: Vec<consensus_muts::Mutation>,
17    pub muts_gas: Vec<consensus_muts::Mutation>,
18    pub muts_rev: Vec<consensus_muts::Mutation>,
19    pub muts_rev_gas: Vec<consensus_muts::Mutation>,
20    pub result_log: Vec<HashMap<&'static str, &'static str>>,
21}
22
23pub struct CallerEnv {
24    pub readonly: bool,
25    pub seed: Vec<u8>,
26    pub seedf64: f64,
27    pub entry_signer: PublicKey,
28    pub entry_prev_hash: Hash,
29    pub entry_slot: u64,
30    pub entry_prev_slot: u64,
31    pub entry_height: u64,
32    pub entry_epoch: u64,
33    pub entry_vr: Vec<u8>,
34    pub entry_vr_b3: Hash,
35    pub entry_dr: Hash,
36    pub tx_hash: Vec<u8>,
37    pub tx_signer: PublicKey,
38    pub account_origin: Vec<u8>,
39    pub account_caller: Vec<u8>,
40    pub account_current: Vec<u8>,
41    pub attached_symbol: Vec<u8>,
42    pub attached_amount: Vec<u8>,
43    pub call_counter: u32,
44    pub call_exec_points: u64,
45    pub call_exec_points_remaining: u64,
46}
47
48pub fn make_caller_env(
49    entry_signer: &PublicKey,
50    entry_prev_hash: &Hash,
51    entry_slot: u64,
52    entry_prev_slot: u64,
53    entry_height: u64,
54    entry_epoch: u64,
55    entry_vr: &Signature,
56    entry_vr_b3: &Hash,
57    entry_dr: &Hash,
58) -> CallerEnv {
59    CallerEnv {
60        readonly: false,
61        seed: entry_dr.to_vec(),
62        seedf64: 0.5,
63        entry_signer: *entry_signer,
64        entry_prev_hash: *entry_prev_hash,
65        entry_slot,
66        entry_prev_slot,
67        entry_height,
68        entry_epoch,
69        entry_vr: entry_vr.to_vec(),
70        entry_vr_b3: *entry_vr_b3,
71        entry_dr: *entry_dr,
72        tx_hash: vec![],
73        tx_signer: PublicKey::new([0u8; 48]),
74        account_origin: vec![],
75        account_caller: vec![],
76        account_current: vec![],
77        attached_symbol: vec![],
78        attached_amount: vec![],
79        call_counter: 0,
80        call_exec_points: 10_000_000,
81        call_exec_points_remaining: 10_000_000,
82    }
83}
84
85pub fn make_apply_env<'db>(
86    db: &'db RocksDb,
87    cf_name: &str,
88    entry_signer: &PublicKey,
89    entry_prev_hash: &Hash,
90    entry_slot: u64,
91    entry_prev_slot: u64,
92    entry_height: u64,
93    entry_epoch: u64,
94    entry_vr: &Signature,
95    entry_vr_b3: &Hash,
96    entry_dr: &Hash,
97) -> Result<ApplyEnv<'db>> {
98    Ok(ApplyEnv {
99        caller_env: make_caller_env(
100            entry_signer,
101            entry_prev_hash,
102            entry_slot,
103            entry_prev_slot,
104            entry_height,
105            entry_epoch,
106            entry_vr,
107            entry_vr_b3,
108            entry_dr,
109        ),
110        cf: db.inner.cf_handle(cf_name).ok_or("cf_handle_failed")?,
111        txn: db.begin_transaction(),
112        muts_final: Vec::new(),
113        muts_final_rev: Vec::new(),
114        muts: Vec::new(),
115        muts_gas: Vec::new(),
116        muts_rev: Vec::new(),
117        muts_rev_gas: Vec::new(),
118        result_log: Vec::new(),
119    })
120}
121
122pub fn valid_bic_action(contract: Vec<u8>, function: Vec<u8>) -> bool {
123    let c = contract.as_slice();
124    let f = function.as_slice();
125
126    (c == b"Epoch" || c == b"Coin" || c == b"Contract")
127        && (f == b"submit_sol"
128            || f == b"transfer"
129            || f == b"set_emission_address"
130            || f == b"slash_trainer"
131            || f == b"deploy"
132            || f == b"create_and_mint"
133            || f == b"mint"
134            || f == b"pause")
135}