oasis_core_runtime/consensus/
transaction.rs

1use crate::common::{
2    crypto::signature::{signature_context_with_chain_separation, Signed},
3    quantity::Quantity,
4};
5
6pub const SIGNATURE_CONTEXT: &[u8] = b"oasis-core/consensus: tx";
7
8fn signature_context(chain_context: &String) -> Vec<u8> {
9    let context = SIGNATURE_CONTEXT.to_vec();
10    signature_context_with_chain_separation(context, chain_context)
11}
12
13/// Unsigned consensus transaction.
14#[derive(Debug, cbor::Encode, cbor::Decode)]
15#[cbor(no_default)]
16pub struct Transaction {
17    /// Nonce to prevent replay.
18    pub nonce: u64,
19    /// Optional fee that the sender commits to pay to execute this transaction.
20    pub fee: Option<Fee>,
21
22    /// Method that should be called.
23    pub method: MethodName,
24    /// Method call body.
25    pub body: cbor::Value,
26}
27
28/// Signed consensus transaction.
29pub type SignedTransaction = Signed;
30
31impl SignedTransaction {
32    /// Returns true iff the signature is valid.
33    pub fn verify(&self, chain_context: &String) -> bool {
34        let context = signature_context(chain_context);
35        self.signature.verify(&context, &self.blob)
36    }
37}
38
39/// Consensus transaction fee the sender wishes to pay for operations which
40/// require a fee to be paid to validators.
41#[derive(Debug, Default, cbor::Encode, cbor::Decode)]
42pub struct Fee {
43    /// Fee amount to be paid.
44    pub amount: Quantity,
45    /// Maximum gas that a transaction can use.
46    pub gas: Gas,
47}
48
49/// Consensus gas representation.
50pub type Gas = u64;
51
52/// Method name.
53pub type MethodName = String;
54
55/// Proof of transaction inclusion in a block.
56#[derive(Debug, Default, cbor::Encode, cbor::Decode)]
57pub struct Proof {
58    /// Block height at which the transaction was published.
59    pub height: u64,
60    /// Actual raw proof.
61    pub raw_proof: Vec<u8>,
62}
63
64/// Signed consensus transaction with a proof of its inclusion in a block.
65#[derive(Debug, Default, cbor::Encode, cbor::Decode)]
66pub struct SignedTransactionWithProof {
67    /// Signed transaction.
68    pub signed_tx: SignedTransaction,
69    /// Proof of transaction inclusion in a block.
70    pub proof: Proof,
71}