i am taking an online blockchain course and the code runs normal for him but its not on my side ,blockchain nodes are connected with each other when using postman but when i mine a block only one node is receiving the mined block.
i tried running it again, make the algorithm harder to take longer time, changed the ports, made sure that the nodes where connected but nothing worked, i am trying to make an lms system with blockchain as a dataholder, do you recommend to keep doing it and if yes i am little lost and i need help.
here is the code where the problem is
app.get('/mine', function(req,res){
const lastBlock = blockchain.getLastBlock();
const previousBlockhash = lastBlock['hash'];
const currentBlockData = {
USER: blockchain.pendingUSER,
index: lastBlock['index'] +1
};
const nonce = blockchain.ProofOfWork(previousBlockhash,currentBlockData);
const blockhash = blockchain.hashBlock(previousBlockhash,currentBlockData,nonce);
const newblock = blockchain.createBlock(nonce,previousBlockhash,blockhash);
const request_promises = [];
blockchain.networkNodes.forEach(networkNodeURL =>{
const request_Options = {
uri: networkNodeURL + '/receive-new-block',
method: 'POST',
body: {
newblock:newblock
},
json:true
};
request_promises.push(request_promise(request_Options));
});
Promise.all(request_promises)
.then(data =>{
res.json({
note:"new block mined successfully",
block: newblock
});
})
});
app.post('/receive-new-block',function(req,res){
const newblock = req.body.newblock;
const lastBlock = blockchain.getLastBlock();
const correct_hash = lastBlock.hash === newblock.previousBlockhash;
const correct_index = lastBlock['index'] +1 === newblock['index'];
if(correct_hash && correct_index){
blockchain.chain.push(newblock);
res.json({
note:'new block is added to the chain.',
newblock:newblock
});
}
else{
res.json({
note:'block was rejected',
newblock:newblock
})
}
});
app.post('/USER', function(req,res){
const newUSER = req.body;
const blockIndex= blockchain.addUserToPendingUSER(newUSER);
res.json({note:`USER will be added in block ${blockIndex}.`})
});
app.post('/USER/broadcast',function(req,res){
const newUSER = blockchain.CreateNewUSER(req.body.Academic_id,req.body.Firstname,req.body.Lastname,req.body.birthdate,req.body.academic_year);
blockchain.addUserToPendingUSER(newUSER);
const request_promises = [];
blockchain.networkNodes.forEach(networkNodeURL =>{
const request_Options = {
uri: networkNodeURL + '/USER',
method: 'POST',
body: newUSER,
json:true
};
request_promises.push(request_promise(request_Options));
});
Promise.all(request_promises)
.then(data=>{
res.json({note:'user is created and broadcasted to other nodes'})
});
});
even though this function almost works the same it works normal and all nodes do receive the message that is sent to 1 node