Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faucet PoW #961

Closed
wants to merge 45 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
603c562
test in apps/ledger/storage: test prefix iterators
tzemanovic Dec 16, 2022
f78ee60
storage: remove rev_iter_prefix, which doesn't work as expected
tzemanovic Dec 16, 2022
0d4688d
wasm_for_tests: make
tzemanovic Dec 16, 2022
c25a852
changelog: add #912
tzemanovic Dec 16, 2022
65339b1
[ci] wasm checksums update
github-actions[bot] Dec 17, 2022
5d9f3d7
Add StorageKeys derive macro
sug0 Dec 19, 2022
4ad37a1
Fix gen of identifier in StorageKeys proc-macro
sug0 Dec 19, 2022
028924e
Fix static str type check
sug0 Dec 19, 2022
6612ed8
Update macros/src/lib.rs
sug0 Dec 20, 2022
63a4fea
Update macros/src/lib.rs
sug0 Dec 20, 2022
9ac07db
Added unit test for the new proc macro
batconjurer Dec 20, 2022
306829f
Format and inline some code
sug0 Dec 20, 2022
189e9b6
Some TODO items
sug0 Dec 20, 2022
a74d8e3
Use quote!() in macro test
sug0 Dec 20, 2022
fe53976
Add StorageKeys unit tests
sug0 Dec 20, 2022
32d4f49
Add unit tests for tuple and unit structs as well as enums, on Storag…
sug0 Dec 20, 2022
05eb133
Update macros/src/lib.rs
sug0 Dec 20, 2022
56599b1
Remove allow dead code TODO
sug0 Dec 20, 2022
ad0d2bc
Strip out #[allow(dead_code)] from VALUES
sug0 Dec 20, 2022
73c686d
wasm: update checksums.json
juped Dec 21, 2022
51844e6
core/storage/mockDB: remove unused `reverse_order` flag
tzemanovic Dec 23, 2022
5f219bf
Adds fee burning and checks
grarco Dec 23, 2022
0b51129
Fixes fee value in tx contruction
grarco Dec 23, 2022
b934636
Fixes process proposal fee token
grarco Dec 23, 2022
0081be6
Fixes unit tests
grarco Dec 23, 2022
db0cac3
changelog: add #962
grarco Dec 27, 2022
c20b80f
core/storage_api: use GATs to hide lifetime in StorageRead trait
tzemanovic Dec 27, 2022
c40fd1b
changelog: #966
tzemanovic Dec 27, 2022
dd2d504
core/storage: remove redundant `StorageWrite` impl for mut ref
tzemanovic Dec 27, 2022
c6564c2
[ci] wasm checksums update
github-actions[bot] Dec 27, 2022
842bcd5
Merge branch 'origin/tomas/gats-lifetimes-refactor' (#966)
tzemanovic Dec 28, 2022
02c41e8
Merge branch 'origin/grarco/basic-fee' (#962)
tzemanovic Dec 28, 2022
f7b6378
re-export `namada` from `namada_tests` for `namada_core` dev-deps
tzemanovic Dec 28, 2022
3691e52
core: add faucet PoW logic
tzemanovic Dec 23, 2022
abf5b8e
vp_testnet_faucet: integrate faucet PoW check
tzemanovic Dec 26, 2022
42f6b12
ledger/init_chain: init testnet faucet storage when used
tzemanovic Dec 26, 2022
4354285
wasm: add tx_testnet_faucet_withdrawal
tzemanovic Dec 26, 2022
615a245
storage_api/lazy_map: export `get_data_key` fn for client
tzemanovic Dec 26, 2022
094e63e
ledger/queries/vp/client: add method to get faucet PoW challenge
tzemanovic Dec 26, 2022
a0ba265
client: get a PoW challenge solution for faucet withdrawal transfer
tzemanovic Dec 26, 2022
e75ec2d
genesis: add `faucet_pow_difficulty`
tzemanovic Dec 26, 2022
f796169
test/e2e: add a case to withdraw from PoW faucet
tzemanovic Dec 26, 2022
0d521e0
changelog: add #961
tzemanovic Dec 26, 2022
cd8b45e
wasm/vp_testnet_faucet/test: update tests for PoW challenge
tzemanovic Dec 27, 2022
c7c2b7d
vp_testnet_faucet: R/W withdrawal limit from storage, add to genesis
tzemanovic Dec 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
vp_testnet_faucet: integrate faucet PoW check
tzemanovic committed Dec 28, 2022
commit abf5b8e11f53313ea44df0388a411b3d5d00ac74
101 changes: 96 additions & 5 deletions wasm/wasm_source/src/vp_testnet_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//!
//! Any other storage key changes are allowed only with a valid signature.
use namada_vp_prelude::{SignedTxData, *};
use namada_vp_prelude::*;
use once_cell::unsync::Lazy;

/// Allows anyone to withdraw up to 1_000 tokens in a single tx
Expand Down Expand Up @@ -46,10 +46,26 @@ fn validate_tx(
_ => false,
});

let solution = Lazy::new(|| match &*signed_tx_data {
Ok(signed) => {
if let Some(data) = &signed.data.as_ref() {
faucet_pow::Solution::try_from_slice(data).ok()
} else {
None
}
}
_ => None,
});

if !is_valid_tx(ctx, &tx_data)? {
return reject();
}

// An address whose counter has been incremented
let mut incremented_counter: Option<Address> = None;
// An address other than faucet that received tokens
let mut withdrawal: Option<Address> = None;

for key in keys_changed.iter() {
let is_valid = if let Some(owner) = token::is_any_token_balance_key(key)
{
Expand All @@ -58,11 +74,69 @@ fn validate_tx(
let post: token::Amount =
ctx.read_post(key)?.unwrap_or_default();
let change = post.change() - pre.change();
// Debit over `MAX_FREE_DEBIT` has to signed, credit doesn't
change >= -MAX_FREE_DEBIT || change >= 0 || *valid_sig

if change < 0 {
// If there's a PoW solution
match &*solution {
Some(solution) => {
// Validate it
if solution.validate(ctx, &addr)? {
change >= -MAX_FREE_DEBIT
} else {
debug_log!("Invalid PoW solution");
false
}
}
_ => {
debug_log!(
"No PoW solution, signature is required"
);
// Debit without a solution has to signed
*valid_sig
}
}
} else {
// credit is permissive
true
}
} else {
// If this is not the owner, allow any change
// An address other than self whose balance has changed
let pre: token::Amount = ctx.read_pre(key)?.unwrap_or_default();
let post: token::Amount =
ctx.read_post(key)?.unwrap_or_default();
let change = post.change() - pre.change();

if change < 0 {
// Debit
true
} else {
// Credit
if withdrawal.as_ref().is_none() {
// Store the withdrawal target
withdrawal = Some(owner.clone());
// Allow to withdraw
true
} else {
// Only one withdrawal at the time
false
}
}
}
} else if let Some(owner) = faucet_pow::is_counter_key(key, &addr) {
let counter_pre =
faucet_pow::get_counter(&ctx.pre(), &addr, owner)?;
let counter_post =
faucet_pow::get_counter(&ctx.post(), &addr, owner)?;
// Counters can only increment
if counter_pre + 1 == counter_post &&
// Only one counter update too
incremented_counter.as_ref().is_none()
{
// Store the owner of incremented counter
incremented_counter = Some(owner.clone());
true
} else {
false
}
} else if let Some(owner) = key.is_validity_predicate() {
let has_post: bool = ctx.has_key_post(key)?;
Expand All @@ -81,12 +155,29 @@ fn validate_tx(
// Allow any other key change if authorized by a signature
*valid_sig
};

if !is_valid {
debug_log!("key {} modification failed vp", key);
return reject();
}
}
accept()

let is_valid = match (withdrawal, incremented_counter) {
(Some(withdrawal), Some(incremented_counter)) => {
// A withdrawal target must have incremented counter
withdrawal == incremented_counter
}
(None, None) => {
// No withdrawal without counter change is fine
true
}
_ => {
// Missing withdrawal with a counter update or other way
// round
false
}
};
if is_valid { accept() } else { reject() }
}

#[cfg(test)]
Expand Down