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
#![feature(non_exhaustive)]

#[macro_use]
extern crate derive_more;

mod address;
mod balance;

pub use address::Address;
pub use balance::Balance;

#[derive(PartialEq, Eq, Debug)]
#[repr(u32)]
#[non_exhaustive]
#[doc(hidden)]
pub enum ExtStatusCode {
    Success,
    InsufficientFunds,
    InvalidInput,
    NoAccount,
}

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct AccountMeta {
    pub balance: u128,
    pub expiry: Option<std::time::Duration>,
}

#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Event {
    pub emitter: Address,
    pub topics: Vec<[u8; 32]>,
    pub data: Vec<u8>,
}

#[derive(Debug, thiserror::Error)]
#[cfg_attr(target_os = "wasi", derive(Clone))]
pub enum RpcError {
    /// There was no service at the requested address.
    #[error("invalid callee")]
    InvalidCallee,

    /// The caller does not have enough balance to cover the sent value.
    #[error("caller does not have enough balance to cover transaction")]
    InsufficientFunds,

    /// The caller did not provide enough gas to complete the transaction.
    #[error("not enough gas provided to transaction")]
    InsufficientGas,

    #[error("transaction received invalid input")]
    InvalidInput,

    #[error("transaction returned invalid output")]
    InvalidOutput(Vec<u8>),

    /// The application returned an error.
    #[error("an application error occurred")]
    Execution(Vec<u8>),

    /// The gateway client encountered an error.
    #[cfg(not(target_os = "wasi"))]
    #[error("gateway error: {0}")]
    Gateway(#[source] anyhow::Error),
}

const _IMPL_SERDE_FOR_RPC_ERROR: () = {
    impl oasis_borsh::ser::BorshSerialize for RpcError {
        fn serialize<W: std::io::Write>(
            &self,
            writer: &mut W,
        ) -> std::result::Result<(), std::io::Error> {
            match self {
                RpcError::InvalidCallee => {
                    let variant_idx = 0u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                }
                RpcError::InsufficientFunds => {
                    let variant_idx = 1u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                }
                RpcError::InsufficientGas => {
                    let variant_idx = 2u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                }
                RpcError::InvalidInput => {
                    let variant_idx = 3u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                }
                RpcError::InvalidOutput(output) => {
                    let variant_idx = 4u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                    oasis_borsh::BorshSerialize::serialize(output, writer)?;
                }
                RpcError::Execution(output) => {
                    let variant_idx = 5u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                    oasis_borsh::BorshSerialize::serialize(output, writer)?;
                }
                #[cfg(not(target_os = "wasi"))]
                RpcError::Gateway(e) => {
                    let variant_idx = 6u8;
                    writer.write_all(&variant_idx.to_le_bytes())?;
                    oasis_borsh::BorshSerialize::serialize(&e.to_string(), writer)?;
                }
            }
            Ok(())
        }
    }

    impl oasis_borsh::de::BorshDeserialize for RpcError {
        fn deserialize<R: std::io::Read>(
            reader: &mut R,
        ) -> std::result::Result<Self, std::io::Error> {
            let mut variant_idx = [0u8];
            reader.read_exact(&mut variant_idx)?;
            let variant_idx = variant_idx[0];
            let return_value = match variant_idx {
                0u8 => RpcError::InvalidCallee,
                1u8 => RpcError::InsufficientFunds,
                2u8 => RpcError::InsufficientGas,
                3u8 => RpcError::InvalidInput,
                4u8 => RpcError::InvalidOutput(oasis_borsh::BorshDeserialize::deserialize(reader)?),
                5u8 => RpcError::Execution(oasis_borsh::BorshDeserialize::deserialize(reader)?),
                #[cfg(not(target_os = "wasi"))]
                6u8 => {
                    let err_str: String = oasis_borsh::BorshDeserialize::deserialize(reader)?;
                    RpcError::Gateway(anyhow::anyhow!(err_str))
                }
                _ => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        format!("Unexpected variant index: {}", variant_idx),
                    ))
                }
            };
            Ok(return_value)
        }
    }
};

impl RpcError {
    pub fn execution(&self) -> Option<&[u8]> {
        match self {
            RpcError::Execution(output) => Some(&output),
            _ => None,
        }
    }
}