Skip to content

Commit

Permalink
Merge pull request #7 from DeAccountSystems/develop
Browse files Browse the repository at this point in the history
feat: support super lock to mint accounts
  • Loading branch information
linkdesu authored Aug 17, 2021
2 parents 81375d7 + 18195d2 commit 278dadf
Show file tree
Hide file tree
Showing 21 changed files with 1,095 additions and 361 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/apply-register-cell-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "apply-register-cell-type"
version = "1.0.0"
version = "1.0.1"
edition = "2018"

[features]
Expand Down
2 changes: 0 additions & 2 deletions contracts/apply-register-cell-type/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ pub fn main() -> Result<(), Error> {
"There should be none ApplyRegisterCell in inputs and one in outputs."
);

util::is_cell_use_signall_lock(output_cells[0], Source::Output)?;

// Verify the outputs_data of ApplyRegisterCell.
let index = &output_cells[0];
let data = util::load_cell_data(index.to_owned(), Source::Output)?;
Expand Down
3 changes: 2 additions & 1 deletion contracts/pre-account-cell-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pre-account-cell-type"
version = "1.0.0"
version = "1.1.0"
edition = "2018"

[features]
Expand All @@ -14,3 +14,4 @@ ckb-std = "0.7.1"
hex = { default-features = false, version = "0.4.2"}
das-core = { path = "../../libs/das-core", default-features = false }
das-types = { path = "../../../das-types/rust", default-features = false }
chrono = { version = "0.4", default-features = false }
97 changes: 71 additions & 26 deletions contracts/pre-account-cell-type/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub fn main() -> Result<(), Error> {
apply_register_hash,
)?;

debug!("Verify various fields of PreAccountCell ...");

verify_owner_lock_args(pre_account_cell_witness_reader)?;
verify_quote(pre_account_cell_witness_reader)?;
let config_price = parser.configs.price()?;
Expand All @@ -148,24 +150,49 @@ pub fn main() -> Result<(), Error> {
pre_account_cell_witness_reader,
capacity,
)?;

verify_account_id(pre_account_cell_witness_reader, account_id)?;

let timestamp = util::load_oracle_data(OracleCellType::Time)?;
verify_created_at(timestamp, pre_account_cell_witness_reader)?;
util::verify_account_length_and_years(
pre_account_cell_witness_reader.account().len(),
timestamp,
None,
)?;
verify_account_release_status(
pre_account_cell_witness_reader.account().len(),
pre_account_cell_witness_reader,
)?;

verify_account_length(config_account, pre_account_cell_witness_reader)?;
debug!("Verify if account is available for registration for now ...");
verify_account_max_length(config_account, pre_account_cell_witness_reader)?;

let cells_with_super_lock =
util::find_cells_by_script(ScriptType::Lock, super_lock().as_reader(), Source::Input)?;

match verify_account_length_and_years(pre_account_cell_witness_reader, timestamp) {
Ok(_) => {}
Err(code) => {
if !(code == Error::AccountStillCanNotBeRegister && cells_with_super_lock.len() > 0)
{
return Err(code);
}
debug!("Skip Error::AccountStillCanNotBeRegister because of super lock.");
}
}

match verify_account_release_status(pre_account_cell_witness_reader) {
Ok(_) => {}
Err(code) => {
if !(code == Error::AccountStillCanNotBeRegister && cells_with_super_lock.len() > 0)
{
return Err(code);
}
debug!("Skip Error::AccountStillCanNotBeRegister because of super lock.");
}
}

match verify_preserved_accounts(&mut parser, pre_account_cell_witness_reader) {
Ok(_) => {}
Err(code) => {
if !(code == Error::AccountIsPreserved && cells_with_super_lock.len() > 0) {
return Err(code);
}
debug!("Skip Error::AccountIsPreserved because of super lock.");
}
}

verify_account_chars(&mut parser, pre_account_cell_witness_reader)?;
verify_preserved_accounts(&mut parser, pre_account_cell_witness_reader)?;
} else {
return Err(Error::ActionNotSupported);
}
Expand Down Expand Up @@ -287,13 +314,6 @@ fn verify_owner_lock_args(reader: PreAccountCellDataReader) -> Result<(), Error>
owner_lock_args.len()
);

assert!(
owner_lock_args[0] <= 10,
Error::PreRegisterOwnerLockArgsIsInvalid,
"The first of owner_lock_args should between 0 and 10, but {} found.",
owner_lock_args[0]
);

Ok(())
}

Expand Down Expand Up @@ -408,7 +428,7 @@ fn verify_price_and_capacity(
Ok(())
}

