-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackends.js
More file actions
150 lines (119 loc) · 3.43 KB
/
Copy pathbackends.js
File metadata and controls
150 lines (119 loc) · 3.43 KB
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
import config from "./config"
import { Peer, Messages, Inventory } from "b2p2p"
import bitwork from "bitwork"
import bsv from "bsv"
import txo from "txo"
class Backend {
constructor() {
this.peer = null;
this.txids = {};
this.ready = () => {};
this.waitInterval = null;
this.startTime = Date.now()
}
add(txid) {
this.txids[txid] = false;
}
has(txid) {
return this.txids[txid] === false;
}
complete(txid) {
console.log("💦 HIT", txid);
this.txids[txid] = true;
}
finished() {
return Object.values(this.txids).every(txid => txid === true);
}
incomplete() {
return Object.entries(this.txids).map(([txid, complete]) => {
if (!complete) {
return txid;
}
}).filter(txid => txid);
}
num() {
return Object.keys(this.txids).length;
}
clear() {
clearInterval(this.waitInterval);
this.waitInterval = null;
}
reset() {
this.clear();
}
wait(max=100) {
if (this.waitInterval) {
this.clear();
}
let curr = 0;
this.waitInterval = setInterval(() => {
if (this.finished()) {
const diff = (Date.now() - this.startTime) / 1000;
console.log(`✅ SUCCESS FIRED AND HIT ${this.num()} txs IN ${diff} SECONDS`);
this.reset();
} else {
console.log("WAIT for finish");
for (const txid of this.incomplete()) {
console.log("WAIT ON", txid);
}
}
if (++curr > max) {
console.log("🔴 ERROR stopped waiting, exceeded max");
this.reset();
}
}, 1000);
}
ready(fn) {
this.ready = fn;
}
}
export class B2P2PBackend extends Backend {
constructor() {
super();
const messages = new Messages({ Block: bsv.Block, BlockHeader: bsv.BlockHeader, Transaction: bsv.Transaction, MerkleBlock: bsv.MerkleBlock });
this.peer = new Peer({ host: config.peer.host, messages });
this.peer.on("ready", () => {
console.log("peer connected");
this.ready();
});
this.peer.on("inv", (message) => {
//console.log("peer inventory");
message.inventory.forEach((i) => {
let txid = i.hash.toString('hex').match(/.{2}/g).reverse().join("");
if (this.has(txid)) {
this.complete(txid);
}
})
});
this.peer.on("disconnect", () => {
console.log("peer disconnected");
});
this.peer.on("error", (message) => {
console.log("peer error", message);
});
this.peer.connect()
}
reset() {
this.clear();
this.peer.disconnect();
}
}
export class BitworkBackend extends Backend {
constructor() {
super();
this.bit = new bitwork({ rpc: config.rpc, peer: config.peer });
this.bit.use("parse", "txo");
this.bit.on("ready", async () => {
this.bit.on("mempool", (e) => {
if (this.has(e.tx.h)) {
this.complete(e.tx.h);
}
})
this.ready();
})
}
reset() {
this.clear();
this.bit.peer.disconnect();
}
}