Search in sources :

Example 1 with BsqBlockchainException

use of bisq.core.dao.blockchain.exceptions.BsqBlockchainException in project bisq-core by bisq-network.

the class RpcService method setup.

public void setup() throws BsqBlockchainException {
    try {
        long startTs = System.currentTimeMillis();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        CloseableHttpClient httpProvider = HttpClients.custom().setConnectionManager(cm).build();
        Properties nodeConfig = new Properties();
        nodeConfig.setProperty("node.bitcoind.rpc.protocol", "http");
        nodeConfig.setProperty("node.bitcoind.rpc.host", "127.0.0.1");
        nodeConfig.setProperty("node.bitcoind.rpc.auth_scheme", "Basic");
        nodeConfig.setProperty("node.bitcoind.rpc.user", rpcUser);
        nodeConfig.setProperty("node.bitcoind.rpc.password", rpcPassword);
        nodeConfig.setProperty("node.bitcoind.rpc.port", rpcPort);
        nodeConfig.setProperty("node.bitcoind.notification.block.port", rpcBlockPort);
        nodeConfig.setProperty("node.bitcoind.notification.alert.port", "64647");
        nodeConfig.setProperty("node.bitcoind.notification.wallet.port", "64648");
        nodeConfig.setProperty("node.bitcoind.http.auth_scheme", "Basic");
        BtcdClientImpl client = new BtcdClientImpl(httpProvider, nodeConfig);
        daemon = new BtcdDaemonImpl(client);
        log.info("Setup took {} ms", System.currentTimeMillis() - startTs);
        this.client = client;
    } catch (BitcoindException | CommunicationException e) {
        if (e instanceof CommunicationException)
            log.error("Probably Bitcoin core is not running or the rpc port is not set correctly. rpcPort=" + rpcPort);
        log.error(e.toString());
        e.printStackTrace();
        log.error(e.getCause() != null ? e.getCause().toString() : "e.getCause()=null");
        throw new BsqBlockchainException(e.getMessage(), e);
    } catch (Throwable e) {
        log.error(e.toString());
        e.printStackTrace();
        throw new BsqBlockchainException(e.toString(), e);
    }
}
Also used : BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) BtcdClientImpl(com.neemre.btcdcli4j.core.client.BtcdClientImpl) BtcdDaemonImpl(com.neemre.btcdcli4j.daemon.BtcdDaemonImpl) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException) Properties(java.util.Properties) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 2 with BsqBlockchainException

use of bisq.core.dao.blockchain.exceptions.BsqBlockchainException in project bisq-core by bisq-network.

the class RpcService method requestTx.

