Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jy/development #11

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/client/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ api.post("/mineBlock", async function(req, res) {
const block = await miner.mineBlock("0x" + wl.getPrivateFromWallet());
res.send(block);
});
api.post("/makeTx", function(req, res) {
api.post("/makeTx", async function(req, res) {
const { receiver, value } = req.body;
const miner = req.app.locals.miner;
const tx = miner.makeTx(receiver, value);
const tx = await miner.makeTx(receiver, value);
res.send(tx);
});
api.post("/sendBlock", async function(req, res) {
Expand Down Expand Up @@ -93,10 +93,13 @@ api.post("/sendBlock", async function(req, res) {
res.send(result);
});
api.post("/sendProof", async function(req, res) {
const { blockNonce, receiverId } = req.body;
const { blockNonce, receiverStr } = req.body;
const { db, bc } = req.app.locals;
// Make proof
const receiver = await db.readUserById(receiverId);
let receiver = await db.readUserById(receiverStr);
if (!receiver) {
receiver = await db.readUserByAddress(receiverStr);
}
if (!receiver) {
return res.send("User doesn't exist");
}
Expand All @@ -105,7 +108,7 @@ api.post("/sendProof", async function(req, res) {
return res.send("Block nonce is invalid one");
}
const targetTx = targetBlock.transactions.find(
tx => tx.data.receiver === receiver.addr
tx => tx.data.receiver === receiver.address
);
if (!targetTx) {
return res.send("Block doesn't have receiver related tx");
Expand Down
2 changes: 1 addition & 1 deletion src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Database {
});
}

readUserbyAddress(address) {
readUserByAddress(address) {
return new Promise((resolve, reject) => {
this.db.connect(url, { useNewUrlParser: true }, (err, client) => {
if (err) {
Expand Down
19 changes: 13 additions & 6 deletions src/miner/miner.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,31 @@ class Miner {
/**
* Receives tx information and update newTx list
*
* @param {*} receiver
* @param {*} receiverStr
* @param {*} value
*/
makeTx(receiver, value) {
const index = this.newTxs.findIndex(tx => tx.data.receiver === receiver);
async makeTx(receiverStr, value) {
const index = this.newTxs.findIndex(tx => tx.data.receiver === receiverStr);
let receiver;
if (!index) {
receiver = await this.db.readUserById(receiverStr);
receiverStr = receiver.address;
index = this.newTxs.findIndex(tx => tx.data.receiver === receiverStr);
}
if (index !== -1) {
// update exist tx's value
this.newTxs[index].data.value += value;
return {
success: `Updated exist tx to: ${receiver}, value: ${
success: `Updated exist tx to: ${receiverStr}, value: ${
this.newTxs[index].data.value
}`
};
}
let tx = new Transaction(receiver, value);
let tx = new Transaction(receiverStr, value);
this.newTxs.push(tx);
return { success: `New tx added to: ${receiver}, value: ${value}` };
return { success: `New tx added to: ${receiverStr}, value: ${value}` };
}

/**
* Refresh mined block and all tx list
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/kill.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

fuser -k -n tcp 3000 3001 3002 3003
6 changes: 6 additions & 0 deletions tests/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
HTTP_PORT=3000 node src/index.operator.js 2<&1 &
HTTP_PORT=3001 node src/index.user.js 2<&1 &
HTTP_PORT=3002 node src/index.user.js 2<&1 &
HTTP_PORT=3003 node src/index.user.js 2<&1 &