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

merge main code #3

Merged
merged 17 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ckb-contracts/contracts/bridge-lockscript/src/main.rs
Expand Up @@ -4,8 +4,8 @@
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]

use ckb_std::default_alloc;
use bridge_lockscript_lib::verify;
use ckb_std::default_alloc;

default_alloc!();
contracts_helper::entry!(verify);
17 changes: 12 additions & 5 deletions ckb-contracts/lib/recipient-typescript-lib/src/actions.rs
Expand Up @@ -7,8 +7,7 @@ use alloc::string::String;
use blake2b_ref::{Blake2b, Blake2bBuilder};
use ckb_std::ckb_types::packed::{Byte32, Bytes, Script};
use force_bridge_types::{
config::BRIDGE_LOCK_HASH_TYPE, generated::basic,
generated::force_bridge_lockscript::ForceBridgeLockscriptArgs,
generated::basic, generated::force_bridge_lockscript::ForceBridgeLockscriptArgs,
recipient_cell::RecipientDataView,
};
use molecule::prelude::{Builder, Byte, Entity};
Expand All @@ -17,11 +16,18 @@ use std::prelude::v1::*;
pub const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";

pub fn verify_burn_token<T: Adapter>(data_loader: T, data: RecipientDataView) {
let force_bridge_lock_hash = calc_eth_bridge_lock_hash(
if data.amount == 0 {
panic!(
"burn amount should be greater than 0, burned {:?}",
data.amount
)
}
let force_bridge_lock_hash = calc_xchain_bridge_lock_hash(
&data.owner_lock_hash,
data.chain,
data.asset,
&data.bridge_lock_code_hash,
data.bridge_lock_hash_type,
);
let input_sudt_num =
data_loader.get_sudt_amount_from_source(Source::Input, &force_bridge_lock_hash);
Expand All @@ -48,11 +54,12 @@ pub fn verify_burn_token<T: Adapter>(data_loader: T, data: RecipientDataView) {
}
}

fn calc_eth_bridge_lock_hash(
fn calc_xchain_bridge_lock_hash(
owner_lock_hash: &[u8; 32],
chain: u8,
asset: String,
for_bridge_lock_code_hash: &[u8; 32],
for_bridge_lock_hash_type: u8,
) -> [u8; 32] {
let args = ForceBridgeLockscriptArgs::new_builder()
.owner_lock_hash(
Expand All @@ -70,7 +77,7 @@ fn calc_eth_bridge_lock_hash(
Byte32::from_slice(for_bridge_lock_code_hash)
.expect("for_bridge_lock_code_hash invalid"),
)
.hash_type(Byte::new(BRIDGE_LOCK_HASH_TYPE))
.hash_type(Byte::new(for_bridge_lock_hash_type))
.args(Bytes::new_builder().set(bytes_vec).build())
.build();

Expand Down
6 changes: 3 additions & 3 deletions ckb-contracts/lib/recipient-typescript-lib/src/adapter.rs
Expand Up @@ -40,11 +40,11 @@ where
RecipientDataView::new(data_list[0].as_slice())
.expect("RecipientDataView coding error"),
),
_ => panic!("outputs have more than 1 eth recipient cell"),
_ => panic!("outputs have more than 1 xchain recipient cell"),
}
}

fn get_sudt_amount_from_source(&self, source: Source, eth_bridge_lock_hash: &[u8]) -> u128 {
fn get_sudt_amount_from_source(&self, source: Source, force_bridge_lock_hash: &[u8]) -> u128 {
let mut index = 0;
let mut sudt_sum = 0;
loop {
Expand All @@ -53,7 +53,7 @@ where
Err(SysError::IndexOutOfBound) => break,
Err(err) => panic!("iter input return an error: {:?}", err),
Ok(cell_type) => {
if !(is_sudt_typescript(cell_type, eth_bridge_lock_hash)) {
if !(is_sudt_typescript(cell_type, force_bridge_lock_hash)) {
index += 1;
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions ckb-contracts/lib/recipient-typescript-lib/src/lib.rs
Expand Up @@ -19,9 +19,9 @@ pub fn verify() -> i8 {
0
}

// eth-recipient-typescript has two situations based on whether outputs have eth-recipient-typescript data:
// recipient-typescript has two situations based on whether outputs have recipient-typescript data:
// 1: if outputs have data, we ensure it's a burn-token tx.
// 2: if outputs don't have data, it's a destroy eth-receipt-cell tx, it will always success.
// 2: if outputs don't have data, it's a destroy receipt-cell tx, it will always success.
pub fn _verify<T: Adapter>(data_loader: T) -> i8 {
let data = data_loader.load_output_data();
if let Some(data) = data {
Expand Down
4 changes: 4 additions & 0 deletions ckb-contracts/lib/recipient-typescript-lib/src/test.rs
Expand Up @@ -22,6 +22,7 @@ struct TestParams {
asset: String,
owner_lock_hash: [u8; 32],
bridge_lock_code_hash: [u8; 32],
bridge_lock_hash_type: u8,
bridge_lock_hash: [u8; 32],
}

Expand All @@ -31,6 +32,7 @@ fn get_correct_params() -> TestParams {
let asset = "trx".to_string();
let owner_lock_hash = [100u8; 32];
let bridge_lock_code_hash = [1u8; 32];
let bridge_lock_hash_type = 0;
let bridge_lock_hash = [
218u8, 23, 180, 100, 78, 151, 151, 22, 206, 10, 203, 43, 214, 141, 196, 63, 115, 243, 138,
86, 163, 57, 218, 146, 244, 255, 64, 70, 230, 209, 238, 159,
Expand All @@ -46,6 +48,7 @@ fn get_correct_params() -> TestParams {
asset,
owner_lock_hash,
bridge_lock_code_hash,
bridge_lock_hash_type,
bridge_lock_hash,
}
}
Expand All @@ -58,6 +61,7 @@ fn generate_correct_mock(test_params: TestParams) -> MockDataLoader {
chain: test_params.chain,
asset: test_params.asset,
bridge_lock_code_hash: test_params.bridge_lock_code_hash,
bridge_lock_hash_type: test_params.bridge_lock_hash_type,
owner_lock_hash: test_params.owner_lock_hash,
amount: test_params.amount,
fee: test_params.fee,
Expand Down
1 change: 1 addition & 0 deletions ckb-contracts/tests/src/recipient_typescript.rs
Expand Up @@ -54,6 +54,7 @@ fn get_correct_case() -> TestCase {
chain: 1,
asset: "trx".to_string(),
bridge_lock_code_hash: lock_hash,
bridge_lock_hash_type: 0,
owner_lock_hash: [0u8; 32],
amount: 100,
fee: 10,
Expand Down
1 change: 1 addition & 0 deletions ckb-contracts/types/schemas/recipient_typescript.mol
Expand Up @@ -5,6 +5,7 @@ table RecipientCellData {
chain: byte,
asset: Bytes,
bridge_lock_code_hash: Byte32,
bridge_lock_hash_type: byte,
owner_lock_hash: Byte32,
amount: Uint128,
fee: Uint128,
Expand Down
3 changes: 0 additions & 3 deletions ckb-contracts/types/src/config.rs
Expand Up @@ -6,21 +6,18 @@ cfg_if::cfg_if! {
0, 88, 8, 105, 74, 230, 221, 69, 238, 164, 207, 213,
];
pub const SUDT_HASH_TYPE: u8 = 1;
pub const BRIDGE_LOCK_HASH_TYPE: u8 = 1;
} else if #[cfg(feature = "aggron")] {
pub const SUDT_CODE_HASH: [u8; 32] = [
197, 229, 220, 242, 21, 146, 95, 126, 244, 223, 175, 95, 75, 79, 16, 91, 195, 33, 192,
39, 118, 214, 231, 213, 42, 29, 179, 252, 217, 208, 17, 164,
];
pub const SUDT_HASH_TYPE: u8 = 1;
pub const BRIDGE_LOCK_HASH_TYPE: u8 = 1;
} else if #[cfg(feature = "devnet")] {
pub const SUDT_CODE_HASH: [u8; 32] = [
225, 227, 84, 214, 214, 67, 173, 66, 114, 77, 64, 150, 126, 51, 73, 132, 83, 78, 3, 103,
64, 92, 90, 228, 42, 157, 125, 99, 215, 125, 244, 25,
];
pub const SUDT_HASH_TYPE: u8 = 0;
pub const BRIDGE_LOCK_HASH_TYPE: u8 = 0;
}
}

Expand Down
72 changes: 54 additions & 18 deletions ckb-contracts/types/src/generated/recipient_typescript.rs
Expand Up @@ -30,6 +30,12 @@ impl ::core::fmt::Display for RecipientCellData {
"bridge_lock_code_hash",
self.bridge_lock_code_hash()
)?;
write!(
f,
", {}: {}",
"bridge_lock_hash_type",
self.bridge_lock_hash_type()
)?;
write!(f, ", {}: {}", "owner_lock_hash", self.owner_lock_hash())?;
write!(f, ", {}: {}", "amount", self.amount())?;
write!(f, ", {}: {}", "fee", self.fee())?;
Expand All @@ -43,17 +49,18 @@ impl ::core::fmt::Display for RecipientCellData {
impl ::core::default::Default for RecipientCellData {
fn default() -> Self {
let v: Vec<u8> = vec![
137, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 37, 0, 0, 0, 41, 0, 0, 0, 73, 0, 0, 0, 105, 0,
0, 0, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 41, 0, 0, 0, 45, 0, 0, 0, 77, 0, 0, 0, 78, 0,
0, 0, 110, 0, 0, 0, 126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
];
RecipientCellData::new_unchecked(v.into())
}
}
impl RecipientCellData {
pub const FIELD_COUNT: usize = 7;
pub const FIELD_COUNT: usize = 8;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
Expand Down Expand Up @@ -94,23 +101,29 @@ impl RecipientCellData {
let end = molecule::unpack_number(&slice[20..]) as usize;
Byte32::new_unchecked(self.0.slice(start..end))
}
pub fn owner_lock_hash(&self) -> Byte32 {
pub fn bridge_lock_hash_type(&self) -> Byte {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
let end = molecule::unpack_number(&slice[24..]) as usize;
Byte32::new_unchecked(self.0.slice(start..end))
Byte::new_unchecked(self.0.slice(start..end))
}
pub fn amount(&self) -> Uint128 {
pub fn owner_lock_hash(&self) -> Byte32 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[24..]) as usize;
let end = molecule::unpack_number(&slice[28..]) as usize;
Byte32::new_unchecked(self.0.slice(start..end))
}
pub fn amount(&self) -> Uint128 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[28..]) as usize;
let end = molecule::unpack_number(&slice[32..]) as usize;
Uint128::new_unchecked(self.0.slice(start..end))
}
pub fn fee(&self) -> Uint128 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[28..]) as usize;
let start = molecule::unpack_number(&slice[32..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[32..]) as usize;
let end = molecule::unpack_number(&slice[36..]) as usize;
Uint128::new_unchecked(self.0.slice(start..end))
} else {
Uint128::new_unchecked(self.0.slice(start..))
Expand Down Expand Up @@ -147,6 +160,7 @@ impl molecule::prelude::Entity for RecipientCellData {
.chain(self.chain())
.asset(self.asset())
.bridge_lock_code_hash(self.bridge_lock_code_hash())
.bridge_lock_hash_type(self.bridge_lock_hash_type())
.owner_lock_hash(self.owner_lock_hash())
.amount(self.amount())
.fee(self.fee())
Expand Down Expand Up @@ -180,6 +194,12 @@ impl<'r> ::core::fmt::Display for RecipientCellDataReader<'r> {
"bridge_lock_code_hash",
self.bridge_lock_code_hash()
)?;
write!(
f,
", {}: {}",
"bridge_lock_hash_type",
self.bridge_lock_hash_type()
)?;
write!(f, ", {}: {}", "owner_lock_hash", self.owner_lock_hash())?;
write!(f, ", {}: {}", "amount", self.amount())?;
write!(f, ", {}: {}", "fee", self.fee())?;
Expand All @@ -191,7 +211,7 @@ impl<'r> ::core::fmt::Display for RecipientCellDataReader<'r> {
}
}
impl<'r> RecipientCellDataReader<'r> {
pub const FIELD_COUNT: usize = 7;
pub const FIELD_COUNT: usize = 8;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
Expand Down Expand Up @@ -232,23 +252,29 @@ impl<'r> RecipientCellDataReader<'r> {
let end = molecule::unpack_number(&slice[20..]) as usize;
Byte32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn owner_lock_hash(&self) -> Byte32Reader<'r> {
pub fn bridge_lock_hash_type(&self) -> ByteReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
let end = molecule::unpack_number(&slice[24..]) as usize;
Byte32Reader::new_unchecked(&self.as_slice()[start..end])
ByteReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn amount(&self) -> Uint128Reader<'r> {
pub fn owner_lock_hash(&self) -> Byte32Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[24..]) as usize;
let end = molecule::unpack_number(&slice[28..]) as usize;
Byte32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn amount(&self) -> Uint128Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[28..]) as usize;
let end = molecule::unpack_number(&slice[32..]) as usize;
Uint128Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn fee(&self) -> Uint128Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[28..]) as usize;
let start = molecule::unpack_number(&slice[32..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[32..]) as usize;
let end = molecule::unpack_number(&slice[36..]) as usize;
Uint128Reader::new_unchecked(&self.as_slice()[start..end])
} else {
Uint128Reader::new_unchecked(&self.as_slice()[start..])
Expand Down Expand Up @@ -308,9 +334,10 @@ impl<'r> molecule::prelude::Reader<'r> for RecipientCellDataReader<'r> {
ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
Byte32Reader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
Byte32Reader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
Uint128Reader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
ByteReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
Byte32Reader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
Uint128Reader::verify(&slice[offsets[6]..offsets[7]], compatible)?;
Uint128Reader::verify(&slice[offsets[7]..offsets[8]], compatible)?;
Ok(())
}
}
Expand All @@ -320,12 +347,13 @@ pub struct RecipientCellDataBuilder {
pub(crate) chain: Byte,
pub(crate) asset: Bytes,
pub(crate) bridge_lock_code_hash: Byte32,
pub(crate) bridge_lock_hash_type: Byte,
pub(crate) owner_lock_hash: Byte32,
pub(crate) amount: Uint128,
pub(crate) fee: Uint128,
}
impl RecipientCellDataBuilder {
pub const FIELD_COUNT: usize = 7;
pub const FIELD_COUNT: usize = 8;
pub fn recipient_address(mut self, v: Bytes) -> Self {
self.recipient_address = v;
self
Expand All @@ -342,6 +370,10 @@ impl RecipientCellDataBuilder {
self.bridge_lock_code_hash = v;
self
}
pub fn bridge_lock_hash_type(mut self, v: Byte) -> Self {
self.bridge_lock_hash_type = v;
self
}
pub fn owner_lock_hash(mut self, v: Byte32) -> Self {
self.owner_lock_hash = v;
self
Expand All @@ -364,6 +396,7 @@ impl molecule::prelude::Builder for RecipientCellDataBuilder {
+ self.chain.as_slice().len()
+ self.asset.as_slice().len()
+ self.bridge_lock_code_hash.as_slice().len()
+ self.bridge_lock_hash_type.as_slice().len()
+ self.owner_lock_hash.as_slice().len()
+ self.amount.as_slice().len()
+ self.fee.as_slice().len()
Expand All @@ -380,6 +413,8 @@ impl molecule::prelude::Builder for RecipientCellDataBuilder {
offsets.push(total_size);
total_size += self.bridge_lock_code_hash.as_slice().len();
offsets.push(total_size);
total_size += self.bridge_lock_hash_type.as_slice().len();
offsets.push(total_size);
total_size += self.owner_lock_hash.as_slice().len();
offsets.push(total_size);
total_size += self.amount.as_slice().len();
Expand All @@ -393,6 +428,7 @@ impl molecule::prelude::Builder for RecipientCellDataBuilder {
writer.write_all(self.chain.as_slice())?;
writer.write_all(self.asset.as_slice())?;
writer.write_all(self.bridge_lock_code_hash.as_slice())?;
writer.write_all(self.bridge_lock_hash_type.as_slice())?;
writer.write_all(self.owner_lock_hash.as_slice())?;
writer.write_all(self.amount.as_slice())?;
writer.write_all(self.fee.as_slice())?;
Expand Down