public Tx requestTx(String txId, int blockHeight) throws BsqBlockchainException {
    try {
        RawTransaction rawTransaction = requestRawTransaction(txId);
        // rawTransaction.getTime() is in seconds but we keep it in ms internally
        final long time = rawTransaction.getTime() * 1000;
        final List<TxInput> txInputs = rawTransaction.getVIn().stream().filter(rawInput -> rawInput != null && rawInput.getVOut() != null && rawInput.getTxId() != null).map(rawInput -> new TxInput(rawInput.getTxId(), rawInput.getVOut())).collect(Collectors.toList());
        final List<TxOutput> txOutputs = rawTransaction.getVOut().stream().filter(e -> e != null && e.getN() != null && e.getValue() != null && e.getScriptPubKey() != null).map(rawOutput -> {
            byte[] opReturnData = null;
            final com.neemre.btcdcli4j.core.domain.PubKeyScript scriptPubKey = rawOutput.getScriptPubKey();
            if (scriptPubKey.getType().equals(ScriptTypes.NULL_DATA)) {
                String[] chunks = scriptPubKey.getAsm().split(" ");
                // We get on testnet a lot of "OP_RETURN 0" data, so we filter those away
                if (chunks.length == 2 && chunks[0].equals("OP_RETURN") && !"0".equals(chunks[1])) {
                    try {
                        opReturnData = Utils.HEX.decode(chunks[1]);
                    } catch (Throwable t) {
                        // We get sometimes exceptions, seems BitcoinJ
                        // cannot handle all existing OP_RETURN data, but we ignore them
                        // anyway as our OP_RETURN data is valid in BitcoinJ
                        log.warn("Error at Utils.HEX.decode(chunks[1]): " + t.toString() + " / chunks[1]=" + chunks[1]);
                    }
                }
            }
            // We don't support raw MS which are the only case where scriptPubKey.getAddresses()>1
            String address = scriptPubKey.getAddresses() != null && scriptPubKey.getAddresses().size() == 1 ? scriptPubKey.getAddresses().get(0) : null;
            final PubKeyScript pubKeyScript = dumpBlockchainData ? new PubKeyScript(scriptPubKey) : null;
            return new TxOutput(rawOutput.getN(), rawOutput.getValue().movePointRight(8).longValue(), rawTransaction.getTxId(), pubKeyScript, address, opReturnData, blockHeight);
        }).collect(Collectors.toList());
        return new Tx(txId, blockHeight, rawTransaction.getBlockHash(), time, ImmutableList.copyOf(txInputs), ImmutableList.copyOf(txOutputs));
    } catch (BitcoindException | CommunicationException e) {
        log.error("error at requestTx with txId={}, blockHeight={}", txId, blockHeight);
        throw new BsqBlockchainException(e.getMessage(), e);
    }
}
Also used : BtcdClientImpl(com.neemre.btcdcli4j.core.client.BtcdClientImpl) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) DaoOptionKeys(bisq.core.dao.DaoOptionKeys) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) BtcdDaemonImpl(com.neemre.btcdcli4j.daemon.BtcdDaemonImpl) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) Transaction(com.neemre.btcdcli4j.core.domain.Transaction) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PubKeyScript(bisq.core.dao.blockchain.btcd.PubKeyScript) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException) Named(javax.inject.Named) TxInput(bisq.core.dao.blockchain.vo.TxInput) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Properties(java.util.Properties) Logger(org.slf4j.Logger) Tx(bisq.core.dao.blockchain.vo.Tx) Utils(org.bitcoinj.core.Utils) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) ScriptTypes(com.neemre.btcdcli4j.core.domain.enums.ScriptTypes) BtcdClient(com.neemre.btcdcli4j.core.client.BtcdClient) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Collectors(java.util.stream.Collectors) BlockListener(com.neemre.btcdcli4j.daemon.event.BlockListener) Consumer(java.util.function.Consumer) Block(com.neemre.btcdcli4j.core.domain.Block) List(java.util.List) BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) BtcdDaemon(com.neemre.btcdcli4j.daemon.BtcdDaemon) HttpClients(org.apache.http.impl.client.HttpClients) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Tx(bisq.core.dao.blockchain.vo.Tx) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) PubKeyScript(bisq.core.dao.blockchain.btcd.PubKeyScript) TxInput(bisq.core.dao.blockchain.vo.TxInput) BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException)

Example 3 with BsqBlockchainException

use of bisq.core.dao.blockchain.exceptions.BsqBlockchainException 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 4 with BsqBlockchainException

use of bisq.core.dao.blockchain.exceptions.BsqBlockchainException in project bisq-core by bisq-network.

the class RpcService method requestFees.

public void requestFees(String txId, int blockHeight, Map<Integer, Long> feesByBlock) throws BsqBlockchainException {
    try {
        Transaction transaction = requestTx(txId);
        final BigDecimal fee = transaction.getFee();
        if (fee != null)
            feesByBlock.put(blockHeight, Math.abs(fee.multiply(BigDecimal.valueOf(Coin.COIN.value)).longValue()));
    } catch (BitcoindException | CommunicationException e) {
        log.error("error at requestFees with txId={}, blockHeight={}", txId, blockHeight);
        throw new BsqBlockchainException(e.getMessage(), e);
    }
}
Also used : BsqBlockchainException(bisq.core.dao.blockchain.exceptions.BsqBlockchainException) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) Transaction(com.neemre.btcdcli4j.core.domain.Transaction) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException) BigDecimal(java.math.BigDecimal)

Aggregations

BsqBlockchainException (bisq.core.dao.blockchain.exceptions.BsqBlockchainException)4 BitcoindException (com.neemre.btcdcli4j.core.BitcoindException)3 CommunicationException (com.neemre.btcdcli4j.core.CommunicationException)3 BtcdClientImpl (com.neemre.btcdcli4j.core.client.BtcdClientImpl)2 Block (com.neemre.btcdcli4j.core.domain.Block)2 RawTransaction (com.neemre.btcdcli4j.core.domain.RawTransaction)2 Transaction (com.neemre.btcdcli4j.core.domain.Transaction)2 BtcdDaemonImpl (com.neemre.btcdcli4j.daemon.BtcdDaemonImpl)2 BigDecimal (java.math.BigDecimal)2 Properties (java.util.Properties)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)2 DaoOptionKeys (bisq.core.dao.DaoOptionKeys)1 PubKeyScript (bisq.core.dao.blockchain.btcd.PubKeyScript)1 BlockNotConnectingException (bisq.core.dao.blockchain.exceptions.BlockNotConnectingException)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