-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathw8_updater_headers.php
131 lines (115 loc) · 2.91 KB
/
w8_updater_headers.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
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
<?php
namespace w8io;
if( file_exists( __DIR__ . '/config.php' ) )
require_once __DIR__ . '/config.php';
else
require_once __DIR__ . '/config.sample.php';
require_once __DIR__ . '/include/common.php';
use deemru\Triples;
use deemru\KV;
function offline( int $delay = 3 )
{
if( $delay < 1 )
$delay = 1;
wk()->log( 'w', 'OFFLINE: delay for ' . $delay . ' sec...' );
sleep( $delay );
}
function blockUnique( $headers )
{
return $headers['id'] ?? $headers['signature'];
}
function singleton()
{
require_once __DIR__ . '/include/secqru_flock.php';
static $singleton;
if( !isset( $singleton ) )
{
$singleton = new \secqru_flock( W8IO_DB_DIR . 'w8io_headers.lock' );
if( false === $singleton->open() )
w8_err( 'flock failed, already running?' );
return true;
}
return false;
}
singleton();
$wk = wk();
$db = new Triples( W8IO_DB_DIR . 'headers.sqlite3', 'headers', true );
$kvHeaders = ( new KV )->setStorage( $db, 'headers', true, 'INTEGER PRIMARY KEY', 'TEXT' )->setValueAdapter( function( $value ){ return json_unpack( $value ); }, function( $value ){ return json_pack( $value ); } );
$hi = $db->getHigh( 0 );
if( $hi === false )
for( ;; )
{
$headers = $wk->getBlockAt( 1, true );
if( $headers === false )
{
offline();
continue;
}
$kvHeaders->setKeyValue( 1, $headers );
$kvHeaders->merge();
$wk->log( 1 );
break;
}
function rollback()
{
global $wk;
global $db;
global $kvHeaders;
$hi = $db->getHigh( 0 );
for( $i = $hi; $i >= 1; --$i )
{
$checkpoint = $kvHeaders->getValueByKey( $i );
for( ;; )
{
$headers = $wk->getBlockAt( $i, true );
if( $headers !== false )
break;
offline();
}
if( blockUnique( $checkpoint ) === blockUnique( $headers ) )
break;
$wk->log( 'w', 'fork @ '. $i );
}
if( $i !== $hi )
{
$kvHeaders->reset();
$db->query( 'DELETE from headers WHERE r0 > ' . $i );
$hi = $i;
if( $hi === 0 )
w8_err( 'fork @ GENESIS' );
}
return $hi;
}
for( ;; )
{
$wk->log( '.' );
$height = $wk->height();
if( $height === false )
{
offline( $delay );
continue;
}
$hi = $db->getHigh( 0 );
for( $i = $hi + 1; $i < $height; ++$i )
{
for( ;; )
{
$headers = $wk->getBlockAt( $i, true );
if( $headers !== false )
break;
offline();
}
$checkpoint = $kvHeaders->getValueByKey( $i - 1 );
if( $headers['reference'] !== blockUnique( $checkpoint ) )
{
$i = rollback();
break;
}
$kvHeaders->reset();
$kvHeaders->setKeyValue( $i, $headers );
$kvHeaders->merge();
$wk->log( $i );
}
if( $i >= $height )
sleep( 17 );
}