#![allow(unknown_lints)]
extern crate protobuf;
extern crate rand;
extern crate zmq;
use std;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::error::Error as StdError;
use messages::processor::TpProcessRequest;
use messaging::stream::ReceiveError;
use messaging::stream::SendError;
#[derive(Debug)]
pub enum ApplyError {
InvalidTransaction(String),
InternalError(String),
}
impl std::error::Error for ApplyError {
fn description(&self) -> &str {
match *self {
ApplyError::InvalidTransaction(ref msg) => msg,
ApplyError::InternalError(ref msg) => msg,
}
}
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
ApplyError::InvalidTransaction(_) => None,
ApplyError::InternalError(_) => None,
}
}
}
impl std::fmt::Display for ApplyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
ApplyError::InvalidTransaction(ref s) => write!(f, "InvalidTransaction: {}", s),
ApplyError::InternalError(ref s) => write!(f, "InternalError: {}", s),
}
}
}
#[derive(Debug)]
pub enum ContextError {
AuthorizationError(String),
ResponseAttributeError(String),
TransactionReceiptError(String),
SerializationError(Box<dyn StdError>),
SendError(Box<dyn StdError>),
ReceiveError(Box<dyn StdError>),
}
impl std::error::Error for ContextError {
fn description(&self) -> &str {
match *self {
ContextError::AuthorizationError(ref msg) => msg,
ContextError::ResponseAttributeError(ref msg) => msg,
ContextError::TransactionReceiptError(ref msg) => msg,
ContextError::SerializationError(ref err) => err.description(),
ContextError::SendError(ref err) => err.description(),
ContextError::ReceiveError(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
ContextError::AuthorizationError(_) => None,
ContextError::ResponseAttributeError(_) => None,
ContextError::TransactionReceiptError(_) => None,
ContextError::SerializationError(ref err) => Some(err.borrow()),
ContextError::SendError(ref err) => Some(err.borrow()),
ContextError::ReceiveError(ref err) => Some(err.borrow()),
}
}
}
impl std::fmt::Display for ContextError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
ContextError::AuthorizationError(ref s) => write!(f, "AuthorizationError: {}", s),
ContextError::ResponseAttributeError(ref s) => {
write!(f, "ResponseAttributeError: {}", s)
}
ContextError::TransactionReceiptError(ref s) => {
write!(f, "TransactionReceiptError: {}", s)
}
ContextError::SerializationError(ref err) => {
write!(f, "SerializationError: {}", err.description())
}
ContextError::SendError(ref err) => write!(f, "SendError: {}", err.description()),
ContextError::ReceiveError(ref err) => write!(f, "ReceiveError: {}", err.description()),
}
}
}
impl From<ContextError> for ApplyError {
fn from(context_error: ContextError) -> Self {
match context_error {
ContextError::TransactionReceiptError(..) => {
ApplyError::InternalError(format!("{}", context_error))
}
_ => ApplyError::InvalidTransaction(format!("{}", context_error)),
}
}
}
impl From<protobuf::ProtobufError> for ContextError {
fn from(e: protobuf::ProtobufError) -> Self {
ContextError::SerializationError(Box::new(e))
}
}
impl From<SendError> for ContextError {
fn from(e: SendError) -> Self {
ContextError::SendError(Box::new(e))
}
}
impl From<ReceiveError> for ContextError {
fn from(e: ReceiveError) -> Self {
ContextError::ReceiveError(Box::new(e))
}
}
pub trait TransactionContext {
#[deprecated(
since = "0.3.0",
note = "please use `get_state_entry` or `get_state_entries` instead"
)]
fn get_state(&self, addresses: &[String]) -> Result<Vec<(String, Vec<u8>)>, ContextError> {
self.get_state_entries(addresses)
}
fn get_state_entry(&self, address: &str) -> Result<Option<Vec<u8>>, ContextError> {
Ok(self
.get_state_entries(&[address.to_string()])?
.into_iter()
.map(|(_, val)| val)
.next())
}
fn get_state_entries(
&self,
addresses: &[String],
) -> Result<Vec<(String, Vec<u8>)>, ContextError>;
#[deprecated(
since = "0.3.0",
note = "please use `set_state_entry` or `set_state_entries` instead"
)]
fn set_state(&self, entries: HashMap<String, Vec<u8>>) -> Result<(), ContextError> {
let state_entries: Vec<(String, Vec<u8>)> = entries.into_iter().collect();
self.set_state_entries(state_entries)
}
fn set_state_entry(&self, address: String, data: Vec<u8>) -> Result<(), ContextError> {
self.set_state_entries(vec![(address, data)])
}
fn set_state_entries(&self, entries: Vec<(String, Vec<u8>)>) -> Result<(), ContextError>;
#[deprecated(
since = "0.3.0",
note = "please use `delete_state_entry` or `delete_state_entries` instead"
)]
fn delete_state(&self, addresses: &[String]) -> Result<Vec<String>, ContextError> {
self.delete_state_entries(addresses)
}
fn delete_state_entry(&self, address: &str) -> Result<Option<String>, ContextError> {
Ok(self
.delete_state_entries(&[address.to_string()])?
.into_iter()
.next())
}
fn delete_state_entries(&self, addresses: &[String]) -> Result<Vec<String>, ContextError>;
fn add_receipt_data(&self, data: &[u8]) -> Result<(), ContextError>;
fn add_event(
&self,
event_type: String,
attributes: Vec<(String, String)>,
data: &[u8],
) -> Result<(), ContextError>;
}
pub trait TransactionHandler {
fn family_name(&self) -> String;
fn family_versions(&self) -> Vec<String>;
fn namespaces(&self) -> Vec<String>;
fn apply(
&self,
request: &TpProcessRequest,
context: &mut dyn TransactionContext,
) -> Result<(), ApplyError>;
}