-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcalculation_utils.php
52 lines (40 loc) · 1.29 KB
/
calculation_utils.php
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
<?php
require_once 'config.php';
require_once 'constants.php';
require_once 'jsonRPCClient.php';
function dice_round($value) {
return round((float)$value, COIN_DECIMAL_PLACES);
}
function charge_fee($value) {
global $config;
return charge_fee_internal($value, $config['fee']);
}
function charge_fee_internal($value, $fee) {
return dice_round($value * (1 - $fee));
}
function get_random_v1($value, $txid, $secret) {
}
function get_random_v2($value, $txid, $blockid, $pot_fee) {
if (is_null($txid)) {
return 0;
}
// get two first bytes
$hash = hash_hmac('sha256', hex2bin($txid), hex2bin($blockid));
// not using the secret makes it more auditable.
// $hash = hash_hmac('sha256', $hash, $config['hash_secret']);
$hex = substr($hash, 32, 4); // not sure if initial zeros are pruned
$dec = hexdec ( $hex );
// homogeneous in [0, 2) interval
$ret = $dec / 0x8000;
// pot fee removal
if ($pot_fee > 0 && $ret > 1) {
$ret = 1 + (($ret - 1) * (1.0 - $pot_fee)); // charges only the winnings
}
//print("hex = " . $hex . ", dec = " . $dec . ", ret = " . $ret . "\n");
return dice_round($ret * $value);
}
function get_random($value, $txid, $blockid) {
global $config;
return get_random_v2($value, $txid, $blockid, $config['pot_fee']);
}
?>