-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
291 lines (262 loc) · 10.7 KB
/
lib.rs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use avalanche-types::{
core::{
codec::{Codec, Decode, Encode},
Executable, Execution,
},
crypto::{KeyPair, PrivateKey, PublicKey},
zkp::{BulletproofGroth16, Proof},
};
use std::collections::HashMap;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct MyTransaction {
pub sender: PublicKey,
pub recipient: PublicKey,
pub amount: u64,
pub proof: Proof<BulletproofGroth16>,
}
impl Codec for MyTransaction {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.sender.encode();
encoded.extend(self.recipient.encode());
encoded.extend(self.amount.encode());
encoded.extend(self.proof.encode());
encoded
}
fn decode(bytes: &[u8]) -> Result<Self, String> {
let mut cursor = 0;
let sender = PublicKey::decode(&bytes[cursor..])?;
cursor += sender.encode().len();
let recipient = PublicKey::decode(&bytes[cursor..])?;
cursor += recipient.encode().len();
let amount = u64::decode(&bytes[cursor..])?;
cursor += amount.encode().len();
let proof = Proof::<BulletproofGroth16>::decode(&bytes[cursor..])?;
Ok(MyTransaction {
sender,
recipient,
amount,
proof,
})
}
}
impl Executable for MyTransaction {
fn execute(&self, execution: &mut Execution) -> Result<(), String> {
// Verify the proof before executing the transaction
if !self.proof.verify() {
return Err("invalid proof".to_string());
}
let sender = execution.get_account(&self.sender)?;
let recipient = execution.get_account(&self.recipient)?;
if sender.balance < self.amount {
return Err("insufficient balance".to_string());
}
sender.balance -= self.amount;
recipient.balance += self.amount;
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct SmartContractTransaction {
pub sender: PublicKey,
pub contract_address: PublicKey,
pub data: Vec<u8>,
pub proof: Proof<BulletproofGroth16>,
}
impl Codec for SmartContractTransaction {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.sender.encode();
encoded.extend(self.contract_address.encode());
encoded.extend(self.data.encode());
encoded.extend(self.proof.encode());
encoded
}
fn decode(bytes: &[u8]) -> Result<Self, String> {
let mut cursor = 0;
let sender = PublicKey::decode(&bytes[cursor..])?;
cursor += sender.encode().len();
let contract_address = PublicKey::decode(&bytes[cursor..])?;
cursor += contract_address.encode().len();
let data = Vec::<u8>::decode(&bytes[cursor..])?;
cursor += data.encode().len();
let proof = Proof::<BulletproofGroth16>::decode(&bytes[cursor..])?;
Ok(SmartContractTransaction {
sender,
contract_address,
data,
proof,
})
}
}
impl Executable for SmartContractTransaction {
fn execute(&self, execution: &mut Execution) -> Result<(), String> {
// Verify the proof before executing the transaction
if !self.proof.verify() {
return Err("invalid proof".to_string());
}
let contract = execution.get_account(&self.contract_address)?;
// Execute the smart contract
let result = contract.execute(&self.data)?;
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct MultiAssetTransaction {
pub sender: PublicKey,
pub recipient: PublicKey,
pub assets: Vec<AssetTransfer>,
pub proof: Proof<BulletproofGroth16>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct AssetTransfer {
pub asset_id: Vec<u8>,
pub amount: u64,
}
impl Codec for AssetTransfer {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.asset_id.encode();
encoded.extend(self.amount.encode());
encoded
}
fn decode(bytes: &[u8]) -> Result<Self, String> {
let
impl Codec for MultiAssetTransaction {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.sender.encode();
encoded.extend(self.recipient.encode());
encoded.extend(self.assets.encode());
encoded.extend(self.proof.encode());
encoded
}
fn decode(bytes: &[u8]) -> Result<Self, String> {
let mut cursor = 0;
let sender = PublicKey::decode(&bytes[cursor..])?;
cursor += sender.encode().len();
let recipient = PublicKey::decode(&bytes[cursor..])?;
cursor += recipient.encode().len();
let assets = Vec::<AssetTransfer>::decode(&bytes[cursor..])?;
cursor += assets.encode().len();
let proof = Proof::<BulletproofGroth16>::decode(&bytes[cursor..])?;
Ok(MultiAssetTransaction {
sender,
recipient,
assets,
proof,
})
}
}
impl Executable for MultiAssetTransaction {
fn execute(&self, execution: &mut Execution) -> Result<(), String> {
// Verify the proof before executing the transaction
if !self.proof.verify() {
return Err("invalid proof".to_string());
let sender = execution.get_account(&self.sender)?;
let recipient = execution.get_account(&self.recipient)?;
for asset in self.assets.iter() {
let asset_balance = sender.get_asset_balance(&asset.asset_id)?;
if asset_balance < asset.amount {
return Err("insufficient asset balance".to_string());
}
sender.set_asset_balance(&asset.asset_id, asset_balance - asset.amount)?;
recipient.add_asset_balance(&asset.asset_id, asset.amount)?;
}
Ok(())
}
}
struct MyVirtualMachine {
accounts: HashMap<PublicKey, Account>,
}
impl MyVirtualMachine {
fn new() -> Self {
MyVirtualMachine {
accounts: HashMap::new(),
}
}
fn create_account(&mut self, keypair: KeyPair) -> PublicKey {
let pubkey = keypair.public;
let account = Account {
keypair,
balance: 0,
assets: HashMap::new(),
};
self.accounts.insert(pubkey, account);
pubkey
}
fn get_account(&mut self, pubkey: &PublicKey) -> Result<&mut Account, String> {
self.accounts
.get_mut(pubkey)
.ok_or_else(|| "account not found".to_string())
}
}
impl Execution for MyVirtualMachine {
fn get_account(&mut self, pubkey: &PublicKey) -> Result<&mut Account, String> {
self.get_account(pubkey)
}
}
struct Account {
keypair: KeyPair,
balance: u64,
assets: HashMap<Vec<u8>, u64>,
}
impl Account {
fn get_asset_balance(&self, asset_id: &[u8]) -> Result<u64, String> {
self.assets
.get(asset_id)
.copied()
.ok_or_else(|| "asset not found".to_string())
}
fn set_asset_balance(&mut self, asset_id: &[u8], balance: u64) -> Result<(), String> {
self.assets.insert(asset_id.to_vec(), balance);
Ok(())
}
fn add_asset_balance(&mut self, asset_id: &[u8], amount: u64) -> Result<(), String> {
let balance = self.get_asset_balance(asset_id)?;
self.set_asset_balance(asset_id, balance + amount)?;
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct DecentralizedGovernanceTransaction {
pub proposer: PublicKey,
pub proposal: Vec<u8>,
pub proof: Proof<BulletproofGroth16>,
}
impl Codec for DecentralizedGovernanceTransaction {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.proposer.encode();
encoded.extend(self.proposal.encode());
encoded.extend(self.proof.encode());
encoded
}
fn decode(bytes: &[u8]) -> Result<Self, String> {
let mut cursor = 0;
let proposer = PublicKey::decode(&bytes[cursor..])?;
cursor += proposer.encode().len();
let proposal = Vec::<u8>::decode(&bytes[cursor..])?;
cursor += proposal.encode().len();
let proof = Proof::<BulletproofGroth16>::decode(&bytes[cursor..])?;
Ok(DecentralizedGovernanceTransaction {
proposer,
proposal,
proof,
})
}
}
impl Executable for DecentralizedGovernanceTransaction {
fn execute(&self, execution: &mut Execution) -> Result<(), String> {
// Verify the proof before executing the transaction
if !self.proof.verify() {
return Err("invalid proof".to_string());
}
// Check if the proposer has the required stake to propose a change
let proposer = execution.get_account(&self.proposer)?;
if proposer.stake < MIN_STAKE_TO_PROPOSE {
return Err("insufficient stake".to_string());
}
// Add the proposal to the list of pending proposals
let mut pending_proposals = execution.get_pending_proposals()?;
pending_proposals.push(self.proposal.clone());
execution.set_pending_proposals(pending_proposals)?;
Ok(())
}
}
#