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

Add triple masking #1004

Merged
merged 31 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
efbafc5
triple_masking
harshadptl Mar 24, 2023
3c4e647
removed some non Triple masking related code
harshadptl Aug 13, 2023
36705df
rename checkpoint config var
harshadptl Aug 17, 2023
b31b542
fix derived asset code API
harshadptl Aug 18, 2023
4ba2bd4
trigger tests
harshadptl Aug 19, 2023
298504c
trigger
harshadptl Aug 19, 2023
490f710
Update src/components/abciapp/src/abci/server/callback/mod.rs
weikengchen Aug 20, 2023
16d67c7
Update src/components/contracts/modules/evm/precompile/src/lib.rs
weikengchen Aug 20, 2023
daf348b
Merge branch 'develop' into feat-triple-masking-v5
weikengchen Aug 20, 2023
c862e3a
Update lib.rs
weikengchen Aug 20, 2023
d894690
Update Cargo.toml
weikengchen Aug 20, 2023
2a3f0ec
Apply suggestions from code review
weikengchen Aug 20, 2023
cfd94f9
minor fix in fn.yml
weikengchen Aug 21, 2023
2e51d51
genkey
weikengchen Aug 21, 2023
cf7915f
stake append
weikengchen Aug 21, 2023
e19f640
staker update
weikengchen Aug 21, 2023
5b216c3
unstake and claim
weikengchen Aug 21, 2023
0c5129c
delegate, undelegate
weikengchen Aug 21, 2023
643195c
transfer and transfer_batch
weikengchen Aug 21, 2023
34d5af5
wallet/create, wallet/show
weikengchen Aug 21, 2023
442ff45
asset
weikengchen Aug 21, 2023
7e193d8
other edits on fn
weikengchen Aug 21, 2023
5840bb0
fmt
weikengchen Aug 21, 2023
e104fe5
undo bash script change
harshadptl Aug 21, 2023
34239e7
Update src/components/abciapp/src/api/query_server/query_api/ledger_a…
weikengchen Aug 22, 2023
17672b6
Update src/components/abciapp/src/api/query_server/query_api/server.rs
weikengchen Aug 22, 2023
036af5d
Update src/components/abciapp/src/api/query_server/query_api/mod.rs
weikengchen Aug 22, 2023
70d2575
Apply suggestions from code review
weikengchen Aug 22, 2023
eb01f90
Update src/components/finutils/src/bins/fn.rs
weikengchen Aug 22, 2023
34be947
additional consistency test for the asset code is not necessary
weikengchen Aug 22, 2023
3df9c2b
change apphash calculation
weikengchen Aug 22, 2023
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
30 changes: 21 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,34 @@ members = [
]

[profile.dev]
incremental = false
opt-level = 3
lto = "thin"
incremental = true
debug-assertions = true
debug = true
panic = 'abort'
overflow-checks = true

[profile.release]
opt-level = 3
lto = "thin"
incremental = false
overflow-checks = true
panic = 'abort'

[profile.bench]
opt-level = 3
debug = false
rpath = false
lto = "thin"
codegen-units = 1
incremental = true
debug-assertions = false
overflow-checks = false

[profile.dev.package.curve25519-dalek]
opt-level = 1
overflow-checks = false

[patch.crates-io]
ed25519-dalek = { git = "https://github.com/FindoraNetwork/ed25519-dalek", rev = "ad461f" }
curve25519-dalek = { git = "https://github.com/FindoraNetwork/curve25519-dalek", rev = "a2df65" }
x25519-dalek = { git = "https://github.com/FindoraNetwork/x25519-dalek", rev = "53bb1a" }
[profile.test]
opt-level = 2
lto = "off"
incremental = true
debug-assertions = true
debug = true
1 change: 0 additions & 1 deletion src/components/abciapp/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub fn run() -> Result<()> {
"http://{}:{}",
config.tendermint_host, config.tendermint_port
);

