Skip to content

Commit

Permalink
Anemoi upgrade and integration of platform-lib-noah (#1003)
Browse files Browse the repository at this point in the history
* Anemoi upgrade and integration of platform-lib-noah

* fix lint

* fix unit test failure

* Update Cargo.toml

* trigger Github action

* trigger github action

* trigger

* fix query server API

* fix derived asset code api

---------

Co-authored-by: Weikeng Chen <w.k@berkeley.edu>
  • Loading branch information
harshadptl and weikengchen authored Aug 18, 2023
1 parent 8852e77 commit 2e6a254
Show file tree
Hide file tree
Showing 95 changed files with 983 additions and 781 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ The primary license for Platform is the Business Source License 1.1 (`BUSL-1.1`)
### Exceptions

- All files in `components/contracts` are licensed under `Apache-2.0`


15 changes: 7 additions & 8 deletions src/components/abciapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ 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"
lazy_static = "1.4.0"
futures = { version = "0.3.16", features = ["thread-pool"] }
hex = "0.4.2"
hex = "0.4.3"
ctrlc = { version = "=3.2.5", features = ["termination"] }
protobuf = "2.16"
toml = "0.5.8"
Expand All @@ -41,15 +41,14 @@ percent-encoding = "2.1.0"

nix = "0.22.1"

zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
zei = { package="platform-lib-noah", git = "https://github.com/FindoraNetwork/platform-lib-noah", branch = "develop" }
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" }

globutils = { git = "https://github.com/FindoraNetwork/platform-lib-utils", tag = "v1.0.0" }
cryptohash = { git = "https://github.com/FindoraNetwork/platform-lib-cryptohash", tag = "v1.0.0" }

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

tempfile = "3.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ledger::{
data_model::ASSET_TYPE_FRA,
staking::{FF_ADDR_EXTRA_120_0000, FF_ADDR_LIST},
};
use zei::xfr::asset_record::AssetRecordType;
use zei::noah_api::xfr::asset_record::AssetRecordType;

mod utils;

Expand Down
2 changes: 1 addition & 1 deletion src/components/abciapp/src/abci/server/callback/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
protobuf::RepeatedField,
serde::Serialize,
std::time::SystemTime,
zei::xfr::structs::{XfrAmount, XfrAssetType},
zei::noah_api::xfr::structs::{XfrAmount, XfrAssetType},
};

/// generate attr(tags) for index-ops of tendermint
Expand Down
5 changes: 3 additions & 2 deletions src/components/abciapp/src/abci/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use {
ops::{Deref, DerefMut},
sync::atomic::Ordering,
},
zei::XfrPublicKey,
};

