Search in sources :

Example 1 with PubKeyScript

use of bisq.core.dao.state.blockchain.PubKeyScript in project bisq-core by bisq-network.

the class RpcService method getTxFromRawTransaction.

// /////////////////////////////////////////////////////////////////////////////////////////
// Private
// /////////////////////////////////////////////////////////////////////////////////////////
private RawTx getTxFromRawTransaction(RawTransaction rawBtcTx, com.neemre.btcdcli4j.core.domain.RawBlock rawBtcBlock) {
    String txId = rawBtcTx.getTxId();
    // We convert block time from sec to ms
    long blockTime = rawBtcBlock.getTime() * 1000;
    int blockHeight = rawBtcBlock.getHeight();
    String blockHash = rawBtcBlock.getHash();
    final List<TxInput> txInputs = rawBtcTx.getVIn().stream().filter(rawInput -> rawInput != null && rawInput.getVOut() != null && rawInput.getTxId() != null).map(rawInput -> {
        // We don't support segWit inputs yet as well as no pay to pubkey txs...
        String[] split = rawInput.getScriptSig().getAsm().split("\\[ALL\\] ");
        String pubKeyAsHex;
        if (split.length == 2) {
            pubKeyAsHex = rawInput.getScriptSig().getAsm().split("\\[ALL\\] ")[1];
        } else {
            // If we receive a pay to pubkey tx the pubKey is not included as
            // it is in the output already.
            // Bitcoin Core creates payToPubKey tx when spending mined coins (regtest)...
            pubKeyAsHex = null;
            log.debug("pubKeyAsHex is not set as we received a not supported sigScript " + "(segWit or payToPubKey tx). txId={}, asm={}", rawBtcTx.getTxId(), rawInput.getScriptSig().getAsm());
        }
        return new TxInput(rawInput.getTxId(), rawInput.getVOut(), pubKeyAsHex);
    }).collect(Collectors.toList());
    final List<RawTxOutput> txOutputs = rawBtcTx.getVOut().stream().filter(e -> e != null && e.getN() != null && e.getValue() != null && e.getScriptPubKey() != null).map(rawBtcTxOutput -> {
        byte[] opReturnData = null;
        com.neemre.btcdcli4j.core.domain.PubKeyScript scriptPubKey = rawBtcTxOutput.getScriptPubKey();
        if (ScriptTypes.NULL_DATA.equals(scriptPubKey.getType()) && scriptPubKey.getAsm() != null) {
            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 && "OP_RETURN".equals(chunks[0]) && !"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;
        PubKeyScript pubKeyScript = dumpBlockchainData ? new PubKeyScript(scriptPubKey) : null;
        return new RawTxOutput(rawBtcTxOutput.getN(), rawBtcTxOutput.getValue().movePointRight(8).longValue(), rawBtcTx.getTxId(), pubKeyScript, address, opReturnData, blockHeight);
    }).collect(Collectors.toList());
    return new RawTx(txId, blockHeight, blockHash, blockTime, ImmutableList.copyOf(txInputs), ImmutableList.copyOf(txOutputs));
}
Also used : BtcdClientImpl(com.neemre.btcdcli4j.core.client.BtcdClientImpl) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Utilities(bisq.common.util.Utilities) Inject(com.google.inject.Inject) DaoOptionKeys(bisq.core.dao.DaoOptionKeys) RawTransaction(com.neemre.btcdcli4j.core.domain.RawTransaction) BtcdDaemonImpl(com.neemre.btcdcli4j.daemon.BtcdDaemonImpl) RawTxOutput(bisq.core.dao.state.blockchain.RawTxOutput) ImmutableList(com.google.common.collect.ImmutableList) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BitcoindException(com.neemre.btcdcli4j.core.BitcoindException) Named(javax.inject.Named) TxInput(bisq.core.dao.state.blockchain.TxInput) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Properties(java.util.Properties) RawBlock(bisq.core.dao.state.blockchain.RawBlock) RawTx(bisq.core.dao.state.blockchain.RawTx) Utils(org.bitcoinj.core.Utils) ResultHandler(bisq.common.handlers.ResultHandler) CommunicationException(com.neemre.btcdcli4j.core.CommunicationException) ScriptTypes(com.neemre.btcdcli4j.core.domain.enums.ScriptTypes) BtcdClient(com.neemre.btcdcli4j.core.client.BtcdClient) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) BlockListener(com.neemre.btcdcli4j.daemon.event.BlockListener) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) UserThread(bisq.common.UserThread) BtcdDaemon(com.neemre.btcdcli4j.daemon.BtcdDaemon) NotNull(org.jetbrains.annotations.NotNull) HttpClients(org.apache.http.impl.client.HttpClients) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) RawTxOutput(bisq.core.dao.state.blockchain.RawTxOutput) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) TxInput(bisq.core.dao.state.blockchain.TxInput) RawTx(bisq.core.dao.state.blockchain.RawTx)

Example 2 with PubKeyScript

use of bisq.core.dao.state.blockchain.PubKeyScript in project bisq-core by bisq-network.

the class JsonBlockChainExporter method maybeExport.

