Skip to content

Commit

Permalink
implements transaction subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Aug 6, 2023
1 parent cf9b8e5 commit a1735f7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ debug.log
error.log
.DS_Store
hosts.cache
*.mdb
20 changes: 19 additions & 1 deletion examples/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@ async function main() {
return true;
});

while (running_) {
node.chain.subscribeTransaction((e, tx) => {
if (e !== kth.errors.success) {
console.log(`Error: ${e}`);
return false;
}

if ( ! running_) {
return false;
}


if (tx !== null) {
console.log("Received Transaction: ", tx);
}

return true;
});


while (running_) {
await sleep(1000);
}

Expand Down
43 changes: 43 additions & 0 deletions examples/subscribe_multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ const onBlockArrivedGenerator = (n) => {
}
}

const onTransactionArrivedGenerator = (n) => {
return (e, tx) => {
console.log(`onTransactionArrivedGenerator - `, n, e, tx);

if (e !== kth.errors.success) {
console.log(`Error: ${e}`);
return false;
}

if ( ! running_) {
return false;
}

if (tx !== null) {
console.log("Transaction received: ", tx);
}

if (n >= max.length) {
console.log("n >= max.length");
return false;
}

count[n] += 1;
if (count[n] >= max[n]) {
console.log("count[n] >= max[n]");
return false;
}

return true;
}
}

async function main() {
process.on('SIGINT', shutdown);
const config = kth.settings.getDefault(kth.network.chipnet);
Expand All @@ -64,6 +96,17 @@ async function main() {
node.chain.subscribeBlockchain(onBlockArrivedGenerator(8));
node.chain.subscribeBlockchain(onBlockArrivedGenerator(9));

node.chain.subscribeTransaction(onTransactionArrivedGenerator(0));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(1));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(2));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(3));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(4));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(5));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(6));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(7));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(8));
node.chain.subscribeTransaction(onTransactionArrivedGenerator(9));

while (running_) {
await sleep(1000);
}
Expand Down
54 changes: 27 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"homepage": "https://github.com/k-nuth/js-api#readme",
"dependencies": {
"bluebird": "^3.7.2",
"@knuth/bch-native": "^0.56.0",
"@knuth/bch-native": "^0.57.0",
"memoizee": "^0.4.14"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions src/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ const async_chain = {
subscribe_blockchain: (...args) => {
kth.chain_subscribe_blockchain(...args);
},
subscribe_transaction: (...args) => {
kth.chain_subscribe_transaction(...args);
},

};

function objOrNull(ec, native, fromNative) {
Expand All @@ -116,6 +120,8 @@ class Chain {
this.nodeNative = nodeNative;
this.blockchainSubscribers = [];
this.blockAlreadySubscribed = false;
this.transactionSubscribers = [];
this.transactionAlreadySubscribed = false;
}

async getLastHeight() {
Expand Down Expand Up @@ -202,6 +208,27 @@ class Chain {
});
}

subscribeTransaction(callback) {
this.transactionSubscribers.push(callback);
if (this.transactionAlreadySubscribed) {
return;
}
this.transactionAlreadySubscribed = true;

async_chain.subscribe_transaction(this.nodeNative, this.native, (error, tx) => {
if (tx !== null) {
tx = tx.fromNative(tx);
}

for (const subscriber of [...this.transactionSubscribers]) {
if ( ! subscriber(error, tx)) {
this.transactionSubscribers = this.transactionSubscribers.filter(s => s !== subscriber);
}
}
return true;
});
}

//TODO(fernando): Merkle Block
//TODO(fernando): Compact Block
//TODO(fernando): Spend Block
Expand Down

0 comments on commit a1735f7

Please sign in to comment.