Snippets
Created by
Riccardo Sibani
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dict>
<entry key="blockstrategy">
<dict>
<entry key="name">
<string>net.postchain.base.BaseBlockBuildingStrategy</string>
</entry>
</dict>
</entry>
<entry key="configurationfactory">
<string>net.postchain.gtx.GTXBlockchainConfigurationFactory</string>
</entry>
<entry key="gtx">
<dict>
<entry key="modules">
<array>
<string>net.postchain.rell.module.RellPostchainModuleFactory</string>
<string>net.postchain.gtx.StandardOpsGTXModule</string>
</array>
</entry>
<entry key="rell">
<dict>
<entry key="moduleArgs">
<dict>
<entry key="">
<dict>
<entry key="admin_pub_key">
<string>03E868D007F8C9529978AEE29E989AFF963C12451E393A349C65B5516AA15C605D</string>
</entry>
</dict>
</entry>
<entry key="lib.ft3.core">
<dict>
<entry key="my_blockchain_description">
<string>TESNET ENVIRONMENT where is possible to send tokens and log in into dapps with SSO.</string>
</entry>
<entry key="my_blockchain_name">
<string>testnet_vault</string>
</entry>
<entry key="my_blockchain_website">
<string>vault.testnet.chromia.com</string>
</entry>
<entry key="request_max_count">
<int>20</int>
</entry>
<entry key="request_recovery_time">
<int>60000</int>
</entry>
</dict>
</entry>
</dict>
</entry>
<entry key="modules">
<array>
<string></string>
</array>
</entry>
<entry key="sources_v0.10">
<dict>
<entry key="admin.rell">
<string>import ft3_dev_op: .lib.ft3.core.dev_op;
import ft3_core: .lib.ft3.core;
import ft3_acc : .lib.ft3.account;
function require_dev_admin() {
ft3_dev_op.require_dev();
val adminPubKeyBytes = byte_array(chain_context.args.admin_pub_key);
require(is_signer(adminPubKeyBytes), "Needs to be signed by admin");
}
operation dev_register_asset (name, issuing_chain_rid: byte_array) {
ft3_dev_op.require_dev();
require_dev_admin();
ft3_core.register_asset(name, issuing_chain_rid);
}
operation dev_give_balance (asset_id: byte_array, account_id: byte_array, amount: integer) {
ft3_dev_op.require_dev();
require_dev_admin();
require(amount >= 0);
ft3_core.ensure_balance( ft3_acc.account @ { account_id }, ft3_core.asset @ { .id == asset_id } ).amount += amount;
}
operation dev_register_account (ft3_acc.auth_descriptor) {
ft3_dev_op.require_dev();
ft3_acc.create_account_with_auth( auth_descriptor );
}
/*
* Can be negative
*/
operation dev_give_points(account_id: byte_array, amount: integer){
ft3_dev_op.require_dev();
require_dev_admin();
val account = ft3_acc.account@{.id == account_id};
ft3_dev_op.dev_give_points_(account, amount);
}
operation dev_free_op(account_id: byte_array) {
ft3_dev_op.require_dev();
require_dev_admin();
// this operation does not cost points in the rate limit
val account = ft3_acc.account@{.id == account_id};
ft3_dev_op.dev_give_points_(account, 1);
ft3_acc.rate_limit(account);
}</string>
</entry>
<entry key="lib/ft3/account/auth_basic.rell">
<string>// Auth type:
// "S" - single-sig
// "M" - multi-sig
struct single_sig_args {
flags: set<text>;
pubkey;
}
struct multi_sig_args {
flags: set<text>;
signatures_required: integer;
pubkeys: list<pubkey>;
}
function check_single_sig_auth(args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
require(participants.size() == 1);
val ss_args = single_sig_args.from_bytes(args);
return
ss_args.flags.contains_all(required_flags)
and
is_signer(participants[0]);
}
function check_multi_sig_auth(args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
val multi_sign_args = multi_sig_args.from_bytes(args);
if (not (multi_sign_args.flags.contains_all(required_flags))) return false;
var num_sign = multi_sign_args.signatures_required;
for(s in participants) {
if(is_signer(s)) {
num_sign -= 1;
if(num_sign < 1) {return true;}
}
}
return false;
}
function check_auth_args (a_t: text, args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
return when (a_t) {
"S" -> check_single_sig_auth(args, participants, required_flags);
"M" -> check_multi_sig_auth(args, participants, required_flags);
else -> false;
};
}
function require_auth (account, descriptor_id: byte_array, required_flags: list<text>) {
val account_auth_descriptor = account_auth_descriptor @ {
account,
.descriptor_id == descriptor_id
};
require(is_valid(account_auth_descriptor));
require(
check_auth_args(
account_auth_descriptor.auth_type,
account_auth_descriptor.args,
auth_descriptor_participant @* { account_auth_descriptor } ( .id ),
required_flags
)
);
update_auth_descriptor_rule_variables(account_auth_descriptor);
delete_expired_auth_descriptors(account);
}
</string>
</entry>
<entry key="lib/ft3/account/auth_descriptor_rule.rell">
<string>
enum rule_variable {
block_height,
block_time,
op_count
}
enum rule_operator {
lt,
le,
eq,
ge,
gt
}
struct rule_expression {
variable: text;
operator: text;
value: gtv;
}
entity auth_descriptor_rule_variable {
key account_auth_descriptor, name;
mutable value: byte_array;
}
function is_valid(auth_descriptor: account_auth_descriptor): boolean {
// check if rules is null (encoded null is equal to 0xa0020500)
if (auth_descriptor.rules == x'a0020500') {
return true;
}
return is_rule_valid(
gtv.from_bytes(auth_descriptor.rules),
load_variables(auth_descriptor)
);
}
function is_rule_valid(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val operator = text.from_gtv(parameters[1]);
if (operator == "and") {
return handle_composed_rule(rule, variables);
} else {
return handle_single_rule(rule, variables);
}
}
function handle_single_rule(rule: gtv, variables: map<text, gtv>): boolean {
return evaluate_int_variable_rule(
rule_expression.from_gtv(rule),
variables
);
}
function handle_composed_rule(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val operator = text.from_gtv(parameters[1]);
if (operator == "and") {
return is_rule_valid(parameters[0], variables) and is_rule_valid(parameters[2], variables);
} else {
return false;
}
}
function is_active(account_auth_descriptor): boolean {
if (account_auth_descriptor.rules == x'a0020500') {
return true;
}
return is_rule_active(gtv.from_bytes(account_auth_descriptor.rules), map<text, gtv>());
}
function is_rule_active(r: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = text.from_gtv(parameters[1]);
when (operator) {
"and" -> {
return is_rule_active(parameters[0], variables) and is_rule_active(parameters[2], variables);
}
else -> {
return is_simple_rule_active(r, variables);
}
}
}
function is_simple_rule_active(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val variable_name = text.from_gtv(parameters[0]);
when (rule_variable.value(variable_name)) {
rule_variable.block_height -> {
return is_block_height_rule_active(rule);
}
rule_variable.block_time -> {
return is_block_time_rule_active(rule);
}
rule_variable.op_count -> {
return is_op_count_rule_active(rule);
}
}
}
/*
* Variables
*/
function evaluate_int_variable_rule(rule: rule_expression, variables: map<text, gtv>): boolean {
val variable = rule_variable.value(rule.variable);
val operator = rule_operator.value(rule.operator);
val current_value = variable_value(variable, variables);
val defined_value = integer.from_gtv(rule.value);
when (operator) {
rule_operator.lt -> {
return current_value < defined_value;
}
rule_operator.le -> {
return current_value <= defined_value;
}
rule_operator.eq -> {
return current_value == defined_value;
}
rule_operator.ge -> {
return current_value >= defined_value;
}
rule_operator.gt -> {
return current_value > defined_value;
}
}
}
/*
* Called from _add_auth_descriptor function
*/
function setup_auth_descriptor_variables(account_auth_descriptor) {
if (account_auth_descriptor.rules == x'a0020500') {
return;
}
val variables = get_variable_names_set(gtv.from_bytes(account_auth_descriptor.rules));
for (variable in variables) {
when (variable) {
rule_variable.op_count.name -> {
create auth_descriptor_rule_variable(
account_auth_descriptor,
name = variable,
value = op_count_variable_initial_value().to_bytes()
);
}
}
}
}
function update_variables(account_auth_descriptor, variables: map<text, gtv>) {
for ((name, value) in variables) {
val variable = auth_descriptor_rule_variable @ { account_auth_descriptor, name };
val int_value = integer.from_gtv(value);
when (name) {
rule_variable.op_count.name -> {
variable.value = (int_value + 1).to_gtv().to_bytes();
}
}
}
}
function variable_value(variable: rule_variable, variables: map<text, gtv>): integer {
when (variable) {
rule_variable.block_height -> {
return op_context.block_height;
}
rule_variable.block_time -> {
return op_context.last_block_time;
}
rule_variable.op_count -> {
return integer.from_gtv(variables[rule_variable.op_count.name]);
}
}
}
/*
* Analyze the rules before assigning them to an authenticator descriptor
*/
function analyze_auth_descriptor_rules(rules: gtv, hop: integer) {
require(hop<3, "too many rules");
val rules_bytes = rules.to_bytes();
if (rules_bytes == x'a0020500') {
return;
}
val params = list<gtv>.from_gtv(rules);
require(params.size() <=3);
val operator = text.from_gtv(params[1]);
if(operator == "and") {
analyze_auth_descriptor_rules(params[0], hop+1);
analyze_auth_descriptor_rules(params[2], hop+1);
}
return;
}
/*
* Returns set of variable names used in a rule
*/
function get_variable_names_set(rules: gtv): set<text> {
val parameters = list<gtv>.from_gtv(rules);
val operator = text.from_gtv(parameters[1]);
var vars = set<text>();
if (operator == "and") {
vars.add_all(get_variable_names_set(parameters[0]));
vars.add_all(get_variable_names_set(parameters[2]));
} else {
vars.add(text.from_gtv(parameters[0]));
}
return vars;
}
function load_variables(account_auth_descriptor): map<text, gtv> {
val variables = auth_descriptor_rule_variable @* { account_auth_descriptor };
val variables_map = map<text, gtv>();
for (variable in variables) {
variables_map[variable.name] = gtv.from_bytes(variable.value);
}
return variables_map;
}
function update_auth_descriptor_rule_variables(account_auth_descriptor) {
val variables = load_variables(account_auth_descriptor);
update_variables(account_auth_descriptor, variables);
}
/*
* auth descriptor cleanup
*/
function delete_expired_auth_descriptors(account) {
val auth_descriptors = account_auth_descriptor @* { account };
for (auth_descriptor in auth_descriptors) {
if (not (is_active(auth_descriptor) and is_valid(auth_descriptor))) {
_delete_auth_descriptor(auth_descriptor);
}
}
}
/****************************************************************************************
* block_time *
****************************************************************************************/
function is_block_time_rule_active(r: gtv): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = rule_operator.value(text.from_gtv(parameters[1]));
val value = integer.from_gtv(parameters[2]);
when (operator) {
rule_operator.gt -> {
return op_context.last_block_time > value;
}
rule_operator.ge,
rule_operator.eq -> {
return op_context.last_block_time >= value;
}
else -> {
return true;
}
}
}
/****************************************************************************************
* block_height *
****************************************************************************************/
function is_block_height_rule_active(r: gtv): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = rule_operator.value(text.from_gtv(parameters[1]));
val value = integer.from_gtv(parameters[2]);
when (operator) {
rule_operator.gt -> {
return op_context.block_height > value;
}
rule_operator.ge,
rule_operator.eq -> {
return op_context.block_height >= value;
}
else -> {
return true;
}
}
}
/****************************************************************************************
* op_count *
****************************************************************************************/
function op_count_variable_initial_value(): gtv {
return (1).to_gtv();
}
function is_op_count_rule_active(rule: gtv): boolean {
return true;
}</string>
</entry>
<entry key="lib/ft3/account/module.rell">
<string>@mount('ft3')
module;
import core: ^.core;
struct auth_descriptor {
auth_type: text;
participants: list<byte_array>;
args: list<gtv>;
rules: gtv;
}
entity account {
key id: byte_array;
}
entity account_meta {
meta_key: text;
value: byte_array;
index account, meta_key;
index meta_key, value;
}
entity account_auth_descriptor {
descriptor_id: byte_array;
key account, descriptor_id;
index descriptor_id;
auth_type: text;
args: byte_array;
rules: byte_array;
}
entity auth_descriptor_participant {
key account_auth_descriptor, id: byte_array;
}
// rate limiter
entity rl_state {
key account;
mutable points: integer;
mutable last_update: timestamp;
mutable max_points: integer;
mutable recovery_time: timestamp;
}
function _add_auth_descriptor (account, auth_descriptor) {
val descriptor_id = auth_descriptor.hash();
analyze_auth_descriptor_rules(auth_descriptor.rules, 0);
val account_auth_descriptor = create account_auth_descriptor (
account, descriptor_id,
auth_descriptor.auth_type,
args = auth_descriptor.args.to_gtv().to_bytes(),
rules = auth_descriptor.rules.to_gtv().to_bytes()
);
for (participant in auth_descriptor.participants) {
create auth_descriptor_participant(account_auth_descriptor, id = participant);
}
setup_auth_descriptor_variables(account_auth_descriptor);
}
function _delete_auth_descriptor(auth_descriptor: account_auth_descriptor) {
delete auth_descriptor_rule_variable @* { auth_descriptor };
delete auth_descriptor_participant @* { auth_descriptor };
delete auth_descriptor;
}
function _delete_all_auth_descriptors_exclude(account, auth_descriptor_id: byte_array) {
delete (adrv: auth_descriptor_rule_variable, ad: account_auth_descriptor) @* {
ad.account == account, ad.descriptor_id != auth_descriptor_id, adrv.account_auth_descriptor == ad
};
delete (adp: auth_descriptor_participant, ad: account_auth_descriptor) @* {
ad.account == account and ad.descriptor_id != auth_descriptor_id and adp.account_auth_descriptor == ad
};
delete account_auth_descriptor @* {
.account == account,
.descriptor_id != auth_descriptor_id
};
}
function create_account_with_auth (auth_descriptor): byte_array {
val id = auth_descriptor.hash();
val acc = create account (id);
_add_auth_descriptor(
acc, auth_descriptor
);
val max_points = core.my_blockchain_info.request_max_count;
val recovery_time = core.my_blockchain_info.request_recovery_time;
create rl_state(acc, .points = 0, .last_update = op_context.last_block_time, max_points, recovery_time);
return id;
}
function auth_and_log(account_id: byte_array, auth_descriptor_id: byte_array, required_flags: list<text>): account {
val account = account @ { .id == account_id };
rate_limit(account);
require_auth(account, auth_descriptor_id, required_flags);
return account;
}
query get_account_auth_descriptors(id: byte_array) {
return account_auth_descriptor @* {
.account.id == id
} (
id = .descriptor_id,
type = .auth_type,
args = .args
);
}
query get_account_by_id(id: byte_array) {
return account @? { id } ( .id );
}
query get_account_by_auth_descriptor(auth_descriptor) {
return account @? { .id == auth_descriptor.hash() } ( .id );
}
query get_accounts_by_participant_id(id: byte_array) {
return auth_descriptor_participant @* { id } ( .account_auth_descriptor.account.id );
}
query get_accounts_by_auth_descriptor_id(descriptor_id: byte_array) {
return account_auth_descriptor @* { descriptor_id } ( .account.id );
}
</string>
</entry>
<entry key="lib/ft3/account/operations/module.rell">
<string>@mount('ft3')
module;
import acc: ^^.account;
import core: ^^.core;
operation delete_auth_descriptor (account_id: byte_array, auth_descriptor_id: byte_array, delete_descriptor_id: byte_array) {
val account = acc.auth_and_log(account_id, auth_descriptor_id, ["A"]);
acc._delete_auth_descriptor(acc.account_auth_descriptor @ {
.account == account,
.descriptor_id == delete_descriptor_id
});
}
operation delete_all_auth_descriptors_exclude(account_id: byte_array, auth_descriptor_id: byte_array) {
val account = acc.auth_and_log(account_id, auth_descriptor_id, ["A"]);
acc._delete_all_auth_descriptors_exclude(account, auth_descriptor_id);
}
operation add_auth_descriptor (account_id: byte_array, auth_id: byte_array, new_desc: acc.auth_descriptor) {
val account = acc.auth_and_log(account_id, auth_id, ["A"]);
acc._add_auth_descriptor(account, new_desc);
}
</string>
</entry>
<entry key="lib/ft3/account/rate_limit.rell">
<string>import ^.core;
function rate_limit (account) {
val max_counter = core.my_blockchain_info.request_max_count;
val recovery_time = core.my_blockchain_info.request_recovery_time;
val stat = rl_state@{account} (.points, .last_update);
val delta = op_context.last_block_time - stat.last_update;
var got_points = 0;
var update_time = stat.last_update;
if (delta > recovery_time)
{
got_points = delta / recovery_time;
update_time = stat.last_update + got_points * recovery_time;
if (got_points + stat.points > max_counter) {
got_points = max_counter - stat.points;
// if user is at maximum reset his
// timer
update_time = op_context.last_block_time;
}
}
require(stat.points + got_points > 0);
update rl_state@{account} (
.points += got_points - 1,
.last_update = update_time
);
}
query get_account_rate_limit(account_id: byte_array) {
val t= rl_state@{.account.id == account_id}
(
.points,
.last_update
);
return t;
}
query get_last_timestamp(): integer {
return (block@{.timestamp>0}(@sort_desc .timestamp) limit 1);
}</string>
</entry>
<entry key="lib/ft3/core/asset.rell">
<string>import acc: ^.account;
entity asset {
id: byte_array;
key id;
name;
issuing_chain_rid: byte_array;
}
entity balance {
key acc.account, asset;
mutable amount: integer = 0;
}
function ensure_balance(acc.account, asset): balance {
val balance = balance @? {asset, account};
if (balance != null) {
return balance;
}
else return create balance(account, asset, amount = 0);
}
function register_asset (name, issuing_chain_rid: byte_array): asset {
val id = (name, issuing_chain_rid).hash();
return create asset (id, name, issuing_chain_rid);
}
function _get_asset_balances(account_id: byte_array): list<(id:byte_array,name:text,amount:integer,chain_id:byte_array)> {
return balance @* { .account.id == account_id } (
id = .asset.id,
name = .asset.name,
amount = .amount,
chain_id = .asset.issuing_chain_rid
);
}
query get_asset_balances(account_id: byte_array) {
return _get_asset_balances(account_id);
}
query get_asset_balance(account_id: byte_array, asset_id: byte_array) {
return balance @? {
.account.id == account_id,
.asset.id == asset_id
} (
id = .asset.id,
name = .asset.name,
amount = .amount,
chain_id = .asset.issuing_chain_rid
);
}
query get_asset_by_name(name) {
return asset @* { name } (
.id,
.name,
.issuing_chain_rid
);
}
query get_asset_by_id(asset_id: byte_array) {
return asset@{
.id == asset_id
}(
id = .id,
name = .name,
issuing_chain_rid = .issuing_chain_rid
);
}
query get_all_assets() {
return asset@*{}(
id = .id,
name = .name,
issuing_chain_rid = .issuing_chain_rid
);
}</string>
</entry>
<entry key="lib/ft3/core/dev_op.rell">
<string>module;
import ft3: ^^.core;
import acc: ^^.account;
object dev_state {
mutable allow_dev_ops: boolean = true;
}
function require_dev() {
require(dev_state.allow_dev_ops);
}
function dev_give_points_(account: acc.account, amount: integer) {
require_dev();
update acc.rl_state@{account}(.points += amount);
}</string>
</entry>
<entry key="lib/ft3/core/history.rell">
<string>import acc: ^.account;
@log entity payment_history_entry {
index acc.account, asset;
delta: integer;
// helps to locate exact position of payment entry in transaction
op_index: integer;
is_input: boolean;
entry_index: integer;
}
query get_payment_history(account_id: byte_array, after_block: integer) {
return payment_history_entry @* {
acc.account @ { account_id },
.transaction.block.block_height > after_block
} (
delta = .delta,
asset = .asset.name,
asset_id = .asset.id,
is_input = .is_input,
timestamp = .transaction.block.timestamp,
block_height = .transaction.block.block_height,
entry_index = .entry_index,
tx_rid = .transaction.tx_rid,
tx_data = .transaction.tx_data
);
}
</string>
</entry>
<entry key="lib/ft3/core/module.rell">
<string>@mount('ft3')
module;
struct module_args {
my_blockchain_name: text;
my_blockchain_website: text;
my_blockchain_description: text;
request_max_count: integer;
request_recovery_time: integer;
}</string>
</entry>
<entry key="lib/ft3/core/my_blockchain_info.rell">
<string>
object my_blockchain_info {
mutable name: text = chain_context.args.my_blockchain_name;
mutable website: text = chain_context.args.my_blockchain_website;
mutable description: text = chain_context.args.my_blockchain_description;
mutable request_max_count: integer = chain_context.args.request_max_count;
mutable request_recovery_time: integer = chain_context.args.request_recovery_time;
}
//operation update_blockchain(name, website: text, description: text) {
// blockchain.name = name;
// blockchain.website = website;
// blockchain.description = description;
//}
query get_blockchain_info() {
return (
name = my_blockchain_info.name,
website = my_blockchain_info.website,
description = my_blockchain_info.description,
request_max_count = my_blockchain_info.request_max_count,
request_recovery_time = my_blockchain_info.request_recovery_time
);
}
</string>
</entry>
<entry key="lib/ft3/core/operations/transfer.rell">
<string>@mount('ft3')
module;
import ft3: ^^;
operation transfer (inputs: list<ft3.xfer_input>, outputs: list<ft3.xfer_output>) {
ft3._transfer(inputs, outputs);
}</string>
</entry>
<entry key="lib/ft3/core/transfer.rell">
<string>import acc: ^.account;
struct xfer_input {
account_id: byte_array;
asset_id: byte_array;
auth_descriptor_id: byte_array;
amount: integer;
extra: map<text, gtv>;
}
struct xfer_output {
account_id: byte_array;
asset_id: byte_array;
amount: integer;
extra: map<text, gtv>;
}
function deduct_balance (acc.account, asset, d_amount: integer) {
val b = balance @ { account, asset };
require( b.amount >= d_amount, "Balance is too low");
update b (amount -= d_amount);
}
function consume_input (i: xfer_input, idx: integer, assets: map<asset, integer>): payment_history_entry {
val asset = asset @ { .id == i.asset_id };
val account = acc.auth_and_log(i.account_id, i.auth_descriptor_id, ["T"]);
require( i.amount > 0, "Amount should be non 0 positive." );
deduct_balance(account, asset, i.amount);
assets[asset] = i.amount + if (asset in assets) assets[asset] else 0;
return create payment_history_entry (
account,
asset,
.delta = i.amount,
.op_index = 500, //op_context ???
.is_input = true,
.entry_index = idx
);
}
function process_transfer_output (o: xfer_output, idx: integer, available_assets: map<asset, integer>): payment_history_entry {
if (o.extra.contains("reg_auth_desc")) {
val auth_desc = acc.auth_descriptor.from_gtv(o.extra["reg_auth_desc"]);
require(o.account_id
== acc.create_account_with_auth(auth_desc));
}
val target_account = acc.account @ { .id == o.account_id };
val asset = asset @ { .id == o.asset_id };
require(o.amount > 0, "You cannot tx output negative numbers");
require( available_assets[asset] >= o.amount );
available_assets[asset] -= o.amount;
require(available_assets[asset] >= 0);
val balance = ensure_balance(target_account, asset);
balance.amount += o.amount;
return create payment_history_entry (
.account = target_account,
asset,
.delta = o.amount,
.op_index = 500, // something from op_context,
.is_input = false,
.entry_index = idx
);
}
function _transfer (inputs: list<xfer_input>, outputs: list<xfer_output>) {
val sum_inputs = map<asset, integer>();
var idx = 0;
for (i in inputs) {
consume_input(i, idx, sum_inputs);
idx += 1;
}
idx = 0;
for (o in outputs) {
process_transfer_output(o, idx, sum_inputs);
idx += 1;
}
}
</string>
</entry>
<entry key="lib/ft3/ft3_basic.rell">
<string>module;
namespace ft3 {
import acc: lib.ft3.account;
import acc_op: lib.ft3.account.operations;
import transfer_op: lib.ft3.core.operations.transfer;
}
</string>
</entry>
<entry key="lib/ft3/operations/dev_op_account.rell">
<string>@mount('ft3')
module;
import dev_op: ^^.core.dev_op;
import acc: ^^.account;
operation dev_register_account (acc.auth_descriptor) {
dev_op.require_dev();
acc.create_account_with_auth( auth_descriptor );
}
</string>
</entry>
<entry key="module.rell">
<string>module;
import lib.ft3.ft3_basic;
import lib.ft3.operations.dev_op_account;
struct module_args {
admin_pub_key: text;
}
</string>
</entry>
</dict>
</entry>
</dict>
</entry>
</dict>
</entry>
<entry key="signers">
<array>
<bytea>0350FE40766BC0CE8D08B3F5B810E49A8352FDD458606BD5FAFE5ACDCDC8FF3F57</bytea>
</array>
</entry>
</dict>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dict>
<entry key="blockstrategy">
<dict>
<entry key="name">
<string>net.postchain.base.BaseBlockBuildingStrategy</string>
</entry>
</dict>
</entry>
<entry key="configurationfactory">
<string>net.postchain.gtx.GTXBlockchainConfigurationFactory</string>
</entry>
<entry key="gtx">
<dict>
<entry key="modules">
<array>
<string>net.postchain.rell.module.RellPostchainModuleFactory</string>
<string>net.postchain.gtx.StandardOpsGTXModule</string>
</array>
</entry>
<entry key="rell">
<dict>
<entry key="moduleArgs">
<dict>
<entry key="lib.ft3.core">
<dict>
<entry key="my_blockchain_description">
<string>FT3 vault DEVELOPMENT MODE - TESTNET</string>
</entry>
<entry key="my_blockchain_name">
<string>testnet ft3</string>
</entry>
<entry key="my_blockchain_website">
<string>https://vault-testnet.chromia.com/</string>
</entry>
<entry key="rate_limit_active">
<int>1</int>
</entry>
<entry key="rate_limit_max_points">
<int>20</int>
</entry>
<entry key="rate_limit_points_at_account_creation">
<int>1</int>
</entry>
<entry key="rate_limit_recovery_time">
<int>60000</int>
</entry>
</dict>
</entry>
<entry key="testnet">
<dict>
<entry key="admin_pub_key">
<string>03E868D007F8C9529978AEE29E989AFF963C12451E393A349C65B5516AA15C605D</string>
</entry>
</dict>
</entry>
</dict>
</entry>
<entry key="modules">
<array>
<string>testnet</string>
</array>
</entry>
<entry key="sources_v0.10">
<dict>
<entry key="lib/ft3/account/auth_basic.rell">
<string>// Auth type:
// "S" - single-sig
// "M" - multi-sig
struct single_sig_args {
flags: set<text>;
pubkey;
}
struct multi_sig_args {
flags: set<text>;
signatures_required: integer;
pubkeys: list<pubkey>;
}
function check_single_sig_auth(args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
require(participants.size() == 1);
val ss_args = single_sig_args.from_bytes(args);
return
ss_args.flags.contains_all(required_flags)
and
is_signer(participants[0]);
}
function check_multi_sig_auth(args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
val multi_sign_args = multi_sig_args.from_bytes(args);
if (not (multi_sign_args.flags.contains_all(required_flags))) return false;
var num_sign = multi_sign_args.signatures_required;
for(s in participants) {
if(is_signer(s)) {
num_sign -= 1;
if(num_sign < 1) {return true;}
}
}
return false;
}
function check_auth_args (a_t: text, args: byte_array, participants: list<byte_array>, required_flags: list<text>): boolean {
return when (a_t) {
"S" -> check_single_sig_auth(args, participants, required_flags);
"M" -> check_multi_sig_auth(args, participants, required_flags);
else -> false;
};
}
function require_auth (account, descriptor_id: byte_array, required_flags: list<text>) {
val account_auth_descriptor = account_auth_descriptor @ {
account,
.descriptor_id == descriptor_id
};
require(is_valid(account_auth_descriptor));
require(
check_auth_args(
account_auth_descriptor.auth_type,
account_auth_descriptor.args,
auth_descriptor_participant @* { account_auth_descriptor } ( .id ),
required_flags
)
);
update_auth_descriptor_rule_variables(account_auth_descriptor);
delete_expired_auth_descriptors(account);
}
</string>
</entry>
<entry key="lib/ft3/account/auth_descriptor_rule.rell">
<string>
enum rule_variable {
block_height,
block_time,
op_count
}
enum rule_operator {
lt,
le,
eq,
ge,
gt
}
struct rule_expression {
variable: text;
operator: text;
value: gtv;
}
entity auth_descriptor_rule_variable {
key account_auth_descriptor, name;
mutable value: byte_array;
}
function is_valid(auth_descriptor: account_auth_descriptor): boolean {
// check if rules is null (encoded null is equal to 0xa0020500)
if (auth_descriptor.rules == x'a0020500') {
return true;
}
return is_rule_valid(
gtv.from_bytes(auth_descriptor.rules),
load_variables(auth_descriptor)
);
}
function is_rule_valid(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val operator = text.from_gtv(parameters[1]);
if (operator == "and") {
return handle_composed_rule(rule, variables);
} else {
return handle_single_rule(rule, variables);
}
}
function handle_single_rule(rule: gtv, variables: map<text, gtv>): boolean {
return evaluate_int_variable_rule(
rule_expression.from_gtv(rule),
variables
);
}
function handle_composed_rule(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val operator = text.from_gtv(parameters[1]);
if (operator == "and") {
return is_rule_valid(parameters[0], variables) and is_rule_valid(parameters[2], variables);
} else {
return false;
}
}
function is_active(account_auth_descriptor): boolean {
if (account_auth_descriptor.rules == x'a0020500') {
return true;
}
return is_rule_active(gtv.from_bytes(account_auth_descriptor.rules), map<text, gtv>());
}
function is_rule_active(r: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = text.from_gtv(parameters[1]);
when (operator) {
"and" -> {
return is_rule_active(parameters[0], variables) and is_rule_active(parameters[2], variables);
}
else -> {
return is_simple_rule_active(r, variables);
}
}
}
function is_simple_rule_active(rule: gtv, variables: map<text, gtv>): boolean {
val parameters = list<gtv>.from_gtv(rule);
val variable_name = text.from_gtv(parameters[0]);
when (rule_variable.value(variable_name)) {
rule_variable.block_height -> {
return is_block_height_rule_active(rule);
}
rule_variable.block_time -> {
return is_block_time_rule_active(rule);
}
rule_variable.op_count -> {
return is_op_count_rule_active(rule);
}
}
}
/*
* Variables
*/
function evaluate_int_variable_rule(rule: rule_expression, variables: map<text, gtv>): boolean {
val variable = rule_variable.value(rule.variable);
val operator = rule_operator.value(rule.operator);
val current_value = variable_value(variable, variables);
val defined_value = integer.from_gtv(rule.value);
when (operator) {
rule_operator.lt -> {
return current_value < defined_value;
}
rule_operator.le -> {
return current_value <= defined_value;
}
rule_operator.eq -> {
return current_value == defined_value;
}
rule_operator.ge -> {
return current_value >= defined_value;
}
rule_operator.gt -> {
return current_value > defined_value;
}
}
}
/*
* Called from _add_auth_descriptor function
*/
function setup_auth_descriptor_variables(account_auth_descriptor) {
if (account_auth_descriptor.rules == x'a0020500') {
return;
}
val variables = get_variable_names_set(gtv.from_bytes(account_auth_descriptor.rules));
for (variable in variables) {
when (variable) {
rule_variable.op_count.name -> {
create auth_descriptor_rule_variable(
account_auth_descriptor,
name = variable,
value = op_count_variable_initial_value().to_bytes()
);
}
}
}
}
function update_variables(account_auth_descriptor, variables: map<text, gtv>) {
for ((name, value) in variables) {
val variable = auth_descriptor_rule_variable @ { account_auth_descriptor, name };
val int_value = integer.from_gtv(value);
when (name) {
rule_variable.op_count.name -> {
variable.value = (int_value + 1).to_gtv().to_bytes();
}
}
}
}
function variable_value(variable: rule_variable, variables: map<text, gtv>): integer {
when (variable) {
rule_variable.block_height -> {
return op_context.block_height;
}
rule_variable.block_time -> {
return op_context.last_block_time;
}
rule_variable.op_count -> {
return integer.from_gtv(variables[rule_variable.op_count.name]);
}
}
}
/*
* Analyze the rules before assigning them to an authenticator descriptor
*/
function analyze_auth_descriptor_rules(rules: gtv, hop: integer) {
require(hop<3, "too many rules");
val rules_bytes = rules.to_bytes();
if (rules_bytes == x'a0020500') {
return;
}
val params = list<gtv>.from_gtv(rules);
require(params.size() <=3);
val operator = text.from_gtv(params[1]);
if(operator == "and") {
analyze_auth_descriptor_rules(params[0], hop+1);
analyze_auth_descriptor_rules(params[2], hop+1);
}
return;
}
/*
* Returns set of variable names used in a rule
*/
function get_variable_names_set(rules: gtv): set<text> {
val parameters = list<gtv>.from_gtv(rules);
val operator = text.from_gtv(parameters[1]);
var vars = set<text>();
if (operator == "and") {
vars.add_all(get_variable_names_set(parameters[0]));
vars.add_all(get_variable_names_set(parameters[2]));
} else {
vars.add(text.from_gtv(parameters[0]));
}
return vars;
}
function load_variables(account_auth_descriptor): map<text, gtv> {
val variables = auth_descriptor_rule_variable @* { account_auth_descriptor };
val variables_map = map<text, gtv>();
for (variable in variables) {
variables_map[variable.name] = gtv.from_bytes(variable.value);
}
return variables_map;
}
function update_auth_descriptor_rule_variables(account_auth_descriptor) {
val variables = load_variables(account_auth_descriptor);
update_variables(account_auth_descriptor, variables);
}
/*
* auth descriptor cleanup
*/
function delete_expired_auth_descriptors(account) {
val auth_descriptors = account_auth_descriptor @* { account };
for (auth_descriptor in auth_descriptors) {
if (not (is_active(auth_descriptor) and is_valid(auth_descriptor))) {
_delete_auth_descriptor(auth_descriptor);
}
}
}
/****************************************************************************************
* block_time *
****************************************************************************************/
function is_block_time_rule_active(r: gtv): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = rule_operator.value(text.from_gtv(parameters[1]));
val value = integer.from_gtv(parameters[2]);
when (operator) {
rule_operator.gt -> {
return op_context.last_block_time > value;
}
rule_operator.ge,
rule_operator.eq -> {
return op_context.last_block_time >= value;
}
else -> {
return true;
}
}
}
/****************************************************************************************
* block_height *
****************************************************************************************/
function is_block_height_rule_active(r: gtv): boolean {
val parameters = list<gtv>.from_gtv(r);
val operator = rule_operator.value(text.from_gtv(parameters[1]));
val value = integer.from_gtv(parameters[2]);
when (operator) {
rule_operator.gt -> {
return op_context.block_height > value;
}
rule_operator.ge,
rule_operator.eq -> {
return op_context.block_height >= value;
}
else -> {
return true;
}
}
}
/****************************************************************************************
* op_count *
****************************************************************************************/
function op_count_variable_initial_value(): gtv {
return (1).to_gtv();
}
function is_op_count_rule_active(rule: gtv): boolean {
return true;
}</string>
</entry>
<entry key="lib/ft3/account/module.rell">
<string>@mount('ft3')
module;
import core: ^.core;
struct auth_descriptor {
auth_type: text;
participants: list<byte_array>;
args: list<gtv>;
rules: gtv;
}
entity account {
key id: byte_array;
}
entity account_meta {
meta_key: text;
value: byte_array;
index account, meta_key;
index meta_key, value;
}
entity account_auth_descriptor {
descriptor_id: byte_array;
key account, descriptor_id;
index descriptor_id;
auth_type: text;
args: byte_array;
rules: byte_array;
}
entity auth_descriptor_participant {
key account_auth_descriptor, id: byte_array;
}
// rate limiter
entity rl_state {
key account;
mutable points: integer;
mutable last_update: timestamp;
mutable max_points: integer;
mutable recovery_time: timestamp;
}
function _add_auth_descriptor (account, auth_descriptor) {
val descriptor_id = auth_descriptor.hash();
analyze_auth_descriptor_rules(auth_descriptor.rules, 0);
val account_auth_descriptor = create account_auth_descriptor (
account, descriptor_id,
auth_descriptor.auth_type,
args = auth_descriptor.args.to_gtv().to_bytes(),
rules = auth_descriptor.rules.to_gtv().to_bytes()
);
for (participant in auth_descriptor.participants) {
require(is_signer(participant));
create auth_descriptor_participant(account_auth_descriptor, id = participant);
}
setup_auth_descriptor_variables(account_auth_descriptor);
}
function _delete_auth_descriptor(auth_descriptor: account_auth_descriptor) {
delete auth_descriptor_rule_variable @* { auth_descriptor };
delete auth_descriptor_participant @* { auth_descriptor };
delete auth_descriptor;
}
function _delete_all_auth_descriptors_exclude(account, auth_descriptor_id: byte_array) {
delete (adrv: auth_descriptor_rule_variable, ad: account_auth_descriptor) @* {
ad.account == account, ad.descriptor_id != auth_descriptor_id, adrv.account_auth_descriptor == ad
};
delete (adp: auth_descriptor_participant, ad: account_auth_descriptor) @* {
ad.account == account and ad.descriptor_id != auth_descriptor_id and adp.account_auth_descriptor == ad
};
delete account_auth_descriptor @* {
.account == account,
.descriptor_id != auth_descriptor_id
};
}
function create_account_with_auth (auth_descriptor): byte_array {
val id = auth_descriptor.hash();
val acc = create account (id);
_add_auth_descriptor(
acc, auth_descriptor
);
val max_points = core.my_blockchain_info.rate_limit_max_points;
val recovery_time = core.my_blockchain_info.rate_limit_recovery_time;
val points_at_start = core.my_blockchain_info.rate_limit_points_at_account_creation;
create rl_state(acc, .points = points_at_start, .last_update = op_context.last_block_time, max_points, recovery_time);
return id;
}
function auth_and_log(account_id: byte_array, auth_descriptor_id: byte_array, required_flags: list<text>): account {
val account = account @ { .id == account_id };
rate_limit(account);
require_auth(account, auth_descriptor_id, required_flags);
return account;
}
query is_auth_descriptor_valid(account_id: byte_array, auth_descriptor_id: byte_array) {
return is_valid(account_auth_descriptor @ {
.account.id == account_id,
.descriptor_id == auth_descriptor_id
});
}
query get_account_auth_descriptors(id: byte_array) {
return account_auth_descriptor @* {
.account.id == id
} (
id = .descriptor_id,
type = .auth_type,
args = .args
);
}
query get_account_by_id(id: byte_array) {
return account @? { id } ( .id );
}
query get_account_by_auth_descriptor(auth_descriptor) {
return account @? { .id == auth_descriptor.hash() } ( .id );
}
query get_accounts_by_participant_id(id: byte_array) {
return auth_descriptor_participant @* { id } ( .account_auth_descriptor.account.id );
}
query get_accounts_by_auth_descriptor_id(descriptor_id: byte_array) {
return account_auth_descriptor @* { descriptor_id } ( .account.id );
}
</string>
</entry>
<entry key="lib/ft3/account/operations/module.rell">
<string>@mount('ft3')
module;
import acc: ^^.account;
import core: ^^.core;
operation delete_auth_descriptor(account_id: byte_array, auth_descriptor_id: byte_array, delete_descriptor_id: byte_array) {
val flags = if (auth_descriptor_id == delete_descriptor_id) list<text>() else ["A"];
val account = acc.auth_and_log(account_id, auth_descriptor_id, flags);
acc._delete_auth_descriptor(acc.account_auth_descriptor @ {
.account == account,
.descriptor_id == delete_descriptor_id
});
}
operation delete_all_auth_descriptors_exclude(account_id: byte_array, auth_descriptor_id: byte_array) {
val account = acc.auth_and_log(account_id, auth_descriptor_id, ["A"]);
acc._delete_all_auth_descriptors_exclude(account, auth_descriptor_id);
}
operation add_auth_descriptor (account_id: byte_array, auth_id: byte_array, new_desc: acc.auth_descriptor) {
val account = acc.auth_and_log(account_id, auth_id, ["A"]);
acc._add_auth_descriptor(account, new_desc);
}
</string>
</entry>
<entry key="lib/ft3/account/rate_limit.rell">
<string>import ^.core;
function rate_limit (account) {
if(core.my_blockchain_info.rate_limit_active == false ) return;
val max_counter = core.my_blockchain_info.rate_limit_max_points;
val recovery_time = core.my_blockchain_info.rate_limit_recovery_time;
val stat = rl_state@{account} (.points, .last_update);
val delta = op_context.last_block_time - stat.last_update;
var got_points = 0;
var update_time = stat.last_update;
if (delta > recovery_time)
{
got_points = delta / recovery_time;
update_time = stat.last_update + got_points * recovery_time;
if (got_points + stat.points > max_counter) {
got_points = max_counter - stat.points;
// if user is at maximum reset his
// timer
update_time = op_context.last_block_time;
}
}
require(stat.points + got_points > 0);
update rl_state@{account} (
.points += got_points - 1,
.last_update = update_time
);
}
query get_account_rate_limit_last_update(account_id: byte_array) {
if(core.my_blockchain_info.rate_limit_active == false ) return (points = 0, last_update = 0);
val t= rl_state@{.account.id == account_id}
(
.points,
.last_update
);
return t;
}
query get_last_timestamp(): integer {
return (block@{.timestamp>0}(@sort_desc .timestamp) limit 1);
}</string>
</entry>
<entry key="lib/ft3/core/asset.rell">
<string>import acc: ^.account;
entity asset {
id: byte_array;
key id;
name;
issuing_chain_rid: byte_array;
}
entity balance {
key acc.account, asset;
mutable amount: integer = 0;
}
function ensure_balance(acc.account, asset): balance {
val balance = balance @? {asset, account};
if (balance != null) {
return balance;
}
else return create balance(account, asset, amount = 0);
}
function register_asset (name, issuing_chain_rid: byte_array): asset {
val id = (name, issuing_chain_rid).hash();
return create asset (id, name, issuing_chain_rid);
}
function _get_asset_balances(account_id: byte_array): list<(id:byte_array,name:text,amount:integer,chain_id:byte_array)> {
return balance @* { .account.id == account_id } (
id = .asset.id,
name = .asset.name,
amount = .amount,
chain_id = .asset.issuing_chain_rid
);
}
query get_asset_balances(account_id: byte_array) {
return _get_asset_balances(account_id);
}
query get_asset_balance(account_id: byte_array, asset_id: byte_array) {
return balance @? {
.account.id == account_id,
.asset.id == asset_id
} (
id = .asset.id,
name = .asset.name,
amount = .amount,
chain_id = .asset.issuing_chain_rid
);
}
query get_asset_by_name(name) {
return asset @* { name } (
.id,
.name,
.issuing_chain_rid
);
}
query get_asset_by_id(asset_id: byte_array) {
return asset@{
.id == asset_id
}(
id = .id,
name = .name,
issuing_chain_rid = .issuing_chain_rid
);
}
query get_all_assets() {
return asset@*{}(
id = .id,
name = .name,
issuing_chain_rid = .issuing_chain_rid
);
}</string>
</entry>
<entry key="lib/ft3/core/dev_op.rell">
<string>module;
import ft3: ^^.core;
import acc: ^^.account;
object dev_state {
mutable allow_dev_ops: boolean = true;
}
function require_dev() {
require(dev_state.allow_dev_ops);
}
function dev_give_points_(account: acc.account, amount: integer) {
require_dev();
update acc.rl_state@{account}(.points += amount);
}</string>
</entry>
<entry key="lib/ft3/core/history.rell">
<string>import acc: ^.account;
@log entity payment_history_entry {
index acc.account, asset;
delta: integer;
// helps to locate exact position of payment entry in transaction
op_index: integer;
is_input: boolean;
entry_index: integer;
}
query get_payment_history(account_id: byte_array, after_block: integer) {
return payment_history_entry @* {
acc.account @ { account_id },
.transaction.block.block_height > after_block
} (
delta = .delta,
asset = .asset.name,
asset_id = .asset.id,
is_input = .is_input,
timestamp = .transaction.block.timestamp,
block_height = .transaction.block.block_height,
entry_index = .entry_index,
tx_rid = .transaction.tx_rid,
tx_data = .transaction.tx_data
);
}
</string>
</entry>
<entry key="lib/ft3/core/module.rell">
<string>@mount('ft3')
module;
struct module_args {
my_blockchain_name: text;
my_blockchain_website: text;
my_blockchain_description: text;
rate_limit_active: boolean;
rate_limit_max_points: integer;
rate_limit_recovery_time: integer;
rate_limit_points_at_account_creation: integer;
}</string>
</entry>
<entry key="lib/ft3/core/my_blockchain_info.rell">
<string>
object my_blockchain_info {
mutable name: text = chain_context.args.my_blockchain_name;
mutable website: text = chain_context.args.my_blockchain_website;
mutable description: text = chain_context.args.my_blockchain_description;
mutable rate_limit_active: boolean = chain_context.args.rate_limit_active;
mutable rate_limit_max_points: integer = chain_context.args.rate_limit_max_points;
mutable rate_limit_recovery_time: integer = chain_context.args.rate_limit_recovery_time;
mutable rate_limit_points_at_account_creation: integer = chain_context.args.rate_limit_points_at_account_creation;
}
//operation update_blockchain(name, website: text, description: text) {
// blockchain.name = name;
// blockchain.website = website;
// blockchain.description = description;
//}
query get_blockchain_info() {
return (
name = my_blockchain_info.name,
website = my_blockchain_info.website,
description = my_blockchain_info.description,
rate_limit_active = my_blockchain_info.rate_limit_active,
rate_limit_max_points = my_blockchain_info.rate_limit_max_points,
rate_limit_recovery_time = my_blockchain_info.rate_limit_recovery_time,
rate_limit_points_at_account_creation = chain_context.args.rate_limit_points_at_account_creation
);
}
</string>
</entry>
<entry key="lib/ft3/core/operations/transfer.rell">
<string>@mount('ft3')
module;
import ft3: ^^;
operation transfer (inputs: list<ft3.xfer_input>, outputs: list<ft3.xfer_output>) {
ft3._transfer(inputs, outputs);
}</string>
</entry>
<entry key="lib/ft3/core/transfer.rell">
<string>import acc: ^.account;
struct xfer_input {
account_id: byte_array;
asset_id: byte_array;
auth_descriptor_id: byte_array;
amount: integer;
extra: map<text, gtv>;
}
struct xfer_output {
account_id: byte_array;
asset_id: byte_array;
amount: integer;
extra: map<text, gtv>;
}
function deduct_balance (acc.account, asset, d_amount: integer) {
val b = balance @ { account, asset };
require( b.amount >= d_amount, "Balance is too low");
update b (amount -= d_amount);
}
function consume_input (i: xfer_input, idx: integer, assets: map<asset, integer>): payment_history_entry {
val asset = asset @ { .id == i.asset_id };
val account = acc.auth_and_log(i.account_id, i.auth_descriptor_id, ["T"]);
require( i.amount > 0, "Amount should be non 0 positive." );
deduct_balance(account, asset, i.amount);
assets[asset] = i.amount + if (asset in assets) assets[asset] else 0;
return create payment_history_entry (
account,
asset,
.delta = i.amount,
.op_index = 500, //op_context ???
.is_input = true,
.entry_index = idx
);
}
function process_transfer_output (o: xfer_output, idx: integer, available_assets: map<asset, integer>): payment_history_entry {
if (o.extra.contains("reg_auth_desc")) {
val auth_desc = acc.auth_descriptor.from_gtv(o.extra["reg_auth_desc"]);
require(o.account_id
== acc.create_account_with_auth(auth_desc));
}
val target_account = acc.account @ { .id == o.account_id };
val asset = asset @ { .id == o.asset_id };
require(o.amount > 0, "You cannot tx output negative numbers");
require( available_assets[asset] >= o.amount );
available_assets[asset] -= o.amount;
require(available_assets[asset] >= 0);
val balance = ensure_balance(target_account, asset);
balance.amount += o.amount;
return create payment_history_entry (
.account = target_account,
asset,
.delta = o.amount,
.op_index = 500, // something from op_context,
.is_input = false,
.entry_index = idx
);
}
function _transfer (inputs: list<xfer_input>, outputs: list<xfer_output>) {
val sum_inputs = map<asset, integer>();
var idx = 0;
for (i in inputs) {
consume_input(i, idx, sum_inputs);
idx += 1;
}
idx = 0;
for (o in outputs) {
process_transfer_output(o, idx, sum_inputs);
idx += 1;
}
}
</string>
</entry>
<entry key="lib/ft3/crosschain/blockchain.rell">
<string>import ft3: ^.core;
import acc: ^.account;
entity asset_origin {
key ft3.asset;
origin_chain_rid: byte_array;
}
entity linked_chain {
key chain_rid: byte_array;
}
function blockchain_account_id (blockchain_rid: byte_array): byte_array {
return ("B", blockchain_rid).hash();
}
function ensure_blockchain_account (blockchain_rid: byte_array): acc.account {
val blockchain_account_id = blockchain_account_id(blockchain_rid);
val acc = acc.account @? { .id == blockchain_account_id };
return if (acc??) acc
else create acc.account (id = blockchain_account_id);
}
function get_blockchain_account (blockchain_rid: byte_array): acc.account {
return acc.account @ { .id == blockchain_account_id(blockchain_rid) };
}
query get_linked_chains() {
return linked_chain @* {} ( .chain_rid );
}
query is_linked_with_chain(chain_rid: byte_array) {
return linked_chain @? { chain_rid } != null;
}
</string>
</entry>
<entry key="lib/ft3/crosschain/module.rell">
<string>@mount('ft3.xc')
module;
</string>
</entry>
<entry key="lib/ft3/crosschain/structures.rell">
<string>struct operation_rec {
name;
args: list<gtv>;
}
struct transaction_body_rec {
blockchain_rid: byte_array;
operations: list<operation_rec>;
signers: list<gtv>;
}
struct transaction_rec {
body: transaction_body_rec;
signatures: list<gtv>;
}
struct transaction_proof {
tx: virtual<transaction_rec>;
// block_header: byte_array;
// merkle_path: virtual<list<byte_array>>;
// tx_index: integer;
block_rid: byte_array;
block_signers: list<byte_array>;
signatures: list<byte_array>;
}
function verify_transaction_proof( p: transaction_proof ) {
// TODO
}</string>
</entry>
<entry key="lib/ft3/crosschain/transfer.rell">
<string>import ft3: ^.core;
struct xc_target {
account_id: byte_array;
extra: map<text, gtv>;
}
function add_to_blockchain_balance (blockchain_rid: byte_array, ft3.asset, amount: integer) {
require( chain_context.blockchain_rid != blockchain_rid ); // cannot add to own account
val origin_chain_rid = asset_origin @? { asset } .origin_chain_rid;
if (exists(origin_chain_rid) and (blockchain_rid == origin_chain_rid)) {
// do nothing. in future we might track origin
} else {
val bc_acc = ensure_blockchain_account(blockchain_rid);
ft3.ensure_balance(bc_acc, asset).amount += amount;
}
}
struct init_xfer_args {
source: ft3.xfer_input;
target: xc_target;
hops: list<byte_array>;
}
entity applied_transfers { key tx_rid: byte_array; }
</string>
</entry>
<entry key="lib/ft3/ft3_xc_basic.rell">
<string>module;
namespace ft3 {
import acc: ^.account;
import acc_op: ^.account.operations;
import transfer_op: ^.core.operations.transfer;
import xc: ^.crosschain;
}
</string>
</entry>
<entry key="lib/ft3/operations/dev_op_account.rell">
<string>@mount('ft3')
module;
import dev_op: ^^.core.dev_op;
import acc: ^^.account;
operation dev_register_account (acc.auth_descriptor) {
dev_op.require_dev();
acc.create_account_with_auth( auth_descriptor );
}
</string>
</entry>
<entry key="testnet/admin.rell">
<string>import ft3_dev_op: ^.lib.ft3.core.dev_op;
import ft3_core: ^.lib.ft3.core;
import ft3_acc : ^.lib.ft3.account;
function require_dev_admin() {
ft3_dev_op.require_dev();
val adminPubKeyBytes = byte_array(chain_context.args.admin_pub_key);
require(is_signer(adminPubKeyBytes), "Needs to be signed by admin");
}
operation dev_register_asset (name, issuing_chain_rid: byte_array) {
ft3_dev_op.require_dev();
require_dev_admin();
ft3_core.register_asset(name, issuing_chain_rid);
}
operation dev_give_balance (asset_id: byte_array, account_id: byte_array, amount: integer) {
ft3_dev_op.require_dev();
require_dev_admin();
require(amount >= 0);
ft3_core.ensure_balance( ft3_acc.account @ { account_id }, ft3_core.asset @ { .id == asset_id } ).amount += amount;
}
operation dev_register_account (ft3_acc.auth_descriptor) {
ft3_dev_op.require_dev();
ft3_acc.create_account_with_auth( auth_descriptor );
}
/*
* Can be negative
*/
operation dev_give_points(account_id: byte_array, amount: integer){
ft3_dev_op.require_dev();
require_dev_admin();
val account = ft3_acc.account@{.id == account_id};
ft3_dev_op.dev_give_points_(account, amount);
}
operation dev_free_op(account_id: byte_array) {
ft3_dev_op.require_dev();
require_dev_admin();
// this operation does not cost points in the rate limit
val account = ft3_acc.account@{.id == account_id};
ft3_dev_op.dev_give_points_(account, 1);
ft3_acc.rate_limit(account);
}</string>
</entry>
<entry key="testnet/module.rell">
<string>
import ^.lib.ft3.ft3_xc_basic;
import ^.lib.ft3.operations.dev_op_account;
struct module_args {
admin_pub_key: text;
}
</string>
</entry>
</dict>
</entry>
</dict>
</entry>
</dict>
</entry>
<entry key="signers">
<array>
<bytea>0350FE40766BC0CE8D08B3F5B810E49A8352FDD458606BD5FAFE5ACDCDC8FF3F57</bytea>
</array>
</entry>
</dict>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.