-
Notifications
You must be signed in to change notification settings - Fork 1
/
VECRO.php
381 lines (317 loc) · 13.6 KB
/
VECRO.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
namespace deemru;
use deemru\WavesKit;
use deemru\Pairs;
class VECRO
{
public function __construct( $wk, $pairsFile, $alias, $aliasInit, $aliasRandom, $assetInit, $assetRandom )
{
require_once __DIR__ . '/../support/secqru_flock.php';
$this->lock = new \secqru_flock( "$pairsFile.lock" );
if( false === $this->lock->open() )
exit( $wk->log( 'e', 'flock failed, already running?' ) );
$this->wk = $wk;
$this->used = new Pairs( $pairsFile, 'used', true, 'TEXT UNIQUE|TEXT|0|0' );
$this->db = $this->used;
$this->queue = new Pairs( $this->db->db(), 'queue', true, 'TEXT UNIQUE|TEXT|0|0' );
$this->done = new Pairs( $this->db->db(), 'done', true, 'TEXT UNIQUE|TEXT|0|0' );
$this->rid = new Pairs( $this->db->db(), 'rid', true, 'TEXT UNIQUE|TEXT|0|0' );
$this->wk->setPairsDatabase( $this->db->db() );
$this->address = $wk->getAddress();
$this->alias = $alias;
$chainId = $wk->getChainId();
$this->aliasInit = "alias:$chainId:$aliasInit";
$this->aliasRandom = "alias:$chainId:$aliasRandom";
$this->assetInit = $assetInit;
$this->assetRandom = $assetRandom;
$this->triggerCost = 500000;
$this->triggerMaxCost = 100000000;
$this->sureness = 2;
$this->iSig = 0;
$this->iRaw = 1;
$this->iTrig = 2;
$this->iInit = 3;
$this->codeR = 'R';
$this->codeS = 'S';
$this->codeSize = 45;
}
private function prepare()
{
while( $this->isUnconfirmed() )
{
$this->wk->log( 'i', "wait unconfirmed" );
sleep( 5 );
}
}
private function isUnconfirmed()
{
if( false === ( $utxs = $this->wk->fetch( '/transactions/unconfirmed' ) ) ||
false === ( $utxs = $this->wk->json_decode( $utxs ) ) )
$this->error( 'fetch failed' );
foreach( $utxs as $utx )
if( $utx['type'] === 4 && $utx['sender'] === $this->address )
return true;
return false;
}
private function error( $message )
{
trigger_error( $this->wk->log( 'e', $message ), E_USER_ERROR );
}
public function run()
{
$this->prepare();
for( ;; )
{
$this->wk->txMonitor( function( $wk, $refreshed, $newTransactions ){ return $this->proc( $wk, $refreshed, $newTransactions ); } );
$this->wk->log( 'w', 'txMonitor exited' );
$this->wk->log( 'i', 'retrying...' );
sleep( 1 );
}
}
private function RS( $triggerInitTx, $msg = null )
{
$this->wk->setRSEED( $triggerInitTx['id'] . $this->address . $this->alias );
return $this->wk->sign( $msg );
}
private function init( $tx )
{
$txid = $tx['id'];
$rawid = $this->wk->base58Decode( $txid );
$triggerInitTx = $tx;
if( isset( $triggerInitTx['assetId'] ) )
return 'assetId != WAVES';
if( $triggerInitTx['amount'] < $this->triggerCost )
return "amount < {$this->triggerCost}";
$R = $this->RS( $triggerInitTx );
$R58 = $this->wk->base58Encode( $R );
$Rcode = $this->wk->base58Encode( $this->codeR . $R );
if( strlen( $Rcode ) !== $this->codeSize )
$this->error( "init ($txid) strlen( Rcode ) !== codeSize" );
$attachment = $this->wk->base58Encode( $Rcode );
$initTx = $this->wk->txTransfer( $triggerInitTx['sender'], 1, $this->assetInit, [ 'fee' => min( $this->triggerMaxCost, $triggerInitTx['amount'] ), 'attachment' => $attachment ] );
$initTxBodyBytes = $this->wk->txBody( $initTx ) . $this->wk->base58Decode( $txid );
$initTx['proofs'][$this->iSig] = $this->wk->base58Encode( $this->wk->sign( $initTxBodyBytes ) );
$initTx['proofs'][$this->iRaw] = $R58;
$initTx['proofs'][$this->iTrig] = $txid;
if( false === $this->rid->setKeyValue( $R, $rawid, 's' ) )
$this->error( "init ($txid) setKeyValue failed" );
if( false === ( $initTx = $this->wk->txBroadcast( $initTx ) ) )
{
$this->wk->log( 'w', "init ($txid) txBroadcast failed" );
return false;
}
$this->wk->log( 's', "init ($txid) >> ({$initTx['id']})" );
return true;
}
private function random( $tx )
{
$txid = $tx['id'];
$triggerRandomTx = $tx;
if( isset( $triggerRandomTx['assetId'] ) )
return 'assetId != WAVES';
if( $triggerRandomTx['amount'] < $this->triggerCost )
return "amount < {$this->triggerCost}";
$msg = $this->wk->base58Decode( $triggerRandomTx['attachment'] );
if( strlen( $msg ) < $this->codeSize )
return 'attachment < sizeR';
$Rcode = substr( $msg, 0, $this->codeSize );
if( false === ( $R = $this->wk->base58Decode( $Rcode ) ) )
return 'can not decode R-value';
if( $R[0] !== $this->codeR )
return 'wrong prefix';
$R = substr( $R, 1 );
if( false === ( $rawid = $this->rid->getValue( $R, 's' ) ) )
return 'R-value not found';
if( false === ( $doneTx = $this->done->getValue( $rawid, 'j' ) ) )
{
$this->wk->log( 'w', "random ($txid) triggerInitTx not found in done" );
return false;
}
$initTx = $doneTx['status'];
$triggerInitTx = $doneTx['tx'];
if( !is_array( $initTx ) )
return "initTx status: $initTx";
if( $triggerInitTx['sender'] !== $triggerRandomTx['sender'] )
return "not equal senders";
$userStr = substr( $msg, $this->codeSize );
if( false !== ( $used = $this->used->getValue( $R, 's' ) ) )
{
if( $userStr === $used )
$this->wk->log( 'w', "random ($txid) same R-value with same msg" );
else
return 'R-value has already been used';
}
else if( false === $this->used->setKeyValue( $R, $userStr, 's' ) )
$this->error( "random ($txid) setKeyValue failed" );
$RS = $this->RS( $triggerInitTx, $msg );
if( substr( $RS, 0, 32 ) !== $R )
return "not equal R-values";
$S = substr( $RS, 32 );
$S58 = $this->wk->base58Encode( $S );
$Scode = $this->wk->base58Encode( $this->codeS . $S );
if( strlen( $Scode ) !== $this->codeSize )
$this->error( "random ($txid) strlen( Scode ) !== codeSize" );
$attachment = $this->wk->base58Encode( $Scode );
$initTxId = $initTx['id'];
$randomTx = $this->wk->txTransfer( $triggerRandomTx['sender'], 1, $this->assetRandom, [ 'fee' => min( $this->triggerMaxCost, $triggerRandomTx['amount'] ), 'attachment' => $attachment ] );
$randomTxBodyBytes = $this->wk->txBody( $randomTx ) . $this->wk->base58Decode( $txid ) . $this->wk->base58Decode( $initTxId );
$randomTx['proofs'][$this->iSig] = $this->wk->base58Encode( $this->wk->sign( $randomTxBodyBytes ) );
$randomTx['proofs'][$this->iRaw] = $S58;
$randomTx['proofs'][$this->iTrig] = $txid;
$randomTx['proofs'][$this->iInit] = $initTxId;
if( false === ( $randomTx = $this->wk->txBroadcast( $randomTx ) ) )
{
$this->wk->log( 'w', "random ($txid) txBroadcast failed" );
return false;
}
$this->wk->log( 's', "random ($txid) >> ({$randomTx['id']})" );
return true;
}
private function queue( $tx )
{
$qtxid = $tx['id'];
$rawid = $this->wk->base58Decode( $qtxid );
if( false !== ( $qtx = $this->queue->getValue( $rawid, 'j' ) ) )
{
$status = isset( $qtx['status'] ) ? $qtx['status'] : 'processing...';
$this->wk->log( 'w', "queue ($qtxid) already in queue ($status)" );
return;
}
if( false !== ( $qtxdone = $this->done->getValue( $rawid, 'j' ) ) )
{
$doneId = $qtxdone['status']['id'];
$this->wk->log( 'w', "queue ($qtxid) already in done ($doneId)" );
return;
}
$qtx = [];
$qtx['tx'] = $tx;
if( false === $this->queue->setKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "queue ($qtxid) setKeyValue failed" );
$this->wk->log( 'i', "queue ($qtxid)" );
}
private function done( $tx )
{
if( isset( $tx['proofs'][$this->iTrig] ) )
{
$qtxid = $tx['proofs'][$this->iTrig];
$rawid = $this->wk->base58Decode( $qtxid );
if( false !== ( $qtxdone = $this->done->getValue( $rawid, 'j' ) ) )
{
$doneId = $qtxdone['status']['id'];
$this->wk->log( 'w', "done ($qtxid) already in done ($doneId)" );
return;
}
if( false === ( $qtx = $this->queue->getValue( $rawid, 'j' ) ) )
{
$this->wk->log( 'w', "done ($qtxid) not found in queue" );
$qtx = [];
if( false === ( $qtx['tx'] = $this->wk->getTransactionById( $qtxid ) ) )
$this->error( "done ($qtxid) getTransactionById() failed" );
}
$this->db->begin();
{
if( false === $this->queue->unsetKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "done ($qtxid) unsetKeyValue failed" );
$qtx['status'] = $tx;
if( false === $this->done->setKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "done ($qtxid) setKeyValue failed" );
if( !isset( $tx['proofs'][$this->iInit] ) )
// R
{
$R = $this->wk->base58Decode( $tx['proofs'][$this->iRaw] );
if( false === $this->rid->setKeyValue( $R, $rawid, 's' ) )
$this->error( "done ($qtxid) setKeyValue failed" );
}
else
// S
{
$msg = $this->wk->base58Decode( $qtx['tx']['attachment'] );
$Rcode = substr( $msg, 0, $this->codeSize );
$R = $this->wk->base58Decode( $Rcode );
$R = substr( $R, 1 );
$userStr = substr( $msg, $this->codeSize );
if( false !== ( $used = $this->used->getValue( $R, 's' ) ) && $userStr !== $used )
{
$asset = $tx['assetId'];
if( $asset === $this->assetInit || $asset === $this->assetRandom )
$this->error( "done ($qtxid) userStr !== used" );
else
$this->wk->log( 'w', "done ($qtxid) userStr !== used ($asset)" );
}
if( false === $this->used->setKeyValue( $R, $userStr, 's' ) )
$this->error( "done ($qtxid) setKeyValue failed" );
}
}
$this->db->commit();
$this->wk->log( 's', "done ($qtxid)" );
}
}
private function proc( $wk, $refreshed, $newTransactions )
{
if( $refreshed )
{
foreach( $newTransactions as $tx )
{
if( $tx['type'] !== 4 )
continue;
$sender = $tx['sender'];
$recipient = $tx['recipient'];
if( $recipient === $this->aliasInit || $recipient === $this->aliasRandom )
$this->queue( $tx );
else if( $sender === $this->address )
$this->done( $tx );
}
}
$queue = $this->getQueue();
if( $queue === false )
$this->error( 'proc: getQueue() failed' );
$work = [];
foreach( $queue as $rec )
{
if( false === ( $qtx = $this->wk->json_decode( $rec['value'] ) ) )
$this->error( 'proc: json_decode failed' );
if( isset( $qtx['status'] ) )
{
if( $qtx['status'] !== 'sent' )
$this->error( 'proc: unknown status in queue' );
continue;
}
$work[] = $qtx;
}
foreach( $work as $qtx )
{
$tx = $qtx['tx'];
$qtxid = $tx['id'];
$rawid = $wk->base58Decode( $qtxid );
$recipient = $tx['recipient'];
if( $recipient !== $this->aliasInit && $recipient !== $this->aliasRandom )
$this->error( "proc ($qtxid) unknown recipient in queue" );
$status = $recipient === $this->aliasInit ? $this->init( $tx ) : $this->random( $tx );
if( $status === true )
{
$qtx['status'] = 'sent';
if( false === $this->queue->setKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "proc ($qtxid) setKeyValue failed" );
continue;
}
else if( $status === false )
continue;
$this->wk->log( 'w', "proc ($qtxid) with status: $status" );
// status not 'sent' but done
$this->db->begin();
{
if( false === $this->queue->unsetKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "proc ($qtxid) unsetKeyValue failed" );
$qtx['status'] = $status;
if( false === $this->done->setKeyValue( $rawid, $qtx, 'j' ) )
$this->error( "proc ($qtxid) setKeyValue failed" );
}
$this->db->commit();
}
return defined( 'VECRO_RECHECK_INTERVAL') ? VECRO_RECHECK_INTERVAL : 30;
}
private function getQueue()
{
return $this->queue->query( 'SELECT * FROM queue ORDER BY rowid ASC' );
}
}