// The top 50~ candidate validators
Expand Down Expand Up @@ -365,7 +366,7 @@ pub fn system_prism_mint_pay(
let atc = AssetTypeCode { val: mint.asset };
let at = if let Some(mut at) = la.get_asset_type(&atc) {
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
key: XfrPublicKey::from_noah(&BLACK_HOLE_PUBKEY_STAKING),
};
if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
Expand All @@ -375,7 +376,7 @@ pub fn system_prism_mint_pay(
} else {
let mut at = AssetType::default();
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
key: XfrPublicKey::from_noah(&BLACK_HOLE_PUBKEY_STAKING),
};

if mint.max_supply != 0 {
Expand Down
23 changes: 15 additions & 8 deletions src/components/abciapp/src/abci/staking/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ use {
rand_chacha::ChaChaRng,
rand_core::SeedableRng,
ruc::*,
zei::xfr::{
asset_record::{open_blind_asset_record, AssetRecordType},
sig::{XfrKeyPair, XfrPublicKey},
structs::{AssetRecordTemplate, XfrAmount},
zei::{
noah_api::xfr::{
asset_record::{open_blind_asset_record, AssetRecordType},
structs::{AssetRecordTemplate, XfrAmount},
},
{XfrKeyPair, XfrPublicKey},
},
};

Expand Down Expand Up @@ -51,7 +53,7 @@ fn check_block_rewards_rate() -> Result<()> {
let tx = gen_transfer_tx(
&ledger,
&root_kp,
&FF_PK_LIST[random::<usize>() % FF_PK_LIST.len()],
&XfrPublicKey::from_noah(&FF_PK_LIST[random::<usize>() % FF_PK_LIST.len()]),
FRA_PRE_ISSUE_AMOUNT / 200,
seq_id,
)
Expand Down Expand Up @@ -99,7 +101,8 @@ fn gen_transfer_tx(
) -> Result<Transaction> {
let mut tx_builder = TransactionBuilder::from_seq_id(seq_id);

let target_list = vec![(target_pk, am), (&*BLACK_HOLE_PUBKEY, TX_FEE_MIN)];
let binding = XfrPublicKey::from_noah(&BLACK_HOLE_PUBKEY);
let target_list = vec![(target_pk, am), (&binding, TX_FEE_MIN)];

let mut trans_builder = TransferOperationBuilder::new();

Expand All @@ -118,7 +121,11 @@ fn gen_transfer_tx(
continue;
}

open_blind_asset_record(&utxo.0.record, &owner_memo, owner_kp)
open_blind_asset_record(
&utxo.0.record.into_noah(),
&owner_memo.map(|o| o.into_noah()),
&owner_kp.into_noah(),
)
.c(d!())
.and_then(|ob| {
trans_builder
Expand All @@ -138,7 +145,7 @@ fn gen_transfer_tx(
n,
ASSET_TYPE_FRA,
AssetRecordType::NonConfidentialAmount_NonConfidentialAssetType,
*pk,
pk.into_noah(),
)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use {
globutils::HashOf,
ledger::{
data_model::{
AssetType, AssetTypeCode, AuthenticatedUtxo, StateCommitmentData, TxnSID,
TxoSID, UnAuthenticatedUtxo, Utxo,
AssetType, AssetTypeCode, AssetTypePrefix, AuthenticatedUtxo, StateCommitmentData,
TxnSID, TxoSID, UnAuthenticatedUtxo, Utxo,
},
staking::{
DelegationRwdDetail, DelegationState, Staking, TendermintAddr,
Expand All @@ -25,7 +25,7 @@ use {
ruc::*,
serde::{Deserialize, Serialize},
std::{collections::BTreeMap, mem, sync::Arc},
zei::xfr::{sig::XfrPublicKey, structs::OwnerMemo},
zei::{OwnerMemo, XfrPublicKey},
};

/// Ping route to check for liveness of API
Expand Down Expand Up @@ -149,6 +149,27 @@ pub async fn query_asset(
}
}

/// get_derived asset code according to `AssetTypeCode`
pub async fn get_derived_asset_code(
data: web::Data<Arc<RwLock<QueryServer>>>,
info: web::Path<String>,
) -> actix_web::Result<web::Json<String>> {
let qs = data.read();
if let Ok(token_code) = AssetTypeCode::new_from_base64(&info) {
let derived_asset_code = AssetTypeCode::from_prefix_and_raw_asset_type_code(
AssetTypePrefix::UserDefined,
&token_code,
&CFG.checkpoint,
qs.ledger_cloned.get_tendermint_height(),
);
Ok(web::Json(derived_asset_code.to_base64()))
} else {
Err(actix_web::error::ErrorBadRequest(
"Invalid asset definition encoding.",
))
}
}

/// query tx according to `TxnSID`
pub async fn query_txn(
data: web::Data<Arc<RwLock<QueryServer>>>,
Expand Down Expand Up @@ -698,6 +719,7 @@ pub enum ApiRoutes {
UtxoSidList,
AssetIssuanceNum,
AssetToken,
GetDerivedAssetCode,
GlobalState,
TxnSid,
TxnSidLight,
Expand All @@ -717,6 +739,7 @@ impl NetworkRoute for ApiRoutes {
ApiRoutes::UtxoSidList => "utxo_sid_list",
ApiRoutes::AssetIssuanceNum => "asset_issuance_num",
ApiRoutes::AssetToken => "asset_token",
ApiRoutes::GetDerivedAssetCode => "get_derived_asset_code",
ApiRoutes::GlobalState => "global_state",
ApiRoutes::TxnSid => "txn_sid",
ApiRoutes::TxnSidLight => "txn_sid_light",
Expand Down
19 changes: 10 additions & 9 deletions src/components/abciapp/src/api/query_server/query_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ use {
sync::Arc,
},
tracing::info,
zei::{
serialization::ZeiFromToBytes,
xfr::{sig::XfrPublicKey, structs::OwnerMemo},
},
zei::{noah_algebra::serialization::NoahFromToBytes, OwnerMemo, XfrPublicKey},
};

/// Returns the git commit hash and commit date of this build
Expand Down Expand Up @@ -160,7 +157,7 @@ pub async fn get_created_assets(
info: web::Path<String>,
) -> actix_web::Result<web::Json<Vec<DefineAsset>>> {
// Convert from base64 representation
let key: XfrPublicKey = XfrPublicKey::zei_from_bytes(
let key: XfrPublicKey = XfrPublicKey::noah_from_bytes(
&b64dec(&*info)
.c(d!())
.map_err(|e| error::ErrorBadRequest(e.to_string()))?,
Expand All @@ -178,7 +175,7 @@ pub async fn get_issued_records(
info: web::Path<String>,
) -> actix_web::Result<web::Json<Vec<(TxOutput, Option<OwnerMemo>)>>> {
// Convert from base64 representation
let key: XfrPublicKey = XfrPublicKey::zei_from_bytes(
let key: XfrPublicKey = XfrPublicKey::noah_from_bytes(
&b64dec(&*info)
.c(d!())
.map_err(|e| error::ErrorBadRequest(e.to_string()))?,
Expand Down Expand Up @@ -388,7 +385,7 @@ pub async fn get_related_txns(
info: web::Path<String>,
) -> actix_web::Result<web::Json<HashSet<TxnSID>>> {
// Convert from base64 representation
let key: XfrPublicKey = XfrPublicKey::zei_from_bytes(
let key: XfrPublicKey = XfrPublicKey::noah_from_bytes(
&b64dec(&*info)
.c(d!())
.map_err(|e| error::ErrorBadRequest(e.to_string()))?,
Expand Down Expand Up @@ -452,8 +449,8 @@ pub async fn get_total_supply(
data: web::Data<Arc<RwLock<QueryServer>>>,
) -> actix_web::Result<web::Json<BTreeMap<&'static str, f64>>, actix_web::error::Error> {
let l = data.read();
let burn_pubkey = *BLACK_HOLE_PUBKEY;
let extra_pubkey = *FF_PK_EXTRA_120_0000;
let burn_pubkey = XfrPublicKey::from_noah(&BLACK_HOLE_PUBKEY);
let extra_pubkey = XfrPublicKey::from_noah(&FF_PK_EXTRA_120_0000);

let burn_balance = l
.ledger_cloned
Expand Down Expand Up @@ -596,6 +593,10 @@ impl QueryApi {
&ApiRoutes::AssetToken.with_arg_template("code"),
web::get().to(query_asset),
)
.route(
&ApiRoutes::GetDerivedAssetCode.with_arg_template("code"),
web::get().to(get_derived_asset_code),
)
.route(
&ApiRoutes::GlobalState.route(),
web::get().to(query_global_state),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
parking_lot::{Condvar, Mutex, RwLock},
ruc::*,
std::{collections::HashSet, sync::Arc},
zei::xfr::structs::OwnerMemo,
zei::OwnerMemo,
};

lazy_static! {
Expand Down
2 changes: 1 addition & 1 deletion src/components/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde_json = "1.0"
serde-strz = "1.1.1"
toml = "0.5.8"

globutils = { git = "https://github.com/FindoraNetwork/platform-lib-utils", tag = "v1.0.0" }
globutils = { git = "https://github.com/FindoraNetwork/platform-lib-utils", branch = "develop" }

[target.'cfg(target_os= "linux")'.dependencies]
btm = "0.1.6"
Expand Down
9 changes: 9 additions & 0 deletions src/components/config/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub struct CheckPointConfig {
#[serde(default = "def_utxo_asset_prefix_height")]
pub utxo_asset_prefix_height: u64,

#[serde(default = "def_utxo_asset_prefix_height_2nd_update")]
pub utxo_asset_prefix_height_2nd_update: u64,

#[serde(default = "def_prismxx_inital_height")]
pub prismxx_inital_height: i64,

Expand Down Expand Up @@ -172,6 +175,10 @@ fn def_prismxx_inital_height() -> i64 {
DEFAULT_CHECKPOINT_CONFIG.prismxx_inital_height
}

fn def_utxo_asset_prefix_height_2nd_update() -> u64 {
DEFAULT_CHECKPOINT_CONFIG.utxo_asset_prefix_height_2nd_update
}

fn def_prism_bridge_address() -> String {
DEFAULT_CHECKPOINT_CONFIG.prism_bridge_address.clone()
}
Expand Down Expand Up @@ -255,6 +262,7 @@ lazy_static! {
check_signatures_num: 0,
fix_deliver_tx_revert_nonce_height: 0,
utxo_asset_prefix_height: 0,
utxo_asset_prefix_height_2nd_update: 0,
prismxx_inital_height: 128,
prism_bridge_address: "0x5f9552fEd754F20B636C996DaDB32806554Bb995".to_owned(),
remove_fake_staking_hash: 0,
Expand Down Expand Up @@ -302,6 +310,7 @@ lazy_static! {
check_signatures_num: 4004430,
fix_deliver_tx_revert_nonce_height: 4004430,
utxo_asset_prefix_height: 4004430,
utxo_asset_prefix_height_2nd_update: 5000_0000,
prismxx_inital_height: 4004430,
prism_bridge_address: "0x4672372fDB139B7295Fc59b55b43EC5fF2761A0b".to_owned(),
remove_fake_staking_hash: 4004430,
Expand Down
8 changes: 4 additions & 4 deletions src/components/contracts/baseapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ protobuf = "2.16"
ruc = "1.0"
serde = {version = "1.0.124", features = ["derive"]}
serde_json = "1.0.40"
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
sha3 = "0.8"
zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.6" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.6" }
sha3 = "0.10"
zei = { package="platform-lib-noah", git = "https://github.com/FindoraNetwork/platform-lib-noah", branch = "develop" }

config = { path = "../../config"}

Expand Down
13 changes: 7 additions & 6 deletions src/components/contracts/baseapp/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use module_evm::{
use ruc::{d, Result, RucResult};
use sha3::{Digest, Keccak256};
use std::{collections::BTreeMap, str::FromStr};
use zei::xfr::sig::XfrPublicKey;
use zei::noah_algebra::prelude::NoahFromToBytes;
use zei::XfrPublicKey;

impl EVMStaking for BaseApp {
fn import_validators(
Expand All @@ -41,7 +42,7 @@ impl EVMStaking for BaseApp {
memo: serde_json::to_string(&v.memo).c(d!())?,
rate: mapping_rate(v.commission_rate),
staker: mapping_address(&v.id),
staker_pk: v.id.as_bytes().to_vec(),
staker_pk: v.id.noah_to_bytes(),
power: U256::from(v.td_power),
begin_block: U256::from(begin_block),
});
Expand Down Expand Up @@ -111,7 +112,7 @@ impl EVMStaking for BaseApp {
delegators.push(DelegatorParam {
validator: *validator_address,
delegator: delegator_address,
delegator_pk: public_key.as_bytes().to_vec(),
delegator_pk: public_key.noah_to_bytes(),
bound_amount,
unbound_amount,
});
Expand Down Expand Up @@ -182,7 +183,7 @@ impl EVMStaking for BaseApp {
memo: String,
rate: [u64; 2],
) -> Result<()> {
let staker_pk = staker.as_bytes().to_vec();
let staker_pk = staker.noah_to_bytes();
let staker_address = mapping_address(staker);

let amount =
Expand Down Expand Up @@ -224,7 +225,7 @@ impl EVMStaking for BaseApp {
amount: u64,
td_addr: &[u8],
) -> Result<()> {
let delegator_pk = delegator.as_bytes().to_vec();
let delegator_pk = delegator.noah_to_bytes();
let delegator_address = mapping_address(delegator);

let amount =
Expand Down Expand Up @@ -376,6 +377,6 @@ fn mapping_rate(rate: [u64; 2]) -> U256 {
}

pub fn mapping_address(pk: &XfrPublicKey) -> H160 {
let result = Keccak256::digest(pk.as_bytes());
let result = Keccak256::digest(pk.noah_to_bytes());
H160::from_slice(&result.as_slice()[..20])
}
Loading

0 comments on commit 2e6a254

Please sign in to comment.