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
use blockchain_traits::TransactionOutcome;
use oasis_types::{AccountMeta, Address, Event};

use crate::{output::Receipt, pending_transaction::PendingTransaction, State};

#[derive(Debug, PartialEq, Eq)]
pub struct Block<'bc> {
    pub base_gas: u64,
    pub height: u64,
    pub state: State<'bc>,
    pub completed_transactions: Vec<Receipt>,
}

impl<'bc> Block<'bc> {
    pub fn new(height: u64, state: State<'bc>, base_gas: u64) -> Self {
        Self {
            height,
            state,
            completed_transactions: Vec::new(),
            base_gas,
        }
    }
}

impl<'bc> blockchain_traits::Block for Block<'bc> {
    fn height(&self) -> u64 {
        self.height
    }

    fn transact(
        &mut self,
        caller: Address,
        callee: Address,
        payer: Address,
        value: u128,
        input: &[u8],
        gas: u64,
        gas_price: u64,
    ) -> Box<dyn blockchain_traits::Receipt> {
        let mut receipt = Receipt {
            caller,
            callee,
            value,
            gas_used: gas,
            output: Vec::new(),
            events: Vec::new(),
            outcome: TransactionOutcome::Success,
        };

        macro_rules! early_return {
            ($outcome:ident) => {{
                receipt.outcome = TransactionOutcome::$outcome;
                self.completed_transactions.push(receipt.clone());
                return box receipt;
            }};
        }

        if !self.state.contains_key(&callee) {
            early_return!(InvalidCallee);
        }

        if gas < self.base_gas {
            early_return!(InsufficientGas);
        }

        match self.state.get_mut(&payer) {
            Some(payer_acct) => {
                let payer_acct = payer_acct.to_mut();
                let gas_cost = gas * gas_price;
                if payer_acct.balance < u128::from(gas_cost) {
                    payer_acct.balance = 0;
                    early_return!(InsufficientFunds);
                }
                payer_acct.balance -= u128::from(gas_cost);
            }
            None => early_return!(InvalidCallee),
        };

        let mut ptx_state = self.state.clone();

        match ptx_state.get_mut(&caller) {
            Some(caller_acct) => {
                let caller_acct = caller_acct.to_mut();
                if caller_acct.balance < value {
                    early_return!(InsufficientFunds);
                }
                caller_acct.balance -= value;
            }
            None => early_return!(InvalidCallee),
        };

        ptx_state.get_mut(&callee).unwrap().to_mut().balance += value;

        let mut pending_transaction = PendingTransaction {
            caller,
            callee,
            value,
            input: input.to_vec(),
            outcome: TransactionOutcome::Success,
            state: ptx_state,
            events: Vec::new(),
            output: Vec::new(),
            base_gas: self.base_gas,
            gas_left: gas - self.base_gas,
        };

        if let Some(main) = self.state.get(&callee).unwrap().main {
            let ptx: &mut dyn blockchain_traits::PendingTransaction = &mut pending_transaction;
            let errno = main(unsafe {
                // Extend the lifetime, as required by the FFI type.
                // This is only unsafe if the `main` fn stores the pointer,
                // but this is disallowed by the precondition on `main`.
                &(std::mem::transmute::<&mut _, &'static mut _>(ptx) as *mut _) as *const _
            });
            if errno != 0 {
                pending_transaction.outcome = TransactionOutcome::Aborted;
            }
        }

        receipt.outcome = pending_transaction.outcome;
        receipt.output = pending_transaction.output;
        if blockchain_traits::Receipt::reverted(&receipt) {
            receipt.events.clear();
        } else {
            self.state = pending_transaction.state;
            receipt.events.append(&mut pending_transaction.events);
        }
        self.completed_transactions.push(receipt.clone());
        box receipt
    }

    fn code_at(&self, addr: &Address) -> Option<&[u8]> {
        self.state.get(addr).map(|acct| acct.code.as_ref())
    }

    fn account_meta_at(&self, addr: &Address) -> Option<AccountMeta> {
        self.state.get(addr).map(|acct| AccountMeta {
            balance: acct.balance,
            expiry: acct.expiry,
        })
    }

    fn state_at(&self, addr: &Address) -> Option<&dyn blockchain_traits::KVStore> {
        self.state.get(addr).map(|acct| &**acct as _)
    }

    fn events(&self) -> Vec<&Event> {
        self.completed_transactions
            .iter()
            .flat_map(|r| blockchain_traits::Receipt::events(r))
            .collect()
    }

    fn receipts(&self) -> Vec<&dyn blockchain_traits::Receipt> {
        self.completed_transactions.iter().map(|r| r as _).collect()
    }
}