Search in sources :

Example 1 with InvalidGenesisTxException

use of bisq.core.dao.node.parser.exceptions.InvalidGenesisTxException in project bisq-core by bisq-network.

the class TxParser method findGenesisTx.

/**
 * Parse and return the genesis transaction for bisq, if applicable.
 *
 * @param genesisTxId         The transaction id of the bisq genesis transaction.
 * @param genesisBlockHeight  The block height of the bisq genesis transaction.
 * @param genesisTotalSupply  The total supply of the genesis issuance for bisq.
 * @param rawTx               The candidate transaction.
 * @return The genesis transaction if applicable, or Optional.empty() otherwise.
 */
public static Optional<TempTx> findGenesisTx(String genesisTxId, int genesisBlockHeight, Coin genesisTotalSupply, RawTx rawTx) {
    boolean isGenesis = rawTx.getBlockHeight() == genesisBlockHeight && rawTx.getId().equals(genesisTxId);
    if (!isGenesis)
        return Optional.empty();
    TempTx tempTx = TempTx.fromRawTx(rawTx);
    tempTx.setTxType(TxType.GENESIS);
    long remainingInputValue = genesisTotalSupply.getValue();
    for (int i = 0; i < tempTx.getTempTxOutputs().size(); ++i) {
        TempTxOutput txOutput = tempTx.getTempTxOutputs().get(i);
        long value = txOutput.getValue();
        boolean isValid = value <= remainingInputValue;
        if (!isValid)
            throw new InvalidGenesisTxException("Genesis tx is invalid; using more than available inputs. " + "Remaining input value is " + remainingInputValue + " sat; tx info: " + tempTx.toString());
        remainingInputValue -= value;
        txOutput.setTxOutputType(TxOutputType.GENESIS_OUTPUT);
    }
    if (remainingInputValue > 0) {
        throw new InvalidGenesisTxException("Genesis tx is invalid; not using all available inputs. " + "Remaining input value is " + remainingInputValue + " sat, tx info: " + tempTx.toString());
    }
    return Optional.of(tempTx);
}
Also used : TempTx(bisq.core.dao.state.blockchain.TempTx) InvalidGenesisTxException(bisq.core.dao.node.parser.exceptions.InvalidGenesisTxException) TempTxOutput(bisq.core.dao.state.blockchain.TempTxOutput)

Example 2 with InvalidGenesisTxException

use of bisq.core.dao.node.parser.exceptions.InvalidGenesisTxException in project bisq-core by bisq-network.

the class TxParserTest method testGetGenesisTx.

