Macro exonum::transactions [] [src]

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 must implement 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 reference about data layout see the documentation of the encoding module.

Additionally, the macro must define 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 fields with the same names as fields. In addition, two constructors are defined:

  • new takes all fields in the order of their declaration in the macro, and a SecretKey to sign the message as the last argument.
  • new_with_signature takes all fields in the order of their declaration in the macro, and a message Signature.

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

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

Examples

#[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,
        }
    }
}