Skip to content

Commit

Permalink
triple_masking (#878)
Browse files Browse the repository at this point in the history
* ledger add noah

* components add noah

* fix lint

* update keypair

* fix lint

* remove is_address_fra

* fix xfrboxy

* signature.verify

* update enable_triple_masking_height to
enable_ed25519_triple_masking_height

* fix OwnerMemo

* fix test

* update noah version

* fix anon transfer batch

---------

Co-authored-by: shaorongqiang <shaorongqiang@aliyun.com>
  • Loading branch information
shaorongqiang and shiran555 committed Apr 22, 2023
1 parent 536ac42 commit 4a83430
Show file tree
Hide file tree
Showing 102 changed files with 7,963 additions and 1,319 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ 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" }
ed25519-dalek = { git = "https://github.com/FindoraNetwork/ed25519-dalek", tag = "v1.0.1-f" }
15 changes: 9 additions & 6 deletions src/components/abciapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ path = "src/bins/abcid.rs"

[dependencies]
parking_lot = "0.12"
base64 = "0.12"
base64 = "0.13"
bincode = "1.3.1"
tracing = "0.1"
rand = "0.8"
rand_chacha = "0.2"
rand_core = { version = "0.5", default-features = false, features = ["alloc"] }
rand_chacha = "0.3"
rand_core = { version = "0.6", default-features = false, features = ["alloc"] }
attohttpc = { version = "0.23", default-features = false, features = ["compress", "json", "tls-rustls"] }
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.40"
Expand All @@ -41,14 +41,16 @@ percent-encoding = "2.1.0"

nix = "0.22.1"

zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
noah = { git = "https://github.com/FindoraNetwork/noah", tag = "v0.4.3" }
noah-algebra = { git = "https://github.com/FindoraNetwork/noah", tag = "v0.4.3" }
ruc = { version = "1.0.5", default-features = false, features = ["compact"] }
abci = { git = "https://github.com/FindoraNetwork/tendermint-abci", tag = "0.7.6" }
config = { path = "../config"}
ledger = { path = "../../ledger" }
zei = { package="platform-lib-noah", git = "https://github.com/FindoraNetwork/platform-lib-noah", branch = "main" }

globutils = { git = "https://github.com/FindoraNetwork/platform-lib-utils", branch = "main" }
cryptohash = { git = "https://github.com/FindoraNetwork/platform-lib-cryptohash", branch = "main" }
globutils = { git = "https://github.com/shaorongqiang/platform-lib-utils", branch = "main" }
cryptohash = { git = "https://github.com/shaorongqiang/platform-lib-cryptohash", branch = "main" }

finutils = { path = "../finutils" }

Expand All @@ -61,6 +63,7 @@ fp-types = {path = "../contracts/primitives/types"}

enterprise-web3 = { path = "../contracts/primitives/enterprise-web3" }
module-evm = { path = "../contracts/modules/evm"}
ethereum-types = { version = "0.13.1", default-features = false }

[target.'cfg(target_os= "linux")'.dependencies]
btm = "0.1.6"
Expand Down
3 changes: 2 additions & 1 deletion src/components/abciapp/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
crate::api::{
query_server::query_api, submission_server::submission_api::SubmissionApi,
},
baseapp::tm_events::init_url,
config::abci::{global_cfg::CFG, ABCIConfig},
futures::executor::ThreadPool,
lazy_static::lazy_static,
Expand Down Expand Up @@ -88,7 +89,7 @@ pub fn run() -> Result<()> {
"http://{}:{}",
config.tendermint_host, config.tendermint_port
);

pnk!(init_url(tendermint_rpc.as_str()));
// keep them running in the background,
// avoid being dropped by the jsonrpc crate.
mem::forget(fc_rpc::start_web3_service(
Expand Down
128 changes: 112 additions & 16 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ use {
lazy_static::lazy_static,
ledger::{
converter::is_convert_account,
data_model::Operation,
data_model::{Operation, Transaction},
fbnc::{new_mapx, Mapx},
staking::KEEP_HIST,
store::{
api_cache,
fbnc::{new_mapx, Mapx},
},
store::api_cache,
},
parking_lot::{Mutex, RwLock},
protobuf::RepeatedField,
Expand Down Expand Up @@ -82,23 +80,29 @@ pub fn info(s: &mut ABCISubmissionServer, req: &RequestInfo) -> ResponseInfo {
let commitment = state.get_state_commitment();
let la_hash = commitment.0.as_ref().to_vec();

let h = state.get_tendermint_height() as i64;
TENDERMINT_BLOCK_HEIGHT.swap(h, Ordering::Relaxed);
resp.set_last_block_height(h);
if 0 < h {
if CFG.checkpoint.disable_evm_block_height < h
&& h < CFG.checkpoint.enable_frc20_height
let td_height = state.get_tendermint_height() as i64;
TENDERMINT_BLOCK_HEIGHT.swap(td_height, Ordering::Relaxed);
resp.set_last_block_height(td_height);
if 0 < td_height {
if CFG.checkpoint.disable_evm_block_height < td_height
&& td_height < CFG.checkpoint.enable_frc20_height
{
resp.set_last_block_app_hash(la_hash);
} else if td_height < CFG.checkpoint.enable_ed25519_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", td_height, la_hash, cs_hash));
} else {
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));
let tm_hash = state.get_anon_state_commitment().0;
resp.set_last_block_app_hash(app_hash_v2(
"info", td_height, la_hash, cs_hash, tm_hash,
));
}
}

drop(state);

info!(target: "abciapp", "======== Last committed height: {} ========", h);
info!(target: "abciapp", "======== Last committed height: {} ========", td_height);

if la.all_commited() {
la.begin_block();
Expand Down Expand Up @@ -130,6 +134,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();
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 @@ -161,6 +177,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_ed25519_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 @@ -204,7 +226,7 @@ pub fn begin_block(
#[cfg(target_os = "linux")]
{
// snapshot the last block
ledger::store::fbnc::flush_data();
ledger::fbnc::flush_data();
let last_height = TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed);
info_omit!(CFG.btmcfg.snapshot(last_height as u64));
}
Expand Down Expand Up @@ -262,6 +284,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();
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 @@ -326,7 +360,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 @@ -365,6 +399,17 @@ pub fn deliver_tx(
.db
.write()
.discard_session();
} else if is_tm_transaction(&tx)
&& td_height
< CFG.checkpoint.enable_ed25519_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 @@ -467,6 +512,12 @@ pub fn end_block(

let mut la = s.la.write();

if td_height <= CFG.checkpoint.disable_evm_block_height
|| td_height >= CFG.checkpoint.enable_frc20_height
{
let _ = s.account_base_app.write().end_block(req);
}

// mint coinbase, cache system transactions to ledger
{
let laa = la.get_committed_state().read();
Expand Down Expand Up @@ -531,8 +582,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_ed25519_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 @@ -668,3 +722,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);
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()
}
64 changes: 62 additions & 2 deletions src/components/abciapp/src/abci/server/callback/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use {
abci::{Event, Pair},
ledger::data_model::{Operation, Transaction, TxnSID},
noah::xfr::structs::{XfrAmount, XfrAssetType},
protobuf::RepeatedField,
serde::Serialize,
std::time::SystemTime,
zei::xfr::structs::{XfrAmount, XfrAssetType},
};

/// generate attr(tags) for index-ops of tendermint
Expand Down 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
Loading

0 comments on commit 4a83430

Please sign in to comment.