hf alias and mining

This commit is contained in:
Arionum
2018-08-06 00:13:36 +03:00
parent 066211b2c0
commit 8b006fc26a
15 changed files with 918 additions and 143 deletions

View File

@@ -93,7 +93,71 @@ class Account
return true;
}
//check alias validity
public function free_alias($id)
{
global $db;
$orig=$id;
$id=strtoupper($id);
$id = san($id);
if (strlen($id)<4||strlen($id)>25) {
return false;
}
if($orig!=$id){
return false;
}
if ($db->single("SELECT COUNT(1) FROM accounts WHERE alias=:alias", [":alias"=>$id])==0) {
return true;
} else {
return false;
}
}
//check if an account already has an alias
public function has_alias($public_key){
global $db;
$public_key=san($public_key);
$res=$db->single("SELECT COUNT(1) FROM accounts WHERE public_key=:public_key AND alias IS NOT NULL",[":public_key"=>$public_key]);
if($res!=0) return true;
else return false;
}
//check alias validity
public function valid_alias($id)
{
global $db;
$orig=$id;
$banned=["MERCURY","DEVS","DEVELOPMENT", "MARKETING", "MERCURY80","DEVARO", "DEVELOPER","DEVELOPERS","ARODEV", "DONATION","MERCATOX", "OCTAEX", "MERCURY", "ARIONUM", "ESCROW","OKEX","BINANCE","CRYPTOPIA","HUOBI","ITFINEX","HITBTC","UPBIT","COINBASE","KRAKEN","BITSTAMP","BITTREX","POLONIEX"];
$id=strtoupper($id);
$id = san($id);
if (in_array($id, $banned)) {
return false;
}
if (strlen($id)<4||strlen($id)>25) {
return false;
}
if($orig!=$id){
return false;
}
return $db->single("SELECT COUNT(1) FROM accounts WHERE alias=:alias", [":alias"=>$id]);
}
//returns the account of an alias
public function alias2account($alias){
global $db;
$alias=strtoupper($alias);
$res=$db->single("SELECT id FROM accounts WHERE alias=:alias LIMIT 1",[":alias"=>$alias]);
return $res;
}
//returns the alias of an account
public function account2alias($id){
global $db;
$id=san($id);
$res=$db->single("SELECT alias FROM accounts WHERE id=:id LIMIT 1",[":id"=>$id]);
return $res;
}
// check the validity of an address. At the moment, it checks only the characters to be base58 and the length to be >=70 and <=128.
public function valid($id)
{
@@ -234,4 +298,12 @@ class Account
$res = $db->single("SELECT public_key FROM accounts WHERE id=:id", [":id" => $id]);
return $res;
}
public function get_masternode($public_key){
global $db;
$res = $db->row("SELECT * FROM masternode WHERE public_key=:public_key", [":public_key" => $public_key]);
if(empty($res['public_key'])) return false;
return $res;
}
}

View File

