[][src]Macro exonum::transactions

macro_rules! transactions {
    {} => { ... };
    {
        $(#[$tx_set_attr:meta])*
        $transaction_set:ident {
            const SERVICE_ID = $service_id:expr;

            $(
                $(#[$tx_attr:meta])*
                struct $name:ident {
                    $($def:tt)*
                }
            )*
        }
    } => { ... };
    {
        $(#[$tx_set_attr:meta])*
        pub $transaction_set:ident {
            const SERVICE_ID = $service_id:expr;

            $(
                $(#[$tx_attr:meta])*
                struct $name:ident {
                    $($def:tt)*
                }
            )*
        }
    } => { ... };
    {
        $(#[$tx_set_attr:meta])*
        pub($($vis:tt)+) $transaction_set:ident {
            const SERVICE_ID = $service_id:expr;

            $(
                $(#[$tx_attr:meta])*
                struct $name:ident {
                    $($def:tt)*
                }
            )*
        }
    } => { ... };
    (@implement $transaction_set:ident, $($name:ident)*) => { ... };
}

transactions! is used to declare a set of transactions of a particular service.

The macro generates a type for each transaction and a helper enum which can hold any of the transactions. You need to implement the Transaction trait for each of the transactions yourself.

See Service trait documentation for a full example of usage.

Each transaction is specified as a Rust struct. For additional information about data layout, see the documentation on the encoding module.

Additionally, the macro must define the identifier of a service, which will be used in parsing messages, as const SERVICE_ID. Service ID should be unique within the Exonum blockchain.

For each transaction, the macro creates getter methods for all defined fields. The names of the methods coincide with the field names. In addition, two constructors are defined:

  • new accepts as arguments all fields in the order of their declaration in the macro, and a SecretKey as the last argument. A SecretKey is used to sign the message. The constructor returns a transaction which contains the fields and a signature. In this case, the constructor creates a signature for the message using the secret key.
  • new_with_signature accepts as arguments all fields in the order of their declaration in the macro, and a message Signature. The constructor returns a transaction which contains the fields and a signature. In this case, the constructor signs the message using the indicated signature.

Each transaction also implements Message, ServiceMessage, SegmentField, ExonumJson and StorageValue traits for the declared datatype.

Note. transactions! uses other macros in the exonum crate internally. Be sure to add them to the global scope.

Examples

The example below uses the transactions! macro; declares a set of transactions for a service with the indicated ID and adds two transactions.

#[macro_use] extern crate exonum;
use exonum::crypto::PublicKey;

transactions! {
    WalletTransactions {
        const SERVICE_ID = 1;

        struct Create {
            key: &PublicKey
        }

        struct Transfer {
            from: &PublicKey,
            to: &PublicKey,
            amount: u64,
        }
    }
}