Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
mlibre committed May 30, 2024
1 parent 9195596 commit d084e35
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 220 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"author": "mlibre",
"license": "GPL-3.0-only",
"dependencies": {
"@types/level": "^6.0.3",
"axios": "^1.6.7",
"cookie-parser": "~1.4.4",
"express": "^4.19.2",
Expand All @@ -44,6 +43,7 @@
"@types/cookie-parser": "^1.4.7",
"@types/eslint__js": "^8.42.3",
"@types/express": "^4.17.21",
"@types/level": "^6.0.3",
"@types/lodash": "^4.17.1",
"@types/minimist": "^1.2.5",
"@types/morgan": "^1.9.9",
Expand Down
6 changes: 3 additions & 3 deletions src/API/routes/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ router.post( "/update", async function ( req, res )
let continueUpdate = true;
while ( continueUpdate )
{
const currentIndex = blockchain.chain.latestBlock.index;
const currentIndex = ( await blockchain.chain.latestBlock() ).index;
const nodesLatestBlocks = [];
for ( const node of blockchain.nodes.list )
{
Expand Down Expand Up @@ -49,7 +49,7 @@ router.post( "/update", async function ( req, res )

router.put( "/sync", async function ( req, res )
{
const myLastestBlock = blockchain.chain.latestBlock;
const myLastestBlock = await blockchain.chain.latestBlock();
const otherNodesLastestBlocks = [];
for ( const node of blockchain.nodes.list )
{
Expand All @@ -59,7 +59,7 @@ router.put( "/sync", async function ( req, res )
await axios.get( `${node}/block`, { params: { firstAndLast: true } })
).data;
if (
isEqualBlock( firstBlock, blockchain.chain.genesisBlock ) &&
isEqualBlock( firstBlock, await blockchain.chain.genesisBlock() ) &&
!isEqualBlock( myLastestBlock, lastBlock )
)
{
Expand Down
4 changes: 2 additions & 2 deletions src/API/routes/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import express from "express";
const router = express.Router();
import blockchain from "../blockchain.js";

router.get( "/", function ( req, res )
router.get( "/", async function ( req, res )
{
res.send( blockchain.wallet.allData );
res.send( await blockchain.wallet.allWallets() );
});

export default router;
19 changes: 13 additions & 6 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
interface BlockchainConstructorParams
{
readonly dbPath: string;
Expand All @@ -10,18 +11,24 @@ interface BlockchainConstructorParams
readonly consensus: Consensus;
}

interface WalletData
{
blockNumber: number;
list: Record<string, WalletBalance>;
}
type AllWallets = Record<string, UserWallet>;

type UserWallets = UserWallet[];

interface WalletBalance
interface UserWallet
{
balance: number;
transaction_number: number;
}

interface PutAction
{
type: "put",
sublevel: any,
key: string,
value: any
}

interface BlockData
{
[x: string]: string | number | TransactionData[];
Expand Down
79 changes: 31 additions & 48 deletions src/library/chain.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import _ from "lodash";
import * as Block from "./block.js";
import { Level } from "level";
import * as Block from "./block.js";

export default class ChainStore
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public db: any;
sublevel: string;
constructor ( leveldb: Level<string, BlockData> )
{
this.db = leveldb.sublevel<string, BlockData>( "chain", { valueEncoding: "json" });
this.sublevel = "chain";
this.db = leveldb.sublevel<string, BlockData>( this.sublevel, { valueEncoding: "json" });
}

async length (): Promise<number>
{
const lastKey = await this.lastKey();
return lastKey;
return await this.lastKey();
}

async lastKey (): Promise<number>
Expand All @@ -29,7 +29,7 @@ export default class ChainStore
}
if ( !lastKey )
{
return 0;
throw new Error( "No blocks found" );
}
return Number( lastKey );
}
Expand All @@ -54,7 +54,7 @@ export default class ChainStore
async getRange ( from: number, to?: number ): Promise<BlockData[]>
{
const blocks: BlockData[] = [];
to = to ?? await this.length() - 1;
to = to ?? await this.length();
for ( let i = from; i <= to; i++ )
{
blocks.push( await this.get( i ) );
Expand All @@ -78,18 +78,15 @@ export default class ChainStore
return lastBlock;
}

async push ( block: BlockData ): Promise<void>
pushAction ( block: BlockData ): PutAction
{
await this.db.put( block.index.toString(), block );
}

async replaceChain ( blocks: BlockData[] ): Promise<void>
{
await this.db.clear();
for ( const block of blocks )
{
await this.push( block );
}
const action: PutAction = {
type: "put",
sublevel: this.sublevel,
key: block.index.toString(),
value: block
};
return action;
}

async lastTwoBlocks (): Promise<[BlockData, BlockData]>
Expand All @@ -99,50 +96,36 @@ export default class ChainStore
return [ lastBlock, secondLastBlock ];
}


async checkFinalDBState ( proposedBlock: BlockData ): Promise<boolean>
async validateChain (): Promise<boolean>
{
if ( proposedBlock.index === 0 )
for ( let i = 0; i <= await this.length(); i++ )
{
const lastBlock = await this.latestBlock();
Block.verifyGenesisBlock( lastBlock );
if ( !_.isEqual( lastBlock, proposedBlock ) )
if ( i === 0 )
{
throw new Error( "Invalid chain" );
Block.verifyGenesisBlock( await this.get( i ) );
}
else
{
Block.verifyBlock( await this.get( i ), await this.get( i - 1 ) );
}
return true;
}
const [ lastBlock, secondLastBlock ] = [ await this.get( proposedBlock.index ), await this.get( proposedBlock.index - 1 ) ];
Block.verifyBlock( lastBlock, secondLastBlock );
if ( !_.isEqual( lastBlock, proposedBlock ) )
{
throw new Error( "Invalid chain" );
}
const [ lastBlockFile, secondLastBlockFile ] = await this.lastTwoBlocks();
if ( !_.isEqual( lastBlockFile, lastBlock ) || !_.isEqual( secondLastBlockFile, secondLastBlock ) )
{
throw new Error( "Invalid chain" );
}
return true;
}

async validateChain (): Promise<boolean>
async isEmpty (): Promise<boolean>
{
if ( await this.length() === 0 )
try
{
return true;
await this.lastKey();
return false;
}
for ( let i = 0; i < await this.length(); i++ )
catch ( error: unknown )
{
if ( i === 0 )
if ( error instanceof Error && error.message === "No blocks found" )
{
Block.verifyGenesisBlock( await this.get( i ) );
}
else
{
Block.verifyBlock( await this.get( i ), await this.get( i - 1 ) );
return true;
}
throw error;
}
return true;
}
}
11 changes: 3 additions & 8 deletions src/library/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ export default class levelDatabase
this.db = leveldb;
}

async revert ( key: string )
async batch ( batch: PutAction[] )
{
const batch: { type: "del"; key: string; }[] = [];

for await ( const [ k ] of this.db.iterator({ reverse: true }) )
if ( batch.length === 0 )
{
if ( k > key )
{
batch.push({ type: "del", key: k });
}
return;
}
await this.db.batch( batch );
}
Expand Down
Loading

0 comments on commit d084e35

Please sign in to comment.