-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy patheth_bridge_vp.rs
More file actions
545 lines (485 loc) · 17.2 KB
/
Copy patheth_bridge_vp.rs
File metadata and controls
545 lines (485 loc) · 17.2 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Validity predicate for the Ethereum bridge
use std::collections::BTreeSet;
use std::marker::PhantomData;
use namada_core::address::Address;
use namada_core::booleans::BoolResultUnitExt;
use namada_core::collections::HashSet;
use namada_core::storage::Key;
use namada_state::StateRead;
use namada_systems::trans_token::{self as token, Amount};
use namada_tx::BatchedTxRef;
use namada_vp::native_vp::{self, Ctx, NativeVp, StorageReader, VpEvaluator};
use crate::storage;
use crate::storage::escrow_key;
/// Generic error that may be returned by the validity predicate
#[derive(thiserror::Error, Debug)]
#[error("Ethereum Bridge VP error: {0}")]
pub struct Error(#[from] native_vp::Error);
/// Validity predicate for the Ethereum bridge
pub struct EthBridge<'ctx, S, CA, EVAL, TokenKeys>
where
S: 'static + StateRead,
EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>,
{
/// Context to interact with the host structures.
pub ctx: Ctx<'ctx, S, CA, EVAL>,
/// Generic types for DI
pub _marker: PhantomData<TokenKeys>,
}
impl<'ctx, S, CA, EVAL, TokenKeys> EthBridge<'ctx, S, CA, EVAL, TokenKeys>
where
S: 'static + StateRead,
CA: 'static + Clone,
EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>,
TokenKeys: token::Keys,
{
/// Instantiate eth bridge VP
pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self {
Self {
ctx,
_marker: PhantomData,
}
}
/// If the Ethereum bridge's escrow key was written to, we check
/// that the NAM balance increased and that the Bridge pool VP has
/// been triggered.
fn check_escrow(&self, verifiers: &BTreeSet<Address>) -> Result<(), Error> {
let escrow_key = TokenKeys::balance_key(
&self.ctx.state.in_mem().native_token,
&crate::ADDRESS,
);
let escrow_pre: Amount =
(&self.ctx).read_pre_value(&escrow_key)?.unwrap_or_default();
let escrow_post: Amount =
(&self.ctx).must_read_post_value(&escrow_key)?;
// The amount escrowed should increase.
if escrow_pre < escrow_post {
// NB: normally, we only escrow NAM under the Ethereum bridge
// address in the context of a Bridge pool transfer
let bridge_pool_is_verifier =
verifiers.contains(&storage::bridge_pool::BRIDGE_POOL_ADDRESS);
bridge_pool_is_verifier.ok_or_else(|| {
native_vp::Error::new_const(
"Bridge pool VP was not marked as a verifier of the \
transaction",
)
.into()
})
} else {
Err(native_vp::Error::new_const(
"User tx attempted to decrease the amount of native tokens \
escrowed in the Ethereum Bridge's account",
)
.into())
}
}
}
impl<'view, 'ctx: 'view, S, CA, EVAL, TokenKeys> NativeVp<'view>
for EthBridge<'ctx, S, CA, EVAL, TokenKeys>
where
S: 'static + StateRead,
CA: 'static + Clone,
EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>,
TokenKeys: token::Keys,
{
type Error = Error;
/// Validate that a wasm transaction is permitted to change keys under this
/// account.
///
/// We only permit increasing the escrowed balance of NAM under the Ethereum
/// bridge address, when writing to storage from wasm transactions.
///
/// Some other changes to the storage subspace of this account are expected
/// to happen natively i.e. bypassing this validity predicate. For example,
/// changes to the `eth_msgs/...` keys. For those cases, we reject here as
/// no wasm transactions should be able to modify those keys.
fn validate_tx(
&'view self,
_: &BatchedTxRef<'_>,
keys_changed: &BTreeSet<Key>,
verifiers: &BTreeSet<Address>,
) -> Result<(), Self::Error> {
tracing::debug!(
keys_changed_len = keys_changed.len(),
verifiers_len = verifiers.len(),
"Ethereum Bridge VP triggered",
);
validate_changed_keys::<TokenKeys>(
&self.ctx.state.in_mem().native_token,
keys_changed,
)?;
self.check_escrow(verifiers)
}
}
/// Checks if `keys_changed` represents a valid set of changed keys.
///
/// This implies checking if two distinct keys were changed:
///
/// 1. The Ethereum bridge escrow account's NAM balance key.
/// 2. Another account's NAM balance key.
///
/// Any other keys changed under the Ethereum bridge account
/// are rejected.
fn validate_changed_keys<TokenKeys: token::Keys>(
nam_addr: &Address,
keys_changed: &BTreeSet<Key>,
) -> Result<(), Error> {
// acquire all keys that either changed our account, or that touched
// nam balances
let keys_changed: HashSet<_> = keys_changed
.iter()
.filter(|&key| {
let changes_eth_storage = storage::has_eth_addr_segment(key);
let changes_nam_balance =
TokenKeys::is_balance_key(nam_addr, key).is_some();
changes_nam_balance || changes_eth_storage
})
.collect();
if keys_changed.is_empty() {
return Err(native_vp::Error::SimpleMessage(
"No keys changed under our account so this validity predicate \
shouldn't have been triggered",
)
.into());
}
tracing::debug!(
relevant_keys.len = keys_changed.len(),
"Found keys changed under our account"
);
let nam_escrow_addr_modified = keys_changed.contains(&escrow_key(nam_addr));
if !nam_escrow_addr_modified {
let error = native_vp::Error::new_const(
"The native token's escrow balance should have been modified",
)
.into();
tracing::debug!("{error}");
return Err(error);
}
let all_keys_are_nam_balance = keys_changed
.iter()
.all(|key| TokenKeys::is_balance_key(nam_addr, key).is_some());
if !all_keys_are_nam_balance {
let error = native_vp::Error::new_const(
"Some modified keys were not a native token's balance key",
)
.into();
tracing::debug!("{error}");
return Err(error);
}
Ok(())
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::env::temp_dir;
use namada_core::address::testing::{established_address_1, nam, wnam};
use namada_core::borsh::BorshSerializeExt;
use namada_core::ethereum_events;
use namada_core::ethereum_events::EthAddress;
use namada_gas::{TxGasMeter, VpGasMeter};
use namada_state::testing::TestState;
use namada_state::{StorageWrite, TxIndex};
use namada_trans_token::storage_key::{balance_key, minted_balance_key};
use namada_tx::data::TxType;
use namada_tx::{Tx, TxCommitments};
use namada_vm::wasm::run::VpEvalWasm;
use namada_vm::wasm::VpCache;
use namada_vm::WasmCacheRwAccess;
use rand::Rng;
use super::*;
use crate::storage::bridge_pool::BRIDGE_POOL_ADDRESS;
use crate::storage::parameters::{
Contracts, EthereumBridgeParams, UpgradeableContract,
};
use crate::storage::wrapped_erc20s;
const ARBITRARY_OWNER_A_ADDRESS: &str =
"tnam1qqwuj7aart6ackjfkk7486jwm2ufr4t7cq4535u4";
const ARBITRARY_OWNER_A_INITIAL_BALANCE: u64 = 100;
const ESCROW_AMOUNT: u64 = 100;
const BRIDGE_POOL_ESCROW_INITIAL_BALANCE: u64 = 0;
type CA = WasmCacheRwAccess;
type Eval = VpEvalWasm<
<TestState as StateRead>::D,
<TestState as StateRead>::H,
CA,
>;
type TokenKeys = namada_token::Store<()>;
type EthBridge<'a, S> =
super::EthBridge<'a, S, VpCache<CA>, Eval, TokenKeys>;
/// Return some arbitrary random key belonging to this account
fn arbitrary_key() -> Key {
let mut rng = rand::thread_rng();
let rn = rng.gen::<u64>();
storage::prefix()
.push(&format!("arbitrary key segment {}", rn))
.expect("should always be able to construct this key")
}
/// Initialize some dummy storage for testing
fn setup_storage() -> TestState {
let mut state = TestState::default();
// setup a user with a balance
let balance_key = balance_key(
&nam(),
&Address::decode(ARBITRARY_OWNER_A_ADDRESS).expect("Test failed"),
);
state
.write(
&balance_key,
Amount::from(ARBITRARY_OWNER_A_INITIAL_BALANCE),
)
.expect("Test failed");
// a dummy config for testing
let config = EthereumBridgeParams {
erc20_whitelist: vec![],
eth_start_height: Default::default(),
min_confirmations: Default::default(),
contracts: Contracts {
native_erc20: wnam(),
bridge: UpgradeableContract {
address: EthAddress([42; 20]),
version: Default::default(),
},
},
};
config.init_storage(&mut state);
state.commit_block().expect("Test failed");
state
}
/// Setup a ctx for running native vps
fn setup_ctx<'ctx>(
tx: &'ctx Tx,
cmt: &'ctx TxCommitments,
state: &'ctx TestState,
gas_meter: &'ctx RefCell<VpGasMeter>,
keys_changed: &'ctx BTreeSet<Key>,
verifiers: &'ctx BTreeSet<Address>,
) -> Ctx<'ctx, TestState, VpCache<WasmCacheRwAccess>, Eval> {
Ctx::new(
&crate::ADDRESS,
state,
tx,
cmt,
&TxIndex(0),
gas_meter,
keys_changed,
verifiers,
VpCache::new(temp_dir(), 100usize),
)
}
#[test]
fn test_accepts_expected_keys_changed() {
let keys_changed = BTreeSet::from([
balance_key(&nam(), &established_address_1()),
balance_key(&nam(), &crate::ADDRESS),
]);
let result = validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_ok());
}
#[test]
fn test_error_if_triggered_without_keys_changed() {
let keys_changed = BTreeSet::new();
let result = validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
#[test]
fn test_rejects_if_not_two_keys_changed() {
{
let keys_changed = BTreeSet::from_iter(vec![arbitrary_key(); 3]);
let result =
validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
{
let keys_changed = BTreeSet::from_iter(vec![
escrow_key(&nam()),
arbitrary_key(),
arbitrary_key(),
]);
let result =
validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
}
#[test]
fn test_rejects_if_not_two_multitoken_keys_changed() {
{
let keys_changed =
BTreeSet::from_iter(vec![arbitrary_key(), arbitrary_key()]);
let result =
validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
{
let keys_changed = BTreeSet::from_iter(vec![
arbitrary_key(),
minted_balance_key(&wrapped_erc20s::token(
ðereum_events::testing::DAI_ERC20_ETH_ADDRESS,
)),
]);
let result =
validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
{
let keys_changed = BTreeSet::from_iter(vec![
arbitrary_key(),
balance_key(
&wrapped_erc20s::token(
ðereum_events::testing::DAI_ERC20_ETH_ADDRESS,
),
&Address::decode(ARBITRARY_OWNER_A_ADDRESS)
.expect("Couldn't set up test"),
),
]);
let result =
validate_changed_keys::<TokenKeys>(&nam(), &keys_changed);
assert!(result.is_err());
}
}
/// Test that escrowing Nam is accepted.
#[test]
fn test_escrow_nam_accepted() {
let mut state = setup_storage();
// debit the user's balance
let account_key = balance_key(
&nam(),
&Address::decode(ARBITRARY_OWNER_A_ADDRESS).expect("Test failed"),
);
state
.write_log_mut()
.write(
&account_key,
Amount::from(ARBITRARY_OWNER_A_INITIAL_BALANCE - ESCROW_AMOUNT)
.serialize_to_vec(),
)
.expect("Test failed");
// credit the balance to the escrow
let escrow_key = balance_key(&nam(), &crate::ADDRESS);
state
.write_log_mut()
.write(
&escrow_key,
Amount::from(
BRIDGE_POOL_ESCROW_INITIAL_BALANCE + ESCROW_AMOUNT,
)
.serialize_to_vec(),
)
.expect("Test failed");
let keys_changed = BTreeSet::from([account_key, escrow_key]);
let verifiers = BTreeSet::from([BRIDGE_POOL_ADDRESS]);
// set up the VP
let mut tx = Tx::from_type(TxType::Raw);
tx.push_default_inner_tx();
let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
&TxGasMeter::new(u64::MAX),
));
let batched_tx = tx.batch_ref_first_tx().unwrap();
let vp = EthBridge::new(setup_ctx(
batched_tx.tx,
batched_tx.cmt,
&state,
&gas_meter,
&keys_changed,
&verifiers,
));
let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers);
assert!(res.is_ok());
}
/// Test that escrowing must increase the balance
#[test]
fn test_escrowed_nam_must_increase() {
let mut state = setup_storage();
// debit the user's balance
let account_key = balance_key(
&nam(),
&Address::decode(ARBITRARY_OWNER_A_ADDRESS).expect("Test failed"),
);
state
.write_log_mut()
.write(
&account_key,
Amount::from(ARBITRARY_OWNER_A_INITIAL_BALANCE - ESCROW_AMOUNT)
.serialize_to_vec(),
)
.expect("Test failed");
// do not credit the balance to the escrow
let escrow_key = balance_key(&nam(), &crate::ADDRESS);
state
.write_log_mut()
.write(
&escrow_key,
Amount::from(BRIDGE_POOL_ESCROW_INITIAL_BALANCE)
.serialize_to_vec(),
)
.expect("Test failed");
let keys_changed = BTreeSet::from([account_key, escrow_key]);
let verifiers = BTreeSet::from([BRIDGE_POOL_ADDRESS]);
// set up the VP
let mut tx = Tx::from_type(TxType::Raw);
tx.push_default_inner_tx();
let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
&TxGasMeter::new(u64::MAX),
));
let batched_tx = tx.batch_ref_first_tx().unwrap();
let vp = EthBridge::new(setup_ctx(
batched_tx.tx,
batched_tx.cmt,
&state,
&gas_meter,
&keys_changed,
&verifiers,
));
let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers);
assert!(res.is_err());
}
/// Test that the VP checks that the bridge pool vp will
/// be triggered if escrowing occurs.
#[test]
fn test_escrowing_must_trigger_bridge_pool_vp() {
let mut state = setup_storage();
// debit the user's balance
let account_key = balance_key(
&nam(),
&Address::decode(ARBITRARY_OWNER_A_ADDRESS).expect("Test failed"),
);
state
.write_log_mut()
.write(
&account_key,
Amount::from(ARBITRARY_OWNER_A_INITIAL_BALANCE - ESCROW_AMOUNT)
.serialize_to_vec(),
)
.expect("Test failed");
// credit the balance to the escrow
let escrow_key = balance_key(&nam(), &crate::ADDRESS);
state
.write_log_mut()
.write(
&escrow_key,
Amount::from(
BRIDGE_POOL_ESCROW_INITIAL_BALANCE + ESCROW_AMOUNT,
)
.serialize_to_vec(),
)
.expect("Test failed");
let keys_changed = BTreeSet::from([account_key, escrow_key]);
let verifiers = BTreeSet::from([]);
// set up the VP
let mut tx = Tx::from_type(TxType::Raw);
tx.push_default_inner_tx();
let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
&TxGasMeter::new(u64::MAX),
));
let batched_tx = tx.batch_ref_first_tx().unwrap();
let vp = EthBridge::new(setup_ctx(
batched_tx.tx,
batched_tx.cmt,
&state,
&gas_meter,
&keys_changed,
&verifiers,
));
let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers);
assert!(res.is_err());
}
}