// keep them running in the background,
// avoid being dropped by the jsonrpc crate.
mem::forget(fc_rpc::start_web3_service(
Expand Down
114 changes: 102 additions & 12 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
//! # Impl function of tendermint ABCI
//!

use globutils::wallet;
use ledger::{
data_model::ASSET_TYPE_FRA,
staking::{FF_ADDR_EXTRA_120_0000, FF_ADDR_LIST},
};
use zei::noah_api::xfr::asset_record::AssetRecordType;

mod utils;

use {
Expand All @@ -32,11 +25,15 @@ use {
STATE_UPDATE_LIST, TXS, WEB3_SERVICE_START_HEIGHT,
},
fp_storage::hash::{Sha256, StorageHasher},
globutils::wallet,
lazy_static::lazy_static,
ledger::{
converter::is_convert_account,
data_model::Operation,
staking::{evm::EVM_STAKING, KEEP_HIST, VALIDATOR_UPDATE_BLOCK_ITV},
data_model::{Operation, Transaction, ASSET_TYPE_FRA},
staking::{
evm::EVM_STAKING, FF_ADDR_EXTRA_120_0000, FF_ADDR_LIST, KEEP_HIST,
VALIDATOR_UPDATE_BLOCK_ITV,
},
store::{
api_cache,
fbnc::{new_mapx, Mapx},
Expand All @@ -56,6 +53,7 @@ use {
},
},
tracing::{error, info},
zei::noah_api::xfr::asset_record::AssetRecordType,
};

pub(crate) static TENDERMINT_BLOCK_HEIGHT: AtomicI64 = AtomicI64::new(0);
Expand Down Expand Up @@ -100,9 +98,15 @@ pub fn info(s: &mut ABCISubmissionServer, req: &RequestInfo) -> ResponseInfo {
&& h < CFG.checkpoint.enable_frc20_height
{
resp.set_last_block_app_hash(la_hash);
} else {
} else if h < CFG.checkpoint.enable_triple_masking_height {
let cs_hash = s.account_base_app.write().info(req).last_block_app_hash;
resp.set_last_block_app_hash(app_hash("info", h, la_hash, cs_hash));
} else {
let cs_hash = s.account_base_app.write().info(req).last_block_app_hash;
let tm_hash = state.get_anon_state_commitment().0;
resp.set_last_block_app_hash(app_hash_v2(
"info", h, la_hash, cs_hash, tm_hash,
));
}
}

Expand Down Expand Up @@ -140,6 +144,18 @@ pub fn check_tx(s: &mut ABCISubmissionServer, req: &RequestCheckTx) -> ResponseC
TxCatalog::FindoraTx => {
if matches!(req.field_type, CheckTxType::New) {
if let Ok(tx) = convert_tx(req.get_tx()) {
for op in tx.body.operations.iter() {
if let Operation::TransferAnonAsset(op) = op {
let mut inputs = op.note.body.inputs.clone();
inputs.sort();
inputs.dedup();
weikengchen marked this conversation as resolved.
Show resolved Hide resolved
if inputs.len() != op.note.body.inputs.len() {
resp.log = "anon transfer input error".to_owned();
resp.code = 1;
return resp;
}
}
}
if td_height > CFG.checkpoint.check_signatures_num {
for op in tx.body.operations.iter() {
if let Operation::TransferAsset(op) = op {
Expand Down Expand Up @@ -171,6 +187,12 @@ pub fn check_tx(s: &mut ABCISubmissionServer, req: &RequestCheckTx) -> ResponseC
} else if TX_HISTORY.read().contains_key(&tx.hash_tm_rawbytes()) {
resp.log = "Historical transaction".to_owned();
resp.code = 1;
} else if is_tm_transaction(&tx)
&& td_height
< CFG.checkpoint.enable_triple_masking_height
{
resp.code = 1;
resp.log = "Triple Masking is disabled".to_owned();
}
} else {
resp.log = "Invalid format".to_owned();
Expand Down Expand Up @@ -272,6 +294,18 @@ pub fn deliver_tx(
match tx_catalog {
TxCatalog::FindoraTx => {
if let Ok(tx) = convert_tx(req.get_tx()) {
for op in tx.body.operations.iter() {
if let Operation::TransferAnonAsset(op) = op {
let mut inputs = op.note.body.inputs.clone();
inputs.sort();
inputs.dedup();
weikengchen marked this conversation as resolved.
Show resolved Hide resolved
if inputs.len() != op.note.body.inputs.len() {
resp.log = "anon Transfer input error".to_owned();
resp.code = 1;
return resp;
}
}
}
if td_height > CFG.checkpoint.check_signatures_num {
for op in tx.body.operations.iter() {
if let Operation::TransferAsset(op) = op {
Expand Down Expand Up @@ -336,7 +370,7 @@ pub fn deliver_tx(
if let Err(err) =
s.account_base_app.write().deliver_findora_tx(&tx, &hash.0)
{
info!(target: "abciapp", "deliver convert account tx failed: {err:?}");
error!(target: "abciapp", "deliver convert account tx failed: {err:?}");

resp.code = 1;
resp.log =
Expand Down Expand Up @@ -375,6 +409,17 @@ pub fn deliver_tx(
.db
.write()
.discard_session();
} else if is_tm_transaction(&tx)
&& td_height
< CFG.checkpoint.enable_triple_masking_height
{
info!(target: "abciapp",
"Triple Masking transaction(FindoraTx) detected at early height {}: {:?}",
td_height, tx
);
resp.code = 2;
resp.log = "Triple Masking is disabled".to_owned();
return resp;
} else if CFG.checkpoint.utxo_checktx_height < td_height {
match tx.check_tx() {
Ok(_) => {
Expand Down Expand Up @@ -597,8 +642,11 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm
&& td_height < CFG.checkpoint.enable_frc20_height
{
r.set_data(la_hash);
} else {
} else if td_height < CFG.checkpoint.enable_triple_masking_height {
r.set_data(app_hash("commit", td_height, la_hash, cs_hash));
} else {
let tm_hash = state.get_anon_state_commitment().0;
r.set_data(app_hash_v2("commit", td_height, la_hash, cs_hash, tm_hash));
}

IN_SAFE_ITV.store(false, Ordering::Release);
Expand Down Expand Up @@ -728,3 +776,45 @@ fn app_hash(
la_hash
}
}

/// Combines ledger state hash and EVM chain state hash
/// and print app hashes for debugging
fn app_hash_v2(
when: &str,
height: i64,
mut la_hash: Vec<u8>,
mut cs_hash: Vec<u8>,
mut tm_hash: Vec<u8>,
) -> Vec<u8> {
info!(target: "abciapp",
"app_hash_{}: {}_{}_{}, height: {}",
when,
hex::encode(la_hash.clone()),
hex::encode(cs_hash.clone()),
hex::encode(tm_hash.clone()),
height
);

// append ONLY non-empty EVM chain state hash
if !tm_hash.is_empty() || !cs_hash.is_empty() {
la_hash.append(&mut cs_hash);
weikengchen marked this conversation as resolved.
Show resolved Hide resolved
la_hash.append(&mut tm_hash);

Sha256::hash(la_hash.as_slice()).to_vec()
} else {
la_hash
}
}

fn is_tm_transaction(tx: &Transaction) -> bool {
tx.body
.operations
.iter()
.try_for_each(|op| match op {
Operation::BarToAbar(_a) => None,
Operation::AbarToBar(_a) => None,
Operation::TransferAnonAsset(_a) => None,
_ => Some(()),
})
.is_none()
}
62 changes: 61 additions & 1 deletion src/components/abciapp/src/abci/server/callback/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ pub fn gen_tendermint_attr(tx: &Transaction) -> RepeatedField<Event> {
res.push(ev);

let (from, to) = gen_tendermint_attr_addr(tx);
let (nullifiers, commitments) = gen_tendermint_attr_anon(tx);

if !from.is_empty() || !to.is_empty() {
if !from.is_empty()
|| !to.is_empty()
|| !nullifiers.is_empty()
|| !commitments.is_empty()
{
let mut ev = Event::new();
ev.set_field_type("addr".to_owned());

Expand Down Expand Up @@ -76,6 +81,8 @@ pub fn gen_tendermint_attr(tx: &Transaction) -> RepeatedField<Event> {

index_addr!(from, "addr.from");
index_addr!(to, "addr.to");
index_addr!(nullifiers, "nullifier.used");
index_addr!(commitments, "commitment.created");
}

RepeatedField::from_vec(res)
Expand Down Expand Up @@ -126,6 +133,59 @@ fn gen_tendermint_attr_addr(tx: &Transaction) -> (Vec<TagAttr>, Vec<TagAttr>) {
Operation::UpdateMemo(d) => {
append_attr!(d);
}
Operation::BarToAbar(d) => {
let mut attr = TagAttr::default();
attr.addr = globutils::wallet::public_key_to_bech32(
&d.input_record().public_key,
);
base.0.push(attr);
}
Operation::AbarToBar(d) => {
let mut attr = TagAttr::default();
attr.addr = globutils::wallet::public_key_to_bech32(
&d.note.get_public_key(),
);
base.1.push(attr);
}
_ => {}
}

base
})
}

fn gen_tendermint_attr_anon(tx: &Transaction) -> (Vec<TagAttr>, Vec<TagAttr>) {
tx.body
.operations
.iter()
.fold((vec![], vec![]), |mut base, op| {
match op {
Operation::BarToAbar(d) => {
let mut attr = TagAttr::default();
attr.addr = globutils::wallet::commitment_to_base58(
&d.output_record().commitment,
);
base.1.push(attr);
}
Operation::AbarToBar(d) => {
let mut attr = TagAttr::default();
attr.addr =
globutils::wallet::nullifier_to_base58(&d.note.get_input());
base.0.push(attr);
}
Operation::TransferAnonAsset(d) => {
for ix in &d.note.body.inputs {
let mut attr = TagAttr::default();
attr.addr = globutils::wallet::nullifier_to_base58(ix);
base.0.push(attr);
}
for ox in &d.note.body.outputs {
let mut attr = TagAttr::default();
attr.addr =
globutils::wallet::commitment_to_base58(&ox.commitment);
base.1.push(attr);
}
}
_ => {}
}

Expand Down
16 changes: 8 additions & 8 deletions src/components/abciapp/src/abci/staking/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use {
asset_record::{open_blind_asset_record, AssetRecordType},
structs::{AssetRecordTemplate, XfrAmount},
},
{XfrKeyPair, XfrPublicKey},
XfrKeyPair, XfrPublicKey,
},
};

Expand Down Expand Up @@ -126,12 +126,12 @@ fn gen_transfer_tx(
&owner_memo.map(|o| o.into_noah()),
&owner_kp.into_noah(),
)
.c(d!())
.and_then(|ob| {
trans_builder
.add_input(TxoRef::Absolute(sid), ob, None, None, i_am)
.c(d!())
})?;
.c(d!())
.and_then(|ob| {
trans_builder
.add_input(TxoRef::Absolute(sid), ob, None, None, i_am)
.c(d!())
})?;

alt!(0 == am, break);
}
Expand Down Expand Up @@ -166,5 +166,5 @@ fn gen_transfer_tx(
.c(d!())?;

tx_builder.add_operation(op);
Ok(tx_builder.take_transaction())
tx_builder.build_and_take_transaction()
}
Loading