-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_stakes.php
More file actions
147 lines (125 loc) · 2.59 KB
/
get_stakes.php
File metadata and controls
147 lines (125 loc) · 2.59 KB
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
<?php
require_once("config.php");
$merged = [];
foreach (Config::$Snapshots as $ss)
{
$reward_height = Helper::GetNextRewardHeight($ss["height"]);
$f = 'data/' . $reward_height . ".json";
if ( !file_exists($f) )
{
echo "file {$f} does not exists";
exit(1);
}
$stakes = GetStakes($f, $ss["pubkey"]);
// sort stakes by value
arsort($stakes);
// save each node wallets
$data_str = StakesToString($stakes);
file_put_contents($ss["filename"], $data_str);
if ($ss["merge"])
MergeStakes($merged, $stakes);
}
// exclude wallets with less than minimal defined stake
$filtered = FilterByMinStake($merged);
// sort stakes by value
arsort($filtered);
// save result wallets
$data_str = StakesToString($filtered);
file_put_contents(Config::$ResultFile, $data_str);
function GetStakes($filename, $pubkey)
{
$stakes = [];
$data = file_get_contents($filename);
$json = json_decode($data);
foreach ($json->candidates as $r)
{
// filter by public key
if ($r->pub_key != $pubkey)
{
continue ;
}
/*
* {"owner":"Mx28ebe0ca6bc8759ba6f4d3b991cea1eeaa3205b0",
* "coin":"ZERO",
* "value":"520000000000000000000",
* "bip_value":"19589251186354662603"}
*/
foreach ($r->stakes as $s)
{
// filter by coin
if ($s->coin != Config::$Coin)
{
continue ;
}
$coin_val = Helper::pip2bip($s->value);
if ( !array_key_exists($s->owner, $stakes) )
{
$stakes[$s->owner] = 0;
}
$stakes[$s->owner] = bcadd($stakes[$s->owner], $coin_val, 18);
}
break;
}
return ($stakes);
}
function MergeStakes(&$stakes1, &$stakes2)
{
foreach ($stakes2 as $k => $v)
{
if ( !array_key_exists($k, $stakes1) )
{
$stakes1[$k] = $v;
}
else
{
$stakes1[$k] = bcadd($stakes1[$k], $v, 18);
}
}
}
function FilterByMinStake($stakes)
{
$new_stakes = [];
foreach ($stakes as $k => $v)
{
// filter by stake
if ( bccomp($v, Config::$CoinMinStake) < 0 )
{
continue ;
}
$new_stakes[$k] = $v;
}
return ($new_stakes);
}
function StakesToString($stakes)
{
$str = "";
foreach ($stakes as $k => $v)
{
$str .= $k . (!Config::$ExportOnlyAddress ? ("\t" . $v) : '') . "\n";
}
return ($str);
}
class Helper
{
public static function GetNextRewardHeight($height)
{
$h = $height;
while( !self::IsRewardHeight($h) )
{
$h++;
}
return ($h);
}
public static function IsRewardHeight($height)
{
return ($height % Config::REWARD_BLOCK_INTERVAL == 0);
}
public static function bip2pip($val)
{
return bcmul($val, 1000000000000000000);
}
public static function pip2bip($val)
{
return bcdiv($val, 1000000000000000000, 18);
}
}