Skip to content

Commit

Permalink
make block simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
mlibre committed May 25, 2024
1 parent e5a7ba2 commit cfeaf4c
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/API/routes/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,47 @@ import { toNum } from "../utils.js";

const router = express.Router();

router.get( "/", function ( req, res )
router.get( "/", ( req, res ) =>
{
const { list } = req.query;
const { to, index, from, firstAndLast } = req.query;
const { list, to, index, from, firstAndLast } = req.query;
if ( !index && !from && !to && !list && !firstAndLast )
{
res.json( blockchain.chain.latestBlock );
return;
}
else if ( index )
if ( index )
{
const blockIndex = toNum( index );
res.json( blockchain.chain.get( blockIndex ) );
res.json( blockchain.chain.get( toNum( index ) ) );
return;
}
else if ( from || to )
if ( from || to )
{
const blockFrom = toNum( from );
const blockTo = toNum( to );
const blocks = blockchain.getBlocks( blockFrom, blockTo );
res.json( blocks );
res.json( blockchain.getBlocks( toNum( from ), toNum( to ) ) );
return;
}
else if ( list )
if ( list )
{
const blockList = list.toString().split( "," );
const blocks = [];
for ( const blcokIndex of blockList )
const blocks = list.toString().split( "," ).map( ( index ) =>
{
blocks.push( blockchain.chain.get( blcokIndex ) );
}
return blockchain.chain.get( toNum( index ) );
});
res.json( blocks );
return;
}
else if ( firstAndLast )
if ( firstAndLast )
{
const blocks = [];
blocks.push( blockchain.chain.get( 0 ) );
blocks.push( blockchain.chain.latestBlock );
res.json( blocks );
res.json( [ blockchain.chain.get( 0 ), blockchain.chain.latestBlock ] );
return;
}
});

router.post( "/", function ( req, res )
router.post( "/", ( req, res ) =>
{
const block = blockchain.addBlock( req.body );
res.send( block );
res.json( block );
});

router.get( "/broadcast", async function ( req, res )
router.get( "/broadcast", async ( req, res ) =>
{
for ( const node of blockchain.nodes.list )
{
Expand Down

0 comments on commit cfeaf4c

Please sign in to comment.