use std::borrow::Borrow;
use std::fmt;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use near_crypto::{KeyType, PublicKey};
use crate::borsh::maybestd::collections::HashMap;
use crate::hash::CryptoHash;
use crate::logging;
use crate::serialize::{dec_format, option_base64_format};
use crate::transaction::{Action, TransferAction};
use crate::types::{AccountId, Balance, ShardId};
#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Receipt {
pub predecessor_id: AccountId,
pub receiver_id: AccountId,
pub receipt_id: CryptoHash,
pub receipt: ReceiptEnum,
}
impl Borrow<CryptoHash> for Receipt {
fn borrow(&self) -> &CryptoHash {
&self.receipt_id
}
}
impl Receipt {
pub fn get_hash(&self) -> CryptoHash {
self.receipt_id
}
pub fn new_balance_refund(receiver_id: &AccountId, refund: Balance) -> Self {
Receipt {
predecessor_id: "system".parse().unwrap(),
receiver_id: receiver_id.clone(),
receipt_id: CryptoHash::default(),
receipt: ReceiptEnum::Action(ActionReceipt {
signer_id: "system".parse().unwrap(),
signer_public_key: PublicKey::empty(KeyType::ED25519),
gas_price: 0,
output_data_receivers: vec![],
input_data_ids: vec![],
actions: vec![Action::Transfer(TransferAction { deposit: refund })],
}),
}
}
pub fn new_gas_refund(
receiver_id: &AccountId,
refund: Balance,
signer_public_key: PublicKey,
) -> Self {
Receipt {
predecessor_id: "system".parse().unwrap(),
receiver_id: receiver_id.clone(),
receipt_id: CryptoHash::default(),
receipt: ReceiptEnum::Action(ActionReceipt {
signer_id: receiver_id.clone(),
signer_public_key,
gas_price: 0,
output_data_receivers: vec![],
input_data_ids: vec![],
actions: vec![Action::Transfer(TransferAction { deposit: refund })],
}),
}
}
}
#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum ReceiptEnum {
Action(ActionReceipt),
Data(DataReceipt),
}
#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct ActionReceipt {
pub signer_id: AccountId,
pub signer_public_key: PublicKey,
#[serde(with = "dec_format")]
pub gas_price: Balance,
pub output_data_receivers: Vec<DataReceiver>,
pub input_data_ids: Vec<CryptoHash>,
pub actions: Vec<Action>,
}
#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)]
pub struct DataReceipt {
pub data_id: CryptoHash,
#[serde(with = "option_base64_format")]
pub data: Option<Vec<u8>>,
}
#[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))]
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq,
)]
pub struct DataReceiver {
pub data_id: CryptoHash,
pub receiver_id: AccountId,
}
impl fmt::Debug for DataReceipt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DataReceipt")
.field("data_id", &self.data_id)
.field("data", &format_args!("{}", logging::pretty_result(&self.data)))
.finish()
}
}
#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)]
pub struct ReceivedData {
pub data: Option<Vec<u8>>,
}
impl fmt::Debug for ReceivedData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReceivedData")
.field("data", &format_args!("{}", logging::pretty_result(&self.data)))
.finish()
}
}
#[derive(Default, BorshSerialize, BorshDeserialize, Clone, PartialEq, Debug)]
pub struct DelayedReceiptIndices {
pub first_index: u64,
pub next_available_index: u64,
}
pub type ReceiptResult = HashMap<ShardId, Vec<Receipt>>;