Skip to content

Data Structures

Anton Bukov edited this page Sep 5, 2019 · 6 revisions

NearProtocol data structures

Primitives:

https://github.com/nearprotocol/nearcore/blob/master/core/primitives/src/types.rs

pub type Gas = u64;
pub type Nonce = u64;
pub type Balance = u128;
pub type AccountId = String;

Crypto:

https://github.com/nearprotocol/nearcore/blob/master/core/crypto/src/signature.rs

pub struct CryptoHash(pub [u8; 32]); // Sha256 digest
pub struct MerkleHash(pub CryptoHash);
pub struct EpochId(pub CryptoHash);

pub enum PublicKey {
    ED25519(sodiumoxide::crypto::sign::ed25519::PublicKey), // [u8; 32]
    SECP256K1(Secp256K1PublicKey),                          // [u8; 64]
}

pub enum Signature {
    ED25519(sodiumoxide::crypto::sign::ed25519::Signature), // [u8; 64]
    SECP256K1(Secp2561KSignature),                          // [u8; 65]
}

Block header:

https://github.com/nearprotocol/nearcore/blob/master/core/primitives/src/block.rs

pub struct ValidatorStake {
    pub account_id: AccountId, /// Account that stakes money.
    pub public_key: PublicKey, /// Public key of the proposed validator.
    pub amount: Balance,       /// Stake / weight of the validator.
}

pub struct BlockHeaderInner {
    pub height: u64,                        /// Height of this block since the genesis block (height 0).
    pub epoch_id: EpochId,                        /// Epoch start hash of this block's epoch. Used for retrieving validator information
    pub prev_hash: CryptoHash,                    /// Hash of the block previous to this in the chain.
    pub prev_state_root: MerkleHash,              /// Root hash of the state at the previous block.
    pub tx_root: MerkleHash,                      /// Root hash of the transactions in the given block.
    pub timestamp: u64,                           /// Timestamp at which the block was built.
    pub approval_mask: Vec<bool>,                 /// Approval mask, given current block producers.
    pub approval_sigs: Vec<Signature>,            /// Approval signatures for previous block.
    pub total_weight: Weight,                     /// Total weight.
    pub validator_proposals: Vec<ValidatorStake>, /// Validator proposals.
    pub chunk_mask: Vec<bool>,                    /// Mask for new chunks included in the block
    pub gas_used: Gas,                            /// Sum of gas used across all chunks.
    pub gas_limit: Gas,                           /// Gas limit. Same for all chunks.
    pub gas_price: Balance,                       /// Gas price. Same for all chunks
    pub total_supply: Balance,
}

pub struct BlockHeader {
    pub inner: BlockHeaderInner, /// Inner part of the block header that gets hashed.
    pub signature: Signature,    /// Signature of the block producer.
}

Transaction:

https://github.com/nearprotocol/nearcore/blob/master/core/primitives/src/transaction.rs

// Access

pub struct FunctionCallPermission {
    pub allowance: Option<Balance>,
    pub receiver_id: AccountId,
    pub method_names: Vec<String>,
}

pub enum AccessKeyPermission {
    FunctionCall(FunctionCallPermission),
    FullAccess,
}

pub struct AccessKey {
    pub nonce: Nonce,
    pub permission: AccessKeyPermission,
}

// Actions

pub struct CreateAccountAction {}

pub struct DeployContractAction {
    pub code: Vec<u8>,
}

pub struct FunctionCallAction {
    pub method_name: String,
    pub args: Vec<u8>,
    pub gas: Gas,
    pub deposit: Balance,
}

pub struct TransferAction {
    pub deposit: Balance,
}

pub struct StakeAction {
    pub stake: Balance,
    pub public_key: PublicKey,
}

pub struct AddKeyAction {
    pub public_key: PublicKey,
    pub access_key: AccessKey,
}

pub struct DeleteKeyAction {
    pub public_key: PublicKey,
}

pub struct DeleteAccountAction {
    pub beneficiary_id: AccountId,
}

// Transaction

pub enum Action {
    CreateAccount(CreateAccountAction),
    DeployContract(DeployContractAction),
    FunctionCall(FunctionCallAction),
    Transfer(TransferAction),
    Stake(StakeAction),
    AddKey(AddKeyAction),
    DeleteKey(DeleteKeyAction),
    DeleteAccount(DeleteAccountAction),
}

pub struct Transaction {
    pub signer_id: AccountId,
    pub public_key: PublicKey,
    pub nonce: Nonce,
    pub receiver_id: AccountId,
    pub block_hash: CryptoHash, /// The hash of the block in the blockchain on top of which the given transaction is valid.

    pub actions: Vec<Action>,
}

pub struct SignedTransaction {
    pub transaction: Transaction,
    pub signature: Signature,
}

Transaction execution results

https://github.com/nearprotocol/nearcore/blob/master/core/primitives/src/transaction.rs

pub enum TransactionStatus {
    Unknown,
    Completed,
    Failed,
}

pub struct TransactionResult {
    pub status: TransactionStatus, /// Transaction status.
    pub logs: Vec<LogEntry>,       /// Logs from this transaction.
    pub receipts: Vec<CryptoHash>, /// Receipt ids generated by this transaction.
    pub result: Option<Vec<u8>>,   /// Execution Result
}

pub struct TransactionLog {
    pub hash: CryptoHash,          /// Hash of a transaction or a receipt that generated this result.
    pub result: TransactionResult,
}

Final transaction result views

pub enum FinalTransactionStatus {
    Unknown,
    Started,
    Failed,
    Completed,
}

pub struct TransactionResultView {
    pub status: TransactionStatus,
    pub logs: Vec<LogEntry>,
    pub receipts: Vec<CryptoHashView>,
    pub result: Option<String>,
}

pub struct TransactionLogView {
    pub hash: CryptoHashView,
    pub result: TransactionResultView,
}

pub struct FinalTransactionResult {
    pub status: FinalTransactionStatus,        /// Status of the whole transaction and it's receipts.
    pub transactions: Vec<TransactionLogView>, /// Transaction results.
}