Skip to content

Commit

Permalink
Update chain.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 21, 2024
1 parent 01328a0 commit 5012ae2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions core/sidra-chain/chain.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import { SidraProtocol } from './protocol';
import { Block } from './block';

class SidraChain {
constructor() {
this.chain = [];
this.currentBlock = null;
this.protocol = new SidraProtocol(this, new SidraNode());
this.blocks = [];
}

init() {
// Initialize the chain with a genesis block
addBlock(block) {
// Validate the block before adding it to the chain
if (this.validateBlock(block)) {
this.blocks.push(block);
}
}

createBlock(transactions) {
const block = new Block(transactions, this.currentBlock.hash);
this.protocol.createBlock(block);
getLatestBlock() {
// Return the latest block in the chain
return this.blocks[this.blocks.length - 1];
}

processTransaction(transaction) {
this.protocol.processTransaction(transaction);
getBlockByHash(hash) {
// Retrieve a block by its hash
return this.blocks.find((block) => block.hash === hash);
}

getBlockByIndex(index) {
// Retrieve a block by its index
return this.blocks[index];
}

validateBlock(block) {
// Validate a block and ensure it meets the chain's requirements
// Check block hash, transactions, and other relevant information
}
}

Expand Down

0 comments on commit 5012ae2

Please sign in to comment.