This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
cyphertx.php
195 lines (158 loc) · 5.41 KB
/
cyphertx.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
<?php
/*
* author: Greg Walker
* website: http://learnmeabitcoin.com
* license: GPLv3
*/
function cypherTx($neo, $transaction, $t, $blockhash, $cypher) {
// =================
// Cypher Parameters
// =================
$decoded = decoderawtransaction($transaction); // decode the raw transaction string
$size = strlen($transaction)/2; $sizedisplay = str_pad('['. $size .' bytes]', 15, ' ');
$relationshipsdisplay = str_pad('('. count($decoded['vin']) .':'. count($decoded['vout']) .')', 7, ' ');
$txid = $decoded['txid'];
$t_display = str_pad($t.'.', 5, ' ');
echo " $t_display $txid $sizedisplay $relationshipsdisplay ";
// skip transaction if it already exists in database
$check = $neo->run('MATCH (tx :tx {txid:$txid}) RETURN tx', ['txid' => $txid]);
$exists = !$check->isEmpty(); // is there a record for this txid?
if ($exists) {
// if this is a coinbase transaction, always merge it to the block (because two coinbase txs can have the same txid)
if ($decoded['vin'][0]['txid'] == '0000000000000000000000000000000000000000000000000000000000000000') {
$vin_coinbase = $decoded['vin'][0]['scriptSig']['hex']; // miners can put what they like in it
$vin_sequence = $decoded['vin'][0]['sequence'];
$neo->run('
MATCH (tx :tx {txid:$txid}), (block :block {hash:$blockhash})-[:coinbase]->(coinbase :output:coinbase)
WITH tx, block, coinbase
MERGE (tx)-[:inc {i:$t}]->(block)
MERGE (coinbase)-[in :in]->(tx)
ON CREATE SET
in.vin=0,
in.scriptSig=$vin_coinbase,
in.sequence=$vin_sequence
',
[
'txid' => $txid,
'blockhash' => $blockhash,
't' => $t,
'vin_coinbase' => $vin_coinbase,
'vin_sequence' => $vin_sequence,
]
);
echo 'exists->block (+coinbase)';
}
else {
// just connect this transaction to the block (in case we've got a transaction from an orphan block - don't want to forget to connect it to the block)
$neo->run('
MATCH (tx :tx {txid:$txid}), (block :block {hash:$blockhash})
WITH tx, block
MERGE (tx)-[:inc {i:$t}]->(block)
',
[
'txid' => $txid,
'blockhash' => $blockhash,
't' => $t,
]
);
echo 'exists->block';
}
}
// if this transaction doesn't exist in neo4j...
else {
// Build Parameter Array
$parameters = array();
$parameters['txid'] = $txid;
$parameters['blockhash'] = $blockhash;
$parameters['t'] = $t;
// ----------
// 1. TX node
// ----------
$parameters['tx']['version'] = $decoded['version'];
$parameters['tx']['locktime'] = $decoded['locktime'];
$parameters['tx']['size'] = $decoded['size'];
if ($decoded['segwit']) {
$parameters['tx']['segwit'] = $decoded['segwit']; // [marker][flag]
}
// ---------
// 2. Inputs
// ---------
$i=0;
$inputs = array();
$coinbase = false; // will use this later to choose correct cypher query (coinbase transaction is slightly different to standard transaction)
foreach ($decoded['vin'] as $vin) {
// Store new witness data if this is a new Segregated Witness transaction
if (array_key_exists('witness', $vin)) {
$witness = $vin['witness']['hex'];
}
else {
$witness = '';
}
$vin_txid = $vin['txid']; // (no need to swapEndian - txid is already in searchable order)
$vin_vout = $vin['vout'];
$vin_scriptSig = $vin['scriptSig']['hex'];
$vin_sequence = $vin['sequence'];
$inputs[$i]['vin'] = $i;
$inputs[$i]['index'] = "$vin_txid:$vin_vout";
$inputs[$i]['scriptSig'] = $vin_scriptSig;
$inputs[$i]['sequence'] = $vin_sequence;
$inputs[$i]['witness'] = $witness;
$i++;
}
// If coinbase transaction
if ($decoded['vin'][0]['txid'] == '0000000000000000000000000000000000000000000000000000000000000000') { // the input txid is all zeros for coinbase transactions
$coinbase = true;
$parameters['coinbase_script'] = $inputs[0]['scriptSig']; // miners can put what they like in this
$parameters['coinbase_sequence'] = $inputs[0]['sequence'];
}
$parameters['inputs'] = $inputs;
// ----------
// 3. Outputs
// ----------
$i=0;
$outputs = [];
$outtotal = 0; // keep track of output values (for calculating fee later)
foreach ($decoded['vout'] as $vout) {
$value = $vout['value'];
$scriptPubKey = $vout['scriptPubKey']['hex'];
$addresses = $vout['scriptPubKey']['addresses'];
$outputs[$i]['vout'] = $i;
$outputs[$i]['index'] = "$txid:$i";
$outputs[$i]['value'] = $value;
$outputs[$i]['scriptPubKey'] = $scriptPubKey;
$outputs[$i]['addresses'] = $addresses;
$outtotal += $value;
$i++;
}
$parameters['outputs'] = $outputs;
$parameters['outtotal'] = $outtotal;
// ============
// Cypher Query
// ============
// Select Cypher Query
if ($coinbase) {
$query = $cypher['tx-coinbase'];
}
else {
$query = $cypher['tx'];
}
// Run the full query to add the tx to the neo4j db (returns input total)
while (true) {
// Catch any errors caught by locks on nodes when writing to Neo4j
try {
$result = $neo->run($query, $parameters);
break;
}
// Echo the error, then wait a second before trying again.
catch (Exception $e) {
echo $e;
exit;
sleep(1);
}
}
// Get the fee (just to check) (Note: The fee will be negative if the inputs for this transaction are not in Neo4j yet, which is cool.)
$fee = $result->first()->get('fee');
echo "fee: $fee";
return $fee;
}
}