I am in the process of setting up an ajax endpoint on my server to create a new transaction object and add it to a block, which is then added to a local blockchain.
The problem arises when I invoke my database function add_block, responsible for adding the new block to the database. Once this call is included, any new blocks created with the ajax function appear empty instead of containing a transaction. Strangely, the block is not empty and is successfully stored in the database.
Upon adding a new block to the database, the add_block function returns a promise.
I observed the blockchain before and after executing the add_block function. Prior to the call, the new block appears correctly with the new transaction inside. However, after the call, the new block is displayed without any transactions, causing all future ajax calls to show empty blocks as well.
---In the following code example, you can see the ajax functionality running on a node server---
app.post("/transact", function(request, response) {
let sendAddress = jerryWalletAddress;
let getAddress = benWalletAddress;
let amount = 5;
let thisTransaction = new Transaction(sendAddress, getAddress, amount);
testChain.addTransaction(thisTransaction);
testChain.minePendingTransactions(jerryWalletAddress);
// This will display the correct output
testChain.printChain();
// The db function that adds the block to the database
blockchainDB.add_block(testChain.chain[testChain.chain.length-1]).then(()=> {
// This will now display the incorrect output
testChain.printChain();
});
});
If we assume there are two valid transactions before the ajax call:
The accurate list when printChain() is first called would be:
Block 1
-Transaction 1
Block 2
-Transaction 1
Block 3
-Transaction 1
However, the list becomes inaccurate upon calling printChain()
a second time:
Block 1
-Transaction 1
Block 2
-Transaction 2
Block 3
Block 4
Block 5