pchain_runtime/
types.rs

1/*
2    Copyright © 2023, ParallelChain Lab
3    Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
4*/
5
6//! Defines common data structures to be used inside this library, or from outside application.
7
8use std::collections::HashMap;
9use std::ops::{Deref, DerefMut};
10
11use pchain_types::{
12    blockchain::{Command, Transaction},
13    cryptography::{PublicAddress, Sha256Hash},
14};
15
16/// BaseTx consists of common fields inside [Transaction].
17#[derive(Clone, Default)]
18pub(crate) struct BaseTx {
19    pub signer: PublicAddress,
20    pub hash: Sha256Hash,
21    pub nonce: u64,
22    pub gas_limit: u64,
23    pub priority_fee_per_gas: u64,
24}
25
26impl From<&Transaction> for BaseTx {
27    fn from(tx: &Transaction) -> Self {
28        Self {
29            signer: tx.signer,
30            hash: tx.hash,
31            nonce: tx.nonce,
32            gas_limit: tx.gas_limit,
33            priority_fee_per_gas: tx.priority_fee_per_gas,
34        }
35    }
36}
37
38/// CallTx is a struct representation of [pchain_types::Command::Call].
39#[derive(Clone)]
40pub(crate) struct CallTx {
41    pub base_tx: BaseTx,
42    pub target: PublicAddress,
43    pub method: String,
44    pub arguments: Option<Vec<Vec<u8>>>,
45    pub amount: Option<u64>,
46}
47
48impl Deref for CallTx {
49    type Target = BaseTx;
50    fn deref(&self) -> &Self::Target {
51        &self.base_tx
52    }
53}
54
55impl DerefMut for CallTx {
56    fn deref_mut(&mut self) -> &mut Self::Target {
57        &mut self.base_tx
58    }
59}
60
61/// DeferredCommand is the command created from contract call.
62#[derive(Clone, Debug)]
63pub(crate) struct DeferredCommand {
64    pub contract_address: PublicAddress,
65    pub command: Command,
66}
67
68/// Defines information that are supplied to state transition function.
69#[derive(Debug, Default, Clone, PartialEq, Eq)]
70pub struct BlockchainParams {
71    /// Height of the Block
72    pub this_block_number: u64,
73    /// Previous Block Hash
74    pub prev_block_hash: Sha256Hash,
75    /// Base fee in the Block
76    pub this_base_fee: u64,
77    /// Unix timestamp
78    pub timestamp: u32,
79    /// Random Bytes (Reserved.)
80    pub random_bytes: Sha256Hash,
81    /// Address of block proposer
82    pub proposer_address: PublicAddress,
83    /// Address of the treasury
84    pub treasury_address: PublicAddress,
85    /// The current view for this block, given from hotstuff_rs
86    pub cur_view: u64,
87    /// Validator performance is measured by the number of proposed blocks for each validators.
88    /// It is optional because it is not needed in every transaction.
89    pub validator_performance: Option<ValidatorPerformance>,
90}
91
92/// Input for epoch transaction, which is a factor in Pool reward calculation
93#[derive(Debug, Clone, Default, PartialEq, Eq)]
94pub struct ValidatorPerformance {
95    /// Number of blocks per epoch
96    pub blocks_per_epoch: u32,
97    /// A map from a pool address to block proposal statistics
98    pub stats: HashMap<PublicAddress, BlockProposalStats>,
99}
100
101/// Statistics on Block Proposal
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub struct BlockProposalStats {
104    /// Number of proposed blocks within an epoch
105    pub num_of_proposed_blocks: u32,
106}
107
108impl BlockProposalStats {
109    pub fn new(num_of_proposed_blocks: u32) -> Self {
110        Self {
111            num_of_proposed_blocks,
112        }
113    }
114}