Skip to content

Commit

Permalink
Improve log messages and swagger documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoqg committed Jun 2, 2018
1 parent 03a0a4e commit 2edeb44
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bin/naivecoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ const argv = require('yargs')
.alias('h', 'help')
.argv;

naivecoin(argv.host, argv.port, argv.peers, argv.logLevel, argv.name, argv.logLevel, argv.broadcastAddress);
naivecoin(argv.host, argv.port, argv.peers, argv.logLevel, argv.name && argv.name.toString(), argv.logLevel, argv.broadcastAddress);
19 changes: 16 additions & 3 deletions lib/httpServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class HttpServer {
constructor(node, blockchain, operator, miner) {
this.app = express();

const projectWallet = (wallet) => {
return {
id: wallet.id,
addresses: R.map(projectAddress, wallet.addresses)
};
};

const projectAddress = (address) => {
return address.publicKey;
};

this.app.use(bodyParser.json());

this.app.set('view engine', 'pug');
Expand Down Expand Up @@ -116,7 +127,9 @@ class HttpServer {
this.app.get('/operator/wallets', (req, res) => {
let wallets = operator.getWallets();

res.status(200).send(wallets);
let projectedWallets = R.map(projectWallet, wallets);

res.status(200).send(projectedWallets);
});

this.app.post('/operator/wallets', (req, res) => {
Expand All @@ -132,7 +145,7 @@ class HttpServer {
let walletFound = operator.getWalletById(req.params.walletId);
if (walletFound == null) throw new HTTPError(404, `Wallet not found with id '${req.params.walletId}'`);

res.status(200).send(walletFound);
res.status(200).send(projectWallet(walletFound));
});

this.app.post('/operator/wallets/:walletId/transactions', (req, res) => {
Expand Down Expand Up @@ -161,7 +174,7 @@ class HttpServer {
let walletId = req.params.walletId;
try {
let addresses = operator.getAddressesForWallet(walletId);
res.status(200).send(addresses);
res.status(200).send(R.map(projectAddress, addresses));
} catch (ex) {
if (ex instanceof ArgumentError) throw new HTTPError(400, ex.message, walletId, ex);
else throw ex;
Expand Down
176 changes: 159 additions & 17 deletions lib/httpServer/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/Wallet"
"$ref": "#/definitions/WalletPostResponse"
}
},
"400": {
Expand Down Expand Up @@ -395,7 +395,7 @@
"type": "object",
"properties": {
"address": {
"$ref": "#/definitions/Address"
"$ref": "#/definitions/AddressPostResponse"
}
}
}
Expand Down Expand Up @@ -521,6 +521,23 @@
}
}
},
"/node/peer": {
"get": {
"tags": [
"node"
],
"summary": "Get the node (peer) information",
"description": "",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Peer"
}
}
}
}
},
"/node/peers": {
"get": {
"tags": [
Expand Down Expand Up @@ -601,7 +618,7 @@
"miner"
],
"summary": "Mine a new block",
"description": "Mine a new block and add it to the end of blockchain.",
"description": "Mine a new block and add it to the end of blockchain",
"parameters": [
{
"in": "body",
Expand All @@ -615,12 +632,20 @@
],
"properties": {
"rewardAddress": {
"description": "Address to send the reward",
"type": "string"
"type": "string",
"description": "Address to send the reward"
},
"feeAddress": {
"description": "Address to send the fee",
"type": "string"
"type": "string",
"description": "Address to send the fee"
},
"secretKey": {
"type": "string",
"description": "If the blokchain uses proof-of-authority, the Secret Key of the sealer"
},
"publicKey": {
"type": "string",
"description": "If the blokchain uses proof-of-authority, the Public Key of the sealer"
}
}
}
Expand All @@ -640,54 +665,171 @@
"definitions": {
"Block": {
"type": "object",
"required": [
"index",
"previousHash",
"timestamp",
"nonce",
"data",
"hash"
],
"properties": {
"index": {
"type": "integer"
"type": "integer",
"description": "Block index"
},
"previousHash": {
"type": "string"
"type": "string",
"description": "The hash of the previous block"
},
"timestamp": {
"type": "integer"
"type": "integer",
"description": "Timestamp in milliseconds of the block creation"
},
"data": {
"type": "object"
"nonce": {
"type": "string",
"description": "Counter of tries to find the correct block (proof-of-work only)"
},
"transactions": {
"type": "array",
"items": {
"$ref": "#/definitions/Transaction"
}
},
"hash": {
"type": "string"
"type": "string",
"description": "Block hash"
},
"sealer": {
"type": "string",
"description": "Address of the block sealer (proof-of-authority)"
},
"signature": {
"type": "string",
"description": "Signature of the block sealer (proof-of-authority)"
}
}
},
"Peer": {
"type": "object",
"required": [
"name",
"network",
"url"
],
"properties": {
"name": {
"type": "string",
"description": "Node name"
},
"network": {
"type": "string",
"description": "Hash of the node configuration"
},
"url": {
"type": "string"
"type": "string",
"description": "Broadcast address that can be accessed by other nodes"
}
}
},
"Wallet": {
"type": "object",
"required": [
"id",
"addresses"
],
"properties": {
"id": {
"type": "string"
"type": "string",
"description": "Wallet id"
},
"addresses": {
"type": "array",
"items": {
"type": "string"
"$ref": "#/definitions/Address"
}
}
}
},
"WalletPostResponse": {
"type": "object",
"required": [
"id",
"passwordHash",
"secret",
"addresses"
],
"properties": {
"id": {
"type": "string",
"description": "Wallet id"
},
"passwordHash": {
"type": "string",
"description": "Password hash"
},
"secret": {
"type": "string",
"description": "Secret generated from the password"
},
"addresses": {
"type": "array",
"items": {
"$ref": "#/definitions/AddressPostResponse"
}
}
}
},
"Address": {
"type": "string"
},
"AddressPostResponse": {
"type": "object",
"required": [
"id",
"index",
"secretKey",
"publicKey"
],
"properties": {
"id": {
"type": "string",
"description": "Address id"
},
"index": {
"type": "integer",
"description": "Address index"
},
"secretKey": {
"type": "string",
"description": "Secret key of the address"
},
"publicKey": {
"type": "string",
"description": "Public key of the address"
}
}
},
"Transaction": {
"type": "object",
"required": [
"id",
"hash",
"type",
"data"
],
"properties": {
"id": {
"type": "string"
"type": "string",
"description": "Transaction id"
},
"hash": {
"type": "string",
"description": "Transaction hash"
},
"type": {
"type": "string",
"description": "Transaction type (reward, fee or regular)"
},
"data": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion lib/miner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Miner {
}
}, candidateTransactions);

console.info(`Selected ${selectedTransactions.length} candidate transactions with ${rejectedTransactions.length} being rejected.`);
console.info(`Selected ${selectedTransactions.length} candidate transactions with ${rejectedTransactions.length} being rejected`);

// Get the first two avaliable transactions, if there aren't TRANSACTIONS_PER_BLOCK, it's empty
let transactions = R.defaultTo([], R.take(Config.TRANSACTIONS_PER_BLOCK, selectedTransactions));
Expand Down
6 changes: 3 additions & 3 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ class Node {
network: this.network,
url: me
});
console.info(`Peer ${peer.url} added to connections.`);
console.info(`Peer ${peer.url} added to connections`);
this.peers.push(peerInformation);
this.initConnection(peerInformation);
this.broadcast(this.sendPeer, peerInformation);
} else {
console.error(`Peer ${peer.url} not added to connections because it doesn't belong to the same network: received ${peer.network}, mine: ${this.network}.`);
console.error(`Peer ${peer.url} not added to connections because it doesn't belong to the same network: received ${peer.network}, mine: ${this.network}`);
}
});
} else {
console.info(`Peer ${peer.url} not added to connections because I already have it.`);
console.info(`Peer ${peer.url} not added to connections because I already have it`);
}
}, this);

Expand Down

0 comments on commit 2edeb44

Please sign in to comment.