Skip to content

Instantly share code, notes, and snippets.

@leightkt
Created April 9, 2021 20:18
Show Gist options
  • Save leightkt/9bb0e25c1ab2eb83bccc5db1266e01fd to your computer and use it in GitHub Desktop.
Save leightkt/9bb0e25c1ab2eb83bccc5db1266e01fd to your computer and use it in GitHub Desktop.
class Blockchain {
constructor(chain){
this.chain = chain
this.difficulty = 4
this.nodeUrl = nodeURL
this.networkNodes = []
}
createGenesisBlock() {
return new Block(0, "03/17/2021", "Lynn Hill is GOAT", '0', 100)
}
getLatestBlock() {
return this.chain[this.chain.length - 1]
}
addBlock(data) {
const newBlock = new Block(
this.chain.length,
Date.now(),
data
)
newBlock.previousHash = this.getLatestBlock().hash
newBlock.mineBlock(this.difficulty)
this.chain.push(newBlock)
return newBlock
}
isChainValid(chain) {
if (!chain) {
return null
}
for(let i = 1; i < chain.length; i++){
const currentBlock = chain[i]
const previousBlock = chain[i - 1]
if(currentBlock.hash !== this.calculateHash(currentBlock)) {
return false
}
if(currentBlock.previousHash !== previousBlock.hash) {
return false
}
}
return true
}
findAllGymBlocks(gym_id) {
return this.chain.filter(block => block.data.gym_id === gym_id)
}
findAllMemberBlocks(user_member_number, gym_id) {
return this.chain.filter(block => block.data.gym_id === gym_id && block.data.user_member_number === user_member_number)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment