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

fix(Clarity2):Factor out 'stx-transfer-memo' into a separate function #2732

Merged
merged 38 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
33203d2
began refactor of memo
Jun 22, 2021
d48c09b
fixed some of the tests
Jun 22, 2021
7dd9f49
added a test for memo's
Jun 22, 2021
2a9a3dc
added tranfer-memo to the docs
Jun 22, 2021
6b472ca
fixed test::events
Jun 22, 2021
d7b54d7
fixed a costs test
Jun 22, 2021
447ca80
Merge branch 'next' into feat/memo
gregorycoppola Jun 24, 2021
c6b78d1
rust fmt
Jun 24, 2021
2ac6a4f
Merge branch 'feat/memo' of github.com:blockstack/stacks-blockchain i…
Jun 24, 2021
3d0e7ab
change version in arithemtic checker
Jun 24, 2021
678f270
fix test_stx_ops
Jun 24, 2021
1cc1bad
cleaning up some imports
Jun 25, 2021
6ae02b9
rearranged the vm tests
Jun 25, 2021
1358e28
copied tests in test_stx_ops to create memo tests
Jun 28, 2021
a546315
added some memo tests to simple_apply_eval
Jun 28, 2021
340bddf
revert change to ast
Jun 28, 2021
1c179af
new test cases for test_stx_ops
gregorycoppola Jun 30, 2021
221e669
began refactor of memo
Jun 22, 2021
b0f6049
fixed some of the tests
Jun 22, 2021
11fe41e
added a test for memo's
Jun 22, 2021
abf221e
added tranfer-memo to the docs
Jun 22, 2021
c829745
fixed test::events
Jun 22, 2021
4cdd7d7
fixed a costs test
Jun 22, 2021
309249d
rust fmt
Jun 24, 2021
fb5be93
change version in arithemtic checker
Jun 24, 2021
6ffbc57
fix test_stx_ops
Jun 24, 2021
c3d2b23
cleaning up some imports
Jun 25, 2021
59f3b60
rearranged the vm tests
Jun 25, 2021
149355c
copied tests in test_stx_ops to create memo tests
Jun 28, 2021
0108e7b
added some memo tests to simple_apply_eval
Jun 28, 2021
eae8745
revert change to ast
Jun 28, 2021
948aeb0
new test cases for test_stx_ops
gregorycoppola Jun 30, 2021
986e982
Merge branch 'feat/memo' of github.com:blockstack/stacks-blockchain i…
gregorycoppola Jul 2, 2021
1f7e8cf
using Unimplemented as the cost
gregorycoppola Jul 2, 2021
5070968
arithmetic_checker separates Clarity1 vs Clarity2 functions
gregorycoppola Jul 2, 2021
7178574
remove some prinln's
gregorycoppola Jul 2, 2021
fac5677
Merge branch 'next' into feat/memo
gregorycoppola Jul 12, 2021
9da7bb5
Merge branch 'next' into feat/memo
gregorycoppola Jul 12, 2021
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
4 changes: 2 additions & 2 deletions src/vm/analysis/arithmetic_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ impl<'a> ArithmeticOnlyChecker<'a> {
match function {
FetchVar | GetBlockInfo | GetTokenBalance | GetAssetOwner | FetchEntry | SetEntry
| DeleteEntry | InsertEntry | SetVar | MintAsset | MintToken | TransferAsset
| TransferToken | ContractCall | StxTransfer | StxBurn | AtBlock | GetStxBalance
| GetTokenSupply | BurnToken | BurnAsset | StxGetAccount => {
| TransferToken | ContractCall | StxTransfer | StxTransferMemo | StxBurn | AtBlock
| GetStxBalance | GetTokenSupply | BurnToken | BurnAsset | StxGetAccount => {
return Err(Error::FunctionNotPermitted(function));
}
Append | Concat | AsMaxLen | ContractOf | PrincipalOf | ListCons | Print
Expand Down
6 changes: 4 additions & 2 deletions src/vm/analysis/arithmetic_checker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn arithmetic_check(contract: &str) -> Result<(), Error> {
contract_identifier,
expressions,
LimitedCostTracker::new_free(),
ClarityVersion::Clarity1,
ClarityVersion::Clarity2,
);

ArithmeticOnlyChecker::run(&analysis)
Expand Down Expand Up @@ -169,8 +169,10 @@ fn test_functions() {
FunctionNotPermitted(NativeFunctions::GetStxBalance)),
("(stx-burn? u100 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF)",
FunctionNotPermitted(NativeFunctions::StxBurn)),
(r#"(stx-transfer? u100 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF 0x00)"#,
(r#"(stx-transfer? u100 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF)"#,
FunctionNotPermitted(NativeFunctions::StxTransfer)),
(r#"(stx-transfer-memo? u100 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF 'SPAXYA5XS51713FDTQ8H94EJ4V579CXMTRNBZKSF 0x010203)"#,
FunctionNotPermitted(NativeFunctions::StxTransferMemo)),
("(define-private (foo (a (list 3 uint)))
(map log2 a))",
FunctionNotPermitted(NativeFunctions::Map)),
Expand Down
5 changes: 3 additions & 2 deletions src/vm/analysis/read_only_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ impl<'a, 'b> ReadOnlyChecker<'a, 'b> {
check_argument_count(2, args)?;
self.check_all_read_only(args)
}
StxTransfer | StxBurn | SetEntry | DeleteEntry | InsertEntry | SetVar | MintAsset
| MintToken | TransferAsset | TransferToken | BurnAsset | BurnToken => {
StxTransfer | StxTransferMemo | StxBurn | SetEntry | DeleteEntry | InsertEntry
| SetVar | MintAsset | MintToken | TransferAsset | TransferToken | BurnAsset
| BurnToken => {
self.check_all_read_only(args)?;
Ok(false)
}
Expand Down
34 changes: 25 additions & 9 deletions src/vm/analysis/type_checker/natives/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,30 @@ pub fn check_special_stx_transfer(
args: &[SymbolicExpression],
context: &TypingContext,
) -> TypeResult {
let memo_passed = if let Ok(()) = check_argument_count(4, args) {
true
} else {
check_argument_count(3, args)?;
false
};
check_argument_count(3, args)?;

let amount_type: TypeSignature = TypeSignature::UIntType;
let from_type: TypeSignature = TypeSignature::PrincipalType;
let to_type: TypeSignature = TypeSignature::PrincipalType;

runtime_cost(ClarityCostFunction::AnalysisTypeLookup, checker, 0)?;

checker.type_check_expects(&args[0], context, &amount_type)?;
checker.type_check_expects(&args[1], context, &from_type)?;
checker.type_check_expects(&args[2], context, &to_type)?;

Ok(
TypeSignature::ResponseType(Box::new((TypeSignature::BoolType, TypeSignature::UIntType)))
.into(),
)
}

pub fn check_special_stx_transfer_memo(
checker: &mut TypeChecker,
args: &[SymbolicExpression],
context: &TypingContext,
) -> TypeResult {
check_argument_count(4, args)?;

let amount_type: TypeSignature = TypeSignature::UIntType;
let from_type: TypeSignature = TypeSignature::PrincipalType;
Expand All @@ -214,9 +232,7 @@ pub fn check_special_stx_transfer(
checker.type_check_expects(&args[0], context, &amount_type)?;
checker.type_check_expects(&args[1], context, &from_type)?;
checker.type_check_expects(&args[2], context, &to_type)?;
if memo_passed {
checker.type_check_expects(&args[3], context, &memo_type)?;
}
checker.type_check_expects(&args[3], context, &memo_type)?;

Ok(
TypeSignature::ResponseType(Box::new((TypeSignature::BoolType, TypeSignature::UIntType)))
Expand Down
3 changes: 3 additions & 0 deletions src/vm/analysis/type_checker/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ impl TypedNativeFunction {
.unwrap(),
}))),
StxTransfer => Special(SpecialNativeFunction(&assets::check_special_stx_transfer)),
StxTransferMemo => Special(SpecialNativeFunction(
&assets::check_special_stx_transfer_memo,
)),
GetTokenBalance => Special(SpecialNativeFunction(&assets::check_special_get_balance)),
GetAssetOwner => Special(SpecialNativeFunction(&assets::check_special_get_owner)),
TransferToken => Special(SpecialNativeFunction(&assets::check_special_transfer_token)),
Expand Down
33 changes: 18 additions & 15 deletions src/vm/analysis/type_checker/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,24 @@ fn test_stx_ops() {
let good = [
"(stx-burn? u10 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)",
r#"(stx-transfer? u10 tx-sender 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)"#,
r#"(stx-transfer? u10 tx-sender 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G 0x00)"#,
r#"(stx-transfer? u10 tx-sender 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G 0x935699)"#,
r#"(stx-transfer-memo? u10 tx-sender 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G 0x0102)"#,
"(stx-get-balance 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)",
];
let expected = [
"(response bool uint)",
"(response bool uint)",
"(response bool uint)",
"(response bool uint)",
"uint",
];
let expected = ["(response bool uint)", "(response bool uint)", "(response bool uint)", "uint"];

let bad = [
r#"(stx-transfer? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x7759)"#,
r#"(stx-transfer? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x000000)"#,
r#"(stx-transfer? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)"#,
r#"(stx-transfer? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true 0x00)"#,
r#"(stx-transfer? u4 u3 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x00)"#,
r#"(stx-transfer? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true 0x00)"#,
r#"(stx-transfer? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true)"#,
"(stx-transfer? u10 tx-sponsor? 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)",
r#"(stx-transfer? u4 u3 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)"#,
r#"(stx-transfer? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true)"#,
gregorycoppola marked this conversation as resolved.
Show resolved Hide resolved
r#"(stx-transfer? u10 tx-sponsor? 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)"#,
r#"(stx-transfer-memo? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x7759 0x0102)"#,
r#"(stx-transfer-memo? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x0102)"#,
r#"(stx-transfer-memo? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true 0x00) 0x0102"#,
r#"(stx-transfer-memo? u4 u3 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x0102)"#,
r#"(stx-transfer-memo? u4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR true 0x0102)"#,
r#"(stx-transfer-memo? u10 tx-sponsor? 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G 0x0102)"#,
"(stx-burn? u4)",
"(stx-burn? 4 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
"(stx-burn? u4 true)",
Expand All @@ -221,7 +219,12 @@ fn test_stx_ops() {
CheckErrors::IncorrectArgumentCount(3, 5),
CheckErrors::TypeError(PrincipalType, UIntType),
CheckErrors::TypeError(PrincipalType, BoolType),
CheckErrors::TypeError(SequenceType(BufferType(BufferLength(34))), BoolType),
CheckErrors::TypeError(PrincipalType, OptionalType(Box::from(PrincipalType))),
CheckErrors::TypeError(PrincipalType, SequenceType(BufferType(BufferLength(2)))),
CheckErrors::TypeError(UIntType, IntType),
CheckErrors::IncorrectArgumentCount(4, 5),
CheckErrors::TypeError(PrincipalType, UIntType),
CheckErrors::TypeError(PrincipalType, BoolType),
CheckErrors::TypeError(PrincipalType, OptionalType(Box::from(PrincipalType))),
CheckErrors::IncorrectArgumentCount(2, 1),
CheckErrors::TypeError(UIntType, IntType),
Expand Down
1 change: 1 addition & 0 deletions src/vm/costs/cost_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ define_named_enum!(ClarityCostFunction {
BlockInfo("cost_block_info"),
StxBalance("cost_stx_balance"),
StxTransfer("cost_stx_transfer"),
StxTransferMemo("cost_stx_transfer"),
gregorycoppola marked this conversation as resolved.
Show resolved Hide resolved
FtMint("cost_ft_mint"),
FtTransfer("cost_ft_transfer"),
FtBalance("cost_ft_balance"),
Expand Down
22 changes: 18 additions & 4 deletions src/vm/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,10 +1648,9 @@ principal isn't materialized, it returns 0.
const STX_TRANSFER: SpecialAPI = SpecialAPI {
input_type: "uint, principal, principal, buff",
output_type: "(response bool uint)",
signature: "(stx-transfer? amount sender recipient memo)",
signature: "(stx-transfer? amount sender recipient)",
description: "`stx-transfer?` is used to increase the STX balance for the `recipient` principal
by debiting the `sender` principal. The `sender` principal _must_ be equal to the current context's `tx-sender`.
The `memo` field is optional, and can be omitted.

This function returns (ok true) if the transfer is successful. In the event of an unsuccessful transfer it returns
one of the following error codes:
Expand All @@ -1665,9 +1664,23 @@ one of the following error codes:
(as-contract
(stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) ;; Returns (ok true)
(as-contract
(stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x00)) ;; Returns (ok true)
(stx-transfer? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)) ;; Returns (ok true)
(as-contract
(stx-transfer? u50 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR tx-sender)) ;; Returns (err u4)
"#
};

const STX_TRANSFER_MEMO: SpecialAPI = SpecialAPI {
input_type: "uint, principal, principal, buff",
output_type: "(response bool uint)",
signature: "(stx-transfer-memo? amount sender recipient memo)",
description: "`stx-transfer-memo?` is similar to `stx-transfer?`, except that it adds a `memo` field.

This function returns (ok true) if the transfer is successful, or, on an error, returns the same codes as `stx-transfer?`.
",
example: r#"
(as-contract
(stx-transfer? u50 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR tx-sender 0x00)) ;; Returns (err u4)
(stx-transfer-memo? u60 tx-sender 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x010203)) ;; Returns (ok true)
"#
};

Expand Down Expand Up @@ -1782,6 +1795,7 @@ fn make_api_reference(function: &NativeFunctions) -> FunctionAPI {
GetStxBalance => make_for_simple_native(&STX_GET_BALANCE, &GetStxBalance, name),
StxGetAccount => make_for_simple_native(&STX_GET_BALANCE, &StxGetAccount, name),
StxTransfer => make_for_special(&STX_TRANSFER, name),
StxTransferMemo => make_for_special(&STX_TRANSFER_MEMO, name),
StxBurn => make_for_simple_native(&STX_BURN, &StxBurn, name),
}
}
Expand Down
36 changes: 26 additions & 10 deletions src/vm/functions/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,40 @@ pub fn special_stx_transfer(
env: &mut Environment,
context: &LocalContext,
) -> Result<Value> {
let memo_passed;
if let Ok(()) = check_argument_count(4, args) {
memo_passed = true;
check_argument_count(3, args)?;

runtime_cost(ClarityCostFunction::StxTransfer, env, 0)?;

let amount_val = eval(&args[0], env, context)?;
let from_val = eval(&args[1], env, context)?;
let to_val = eval(&args[2], env, context)?;
let memo_val = Value::Sequence(SequenceData::Buffer(BuffData::empty()));

if let (
Value::Principal(ref from),
Value::Principal(ref to),
Value::UInt(amount),
Value::Sequence(SequenceData::Buffer(ref memo)),
) = (from_val, to_val, amount_val, memo_val)
{
stx_transfer_consolidated(env, from, to, amount, memo)
} else {
check_argument_count(3, args)?;
memo_passed = false;
Err(CheckErrors::BadTransferSTXArguments.into())
}
}

pub fn special_stx_transfer_memo(
args: &[SymbolicExpression],
env: &mut Environment,
context: &LocalContext,
) -> Result<Value> {
check_argument_count(4, args)?;
gregorycoppola marked this conversation as resolved.
Show resolved Hide resolved
runtime_cost(ClarityCostFunction::StxTransfer, env, 0)?;

let amount_val = eval(&args[0], env, context)?;
let from_val = eval(&args[1], env, context)?;
let to_val = eval(&args[2], env, context)?;
let memo_val = if memo_passed {
eval(&args[3], env, context)?
} else {
Value::Sequence(SequenceData::Buffer(BuffData::empty()))
};
let memo_val = eval(&args[3], env, context)?;

if let (
Value::Principal(ref from),
Expand Down
5 changes: 5 additions & 0 deletions src/vm/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ define_versioned_named_enum!(NativeFunctions(ClarityVersion) {
BurnAsset("nft-burn?", ClarityVersion::Clarity1),
GetStxBalance("stx-get-balance", ClarityVersion::Clarity1),
StxTransfer("stx-transfer?", ClarityVersion::Clarity1),
StxTransferMemo("stx-transfer-memo?", ClarityVersion::Clarity2),
gregorycoppola marked this conversation as resolved.
Show resolved Hide resolved
StxBurn("stx-burn?", ClarityVersion::Clarity1),
StxGetAccount("stx-account", ClarityVersion::Clarity2),
});
Expand Down Expand Up @@ -447,6 +448,10 @@ pub fn lookup_reserved_functions(name: &str, version: &ClarityVersion) -> Option
AtBlock => SpecialFunction("special_at_block", &database::special_at_block),
GetStxBalance => SpecialFunction("special_stx_balance", &assets::special_stx_balance),
StxTransfer => SpecialFunction("special_stx_transfer", &assets::special_stx_transfer),
StxTransferMemo => SpecialFunction(
"special_stx_transfer_memo",
&assets::special_stx_transfer_memo,
),
StxBurn => SpecialFunction("special_stx_burn", &assets::special_stx_burn),
StxGetAccount => SpecialFunction("stx_get_account", &assets::special_stx_account),
};
Expand Down
3 changes: 2 additions & 1 deletion src/vm/tests/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ pub fn get_simple_test(function: &NativeFunctions) -> &'static str {
GetTokenSupply => "(ft-get-supply ft-foo)",
AtBlock => "(at-block 0x55c9861be5cff984a20ce6d99d4aa65941412889bdc665094136429b84f8c2ee 1)", // first stacksblockid
GetStxBalance => "(stx-get-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
StxTransfer => r#"(stx-transfer? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x89995432)"#,
StxTransfer => r#"(stx-transfer? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)"#,
StxTransferMemo => r#"(stx-transfer-memo? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 0x89995432)"#,
StxBurn => "(stx-burn? u1 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
StxGetAccount => "(stx-account 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)",
}
Expand Down
Loading