110 lines
3.0 KiB
Plaintext
110 lines
3.0 KiB
Plaintext
#pragma version ^0.4.1;
|
|
#include "../imports/stdlib.fc";
|
|
#include "../utils/Constants.fc";
|
|
#include "../utils/Math.fc";
|
|
#include "../utils/Security.fc";
|
|
|
|
;; Operation codes
|
|
const int op_register = 1;
|
|
const int op_remove = 2;
|
|
const int op_submit_sig = 3;
|
|
const int op_update_threshold = 4;
|
|
const int op_challenge = 5;
|
|
|
|
;; Error codes
|
|
const int error_unauthorized = 101;
|
|
const int error_invalid_sig = 102;
|
|
const int error_validator_exists = 103;
|
|
const int error_validator_not_found = 104;
|
|
const int error_invalid_threshold = 105;
|
|
const int error_insufficient_stake = 106;
|
|
|
|
;; Storage
|
|
global slice owner;
|
|
global int sig_threshold;
|
|
global int min_stake;
|
|
global int total_validators;
|
|
global cell validators_dict;
|
|
|
|
() load_data() impure {
|
|
slice ds = get_data().begin_parse();
|
|
owner = ds~load_msg_addr();
|
|
sig_threshold = ds~load_uint(8);
|
|
min_stake = ds~load_coins();
|
|
total_validators = ds~load_uint(16);
|
|
validators_dict = ds~load_dict();
|
|
}
|
|
|
|
() save_data() impure {
|
|
set_data(begin_cell()
|
|
.store_slice(owner)
|
|
.store_uint(sig_threshold, 8)
|
|
.store_coins(min_stake)
|
|
.store_uint(total_validators, 16)
|
|
.store_dict(validators_dict)
|
|
.end_cell());
|
|
}
|
|
|
|
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
|
|
if (in_msg_body.slice_empty?()) {
|
|
return ();
|
|
}
|
|
|
|
slice cs = in_msg_full.begin_parse();
|
|
int flags = cs~load_uint(4);
|
|
if (flags & 1) {
|
|
return ();
|
|
}
|
|
|
|
slice sender = cs~load_msg_addr();
|
|
int op = in_msg_body~load_uint(32);
|
|
|
|
load_data();
|
|
|
|
if (op == op_register) {
|
|
handle_registration(sender, msg_value, in_msg_body);
|
|
} elseif (op == op_submit_sig) {
|
|
handle_signature(sender, in_msg_body);
|
|
} elseif (op == op_challenge) {
|
|
handle_challenge(sender, in_msg_body);
|
|
}
|
|
|
|
save_data();
|
|
}
|
|
|
|
() handle_registration(slice sender, int stake, slice msg) impure {
|
|
throw_unless(error_insufficient_stake, stake >= min_stake);
|
|
|
|
(_, int found) = validators_dict.udict_get?(267, slice_hash(sender));
|
|
throw_if(error_validator_exists, found);
|
|
|
|
slice pubkey = msg~load_uint(256);
|
|
cell metadata = msg~load_ref();
|
|
|
|
cell validator_data = begin_cell()
|
|
.store_coins(stake)
|
|
.store_uint(1, 2) ;; status = active
|
|
.store_uint(0, 32) ;; signed_messages
|
|
.store_uint(now(), 64)
|
|
.end_cell();
|
|
|
|
validators_dict~udict_set(267, slice_hash(sender), validator_data);
|
|
total_validators += 1;
|
|
}
|
|
|
|
() handle_signature(slice sender, slice msg) impure {
|
|
(cell validator_data, int found) = validators_dict.udict_get?(267, slice_hash(sender));
|
|
throw_unless(error_validator_not_found, found);
|
|
|
|
slice ds = validator_data.begin_parse();
|
|
int stake = ds~load_coins();
|
|
int status = ds~load_uint(2);
|
|
throw_unless(error_unauthorized, status == 1);
|
|
|
|
;; Rest of signature handling
|
|
}
|
|
|
|
int get_validator_count() method_id {
|
|
load_data();
|
|
return total_validators;
|
|
} |