1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![feature(external_doc)]
#![deny(missing_docs, missing_debug_implementations)]
#![doc(html_favicon_url = "https://exonum.com/favicon.ico")]
#![doc(include = "../docs/implementation.md")]
#[macro_use]
extern crate lazy_static;
extern crate byteorder;
#[macro_use]
extern crate exonum;
extern crate bulletproofs;
extern crate curve25519_dalek as curve25519;
extern crate exonum_sodiumoxide as sodiumoxide;
extern crate failure;
extern crate merlin;
extern crate rand;
#[macro_use]
extern crate failure_derive;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use exonum::{
api::ServiceApiBuilder,
blockchain::{self as bc, ServiceContext, Transaction},
crypto::Hash,
encoding::Error as EncodingError,
messages::RawMessage,
storage::{Fork, Snapshot},
};
use std::ops::Range;
pub mod api;
pub mod crypto;
mod debug;
mod secrets;
pub mod storage;
pub mod transactions;
mod utils;
pub use api::Api;
use debug::DebuggerProbe;
pub use debug::{DebugEvent, Debugger, DebuggerOptions};
pub use secrets::{EncryptedData, SecretState, VerifiedTransfer};
pub use storage::{Schema, Wallet};
pub use transactions::CryptoTransactions as Transactions;
pub const SERVICE_NAME: &str = "private_currency";
pub const SERVICE_ID: u16 = 2_000;
pub const CONFIG: Config = Config {
initial_balance: 1_000_000,
rollback_delay_bounds: 5..1_000,
min_transfer_amount: 1,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Config {
pub initial_balance: u64,
pub rollback_delay_bounds: Range<u32>,
pub min_transfer_amount: u64,
}
#[derive(Debug, Default)]
pub struct Service {
debugger_probe: Option<DebuggerProbe>,
}
impl Service {
pub fn debug(options: DebuggerOptions) -> (Self, Debugger) {
let (probe, debugger) = DebuggerProbe::create_channel(16, options);
let service = Service {
debugger_probe: Some(probe),
};
(service, debugger)
}
}
impl bc::Service for Service {
fn service_id(&self) -> u16 {
SERVICE_ID
}
fn service_name(&self) -> &str {
SERVICE_NAME
}
fn state_hash(&self, snapshot: &dyn Snapshot) -> Vec<Hash> {
Schema::new(snapshot).state_hash()
}
fn tx_from_raw(&self, raw: RawMessage) -> Result<Box<Transaction>, EncodingError> {
use bc::TransactionSet;
Transactions::tx_from_raw(raw).map(|tx| tx.into())
}
fn before_commit(&self, fork: &mut Fork) {
if let Some(ref probe) = self.debugger_probe {
probe.on_before_commit(fork);
}
Schema::new(fork).do_rollback();
}
fn after_commit(&self, context: &ServiceContext) {
if let Some(ref probe) = self.debugger_probe {
probe.on_after_commit(context);
}
}
fn wire_api(&self, builder: &mut ServiceApiBuilder) {
builder
.public_scope()
.endpoint("v1/wallet", Api::wallet)
.endpoint_mut("v1/transaction", Api::transaction);
}
}