fn verify_account_length(
fn verify_account_max_length(
config: ConfigCellAccountReader,
reader: PreAccountCellDataReader,
) -> Result<(), Error> {
Expand Down Expand Up @@ -559,10 +579,35 @@ fn verify_preserved_accounts(
Ok(())
}

fn verify_account_release_status(
account_length: usize,
fn verify_account_length_and_years(
reader: PreAccountCellDataReader,
current_timestamp: u64,
) -> Result<(), Error> {
use chrono::{DateTime, NaiveDateTime, Utc};

let account_length = reader.account().len();
let current = DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(current_timestamp as i64, 0),
Utc,
);

debug!(
"Check if the account is available for registration now. (length: {}, current: {:#?})",
account_length, current
);

// On CKB main net, AKA Lina, accounts of less lengths can be registered only after a specific number of years.
// TODO Trible check.
assert!(
account_length >= 5,
Error::AccountStillCanNotBeRegister,
"The account less than 5 characters can not be registered now."
);

Ok(())
}

fn verify_account_release_status(reader: PreAccountCellDataReader) -> Result<(), Error> {
debug!("Check if account is released for registration.");

let account: Vec<u8> = [
Expand All @@ -574,7 +619,7 @@ fn verify_account_release_status(
let lucky_num = hash[0];

if cfg!(feature = "mainnet") {
if account_length < 10 {
if reader.account().len() < 10 {
// TODO Trible check.
assert!(
lucky_num <= 12,
Expand All @@ -584,11 +629,11 @@ fn verify_account_release_status(
);
}
} else {
if account_length < 10 {
if reader.account().len() < 10 {
assert!(
lucky_num <= 200,
Error::AccountStillCanNotBeRegister,
"The registration is still not started.(lucky_num: {}, required: <= 254)",
"The registration is still not started.(lucky_num: {}, required: <= 200)",
lucky_num
);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/proposal-cell-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proposal-cell-type"
version = "1.0.1"
version = "1.0.2"
edition = "2018"

[features]
Expand Down
6 changes: 0 additions & 6 deletions contracts/proposal-cell-type/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,12 +850,6 @@ fn verify_proposal_execution_result(
item_index, profit, total_capacity, storage_capacity
);

util::verify_account_length_and_years(
input_cell_witness_reader.account().len(),
timestamp,
Some(item_index),
)?;

is_cell_capacity_correct(item_index, output_account_cells[i], storage_capacity)?;
is_new_account_cell_lock_correct(
item_index,
Expand Down
2 changes: 1 addition & 1 deletion libs/das-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ckb_std::error::SysError;

/// Error
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#[repr(i8)]
pub enum Error {
IndexOutOfBound = 1,
Expand Down
54 changes: 0 additions & 54 deletions libs/das-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,60 +669,6 @@ pub fn is_init_day(current_timestamp: u64) -> Result<(), Error> {
Ok(())
}

pub fn verify_account_length_and_years(
account_length: usize,
current_timestamp: u64,
item_index: Option<usize>,
) -> Result<(), Error> {
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};

let current = DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(current_timestamp as i64, 0),
Utc,
);

if item_index.is_some() {
debug!(
" Item[{}] Check if the account is available for registration now. (length: {}, current: {:#?})",
item_index.unwrap(), account_length, current
);
} else {
debug!(
"Check if the account is available for registration now. (length: {}, current: {:#?})",
account_length, current
);
}

// On CKB main net, AKA Lina, accounts of less lengths can be registered only after a specific number of years.
if cfg!(feature = "mainnet") {
// TODO Trible check.
// ⚠️ Before year 2 means the first year.
assert!(
account_length >= 5,
Error::AccountStillCanNotBeRegister,
"The account less than 10 characters can not be registered now."
);
} else if cfg!(feature = "testnet") {
assert!(
account_length >= 2,
Error::AccountStillCanNotBeRegister,
"The account less than 2 characters can not be registered now."
);
} else {
let year_n = Utc.ymd(4444, 4, 4).and_hms(4, 4, 4);
if current < year_n {
assert!(
account_length >= 2,
Error::AccountStillCanNotBeRegister,
"The account less than 2 characters can not be registered now. (available_for_register: {:?})",
year_n
);
}
}

Ok(())
}

pub fn calc_account_storage_capacity(
config_account: das_packed::ConfigCellAccountReader,
account_name_storage: u64,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/account_cell_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ fn gen_account_renew() {
template.write_template("account_renew_account.json");
}

test_with_template!(test_account_renew, "account_renew_account.json");
// test_with_template!(test_account_renew, "account_renew_account.json");

#[test]
fn gen_account_recycle_expired_account_by_keeper() {
Expand Down
7 changes: 5 additions & 2 deletions tests/src/income_cell_type/income_consolidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn init(action: &str) -> TemplateGenerator {
}

#[test]
fn gen_income_consolidate_need_pad() {
fn gen_income_consolidate_need_pad_1() {
let mut template = init("consolidate_income");

let capacity_of_10 = 20_000_000_000;
Expand Down Expand Up @@ -114,7 +114,10 @@ fn gen_income_consolidate_need_pad() {
template.write_template("income_consolidate.json");
}

test_with_template!(test_income_consolidate_need_pad, "income_consolidate.json");
test_with_template!(
test_income_consolidate_need_pad_1,
"income_consolidate.json"
);

test_with_generator!(test_income_consolidate_need_pad_2, || {
let mut template = init("consolidate_income");
Expand Down
Loading

0 comments on commit 278dadf

Please sign in to comment.