Browse Source

Message matchers, Contract type, Code

master
Lev 2 years ago
parent
commit
cb4ea10628
  1. 6
      agolang/src/templates.rs
  2. 3
      agolang/src/value.rs
  3. 11
      agorata_contracts/src/contract.rs
  4. 7
      agorata_contracts/src/error.rs
  5. 2
      agorata_contracts/src/lib.rs
  6. 58
      agorata_contracts/src/message.rs

6
agolang/src/templates.rs

@ -60,6 +60,12 @@ impl Matcher {
} }
} }
impl Default for Matcher {
fn default() -> Self {
Self::Any
}
}
/// **The Complex Matcher** /// **The Complex Matcher**
/// ///

3
agolang/src/value.rs

@ -1,12 +1,13 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// Values used for variables in templates. /// Values used for variables in templates.
/// TODO: Create Vec and Tuple to work with outgoing messages in contracts and tokens in messages.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Value { pub enum Value {
Int(i64), Int(i64),
Bool(bool), Bool(bool),
Address(Address), Address(Address),
Token(Address), Token(String),
Data(Vec<u8>), Data(Vec<u8>),
String(String), String(String),
} }

11
agorata_contracts/src/contract.rs

@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
use agolang::Function;
use crate::state::{State, StateDescriptor};
/// The contract representation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Contract {
state_descriptor: StateDescriptor,
init_state: State,
code: Function,
}

7
agorata_contracts/src/error.rs

@ -0,0 +1,7 @@
use serde::{Serialize, Deserialize};
/// The Error type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgoContractError {
InternalError(String)
}

2
agorata_contracts/src/lib.rs

@ -3,6 +3,8 @@ extern crate serde;
/// Message and Address types /// Message and Address types
pub mod message; pub mod message;
pub mod state; pub mod state;
pub mod contract;
pub mod error;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

58
agorata_contracts/src/message.rs

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use agolang::value::Address; use agolang::templates::{ComplexMatcher, Matcher};
use agolang::value::{Address, Value};
use agolang::value::Type;
/// The Message. For now, it's the TON Message. Later, it will be adapted to other blockchains. /// The Message. For now, it's the TON Message. Later, it will be adapted to other blockchains.
/// See [here](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb) for the full TON message schema (seizure warning) or [here](https://ton.org/docs/#/smart-contracts/messages) for a humane version. /// See [here](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb) for the full TON message schema (seizure warning) or [here](https://ton.org/docs/#/smart-contracts/messages) for a humane version.
@ -16,3 +18,57 @@ pub struct Message {
/// Other currencies. /// Other currencies.
pub amounts: Vec<(String, i64)>, pub amounts: Vec<(String, i64)>,
} }
impl Message {
/// Generate a vec of values for checking it with a `ComplexMatcher`
pub fn to_vec_of_values(&self) -> Vec<Value> {
vec![
Value::Address(self.source),
Value::Address(self.destination),
Value::Data(self.data.clone()),
Value::Int(self.amount),
Value::String(self.amounts.first().unwrap_or(&("".to_string(), 0)).0.clone()),
Value::Int(self.amounts.first().unwrap_or(&("".to_string(), 0)).1),
]
}
}
/// A struct that represent a class of messages (or one message, whatever) for matching
///
/// For now, this can only match the first of amounts (todo)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct MessageTemplate {
pub source: Matcher,
pub destination: Matcher,
pub data: Matcher,
pub amount: Matcher,
/// if `amounts` isn't empty, we check the name of the first token
pub token_name: Matcher,
/// if `amounts` isn't empty, we check the value of the first token
pub token_value: Matcher,
}
impl MessageTemplate {
/// Check the validity of the matcher
pub fn is_valid(&self) -> bool {
self.source.check_correctness_for_type(Type::Address)
&& self.destination.check_correctness_for_type(Type::Address)
&& self.data.check_correctness_for_type(Type::Data)
&& self.amount.check_correctness_for_type(Type::Int)
&& self.token_name.check_correctness_for_type(Type::String)
&& self.token_value.check_correctness_for_type(Type::Int)
}
/// Convert this to a `ComplexMatcher` that takes all the params
pub fn get_complex_matcher(self) -> ComplexMatcher {
ComplexMatcher::Compose(vec![
ComplexMatcher::Primitive(self.source),
ComplexMatcher::Primitive(self.destination),
ComplexMatcher::Primitive(self.data),
ComplexMatcher::Primitive(self.amount),
ComplexMatcher::Primitive(self.token_name),
ComplexMatcher::Primitive(self.token_value),
])
}
}

Loading…
Cancel
Save