public void maybeExport() {
    if (dumpBlockchainData) {
        ListenableFuture<Void> future = executor.submit(() -> {
            final BsqState bsqStateClone = bsqStateService.getClone();
            Map<String, Tx> txMap = bsqStateService.getBlocksFromState(bsqStateClone).stream().filter(Objects::nonNull).flatMap(block -> block.getTxs().stream()).collect(Collectors.toMap(Tx::getId, tx -> tx));
            for (Tx tx : txMap.values()) {
                String txId = tx.getId();
                final Optional<TxType> optionalTxType = bsqStateService.getOptionalTxType(txId);
                optionalTxType.ifPresent(txType1 -> {
                    JsonTxType txType = txType1 != TxType.UNDEFINED_TX_TYPE ? JsonTxType.valueOf(txType1.name()) : null;
                    List<JsonTxOutput> outputs = new ArrayList<>();
                    tx.getTxOutputs().forEach(txOutput -> {
                        final Optional<SpentInfo> optionalSpentInfo = bsqStateService.getSpentInfo(txOutput);
                        final boolean isBsqOutput = bsqStateService.isBsqTxOutputType(txOutput);
                        final PubKeyScript pubKeyScript = txOutput.getPubKeyScript();
                        final JsonTxOutput outputForJson = new JsonTxOutput(txId, txOutput.getIndex(), isBsqOutput ? txOutput.getValue() : 0, !isBsqOutput ? txOutput.getValue() : 0, txOutput.getBlockHeight(), isBsqOutput, bsqStateService.getBurntFee(tx.getId()), txOutput.getAddress(), pubKeyScript != null ? new JsonScriptPubKey(pubKeyScript) : null, optionalSpentInfo.map(JsonSpentInfo::new).orElse(null), tx.getTime(), txType, txType != null ? txType.getDisplayString() : "", txOutput.getOpReturnData() != null ? Utils.HEX.encode(txOutput.getOpReturnData()) : null);
                        outputs.add(outputForJson);
                        txOutputFileManager.writeToDisc(Utilities.objectToJson(outputForJson), outputForJson.getId());
                    });
                    List<JsonTxInput> inputs = tx.getTxInputs().stream().map(txInput -> {
                        Optional<TxOutput> optionalTxOutput = bsqStateService.getConnectedTxOutput(txInput);
                        if (optionalTxOutput.isPresent()) {
                            final TxOutput connectedTxOutput = optionalTxOutput.get();
                            final boolean isBsqOutput = bsqStateService.isBsqTxOutputType(connectedTxOutput);
                            return new JsonTxInput(txInput.getConnectedTxOutputIndex(), txInput.getConnectedTxOutputTxId(), connectedTxOutput.getValue(), isBsqOutput, connectedTxOutput.getAddress(), tx.getTime());
                        } else {
                            return null;
                        }
                    }).filter(Objects::nonNull).collect(Collectors.toList());
                    final JsonTx jsonTx = new JsonTx(txId, tx.getBlockHeight(), tx.getBlockHash(), tx.getTime(), inputs, outputs, txType, txType != null ? txType.getDisplayString() : "", bsqStateService.getBurntFee(tx.getId()));
                    txFileManager.writeToDisc(Utilities.objectToJson(jsonTx), txId);
                });
            }
            jsonFileManager.writeToDisc(Utilities.objectToJson(bsqStateClone), "BsqStateService");
            return null;
        });
        Futures.addCallback(future, new FutureCallback<Void>() {

            public void onSuccess(Void ignore) {
                log.trace("onSuccess");
            }

            public void onFailure(@NotNull Throwable throwable) {
                log.error(throwable.toString());
                throwable.printStackTrace();
            }
        });
    }
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Utilities(bisq.common.util.Utilities) TxOutput(bisq.core.dao.state.blockchain.TxOutput) Inject(com.google.inject.Inject) DaoOptionKeys(bisq.core.dao.DaoOptionKeys) TxType(bisq.core.dao.state.blockchain.TxType) ArrayList(java.util.ArrayList) JsonFileManager(bisq.common.storage.JsonFileManager) Map(java.util.Map) Named(javax.inject.Named) SpentInfo(bisq.core.dao.state.blockchain.SpentInfo) BsqState(bisq.core.dao.state.BsqState) Utils(org.bitcoinj.core.Utils) Tx(bisq.core.dao.state.blockchain.Tx) BsqStateService(bisq.core.dao.state.BsqStateService) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) Objects(java.util.Objects) FileUtil(bisq.common.storage.FileUtil) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Paths(java.nio.file.Paths) Storage(bisq.common.storage.Storage) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) TxOutput(bisq.core.dao.state.blockchain.TxOutput) BsqState(bisq.core.dao.state.BsqState) ArrayList(java.util.ArrayList) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) SpentInfo(bisq.core.dao.state.blockchain.SpentInfo) Tx(bisq.core.dao.state.blockchain.Tx) TxType(bisq.core.dao.state.blockchain.TxType) Objects(java.util.Objects)

Aggregations

Utilities (bisq.common.util.Utilities)2 DaoOptionKeys (bisq.core.dao.DaoOptionKeys)2 PubKeyScript (bisq.core.dao.state.blockchain.PubKeyScript)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 Inject (com.google.inject.Inject)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Named (javax.inject.Named)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Utils (org.bitcoinj.core.Utils)2 NotNull (org.jetbrains.annotations.NotNull)2 UserThread (bisq.common.UserThread)1 ResultHandler (bisq.common.handlers.ResultHandler)1 FileUtil (bisq.common.storage.FileUtil)1 JsonFileManager (bisq.common.storage.JsonFileManager)1 Storage (bisq.common.storage.Storage)1 BsqState (bisq.core.dao.state.BsqState)1