persistence for the blockchain + everything through the chain instead of broadcasting

This commit is contained in:
Josip Milovac 2022-12-05 16:00:26 +11:00
parent 1cb1070d45
commit ea81105df6
5 changed files with 127 additions and 61 deletions

View file

@ -24,7 +24,12 @@ class Block {
static genesis() {
return new this('Genesis time', '-----', 'f1r57-h45h', [], 0, DIFFICULTY);
}
//we want this to eventually be continously running where there are things in the pool,
//however as node is single threaded, this almost has to be a fiber, and yield after every
//other iteration to allow for meaningful forward progress
//we can either add all new transactions into the block as we see them, or stay with the starting list, idk which
//to be done later
static mineBlock(lastBlock, data) {
let hash, timestamp;
const lastHash = lastBlock.hash;

View file

@ -28,17 +28,19 @@ class Blockchain {
return true;
}
//return false on failure, true on success
replaceChain(newChain) {
if (newChain.length <= this.chain.length) {
console.log('Received chain is not longer than the current chain.');
return;
return false;
} else if (!this.isValidChain(newChain)) {
console.log('The received chain is not valid.');
return;
return false;
}
console.log('Replacing blockchain with the new chain.');
this.chain = newChain;
return true;
}
}