@Test
public void testGetGenesisTx() {
    int blockHeight = 200;
    String blockHash = "abc123";
    Coin genesisTotalSupply = Coin.parseCoin("2.5");
    // Thu Jun 20 14:04:25 CEST 2013
    long time = 1371729865;
    final List<TxInput> inputs = Arrays.asList(new TxInput("tx0", 0, null), new TxInput("tx1", 1, null));
    RawTxOutput output = new RawTxOutput(0, genesisTotalSupply.value, null, null, null, null, blockHeight);
    RawTx rawTx = new RawTx("tx2", blockHeight, blockHash, time, ImmutableList.copyOf(inputs), ImmutableList.copyOf(Arrays.asList(output)));
    String genesisTxId = "genesisTxId";
    int genesisBlockHeight = 150;
    // With mismatch in block height and tx id, we should not get genesis tx back.
    Optional<TempTx> result = TxParser.findGenesisTx(genesisTxId, genesisBlockHeight, genesisTotalSupply, rawTx);
    Optional<TempTx> want = Optional.empty();
    Assert.assertEquals(want, result);
    // With correct block height but mismatch in tx id, we should still not get genesis tx back.
    blockHeight = 150;
    rawTx = new RawTx("tx2", blockHeight, blockHash, time, ImmutableList.copyOf(inputs), ImmutableList.copyOf(Arrays.asList(output)));
    result = TxParser.findGenesisTx(genesisTxId, genesisBlockHeight, genesisTotalSupply, rawTx);
    want = Optional.empty();
    Assert.assertEquals(want, result);
    // With correct tx id and block height, we should find our genesis tx with correct tx and output type.
    rawTx = new RawTx(genesisTxId, blockHeight, blockHash, time, ImmutableList.copyOf(inputs), ImmutableList.copyOf(Arrays.asList(output)));
    result = TxParser.findGenesisTx(genesisTxId, genesisBlockHeight, genesisTotalSupply, rawTx);
    TempTx tempTx = TempTx.fromRawTx(rawTx);
    tempTx.setTxType(TxType.GENESIS);
    for (int i = 0; i < tempTx.getTempTxOutputs().size(); ++i) {
        tempTx.getTempTxOutputs().get(i).setTxOutputType(TxOutputType.GENESIS_OUTPUT);
    }
    want = Optional.of(tempTx);
    Assert.assertEquals(want, result);
    // With correct tx id and block height, but too low sum of outputs (lower than genesisTotalSupply), we
    // should see an exception raised.
    output = new RawTxOutput(0, genesisTotalSupply.value - 1, null, null, null, null, blockHeight);
    rawTx = new RawTx(genesisTxId, blockHeight, blockHash, time, ImmutableList.copyOf(inputs), ImmutableList.copyOf(Arrays.asList(output)));
    try {
        result = TxParser.findGenesisTx(genesisTxId, genesisBlockHeight, genesisTotalSupply, rawTx);
        Assert.fail("Expected an InvalidGenesisTxException to be thrown when outputs are too low");
    } catch (InvalidGenesisTxException igtxe) {
        String wantMessage = "Genesis tx is invalid; not using all available inputs. Remaining input value is 1 sat";
        Assert.assertTrue("Unexpected exception, want message starting with " + "'" + wantMessage + "', got '" + igtxe.getMessage() + "'", igtxe.getMessage().startsWith(wantMessage));
    }
    // With correct tx id and block height, but too high sum of outputs (higher than from genesisTotalSupply), we
    // should see an exception raised.
    RawTxOutput output1 = new RawTxOutput(0, genesisTotalSupply.value - 2, null, null, null, null, blockHeight);
    RawTxOutput output2 = new RawTxOutput(0, 3, null, null, null, null, blockHeight);
    rawTx = new RawTx(genesisTxId, blockHeight, blockHash, time, ImmutableList.copyOf(inputs), ImmutableList.copyOf(Arrays.asList(output1, output2)));
    try {
        result = TxParser.findGenesisTx(genesisTxId, genesisBlockHeight, genesisTotalSupply, rawTx);
        Assert.fail("Expected an InvalidGenesisTxException to be thrown when outputs are too high");
    } catch (InvalidGenesisTxException igtxe) {
        String wantMessage = "Genesis tx is invalid; using more than available inputs. Remaining input value is 2 sat";
        Assert.assertTrue("Unexpected exception, want message starting with " + "'" + wantMessage + "', got '" + igtxe.getMessage() + "'", igtxe.getMessage().startsWith(wantMessage));
    }
}
Also used : Coin(org.bitcoinj.core.Coin) TempTx(bisq.core.dao.state.blockchain.TempTx) RawTxOutput(bisq.core.dao.state.blockchain.RawTxOutput) InvalidGenesisTxException(bisq.core.dao.node.parser.exceptions.InvalidGenesisTxException) RawTx(bisq.core.dao.state.blockchain.RawTx) TxInput(bisq.core.dao.state.blockchain.TxInput) Test(org.junit.Test)

Aggregations

InvalidGenesisTxException (bisq.core.dao.node.parser.exceptions.InvalidGenesisTxException)2 TempTx (bisq.core.dao.state.blockchain.TempTx)2 RawTx (bisq.core.dao.state.blockchain.RawTx)1 RawTxOutput (bisq.core.dao.state.blockchain.RawTxOutput)1 TempTxOutput (bisq.core.dao.state.blockchain.TempTxOutput)1 TxInput (bisq.core.dao.state.blockchain.TxInput)1 Coin (org.bitcoinj.core.Coin)1 Test (org.junit.Test)1