forked from in3rsha/bitcoin-to-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyphertx.php
235 lines (191 loc) · 6.8 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
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
<?php
/*
* author: Greg Walker
* website: http://learnmeabitcoin.com
* license: GPLv3
*/
function cypherTx($neo, $transaction, $t, $blockhash) {
// ============
// CYPHER BUILD
// ============
$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");
$exists = $check->size() > 0; // is there a record for this txid?
if ($exists) {
$record = $check->getRecord();
// 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'
");
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)
");
echo 'exists->block';
}
}
// if this transaction doesn't exist in neo4j...
else {
$cypher = '';
// ----------
// 1. TX node
// ----------
$version = $decoded['version'];
$locktime = $decoded['locktime'];
// Set segwit property on tx with the value of [marker][flag] if it's a segwit tx
if ($decoded['segwit']) {
$segwit = $decoded['segwit'];
$setsegwit = "SET tx.segwit='$segwit'";
}
else {
$setsegwit = '';
}
$cypher .= "
MATCH (block :block {hash:'$blockhash'})-[:coinbase]->(coinbase :output:coinbase)
MERGE (tx:tx {txid:'$txid', version:$version, locktime:$locktime, size:$size})
WITH tx, block, coinbase
$setsegwit
";
// ---------
// 2. Inputs
// ---------
$i=0;
$inputs = array(); $inputstack = '';
$coinbase = false;
$inputcount = count($decoded['vin']);
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'];
$witnessstack = ", witness:'$witness'";
$witnessiterate = ", witness: input.witness";
}
else {
$witness = '';
$witnessstack = '';
$witnessiterate = '';
}
// If coinbase transaction
if ($vin['txid'] == '0000000000000000000000000000000000000000000000000000000000000000') { // the input txid is all zeros for coinbase transactions
$coinbase = true;
$vin_coinbase = $vin['scriptSig']['hex']; // miners can put what they like in it
$vin_sequence = $vin['sequence'];
$cypher .= "MERGE (coinbase)-[:in {vin:$i, scriptSig:'$vin_coinbase', sequence:'$vin_sequence'}]->(tx) ";
break;
}
// If not coinbase transaction
else {
$vin_txid = $vin['txid'];
$vin_vout = $vin['vout'];
$vin_scriptSig = $vin['scriptSig']['hex'];
$vin_sequence = $vin['sequence'];
// Prepare a JSON array so that each input can be added using Neo4j's FOREACH
$inputs[] = "{vin:$i, index:'$vin_txid:$vin_vout', scriptSig:'$vin_scriptSig', sequence:'$vin_sequence', witness:'$witness'}";
}
$i++;
}
$inputs = implode(", ", $inputs); // create json of each input array
// iterate over the json array of inputs
$cypher .= "
FOREACH (input in [$inputs] |
MERGE (in :output {index: input.index})
MERGE (in)-[:in {vin: input.vin, scriptSig: input.scriptSig, sequence: input.sequence$witnessiterate}]->(tx)
)
";
// ----------
// 3. Outputs
// ----------
$i=0;
$outputs = []; $outputstack = ''; $addressiterate = [];
$outtotal = 0; // keep track of output values (for calculating fee later)
foreach ($decoded['vout'] as $vout) {
$value = $vout['value']; $outtotal += $value;
$scriptPubKey = $vout['scriptPubKey']['hex'];
$addresses = $vout['scriptPubKey']['addresses'];
// Prepare a JSON array so that each output can be added using Neo4j's FOREACH
$outputs[] = "{vout:$i, index:'$txid:$i', value:$value, scriptPubKey:'$scriptPubKey', addresses:'$addresses'}";
$i++;
}
$outputs = implode(", ", $outputs); // create json of each input array
// 1. MAIN: Iterate over the json array of outputs
// This uses the foreach hack to only create an address node if the address value is not an empty string
// If output is placeholder, it didn't have a value. Increase fee for the tx it's an input for.
$cypher .= "
FOREACH (output in [$outputs] |
MERGE (out :output {index: output.index})
MERGE (tx)-[:out {vout: output.vout}]->(out)
FOREACH(ignoreMe IN CASE WHEN output.addresses <> '' THEN [1] ELSE [] END |
MERGE (address :address {address: output.addresses})
MERGE (out)-[:locked]->(address)
)
MERGE (out)-[:in]->(existing)
ON CREATE SET
out.value= output.value,
out.scriptPubKey= output.scriptPubKey
ON MATCH SET
out.value= output.value,
out.scriptPubKey= output.scriptPubKey,
existing.fee = existing.fee + output.value
)
";
// --------
// 4. Block
// --------
$cypher .= "
MERGE (tx)-[:inc {i:$t}]->(block)
";
// ----------
// 5. Set Fee (and return fee info)
// ----------
$cypher .= "
WITH tx
MATCH (i :output)-[:in]->(tx)
WITH tx, sum(i.value) - $outtotal as fee
SET tx.fee=fee
RETURN fee
";
// ==========
// CYPHER RUN
// ==========
// Run the full query to add the tx to the neo4j db (returns input total)
// Error catching
while (true) {
// Catch any errors caught by locks on nodes when writing to Neo4j
try {
$result = $neo->run($cypher);
break;
}
// Echo the error, then wait a second before trying again.
catch (Exception $e) {
echo '$neo->run($cypher) exception'.PHP_EOL;
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.)
$record = $result->getRecord();
$fee = $record->get('fee');
echo "fee: $fee";
return $fee;
}
}