Search in sources :

Example 1 with GetBlockResult

use of com.amazonaws.services.qldb.model.GetBlockResult in project amazon-qldb-dmv-sample-java by aws-samples.

the class GetBlock method getBlockWithProof.

public static GetBlockResult getBlockWithProof(String ledgerName, BlockAddress blockAddress, BlockAddress tipBlockAddress) {
    log.info("Let's get the block for block address {}, digest tip address {}, for the ledger named {}.", blockAddress, tipBlockAddress, ledgerName);
    try {
        GetBlockRequest request = new GetBlockRequest().withName(ledgerName).withBlockAddress(new ValueHolder().withIonText(Constants.MAPPER.writeValueAsIonValue(blockAddress).toString())).withDigestTipAddress(new ValueHolder().withIonText(Constants.MAPPER.writeValueAsIonValue(tipBlockAddress).toString()));
        GetBlockResult result = client.getBlock(request);
        log.info("Success. GetBlock: {}.", QldbStringUtils.toUnredactedString(result));
        return result;
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
}
Also used : GetBlockResult(com.amazonaws.services.qldb.model.GetBlockResult) IOException(java.io.IOException) ValueHolder(com.amazonaws.services.qldb.model.ValueHolder) GetBlockRequest(com.amazonaws.services.qldb.model.GetBlockRequest)

Example 2 with GetBlockResult

use of com.amazonaws.services.qldb.model.GetBlockResult in project amazon-qldb-dmv-sample-java by aws-samples.

the class GetBlock method getBlock.

public static GetBlockResult getBlock(String ledgerName, BlockAddress blockAddress) {
    log.info("Let's get the block for block address {} of the ledger named {}.", blockAddress, ledgerName);
    try {
        GetBlockRequest request = new GetBlockRequest().withName(ledgerName).withBlockAddress(new ValueHolder().withIonText(Constants.MAPPER.writeValueAsIonValue(blockAddress).toString()));
        GetBlockResult result = client.getBlock(request);
        log.info("Success. GetBlock: {}.", QldbStringUtils.toUnredactedString(result));
        return result;
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
}
Also used : GetBlockResult(com.amazonaws.services.qldb.model.GetBlockResult) IOException(java.io.IOException) ValueHolder(com.amazonaws.services.qldb.model.ValueHolder) GetBlockRequest(com.amazonaws.services.qldb.model.GetBlockRequest)

Example 3 with GetBlockResult

use of com.amazonaws.services.qldb.model.GetBlockResult in project amazon-qldb-dmv-sample-java by aws-samples.

the class GetBlock method verifyBlock.

public static void verifyBlock(String ledgerName, BlockAddress blockAddress) throws Exception {
    log.info("Lets verify blocks for ledger with name={}.", ledgerName);
    try {
        log.info("First, let's get a digest");
        GetDigestResult digestResult = GetDigest.getDigest(ledgerName);
        BlockAddress tipBlockAddress = Constants.MAPPER.readValue(digestResult.getDigestTipAddress().getIonText(), BlockAddress.class);
        ValueHolder digestTipAddress = digestResult.getDigestTipAddress();
        byte[] digestBytes = Verifier.convertByteBufferToByteArray(digestResult.getDigest());
        log.info("Got a ledger digest. Digest end address={}, digest={}.", QldbStringUtils.toUnredactedString(digestTipAddress), Verifier.toBase64(digestBytes));
        GetBlockResult getBlockResult = getBlockWithProof(ledgerName, blockAddress, tipBlockAddress);
        JournalBlock block = Constants.MAPPER.readValue(getBlockResult.getBlock().getIonText(), JournalBlock.class);
        boolean verified = Verifier.verify(block.getBlockHash(), digestBytes, getBlockResult.getProof().getIonText());
        if (!verified) {
            throw new AssertionError("Block is not verified!");
        } else {
            log.info("Success! The block is verified.");
        }
        byte[] alteredDigest = Verifier.flipRandomBit(digestBytes);
        log.info("Let's try flipping one bit in the digest and assert that the block is NOT verified. " + "The altered digest is: {}.", Verifier.toBase64(alteredDigest));
        verified = Verifier.verify(block.getBlockHash(), alteredDigest, getBlockResult.getProof().getIonText());
        if (verified) {
            throw new AssertionError("Expected block to not be verified against altered digest.");
        } else {
            log.info("Success! As expected flipping a bit in the digest causes verification to fail.");
        }
        byte[] alteredBlockHash = Verifier.flipRandomBit(block.getBlockHash());
        log.info("Let's try flipping one bit in the block's hash and assert that the block is NOT " + "verified. The altered block hash is: {}.", Verifier.toBase64(alteredBlockHash));
        verified = Verifier.verify(alteredBlockHash, digestBytes, getBlockResult.getProof().getIonText());
        if (verified) {
            throw new AssertionError("Expected altered block hash to not be verified against digest.");
        } else {
            log.info("Success! As expected flipping a bit in the block hash causes verification to fail.");
        }
    } catch (Exception e) {
        log.error("Failed to verify blocks in the ledger with name={}.", ledgerName, e);
        throw e;
    }
}
Also used : BlockAddress(software.amazon.qldb.tutorial.qldb.BlockAddress) GetDigestResult(com.amazonaws.services.qldb.model.GetDigestResult) GetBlockResult(com.amazonaws.services.qldb.model.GetBlockResult) ValueHolder(com.amazonaws.services.qldb.model.ValueHolder) JournalBlock(software.amazon.qldb.tutorial.qldb.JournalBlock) IOException(java.io.IOException)

Aggregations

GetBlockResult (com.amazonaws.services.qldb.model.GetBlockResult)3 ValueHolder (com.amazonaws.services.qldb.model.ValueHolder)3 IOException (java.io.IOException)3 GetBlockRequest (com.amazonaws.services.qldb.model.GetBlockRequest)2 GetDigestResult (com.amazonaws.services.qldb.model.GetDigestResult)1 BlockAddress (software.amazon.qldb.tutorial.qldb.BlockAddress)1 JournalBlock (software.amazon.qldb.tutorial.qldb.JournalBlock)1