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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use anyhow::Result;
use diem_client::{views, BlockingClient, Response, WaitForTransactionError};
use diem_logger::prelude::info;
use diem_types::{
account_address::AccountAddress,
account_state_blob::AccountStateBlob,
event::EventKey,
ledger_info::LedgerInfoWithSignatures,
proof::{AccumulatorConsistencyProof, TransactionAccumulatorSummary},
state_proof::StateProof,
transaction::{SignedTransaction, Version},
trusted_state::{TrustedState, TrustedStateChange},
waypoint::Waypoint,
};
use reqwest::Url;
use std::{convert::TryFrom, time::Duration};
pub struct DiemClient {
client: BlockingClient,
trusted_state: TrustedState,
latest_epoch_change_li: Option<LedgerInfoWithSignatures>,
}
impl DiemClient {
pub fn new(url: Url, waypoint: Waypoint) -> Result<Self> {
let initial_trusted_state = TrustedState::from_epoch_waypoint(waypoint);
let client = BlockingClient::new(url.to_string());
Ok(DiemClient {
client,
trusted_state: initial_trusted_state,
latest_epoch_change_li: None,
})
}
pub fn submit_transaction(&self, transaction: &SignedTransaction) -> Result<()> {
self.client
.submit(transaction)
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn get_account(&self, account: &AccountAddress) -> Result<Option<views::AccountView>> {
self.client
.get_account(*account)
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn get_account_state_blob(
&self,
account: &AccountAddress,
) -> Result<(Option<AccountStateBlob>, Version)> {
let ret = self
.client
.get_account_state_with_proof(*account, None, None)
.map(Response::into_inner)?;
if let Some(blob) = ret.blob {
Ok((Some(bcs::from_bytes(&blob)?), ret.version))
} else {
Ok((None, ret.version))
}
}
pub fn get_events(
&self,
event_key: EventKey,
start: u64,
limit: u64,
) -> Result<Vec<views::EventView>> {
self.client
.get_events(event_key, start, limit)
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn wait_for_transaction(
&self,
txn: &SignedTransaction,
timeout: Duration,
) -> Result<views::TransactionView, WaitForTransactionError> {
self.client
.wait_for_signed_transaction(txn, Some(timeout), None)
.map(Response::into_inner)
}
pub fn get_metadata(&self) -> Result<views::MetadataView> {
self.client
.get_metadata()
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn get_currency_info(&self) -> Result<Vec<views::CurrencyInfoView>> {
self.client
.get_currencies()
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn update_and_verify_state_proof(&mut self) -> Result<()> {
let current_version = self.trusted_state.version();
let maybe_accumulator = if self.trusted_state.accumulator_summary().is_none() {
let consistency_proof_view = self
.client
.get_accumulator_consistency_proof(None, Some(current_version))
.map(Response::into_inner)?;
let consistency_proof = AccumulatorConsistencyProof::try_from(&consistency_proof_view)?;
let accumulator = TransactionAccumulatorSummary::try_from_genesis_proof(
consistency_proof,
current_version,
)?;
Some(accumulator)
} else {
None
};
let state_proof_view = self
.client
.get_state_proof(self.trusted_state.version())
.map(Response::into_inner)?;
let state_proof = StateProof::try_from(&state_proof_view)?;
self.verify_state_proof(&state_proof, maybe_accumulator.as_ref())
}
fn verify_state_proof(
&mut self,
state_proof: &StateProof,
maybe_accumulator: Option<&TransactionAccumulatorSummary>,
) -> Result<()> {
let state = self.trusted_state();
match state.verify_and_ratchet(state_proof, maybe_accumulator)? {
TrustedStateChange::Epoch {
new_state,
latest_epoch_change_li,
} => {
info!(
"Verified epoch changed to {}",
latest_epoch_change_li
.ledger_info()
.next_epoch_state()
.expect("no validator set in epoch change ledger info"),
);
self.update_trusted_state(new_state);
self.update_latest_epoch_change_li(latest_epoch_change_li.clone());
}
TrustedStateChange::Version { new_state } => {
if state.version() < new_state.version() {
info!("Verified version change to: {}", new_state.version());
}
self.update_trusted_state(new_state);
}
TrustedStateChange::NoChange => (),
}
Ok(())
}
pub(crate) fn latest_epoch_change_li(&self) -> Option<&LedgerInfoWithSignatures> {
self.latest_epoch_change_li.as_ref()
}
pub(crate) fn trusted_state(&self) -> TrustedState {
self.trusted_state.clone()
}
fn update_latest_epoch_change_li(&mut self, ledger: LedgerInfoWithSignatures) {
self.latest_epoch_change_li = Some(ledger);
}
fn update_trusted_state(&mut self, state: TrustedState) {
self.trusted_state = state
}
pub fn get_txn_by_acc_seq(
&self,
account: &AccountAddress,
sequence_number: u64,
fetch_events: bool,
) -> Result<Option<views::TransactionView>> {
self.client
.get_account_transaction(*account, sequence_number, fetch_events)
.map_err(Into::into)
.map(Response::into_inner)
}
pub fn get_txn_by_range(
&self,
start_version: u64,
limit: u64,
fetch_events: bool,
) -> Result<Vec<views::TransactionView>> {
self.client
.get_transactions(start_version, limit, fetch_events)
.map_err(Into::into)
.map(Response::into_inner)
}
}