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
use crate::blocks::{Block, BlockHash, BlockType, Previous};
use crate::node::controller::Controller;
use crate::node::messages::confirm_ack::{Confirm, ConfirmAck};
use crate::{Public, Rai, Signature};
use anyhow::{anyhow, Context};
use tracing::{debug, instrument, warn};
struct AccountDelta {
from: Public,
to: Public,
amount: Rai,
}
impl Controller {
#[instrument(skip(self))]
pub async fn add_vote(&mut self, confirm_ack: &ConfirmAck) -> anyhow::Result<()> {
let context = || format!("Adding vote {:?}", &confirm_ack);
let hashes = if let Confirm::VoteByHash(hashes) = &confirm_ack.confirm {
hashes
} else {
return Err(anyhow!("Confirm::Block not implemented")).with_context(context);
};
for hash in hashes {
self.validate_vote(hash, &confirm_ack.account, &confirm_ack.signature)
.await
.with_context(context)?;
self.state
.lock()
.await
.add_vote(hash, &confirm_ack.account)
.await
.with_context(context)?;
}
Ok(())
}
#[instrument(skip(self))]
pub async fn validate_vote(
&mut self,
hash: &BlockHash,
representative: &Public,
signature: &Signature,
) -> anyhow::Result<()> {
warn!("TODO validate vote");
Ok(())
}
pub async fn add_elected_block(&mut self, block: &Block) -> anyhow::Result<()> {
debug!("Adding elected block {:?}", &block);
let context = || format!("Block {:?}", &block);
let block_hash = block.hash().with_context(context)?;
if self
.state
.lock()
.await
.get_block_by_hash(&block_hash)
.await
.with_context(context)?
.is_some()
{
return Err(anyhow!("Block already exists")).with_context(context);
}
let context = || format!("Block {:?}", block);
block
.verify_signature(&block.account())
.context("Incorrect signature")
.with_context(context)?;
let work = block.work();
if work.is_none() {
return Err(anyhow!("Work is missing from block")).with_context(context);
}
match block.block_type() {
BlockType::Send => {
dbg!(block);
let previous_hash = match block.previous() {
Previous::Block(h) => h,
Previous::Open => {
return Err(anyhow!("Send block has a blank previous block hash"))
.with_context(context)
}
};
let prev_block = self
.state
.lock()
.await
.get_block_by_hash(previous_hash)
.await
.context("Previous block")
.with_context(context)?
.ok_or_else(|| anyhow!("Could not find previous block"))
.with_context(context)?;
let prev_balance = prev_block.balance();
if block.balance() >= prev_balance {
return Err(anyhow!(
"Can not increase balance in a send block. Prev: {:?}",
prev_block
))
.with_context(context);
}
let _to_account = block.destination().with_context(context)?;
let _amount = prev_balance
.checked_sub(block.balance())
.ok_or_else(|| {
anyhow!(
"Subtracting prev_balance {:?} and new balance {:?}",
prev_balance,
block.balance()
)
})
.with_context(context)?;
}
BlockType::Open => {
dbg!(block);
if !block.is_genesis(&self.network)? {
}
}
_ => todo!(),
}
self.state
.lock()
.await
.add_block(block)
.await
.with_context(context)?;
Ok(())
}
pub async fn get_latest_block(&self, account: &Public) -> anyhow::Result<Option<Block>> {
let block_hash = self
.state
.lock()
.await
.get_latest_block_hash_for_account(account)
.await
.with_context(|| format!("Account: {:?}", account))?;
let block_hash = match block_hash {
Some(b) => b,
None => return Ok(None),
};
Ok(self
.state
.lock()
.await
.get_block_by_hash(&block_hash)
.await
.with_context(|| {
format!(
"Could not get block for latest hash for account: {:?}",
account
)
})?)
}
}