Getting a block by height

Get the block information given a height.

Prerequisites

Method #01: Using the SDK

What happened in the genesis block? Run the following code to get the first block header recorded on the blockchain.

// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const blockHttp = repositoryFactory.createBlockRepository();

const height = 1;
blockHttp.getBlockByHeight(UInt64.fromUint(height)).subscribe(
  (block) => console.log(block),
  (err) => console.error(err),
);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new symbol_sdk_1.RepositoryFactoryHttp(nodeUrl);
const blockHttp = repositoryFactory.createBlockRepository();
const height = 1;
blockHttp.getBlockByHeight(symbol_sdk_1.UInt64.fromUint(height)).subscribe((block) => console.log(block), (err) => console.error(err));
        // replace with node endpoint
        try (final RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(
            "http://ngl-dual-101.testnet.symboldev.network:3000")) {
            final BlockRepository blockRepository = repositoryFactory.createBlockRepository();

            // Replace with block height
            final BigInteger blockHeight = BigInteger.valueOf(1);
            final BlockInfo blockInfo = blockRepository.getBlockByHeight(blockHeight).toFuture()
                .get();
            final JsonHelper helper = new JsonHelperJackson2();
            System.out.println(helper.prettyPrint(blockInfo));
        }

The following snippet returns the height of the latest block.

// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();

chainHttp.getChainInfo().subscribe(
  (info: ChainInfo) => console.log(info.height.compact()),
  (err) => console.error(err),
);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new symbol_sdk_1.RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();
chainHttp.getChainInfo().subscribe((info) => console.log(info.height.compact()), (err) => console.error(err));

Method #02: Using the CLI

Get the block header:

symbol-cli block header --height 1

Get the height of the blockchain:

symbol-cli chain height

Last updated by Xavi Artigas on 2021-07-07.

Did you find what you were looking for? Give us your feedback.