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

@@ -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 {