ブロック高によるブロックの取得

与えられたブロック高のブロック情報を取得する。

前提条件

方法 #01: SDK を使用する

ジェネシスブロックでは何が発生したのでしょう?次のコードを実行してブロックチェーンに最初に記録されたブロックのヘッダを取得します。

 // replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.096x.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://api-01.us-east-1.096x.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://api-01.us-east-1.096x.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));
        }

以下のスニペットは最終ブロックの高さを返却します。

 // replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.096x.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();
chainHttp
    .getBlockchainHeight()
    .subscribe((height) => console.log(height.compact()), (err) => console.error(err));
 // replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.096x.symboldev.network:3000';
const repositoryFactory = new symbol_sdk_1.RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();
chainHttp
    .getBlockchainHeight()
    .subscribe((height) => console.log(height.compact()), (err) => console.error(err));

方法 #02: CLI を使用する

ブロックヘッダの取得:

 symbol-cli block header --height 1

ブロックチェーン高の取得:

 symbol-cli chain height