Search in sources :

Example 1 with Block

use of com.neemre.btcdcli4j.core.domain.Block in project bisq-core by bisq-network.

the class FullNodeParser method parseBlocks.

// /////////////////////////////////////////////////////////////////////////////////////////
// Package private
// /////////////////////////////////////////////////////////////////////////////////////////
@VisibleForTesting
void parseBlocks(int startBlockHeight, int chainHeadHeight, Consumer<BsqBlock> newBlockHandler) throws BsqBlockchainException, BlockNotConnectingException {
    try {
        for (int blockHeight = startBlockHeight; blockHeight <= chainHeadHeight; blockHeight++) {
            Block btcdBlock = rpcService.requestBlock(blockHeight);
            final BsqBlock bsqBlock = parseBlock(btcdBlock);
            newBlockHandler.accept(bsqBlock);
        }
    } catch (BlockNotConnectingException e) {
        throw e;
    } catch (Throwable t) {
        log.error(t.toString());
        t.printStackTrace();
        throw new BsqBlockchainException(t);
    }
}
Also used : BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) BsqBlock(bisq.core.dao.blockchain.vo.BsqBlock) Block(com.neemre.btcdcli4j.core.domain.Block) BlockNotConnectingException(bisq.core.dao.blockchain.exceptions.BlockNotConnectingException) BsqBlock(bisq.core.dao.blockchain.vo.BsqBlock) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with Block

use of com.neemre.btcdcli4j.core.domain.Block in project bisq-core by bisq-network.

the class FullNodeParserTest method testParseBlocks.

@Test
public void testParseBlocks() throws BitcoindException, CommunicationException, BlockNotConnectingException, BsqBlockchainException {
    // Setup blocks to test, starting before genesis
    // Only the transactions related to bsq are relevant, no checks are done on correctness of blocks or other txs
    // so hashes and most other data don't matter
    long time = new Date().getTime();
    int genesisHeight = 200;
    int startHeight = 199;
    int headHeight = 201;
    Coin issuance = Coin.parseCoin("2.5");
    // Blockhashes
    String bh199 = "blockhash199";
    String bh200 = "blockhash200";
    String bh201 = "blockhash201";
    // Block 199
    String cbId199 = "cbid199";
    Tx cbTx199 = new Tx(cbId199, 199, bh199, time, new ArrayList<TxInput>(), asList(new TxOutput(0, 25, cbId199, null, null, null, 199)));
    Block block199 = new Block(bh199, 10, 10, 199, 2, "root", asList(cbId199), time, Long.parseLong("1234"), "bits", BigDecimal.valueOf(1), "chainwork", "previousBlockHash", bh200);
    // Genesis Block
    String cbId200 = "cbid200";
    Tx cbTx200 = new Tx(cbId200, 200, bh200, time, new ArrayList<TxInput>(), asList(new TxOutput(0, 25, cbId200, null, null, null, 200)));
    Tx genesisTx = new Tx(genesisTxId, 200, bh200, time, asList(new TxInput("someoldtx", 0)), asList(new TxOutput(0, issuance.getValue(), genesisTxId, null, null, null, 200)));
    Block block200 = new Block(bh200, 10, 10, 200, 2, "root", asList(cbId200, genesisTxId), time, Long.parseLong("1234"), "bits", BigDecimal.valueOf(1), "chainwork", bh199, bh201);
    // Block 201
    // Make a bsq transaction
    String cbId201 = "cbid201";
    String bsqTx1Id = "bsqtx1";
    long bsqTx1Value1 = Coin.parseCoin("2.4").getValue();
    long bsqTx1Value2 = Coin.parseCoin("0.04").getValue();
    Tx cbTx201 = new Tx(cbId201, 201, bh201, time, new ArrayList<TxInput>(), asList(new TxOutput(0, 25, cbId201, null, null, null, 201)));
    Tx bsqTx1 = new Tx(bsqTx1Id, 201, bh201, time, asList(new TxInput(genesisTxId, 0)), asList(new TxOutput(0, bsqTx1Value1, bsqTx1Id, null, null, null, 201), new TxOutput(1, bsqTx1Value2, bsqTx1Id, null, null, null, 201)));
    Block block201 = new Block(bh201, 10, 10, 201, 2, "root", asList(cbId201, bsqTx1Id), time, Long.parseLong("1234"), "bits", BigDecimal.valueOf(1), "chainwork", bh200, "nextBlockHash");
    new Expectations(rpcService) {

        {
            rpcService.requestBlock(199);
            result = block199;
            rpcService.requestBlock(200);
            result = block200;
            rpcService.requestBlock(201);
            result = block201;
            rpcService.requestTx(cbId199, 199);
            result = cbTx199;
            rpcService.requestTx(cbId200, genesisHeight);
            result = cbTx200;
            rpcService.requestTx(genesisTxId, genesisHeight);
            result = genesisTx;
            rpcService.requestTx(cbId201, 201);
            result = cbTx201;
            rpcService.requestTx(bsqTx1Id, 201);
            result = bsqTx1;
        }
    };
    // Running parseBlocks to build the bsq blockchain
    fullNodeParser.parseBlocks(startHeight, headHeight, block -> {
    });
    // Verify that the genesis tx has been added to the bsq blockchain with the correct issuance amount
    assertTrue(readModel.getGenesisTx() == genesisTx);
    assertTrue(readModel.getIssuedAmountAtGenesis().getValue() == issuance.getValue());
    // And that other txs are not added
    assertFalse(readModel.containsTx(cbId199));
    assertFalse(readModel.containsTx(cbId200));
    assertFalse(readModel.containsTx(cbId201));
    // But bsq txs are added
    assertTrue(readModel.containsTx(bsqTx1Id));
    TxOutput bsqOut1 = readModel.getUnspentAndMatureTxOutput(bsqTx1Id, 0).get();
    assertTrue(bsqOut1.isUnspent());
    assertTrue(bsqOut1.getValue() == bsqTx1Value1);
    TxOutput bsqOut2 = readModel.getUnspentAndMatureTxOutput(bsqTx1Id, 1).get();
    assertTrue(bsqOut2.isUnspent());
    assertTrue(bsqOut2.getValue() == bsqTx1Value2);
    assertFalse(readModel.isTxOutputSpendable(genesisTxId, 0));
    assertTrue(readModel.isTxOutputSpendable(bsqTx1Id, 0));
    assertTrue(readModel.isTxOutputSpendable(bsqTx1Id, 1));
}
Also used : Expectations(mockit.Expectations) Coin(org.bitcoinj.core.Coin) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Tx(bisq.core.dao.blockchain.vo.Tx) Block(com.neemre.btcdcli4j.core.domain.Block) Date(java.util.Date) TxInput(bisq.core.dao.blockchain.vo.TxInput) Test(org.junit.Test)

Aggregations

Block (com.neemre.btcdcli4j.core.domain.Block)2 BlockNotConnectingException (bisq.core.dao.blockchain.exceptions.BlockNotConnectingException)1 BsqBlockchainException (bisq.core.dao.blockchain.exceptions.BsqBlockchainException)1 BsqBlock (bisq.core.dao.blockchain.vo.BsqBlock)1 Tx (bisq.core.dao.blockchain.vo.Tx)1 TxInput (bisq.core.dao.blockchain.vo.TxInput)1 TxOutput (bisq.core.dao.blockchain.vo.TxOutput)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Date (java.util.Date)1 Expectations (mockit.Expectations)1 Coin (org.bitcoinj.core.Coin)1 Test (org.junit.Test)1