@@ -2,7 +2,7 @@
class Block
{
public function add($height, $public_key, $nonce, $data, $date, $signature, $difficulty, $reward_signature, $argon)
public function add($height, $public_key, $nonce, $data, $date, $signature, $difficulty, $reward_signature, $argon, $bootstrapping=false)
{
global $db;
$acc = new Account();
@@ -24,16 +24,17 @@ class Block
// create the block data and check it against the signature
$info = "{$generator}-{$height}-{$date}-{$nonce}-{$json}-{$difficulty}-{$argon}";
if (!$acc->check_signature($info, $signature, $public_key)) {
_log("Block signature check failed");
return false;
}
if (!$bootstrapping) {
if (!$acc->check_signature($info, $signature, $public_key)) {
_log("Block signature check failed");
return false;
}
if (!$this->parse_block($hash, $height, $data, true)) {
_log("Parse block failed");
return false;
if (!$this->parse_block($hash, $height, $data, true)) {
_log("Parse block failed");
return false;
}
}
// lock table to avoid race conditions on blocks
$db->exec("LOCK TABLES blocks WRITE, accounts WRITE, transactions WRITE, mempool WRITE");
@@ -55,13 +56,14 @@ class Block
$transaction['signature'] = $reward_signature;
// hash the transaction
$transaction['id'] = $trx->hash($transaction);
// check the signature
$info = $transaction['val']."-".$transaction['fee']."-".$transaction['dst']."-".$transaction['message']."-".$transaction['version']."-".$transaction['public_key']."-".$transaction['date'];
if (!$acc->check_signature($info, $reward_signature, $public_key)) {
_log("Reward signature failed");
return false;
if (!$bootstrapping) {
// check the signature
$info = $transaction['val']."-".$transaction['fee']."-".$transaction['dst']."-".$transaction['message']."-".$transaction['version']."-".$transaction['public_key']."-".$transaction['date'];
if (!$acc->check_signature($info, $reward_signature, $public_key)) {
_log("Reward signature failed");
return false;
}
}
// insert the block into the db
$db->beginTransaction();
$total = count($data);
@@ -92,7 +94,12 @@ class Block
$trx->add($hash, $height, $transaction);
// parse the block's transactions and insert them to db
$res = $this->parse_block($hash, $height, $data, false);
$res = $this->parse_block($hash, $height, $data, false, $bootstrapping);
if (($height-1)%3==2 && $height>=80000) {
$this->blacklist_masternodes();
$this->reset_fails_masternodes($public_key, $height, $hash);
}
// if any fails, rollback
if ($res == false) {
$db->rollback();
@@ -104,6 +111,41 @@ class Block
return true;
}
// resets the number of fails when winning a block and marks it with a transaction
public function reset_fails_masternodes($public_key, $height, $hash)
{
global $db;
$res=$this->masternode_log($public_key, $height, $hash);
if ($res) {
$db->run("UPDATE masternode SET last_won=:last_won,fails=0 WHERE public_key=:public_key", [":public_key"=>$public_key, ":last_won"=>$height]);
}
}
//logs the current masternode status
public function masternode_log($public_key, $height, $hash)
{
global $db;
$mn=$db->row("SELECT blacklist,last_won,fails FROM masternode WHERE public_key=:public_key", [":public_key"=>$public_key]);
if (!$mn) {
return false;
}
$id = hex2coin(hash("sha512", "resetfails-$hash-$height-$public_key"));
$msg="$mn[blacklist],$mn[last_won],$mn[fails]";
$db->run(
"INSERT into transactions SET id=:id, block=:block, height=:height, dst=:dst, val=0, fee=0, signature=:sig, version=111, message=:msg, date=:date, public_key=:public_key",
[":id"=>$id, ":block"=>$hash, ":height"=>$height, ":dst"=>$hash, ":sig"=>$hash, ":msg"=>$msg, ":date"=>time(), ":public_key"=>$public_key]
);
return true;
}
// returns the current block, without the transactions
public function current()
{
@@ -155,23 +197,70 @@ class Block
return $current['difficulty'];
}
// elapsed time between the last 20 blocks
$first = $db->row("SELECT `date` FROM blocks ORDER by height DESC LIMIT $limit,1");
$time = $current['date'] - $first['date'];
// before mnn hf
if ($height<80000) {
// elapsed time between the last 20 blocks
$first = $db->row("SELECT `date` FROM blocks ORDER by height DESC LIMIT $limit,1");
$time = $current['date'] - $first['date'];
// avg block time
$result = ceil($time / $limit);
// avg block time
$result = ceil($time / $limit);
_log("Block time: $result", 3);
// if larger than 200 sec, increase by 5%
if ($result > 220) {
$dif = bcmul($current['difficulty'], 1.05);
} elseif ($result < 260) {
// if lower, decrease by 5%
$dif = bcmul($current['difficulty'], 0.95);
// if larger than 200 sec, increase by 5%
if ($result > 220) {
$dif = bcmul($current['difficulty'], 1.05);
} elseif ($result < 260) {
// if lower, decrease by 5%
$dif = bcmul($current['difficulty'], 0.95);
} else {
// keep current difficulty
$dif = $current['difficulty'];
}
} else {
// keep current difficulty
$dif = $current['difficulty'];
// hardfork 80000, fix difficulty targetting
$type=$height%3;
// for mn, we use gpu diff
if ($type == 2) {
return $current['difficulty'];
}
$blks=0;
$total_time=0;
$blk = $db->run("SELECT `date`, height FROM blocks ORDER by height DESC LIMIT 60");
for ($i=0;$i<59;$i++) {
$ctype=$blk[$i+1]['height']%3;
$time=$blk[$i]['date']-$blk[$i+1]['date'];
if ($type!=$ctype) {
continue;
}
$blks++;
$total_time+=$time;
}
$result=ceil($total_time/$blks);
_log("Block time: $result", 3);
// if larger than 260 sec, increase by 5%
if ($result > 260) {
$dif = bcmul($current['difficulty'], 1.05);
} elseif ($result < 220) {
// if lower, decrease by 5%
$dif = bcmul($current['difficulty'], 0.95);
} else {
// keep current difficulty
$dif = $current['difficulty'];
}
}
if (strpos($dif, '.') !== false) {
$dif = substr($dif, 0, strpos($dif, '.'));
}
@@ -183,7 +272,7 @@ class Block
if ($dif > 9223372036854775800) {
$dif = 9223372036854775800;
}
_log("Difficulty: $dif", 3);
return $dif;
}
@@ -233,8 +322,13 @@ class Block
return false;
}
$acc = new Account();
// generator's public key must be valid
if($data['date']>time()+30){
_log("Future block - $data[date] $data[public_key]",2);
return false;
}
// generator's public key must be valid
if (!$acc->valid_key($data['public_key'])) {
_log("Invalid public key - $data[public_key]");
return false;
@@ -247,7 +341,7 @@ class Block
}
//check the argon hash and the nonce to produce a valid block
if (!$this->mine($data['public_key'], $data['nonce'], $data['argon'])) {
if (!$this->mine($data['public_key'], $data['nonce'], $data['argon'], $data['difficulty'], 0, 0, $data['date'])) {
_log("Mine check failed");
return false;
}
@@ -322,27 +416,148 @@ class Block
}
return true;
}
public function blacklist_masternodes()
{
global $db;
_log("Checking if there are masternodes to be blacklisted", 2);
$current = $this->current();
if (($current['height']-1)%3!=2) {
_log("bad height");
return;
}
$last=$this->get($current['height']-1);
$total_time=$current['date']-$last['date'];
_log("blacklist total time $total_time");
if ($total_time<=600) {
return;
}
$tem=floor($total_time/600);
_log("We have masternodes to blacklist - $tem", 2);
$ban=$db->run(
"SELECT public_key, blacklist, fails, last_won FROM masternode WHERE status=1 AND blacklist<:current AND height<:start ORDER by last_won ASC, public_key ASC LIMIT 0,$tem",
[":current"=>$last['height'], ":start"=>$last['height']-360]
);
_log(json_encode($ban));
$i=0;
foreach ($ban as $b) {
$this->masternode_log($b['public_key'], $current['height'], $current['id']);
_log("Blacklisting masternode - $i $b[public_key]", 2);
$db->run("UPDATE masternode SET fails=fails+1, blacklist=:blacklist WHERE public_key=:public_key", [":public_key"=>$b['public_key'], ":blacklist"=> $current['height']+(($b['fails']+1)*10)]);
$i++;
}
}
// check if the arguments are good for mining a specific block
public function mine($public_key, $nonce, $argon, $difficulty = 0, $current_id = 0, $current_height = 0)
public function mine($public_key, $nonce, $argon, $difficulty = 0, $current_id = 0, $current_height = 0, $time=0)
{
global $_config;
// invalid future blocks
if($time>time()+30){
return false;
}
// if no id is specified, we use the current
if ($current_id === 0) {
if ($current_id === 0 || $current_height === 0) {
$current = $this->current();
$current_id = $current['id'];
$current_height = $current['height'];
}
_log("Block Timestamp $time", 3);
if ($time == 0) {
$time=time();
}
// get the current difficulty if empty
if ($difficulty === 0) {
$difficulty = $this->difficulty();
}
// the argon parameters are hardcoded to avoid any exploits
if ($current_height > 10800) {
$argon = '$argon2i$v=19$m=524288,t=1,p=1'.$argon; //10800 block hard fork - resistance against gpu
if (empty($public_key)) {
_log("Empty public key", 1);
return false;
}
if ($current_height<80000) {
// the argon parameters are hardcoded to avoid any exploits
if ($current_height > 10800) {
_log("Block below 80000 but after 10800, using 512MB argon", 2);
$argon = '$argon2i$v=19$m=524288,t=1,p=1'.$argon; //10800 block hard fork - resistance against gpu
} else {
_log("Block below 10800, using 16MB argon", 2);
$argon = '$argon2i$v=19$m=16384,t=4,p=4'.$argon;
}
} else {
$argon = '$argon2i$v=19$m=16384,t=4,p=4'.$argon;
_log("Block > 80000 - $current_height", 2);
if ($current_height%3==0) {
// cpu mining
_log("CPU Mining - $current_height", 2);
$argon = '$argon2i$v=19$m=524288,t=1,p=1'.$argon;
} elseif ($current_height%3==1) {
// gpu mining
_log("GPU Mining - $current_height", 2);
$argon = '$argon2i$v=19$m=16384,t=4,p=4'.$argon;
} else {
_log("Masternode Mining - $current_height", 2);
// masternode
global $db;
// fake time
if ($time>time()) {
_log("Masternode block in the future - $time", 1);
return false;
}
// selecting the masternode winner in order
$winner=$db->single(
"SELECT public_key FROM masternode WHERE status=1 AND blacklist<:current AND height<:start ORDER by last_won ASC, public_key ASC LIMIT 1",
[":current"=>$current_height, ":start"=>$current_height-360]
);
// if there are no active masternodes, give the block to gpu
if ($winner===false) {
_log("No active masternodes, reverting to gpu", 1);
$argon = '$argon2i$v=19$m=16384,t=4,p=4'.$argon;
} else {
_log("The first masternode winner should be $winner", 1);
// 4 mins need to pass since last block
$last_time=$db->single("SELECT `date` FROM blocks WHERE height=:height", [":height"=>$current_height]);
if ($time-$last_time<240&&$_config['testnet']==false) {
_log("4 minutes have not passed since the last block - $time", 1);
return false;
}
if ($public_key==$winner) {
return true;
}
// if 10 mins have passed, try to give the block to the next masternode and do this every 10mins
_log("Last block time: $last_time, difference: ".($time-$last_time), 3);
if ($time-$last_time>600) {
_log("Current public_key $public_key", 3);
$tem=floor(($time-$last_time)/600);
$winner=$db->single(
"SELECT public_key FROM masternode WHERE status=1 AND blacklist<:current AND height<:start ORDER by last_won ASC, public_key ASC LIMIT $tem,1",
[":current"=>$current_height, ":start"=>$current_height-360]
);
_log("Moving to the next masternode - $tem - $winner", 1);
// if all masternodes are dead, give the block to gpu
if ($winner===false) {
_log("All masternodes failed, giving the block to gpu", 1);
$argon = '$argon2i$v=19$m=16384,t=1,p=1'.$argon;
} elseif ($winner==$public_key) {
return true;
} else {
return false;
}
} else {
_log("A different masternode should win this block $public_key - $winner", 2);
return false;
}
}
}
}
// the hash base for agon
@@ -351,6 +566,7 @@ class Block
// check argon's hash validity
if (!password_verify($base, $argon)) {
_log("Argon verify failed - $base - $argon", 2);
return false;
}
@@ -390,7 +606,7 @@ class Block
// parse the block transactions
public function parse_block($block, $height, $data, $test = true)
public function parse_block($block, $height, $data, $test = true, $bootstrapping=false)
{
global $db;
// data must be array
@@ -411,37 +627,50 @@ class Block
}
$balance = [];
$mns = [];
foreach ($data as &$x) {
// get the sender's account if empty
if (empty($x['src'])) {
$x['src'] = $acc->get_address($x['public_key']);
}
if (!$bootstrapping) {
//validate the transaction
if (!$trx->check($x, $height)) {
return false;
}
if ($x['version']>=100&&$x['version']<110) {
$mns[] = $x['public_key'];
}
//validate the transaction
if (!$trx->check($x, $height)) {
return false;
}
// prepare total balance
$balance[$x['src']] += $x['val'] + $x['fee'];
// prepare total balance
$balance[$x['src']] += $x['val'] + $x['fee'];
// check if the transaction is already on the blockchain
if ($db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $x['id']]) > 0) {
return false;
// check if the transaction is already on the blockchain
if ($db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $x['id']]) > 0) {
return false;
}
}
}
//only a single masternode transaction per block for any masternode
if (count($mns) != count(array_unique($mns))) {
_log("Too many masternode transactions", 3);
return false;
}
// check if the account has enough balance to perform the transaction
foreach ($balance as $id => $bal) {
$res = $db->single(
if (!$bootstrapping) {
// check if the account has enough balance to perform the transaction
foreach ($balance as $id => $bal) {
$res = $db->single(
"SELECT COUNT(1) FROM accounts WHERE id=:id AND balance>=:balance",
[":id" => $id, ":balance" => $bal]
);
if ($res == 0) {
return false; // not enough balance for the transactions
if ($res == 0) {
return false; // not enough balance for the transactions
}
}
}
// if the test argument is false, add the transactions to the blockchain
if ($test == false) {
foreach ($data as $d) {
@@ -511,7 +740,7 @@ class Block
return;
}
$db->beginTransaction();
$db->exec("LOCK TABLES blocks WRITE, accounts WRITE, transactions WRITE, mempool WRITE");
$db->exec("LOCK TABLES blocks WRITE, accounts WRITE, transactions WRITE, mempool WRITE, masternode WRITE");
foreach ($r as $x) {
$res = $trx->reverse($x['id']);
if ($res === false) {
@@ -536,7 +765,6 @@ class Block
// delete specific block
public function delete_id($id)
{
global $db;
$trx = new Transaction();
@@ -574,7 +802,6 @@ class Block
// sign a new block, used when mining
public function sign($generator, $height, $date, $nonce, $data, $key, $difficulty, $argon)
{
$json = json_encode($data);
$info = "{$generator}-{$height}-{$date}-{$nonce}-{$json}-{$difficulty}-{$argon}";
@@ -612,6 +839,10 @@ class Block
$r = $db->run("SELECT * FROM transactions WHERE version>0 AND block=:block", [":block" => $block['id']]);
$transactions = [];
foreach ($r as $x) {
if ($x['version']>110) {
//internal transactions
continue;
}
$trans = [
"id" => $x['id'],
"dst" => $x['dst'],

View File

@@ -36,3 +36,9 @@ $_config['sanity_rebroadcast_locals']=true;
$_config['enable_logging']=false;
// log file, should not be publicly viewable
$_config['log_file']="/var/log/aro.log";
//log verbosity, default 0, maximum 3
$_config['log_verbosity']=0;
//will this run as a masternode?
$_config['masternode']=false;
//masternode public key
$_config['masternode_public_key']="";

View File

@@ -31,6 +31,7 @@ class DB extends PDO
private function debug()
{
global $_config;
if (!$this->debugger) {
return;
}
@@ -56,9 +57,9 @@ class DB extends PDO
$msg .= "\n\n$key:\n$val";
}
if ($this->debugger) {
echo nl2br($msg);
}
_log($msg);
}
private function cleanup($bind, $sql = "")

View File

@@ -37,7 +37,7 @@ function api_echo($data)
}
// log function, shows only in cli atm
function _log($data)
function _log($data, $verbosity = 0)
{
$date = date("[Y-m-d H:i:s]");
$trace = debug_backtrace();
@@ -57,7 +57,7 @@ function _log($data)
echo $res;
}
global $_config;
if ($_config['enable_logging'] == true) {
if ($_config['enable_logging'] == true && $_config['log_verbosity'] >= $verbosity) {
@file_put_contents($_config['log_file'], $res, FILE_APPEND);
}
}

View File

@@ -1,8 +1,8 @@
<?php
// ARO version
define("VERSION", "0.3.0");
define("VERSION", "0.4.0");
// Amsterdam timezone by default, should probably be moved to config
date_default_timezone_set("Europe/Amsterdam");
date_default_timezone_set("UTC");
//error_reporting(E_ALL & ~E_NOTICE);
error_reporting(0);
@@ -24,7 +24,7 @@ if ($_config['db_pass'] == "ENTER-DB-PASS") {
die("Please update your config file and set your db password");
}
// initial DB connection
$db = new DB($_config['db_connect'], $_config['db_user'], $_config['db_pass'], 0);
$db = new DB($_config['db_connect'], $_config['db_user'], $_config['db_pass'], $_config['enable_logging']);
if (!$db) {
die("Could not connect to the DB backend.");
}
@@ -63,6 +63,17 @@ if ($_config['maintenance'] == 1) {
// update the db schema, on every git pull or initial install
if (file_exists("tmp/db-update")) {
//checking if the server has at least 2GB of ram
$ram=file_get_contents("/proc/meminfo");
$ramz=explode("MemTotal:",$ram);
$ramb=explode("kB",$ramz[1]);
$ram=intval(trim($ramb[0]));
if($ram<1700000) {
die("The node requires at least 2 GB of RAM");
}
if($_config['masternode']==true && $ram<7000000){
die("The masternode require at least 8GB of RAM");
}
$res = unlink("tmp/db-update");
if ($res) {
echo "Updating db schema! Please refresh!\n";

View File

@@ -145,6 +145,25 @@ if ($dbversion == 7) {
$db->run("ALTER TABLE `transactions` ADD KEY `dst` (`dst`), ADD KEY `height` (`height`), ADD KEY `public_key` (`public_key`);");
$dbversion++;
}
if ($dbversion == 8) {
$db->run("CREATE TABLE `masternode` (
`public_key` varchar(128) COLLATE utf8mb4_bin NOT NULL,
`height` int(11) NOT NULL,
`ip` varchar(16) COLLATE utf8mb4_bin NOT NULL,
`last_won` int(11) NOT NULL DEFAULT '0',
`blacklist` int(11) NOT NULL DEFAULT '0',
`fails` int(11) NOT NULL DEFAULT '0',
`status` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");
$db->run("ALTER TABLE `masternode`
ADD PRIMARY KEY (`public_key`),
ADD KEY `last_won` (`last_won`),
ADD KEY `status` (`status`),
ADD KEY `blacklist` (`blacklist`),
ADD KEY `height` (`height`);");
$dbversion++;
}

View File

@@ -7,16 +7,24 @@ class Transaction
{
global $db;
$acc = new Account();
$r = $db->run("SELECT * FROM transactions WHERE block=:block", [":block" => $block]);
$r = $db->run("SELECT * FROM transactions WHERE block=:block ORDER by `version` ASC", [":block" => $block]);
foreach ($r as $x) {
if (empty($x['src'])) {
$x['src'] = $acc->get_address($x['public_key']);
}
$db->run(
"UPDATE accounts SET balance=balance-:val WHERE id=:id",
[":id" => $x['dst'], ":val" => $x['val']]
);
if ($x['version'] == 2) {
// payment sent to alias
$db->run(
"UPDATE accounts SET balance=balance-:val WHERE alias=:alias",
[":alias" => $x['dst'], ":val" => $x['val']]
);
} else {
// other type of transactions
$db->run(
"UPDATE accounts SET balance=balance-:val WHERE id=:id",
[":id" => $x['dst'], ":val" => $x['val']]
);
}
// on version 0 / reward transaction, don't credit anyone
if ($x['version'] > 0) {
$db->run(
@@ -24,9 +32,57 @@ class Transaction
[":id" => $x['src'], ":val" => $x['val'] + $x['fee']]
);
}
// removing the alias if the alias transaction is reversed
if ($x['version']==3) {
$db->run(
"UPDATE accounts SET alias=NULL WHERE id=:id",
[":id" => $x['src']]
);
}
if ($x['version']>=100&&$x['version']<110&&$x['height']>=80000) {
if ($x['version']==100) {
$db->run("DELETE FROM masternode WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
} elseif ($x['version']==101) {
$db->run(
"UPDATE masternode SET status=1 WHERE public_key=:public_key",
[':public_key'=>$x['public_key']]
);
} elseif ($x['version']==102) {
$db->run("UPDATE masternode SET status=0 WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
} elseif ($x['version']==103) {
$mnt=$db->row("SELECT height, `message` FROM transactions WHERE version=100 AND public_key=:public_key ORDER by height DESC LIMIT 1", [":public_key"=>$x['public_key']]);
$vers=$db->single(
"SELECT `version` FROM transactions WHERE (version=101 or version=102) AND public_key=:public_key AND height>:height ORDER by height DESC LIMIT 1",
[":public_key"=>$x['public_key'],":height"=>$mnt['height']]
);
$status=1;
if ($vers==101) {
$status=0;
}
$db->run(
"INSERT into masternode SET `public_key`=:public_key, `height`=:height, `ip`=:ip, `status`=:status",
[":public_key"=>$x['public_key'], ":height"=>$mnt['height'], ":ip"=>$mnt['message'], ":status"=>$status]
);
$db->run("UPDATE accounts SET balance=balance-100000 WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
}
}
// internal masternode history
if ($x['version']==111) {
$m=explode(",", $x['message']);
$db->run(
"UPDATE masternode SET fails=:fails, blacklist=:blacklist, last_won=:last_won WHERE public_key=:public_key",
[":public_key"=>$x['public_key'], ":blacklist"=> $m[0], ":fails"=>$m[2], ":last_won"=>$m[1]]
);
}
// add the transactions to mempool
if ($x['version'] > 0) {
if ($x['version'] > 0 && $x['version']<=1000) {
$this->add_mempool($x);
}
$res = $db->run("DELETE FROM transactions WHERE id=:id", [":id" => $x['id']]);
@@ -92,7 +148,7 @@ class Transaction
_log("$x[id] - Transaction Check Failed");
continue;
}
$balance[$x['src']] += $x['val'] + $x['fee'];
if ($db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $x['id']]) > 0) {
_log("$x[id] - Duplicate transaction");
@@ -124,6 +180,9 @@ class Transaction
{
global $db;
$block = new Block();
if ($x['version']>110) {
return true;
}
$current = $block->current();
$height = $current['height'];
$x['id'] = san($x['id']);
@@ -141,6 +200,16 @@ class Transaction
":date" => $x['date'],
":message" => $x['message'],
];
//only a single masternode command of same type, per block
if ($x['version']>=100&&$x['version']<110) {
$check=$db->single("SELECT COUNT(1) FROM mempool WHERE public_key=:public_key", [":public_key"=>$x['public_key']]);
if ($check!=0) {
_log("Masternode transaction already in mempool", 3);
return false;
}
}
$db->run(
"INSERT into mempool SET peer=:peer, id=:id, public_key=:public_key, height=:height, src=:src, dst=:dst, val=:val, fee=:fee, signature=:signature, version=:version, message=:message, `date`=:date",
$bind
@@ -154,7 +223,9 @@ class Transaction
global $db;
$acc = new Account();
$acc->add($x['public_key'], $block);
$acc->add_id($x['dst'], $block);
if ($x['version']==1) {
$acc->add_id($x['dst'], $block);
}
$x['id'] = san($x['id']);
$bind = [
":id" => $x['id'],
@@ -176,7 +247,21 @@ class Transaction
if ($res != 1) {
return false;
}
$db->run("UPDATE accounts SET balance=balance+:val WHERE id=:id", [":id" => $x['dst'], ":val" => $x['val']]);
if ($x['version'] == 2&&$height>=80000) {
$db->run("UPDATE accounts SET balance=balance+:val WHERE alias=:alias", [":alias" => $x['dst'], ":val" => $x['val']]);
} elseif ($x['version']==100&&$height>=80000) {
//master node deposit
} elseif ($x['version']==103&&$height>=80000) {
$blk=new Block();
$blk->masternode_log($x['public_key'],$height,$block);
//master node withdrawal
} else {
$db->run("UPDATE accounts SET balance=balance+:val WHERE id=:id", [":id" => $x['dst'], ":val" => $x['val']]);
}
// no debit when the transaction is reward
if ($x['version'] > 0) {
$db->run(
@@ -184,6 +269,36 @@ class Transaction
[":id" => $x['src'], ":val" => $x['val'], ":fee" => $x['fee']]
);
}
// set the alias
if ($x['version']==3&&$height>=80000) {
$db->run(
"UPDATE accounts SET alias=:alias WHERE id=:id",
[":id" => $x['src'], ":alias"=>$x['message']]
);
}
if ($x['version']>=100&&$x['version']<110&&$height>=80000) {
$message=$x['message'];
$message=preg_replace("/[^0-9\.]/", "", $message);
if ($x['version']==100) {
$db->run("INSERT into masternode SET `public_key`=:public_key, `height`=:height, `ip`=:ip, `status`=1", [":public_key"=>$x['public_key'], ":height"=>$height, ":ip"=>$message]);
} else {
if ($x['version']==101) {
$db->run("UPDATE masternode SET status=0 WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
} elseif ($x['version']==102) {
$db->run("UPDATE masternode SET status=1 WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
} elseif ($x['version']==103) {
$db->run("DELETE FROM masternode WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
$db->run("UPDATE accounts SET balance=balance+100000 WHERE public_key=:public_key", [':public_key'=>$x['public_key']]);
}
}
}
$db->run("DELETE FROM mempool WHERE id=:id", [":id" => $x['id']]);
return true;
}
@@ -208,15 +323,25 @@ class Transaction
$acc = new Account();
$info = $x['val']."-".$x['fee']."-".$x['dst']."-".$x['message']."-".$x['version']."-".$x['public_key']."-".$x['date'];
// hard fork at 80000 to implement alias, new mining system, assets
// if($x['version']>1 && $height<80000){
// return false;
// }
// internal transactions
if($x['version']>110){
return false;
}
// the value must be >=0
if ($x['val'] < 0) {
_log("$x[id] - Value below 0");
_log("$x[id] - Value below 0", 3);
return false;
}
// the fee must be >=0
if ($x['fee'] < 0) {
_log("$x[id] - Fee below 0");
_log("$x[id] - Fee below 0", 3);
return false;
}
@@ -226,46 +351,115 @@ class Transaction
if ($fee < 0.00000001) {
$fee = 0.00000001;
}
//alias fee
if ($x['version']==3&&$height>=80000) {
$fee=10;
if (!$acc->free_alias($x['message'])) {
_log("Alias not free", 3);
return false;
}
// alias can only be set once per account
if ($acc->has_alias($x['public_key'])) {
_log("The account already has an alias", 3);
return false;
}
}
//masternode transactions
if ($x['version']>=100&&$x['version']<110&&$height>=80000) {
if ($x['version']==100) {
$message=$x['message'];
$message=preg_replace("/[^0-9\.]/", "", $message);
if (!filter_var($message, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
_log("The Masternode IP is invalid", 3);
return false;
}
}
if ($x['version']==100&&$x['val']!=100000) {
_log("The masternode transaction is not 100k", 3);
return false;
} elseif ($x['version']!=100) {
$mn=$acc->get_masternode($x['public_key']);
if (!$mn) {
_log("The masternode does not exist", 3);
return false;
}
if ($x['version']==101&&$mn['status']!=1) {
_log("The masternode does is not running", 3);
return false;
} elseif ($x['version']==102 && $mn['status']!=0) {
_log("The masternode is not paused", 3);
return false;
} elseif ($x['version']==103) {
if ($mn['status']!=0) {
_log("The masternode is not paused", 3);
return false;
} elseif ($height-$mn['last_won']<10800) { //10800
_log("The masternode last won block is less than 10800 blocks", 3);
return false;
} elseif ($height-$mn['height']<32400) { //32400
_log("The masternode start height is less than 32400 blocks! $height - $mn[height]", 3);
return false;
}
}
}
}
// max fee after block 10800 is 10
if ($height > 10800 && $fee > 10) {
$fee = 10; //10800
}
// added fee does not match
if ($fee != $x['fee']) {
_log("$x[id] - Fee not 0.25%");
_log("$x[id] - Fee not 0.25%", 3);
_log(json_encode($x), 3);
return false;
}
// invalid destination address
if (!$acc->valid($x['dst'])) {
_log("$x[id] - Invalid destination address");
return false;
if ($x['version']==1) {
// invalid destination address
if (!$acc->valid($x['dst'])) {
_log("$x[id] - Invalid destination address", 3);
return false;
}
} elseif ($x['version']==2&&$height>=80000) {
if (!$acc->valid_alias($x['dst'])) {
_log("$x[id] - Invalid destination alias", 3);
return false;
}
}
// reward transactions are not added via this function
if ($x['version'] < 1) {
_log("$x[id] - Invalid version <1");
_log("$x[id] - Invalid version <1", 3);
return false;
}
//if($x['version']>1) { _log("$x[id] - Invalid version >1"); return false; }
// public key must be at least 15 chars / probably should be replaced with the validator function
if (strlen($x['public_key']) < 15) {
_log("$x[id] - Invalid public key size");
_log("$x[id] - Invalid public key size", 3);
return false;
}
// no transactions before the genesis
if ($x['date'] < 1511725068) {
_log("$x[id] - Date before genesis");
_log("$x[id] - Date before genesis", 3);
return false;
}
// no future transactions
if ($x['date'] > time() + 86400) {
_log("$x[id] - Date in the future");
_log("$x[id] - Date in the future", 3);
return false;
}
// prevent the resending of broken base58 transactions
if ($height > 16900 && $x['date'] < 1519327780) {
_log("$x[id] - Broken base58 transaction", 3);
return false;
}
$id = $this->hash($x);
@@ -284,7 +478,7 @@ class Transaction
//verify the ecdsa signature
if (!$acc->check_signature($info, $x['signature'], $x['public_key'])) {
_log("$x[id] - Invalid signature");
_log("$x[id] - Invalid signature - $info");
return false;
}
@@ -295,6 +489,7 @@ class Transaction
public function sign($x, $private_key)
{
$info = $x['val']."-".$x['fee']."-".$x['dst']."-".$x['message']."-".$x['version']."-".$x['public_key']."-".$x['date'];
$signature = ec_sign($info, $private_key);
return $signature;
@@ -339,7 +534,7 @@ class Transaction
if ($x['version'] == 0) {
$trans['type'] = "mining";
} elseif ($x['version'] == 1) {
} elseif ($x['version'] == 1 || $x['version'] == 2) {
if ($x['dst'] == $id) {
$trans['type'] = "credit";
} else {
@@ -371,6 +566,9 @@ class Transaction
}
$res = [];
foreach ($r as $x) {
if ($x['version']>110) {
continue; //internal transactions
}
$trans = [
"block" => $x['block'],
"height" => $x['height'],
@@ -389,7 +587,7 @@ class Transaction
if ($x['version'] == 0) {
$trans['type'] = "mining";
} elseif ($x['version'] == 1) {
} elseif ($x['version'] == 1||$x['version'] == 2) {
if ($x['dst'] == $id) {
$trans['type'] = "credit";
} else {