use crate::crypto::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use near_primitives_core::{
account::{AccessKey, Account},
hash::CryptoHash,
serialize::dec_format,
types::*,
};
pub type StateRoot = CryptoHash;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
pub enum Finality {
#[serde(rename = "optimistic")]
None,
#[serde(rename = "near-final")]
DoomSlug,
#[serde(rename = "final")]
#[default]
Final,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AccountWithPublicKey {
pub account_id: AccountId,
pub public_key: Ed25519PublicKey,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct AccountInfo {
pub account_id: AccountId,
pub public_key: Ed25519PublicKey,
#[serde(with = "dec_format")]
pub amount: Balance,
}
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
pub struct FunctionArgs(Vec<u8>);
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub enum StateChangeKind {
AccountTouched { account_id: AccountId },
AccessKeyTouched { account_id: AccountId },
DataTouched { account_id: AccountId },
ContractCodeTouched { account_id: AccountId },
}
pub type StateChangesKinds = Vec<StateChangeKind>;
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub enum StateChangeCause {
NotWritableToDisk,
InitialState,
TransactionProcessing { tx_hash: CryptoHash },
ActionReceiptProcessingStarted { receipt_hash: CryptoHash },
ActionReceiptGasReward { receipt_hash: CryptoHash },
ReceiptProcessing { receipt_hash: CryptoHash },
PostponedReceipt { receipt_hash: CryptoHash },
UpdatedDelayedReceipts,
ValidatorAccountsUpdate,
Migration,
Resharding,
}
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub struct RawStateChange {
pub cause: StateChangeCause,
pub data: Option<Vec<u8>>,
}
#[derive(Debug)]
pub enum StateChangesRequest {
AccountChanges { account_ids: Vec<AccountId> },
SingleAccessKeyChanges { keys: Vec<AccountWithPublicKey> },
AllAccessKeyChanges { account_ids: Vec<AccountId> },
ContractCodeChanges { account_ids: Vec<AccountId> },
}
#[derive(Debug)]
pub enum StateChangeValue {
AccountUpdate {
account_id: AccountId,
account: Account,
},
AccountDeletion {
account_id: AccountId,
},
AccessKeyUpdate {
account_id: AccountId,
public_key: Ed25519PublicKey,
access_key: AccessKey,
},
AccessKeyDeletion {
account_id: AccountId,
public_key: Ed25519PublicKey,
},
ContractCodeUpdate {
account_id: AccountId,
code: Vec<u8>,
},
ContractCodeDeletion {
account_id: AccountId,
},
}
impl StateChangeValue {
pub fn affected_account_id(&self) -> &AccountId {
match &self {
StateChangeValue::AccountUpdate { account_id, .. }
| StateChangeValue::AccountDeletion { account_id }
| StateChangeValue::AccessKeyUpdate { account_id, .. }
| StateChangeValue::AccessKeyDeletion { account_id, .. }
| StateChangeValue::ContractCodeUpdate { account_id, .. }
| StateChangeValue::ContractCodeDeletion { account_id } => account_id,
}
}
}
#[derive(Debug)]
pub struct StateChangeWithCause {
pub cause: StateChangeCause,
pub value: StateChangeValue,
}
pub type StateChanges = Vec<StateChangeWithCause>;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BlockId {
Height(BlockHeight),
Hash(CryptoHash),
}
pub type MaybeBlockId = Option<BlockId>;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncCheckpoint {
Genesis,
EarliestAvailable,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BlockReference {
BlockId(BlockId),
Finality(Finality),
SyncCheckpoint(SyncCheckpoint),
}
impl BlockReference {
pub fn latest() -> Self {
Self::Finality(Finality::None)
}
}
impl From<BlockId> for BlockReference {
fn from(block_id: BlockId) -> Self {
Self::BlockId(block_id)
}
}
impl From<Finality> for BlockReference {
fn from(finality: Finality) -> Self {
Self::Finality(finality)
}
}
#[derive(Default, BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq, Eq)]
pub struct ValidatorStats {
pub produced: NumBlocks,
pub expected: NumBlocks,
}
#[derive(Debug, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
pub struct BlockChunkValidatorStats {
pub block_stats: ValidatorStats,
pub chunk_stats: ValidatorStats,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TransactionOrReceiptId {
Transaction {
transaction_hash: CryptoHash,
sender_id: AccountId,
},
Receipt {
receipt_id: CryptoHash,
receiver_id: AccountId,
},
}
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)]
pub enum CompiledContract {
CompileModuleError,
Code(Vec<u8>),
}