Skip to content

Commit

Permalink
Add onNewUnfinishedBlock()
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbrucker committed Jun 27, 2024
1 parent f17d3d0 commit 643ee89
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4.1.0 / 2024-06-27
==================

* Add `onNewUnfinishedBlock()` method to full node api, requires a full node running 2.4.0 or greater.

4.0.0 / 2023-12-29
==================

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { Connection, constants, ApiClient } = require('chia-api');
conn.onMessage((message) => {
console.log(message);
});
conn.addService(constants.SERVICE().walletUi);
conn.ensureService(constants.makeServiceNames().walletUi);
const fullNode = new ApiClient.FullNode({ connection: conn, origin: 'my-cool-service' });
await fullNode.init();
const blockchainState = await fullNode.getBlockchainState();
Expand Down
2 changes: 1 addition & 1 deletion lib/api-client/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Base {
}

async init() {
this.connection.addService(this.origin);
this.connection.ensureService(this.origin);
if (!this.connection.connected) {
await this.connection.connect();
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/api-client/daemon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Base = require('./base');
const Message = require('../message');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

class Daemon extends Base {
get destination() {
return SERVICE(this.connection.coin).daemon;
return makeServiceNames(this.connection.coin).daemon;
}

async registerService(serviceName) {
Expand Down
4 changes: 2 additions & 2 deletions lib/api-client/farmer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Base = require('./base');
const Message = require('../message');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

class Farmer extends Base {
get destination() {
return SERVICE(this.connection.coin).farmer;
return makeServiceNames(this.connection.coin).farmer;
}

onNewFarmingInfo(cb) {
Expand Down
14 changes: 12 additions & 2 deletions lib/api-client/full-node.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Base = require('./base');
const Message = require('../message');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

class FullNode extends Base {
get destination() {
return SERVICE(this.connection.coin).fullNode;
return makeServiceNames(this.connection.coin).fullNode;
}

onNewBlockchainState(cb) {
Expand All @@ -16,6 +16,16 @@ class FullNode extends Base {
});
}

onNewUnfinishedBlock(cb) {
this.connection.ensureService(makeServiceNames(this.connection.coin).unfinishedBlockInfo)
this.connection.onMessage(message => {
if (message.command !== 'unfinished_block') {
return
}
cb(message.data)
})
}

async getBlockchainState() {
const res = await this.connection.send(new Message({
command: 'get_blockchain_state',
Expand Down
4 changes: 2 additions & 2 deletions lib/api-client/harvester.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Base = require('./base');
const Message = require('../message');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

class Harvester extends Base {
get destination() {
return SERVICE(this.connection.coin).harvester;
return makeServiceNames(this.connection.coin).harvester;
}

async getPlots() {
Expand Down
6 changes: 3 additions & 3 deletions lib/api-client/plotter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Base = require('./base');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

class Plotter extends Base {
get destination() {
return SERVICE(this.connection.coin).plotter;
return makeServiceNames(this.connection.coin).plotter;
}

async init() {
this.connection.addService(SERVICE(this.connection.coin).plotter);
this.connection.ensureService(makeServiceNames(this.connection.coin).plotter);
if (!this.connection.connected) {
await this.connection.connect();
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/api-client/wallet.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Base = require('./base');
const Message = require('../message');
const { SERVICE } = require('../constants');
const { makeServiceNames } = require('../constants');

const BACKUP_HOST = 'https://backup.chia.net';

class Wallet extends Base {
get destination() {
return SERVICE(this.connection.coin).wallet;
return makeServiceNames(this.connection.coin).wallet;
}

async getBalance({ walletId }) {
Expand Down
6 changes: 5 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class Connection {
}

addService(serviceName) {
this.services.set(serviceName, serviceName);
this.ensureService(serviceName)
}

ensureService(serviceName) {
this.services.set(serviceName, serviceName)
}

async connect() {
Expand Down
11 changes: 7 additions & 4 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SERVICE = (coin = 'chia') => {
const makeServiceNames = (coin = 'chia') => {
return {
daemon: 'daemon',
fullNode: `${coin}_full_node`,
Expand All @@ -7,8 +7,10 @@ const SERVICE = (coin = 'chia') => {
wallet: `${coin}_wallet`,
walletUi: 'wallet_ui',
plotter: `${coin}_plotter`,
};
};
metrics: 'metrics',
unfinishedBlockInfo: 'unfinished_block_info',
}
}
const SERVICE_TYPE = {
fullNode: 1,
harvester: 2,
Expand All @@ -27,7 +29,8 @@ const PLOTTING_STATE = {
};

module.exports = {
SERVICE,
SERVICE: makeServiceNames,
makeServiceNames,
SERVICE_TYPE,
PLOTTING_STATE,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chia-api",
"version": "4.0.0",
"version": "4.1.0",
"repository": "https://github.com/foxypool/chia-api.git",
"bugs": "https://github.com/foxypool/chia-api/issues",
"license": "GPL-3.0",
Expand Down

0 comments on commit 643ee89

Please sign in to comment.