Search in sources :

Example 6 with HashedTrie

use of snowblossom.lib.trie.HashedTrie in project snowblossom by snowblossomcoin.

the class DB method open.

public void open() throws Exception {
    block_map = new ProtoDBMap(Block.newBuilder().build().getParserForType(), prov.openMap("block"));
    tx_map = new ProtoDBMap(Transaction.newBuilder().build().getParserForType(), prov.openMap("tx"));
    block_summary_map = new ProtoDBMap(BlockSummary.newBuilder().build().getParserForType(), prov.openMap("blocksummary"));
    block_header_map = new ProtoDBMap(BlockHeader.newBuilder().build().getParserForType(), prov.openMap("bh"));
    imported_block_map = new ProtoDBMap(ImportedBlock.newBuilder().build().getParserForType(), prov.openMap("ib"));
    external_shard_head_map = new ProtoDBMap(ExternalHeadList.newBuilder().build().getParserForType(), prov.openMap("xshm"));
    utxo_node_map = prov.openMap("u");
    block_height_map = prov.openMap("height");
    special_map = prov.openMap("special");
    chain_index_map = prov.openMap("cit");
    best_block_map = prov.openMap("bbm");
    trust_map = prov.openMap("trust");
    chain_index_trie = new HashedTrie(new TrieDBMap(chain_index_map), true, true);
    utxo_hashed_trie = new HashedTrie(new TrieDBMap(utxo_node_map), true, false);
    try {
        // Lobstack for example doesn't have multation map set implemented
        // but most htings don't need this so whatever
        special_map_set = prov.openMutationMapSet("special_map_set");
        child_block_map_set = prov.openMutationMapSet("cbms");
    } catch (Throwable t) {
    }
    if (config.getBoolean("addr_index")) {
    // This has been swithed to Hashed Trie in chain_index_trie
    }
    if (config.getBoolean("tx_index")) {
    // This has been swithed to Hashed Trie in chain_index_trie
    }
}
Also used : HashedTrie(snowblossom.lib.trie.HashedTrie) TrieDBMap(snowblossom.lib.trie.TrieDBMap)

Example 7 with HashedTrie

use of snowblossom.lib.trie.HashedTrie in project snowblossom by snowblossomcoin.

the class Validation method deepTransactionCheck.

/**
 * The block header need not be complete or real
 * It only needs block height and timestamp set for the purpose of this check
 * @return the fee amount in flakes if tx is good
 */
public static long deepTransactionCheck(Transaction tx, UtxoUpdateBuffer utxo_buffer, BlockHeader block_header, NetworkParams params, Set<Integer> shard_cover_set, Map<Integer, UtxoUpdateBuffer> export_buffers) throws ValidationException {
    try (TimeRecordAuto tra_blk = TimeRecord.openAuto("Validation.deepTransactionCheck")) {
        TransactionInner inner = null;
        try {
            inner = TransactionInner.parseFrom(tx.getInnerData());
        } catch (java.io.IOException e) {
            throw new ValidationException("error parsing tx on second pass somehow", e);
        }
        long sum_of_inputs = 0L;
        // Make sure all inputs exist
        for (TransactionInput in : inner.getInputsList()) {
            TransactionOutput matching_out = utxo_buffer.getOutputMatching(in);
            if (matching_out == null) {
                throw new ValidationException(String.format("No matching output for input %s", new ChainHash(in.getSrcTxId())));
            }
            validateSpendable(matching_out, block_header, params);
            sum_of_inputs += matching_out.getValue();
            // SIP-4 check
            if (block_header.getBlockHeight() >= params.getActivationHeightTxInValue()) {
                if (in.getValue() != 0L) {
                    if (in.getValue() != matching_out.getValue()) {
                        throw new ValidationException(String.format("Input value does not match: %d %d", matching_out.getValue(), in.getValue()));
                    }
                }
            }
            utxo_buffer.useOutput(matching_out, new ChainHash(in.getSrcTxId()), in.getSrcTxOutIdx());
        }
        long spent = 0L;
        // Sum up all outputs
        int out_idx = 0;
        ArrayList<ByteString> raw_output_list = TransactionUtil.extractWireFormatTxOut(tx);
        for (TransactionOutput out : inner.getOutputsList()) {
            validateTransactionOutput(out, block_header, params);
            spent += out.getValue();
            if (shard_cover_set.contains(out.getTargetShard())) {
                utxo_buffer.addOutput(raw_output_list, out, new ChainHash(tx.getTxHash()), out_idx);
            } else {
                int target_shard = out.getTargetShard();
                if (!export_buffers.containsKey(target_shard)) {
                    HashedTrie hashed_trie_mem = new HashedTrie(new TrieDBMem(), true, false);
                    UtxoUpdateBuffer export_txo_buffer = new UtxoUpdateBuffer(hashed_trie_mem, UtxoUpdateBuffer.EMPTY);
                    export_buffers.put(target_shard, export_txo_buffer);
                }
                export_buffers.get(target_shard).addOutput(raw_output_list, out, new ChainHash(tx.getTxHash()), out_idx);
            }
            out_idx++;
        }
        spent += inner.getFee();
        if (!inner.getIsCoinbase()) {
            if (sum_of_inputs != spent) {
                throw new ValidationException(String.format("Transaction took in %d and spent %d", sum_of_inputs, spent));
            }
        }
        return inner.getFee();
    }
}
Also used : HashedTrie(snowblossom.lib.trie.HashedTrie) TrieDBMem(snowblossom.lib.trie.TrieDBMem) ByteString(com.google.protobuf.ByteString) TimeRecordAuto(duckutil.TimeRecordAuto)

Example 8 with HashedTrie

use of snowblossom.lib.trie.HashedTrie in project snowblossom by snowblossomcoin.

the class Validation method getUtxoHashOfImportedOutputList.

public static ChainHash getUtxoHashOfImportedOutputList(ImportedOutputList lst, int expected_shard) throws ValidationException {
    HashedTrie hashed_trie = new HashedTrie(new TrieDBMem(), true, false);
    UtxoUpdateBuffer utxo_buffer = new UtxoUpdateBuffer(hashed_trie, UtxoUpdateBuffer.EMPTY);
    for (ImportedOutput io : lst.getTxOutsList()) {
        try {
            TransactionOutput out = TransactionOutput.parseFrom(io.getRawOutput());
            if (out.getTargetShard() != expected_shard) {
                throw new ValidationException("Import for unexpected shard");
            }
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            throw new ValidationException(e);
        }
        validateChainHash(io.getTxId(), "import tx_id");
        validateNonNegValue(io.getOutIdx(), "import out_idx");
    }
    utxo_buffer.addOutputs(lst);
    return utxo_buffer.simulateUpdates();
}
Also used : HashedTrie(snowblossom.lib.trie.HashedTrie) TrieDBMem(snowblossom.lib.trie.TrieDBMem)

Example 9 with HashedTrie

use of snowblossom.lib.trie.HashedTrie in project snowblossom by snowblossomcoin.

the class TrieRocksTest method setupDB.

@Before
public void setupDB() throws Exception {
    File db_dir = testFolder.newFolder();
    db = new TrieDBRocks(db_dir);
    trie = new HashedTrie(db, true, false);
}
Also used : TrieDBRocks(snowblossom.lib.trie.TrieDBRocks) HashedTrie(snowblossom.lib.trie.HashedTrie) File(java.io.File)

Example 10 with HashedTrie

use of snowblossom.lib.trie.HashedTrie in project snowblossom by snowblossomcoin.

the class MemPoolTest method testBasicTxDoubleSpend.

@Test
public void testBasicTxDoubleSpend() throws Exception {
    HashedTrie utxo_trie = newMemoryTrie();
    KeyPair keys = KeyUtil.generateECCompressedKey();
    UtxoUpdateBuffer utxo_buffer = new UtxoUpdateBuffer(utxo_trie, UtxoUpdateBuffer.EMPTY);
    TransactionInput in = addUtxoToUseAtInput(utxo_buffer, keys, 100000L);
    ChainHash utxo_root = utxo_buffer.commit();
    TransactionOutput out = TransactionOutput.newBuilder().setRecipientSpecHash(in.getSpecHash()).setValue(100000L).build();
    Transaction tx = TransactionUtil.createTransaction(ImmutableList.of(in), ImmutableList.of(out), keys);
    MemPool mem_pool = new MemPool(utxo_trie, new DummyChainState(100));
    mem_pool.rebuildPriorityMap(utxo_root);
    // Pool starts empty
    Assert.assertEquals(0, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    mem_pool.addTransaction(tx, false);
    TransactionOutput out_a = TransactionOutput.newBuilder().setRecipientSpecHash(in.getSpecHash()).setValue(50000L).build();
    Transaction tx2 = TransactionUtil.createTransaction(ImmutableList.of(in), ImmutableList.of(out_a, out_a), keys);
    Assert.assertNotEquals(tx.getTxHash(), tx2.getTxHash());
    try {
        mem_pool.addTransaction(tx2, false);
        Assert.fail();
    } catch (ValidationException e) {
        System.out.println(e);
    }
}
Also used : HashedTrie(snowblossom.lib.trie.HashedTrie) KeyPair(java.security.KeyPair) TransactionOutput(snowblossom.proto.TransactionOutput) Transaction(snowblossom.proto.Transaction) TransactionInput(snowblossom.proto.TransactionInput) MemPool(snowblossom.node.MemPool) Test(org.junit.Test)

Aggregations

HashedTrie (snowblossom.lib.trie.HashedTrie)12 KeyPair (java.security.KeyPair)6 Test (org.junit.Test)6 MemPool (snowblossom.node.MemPool)6 Transaction (snowblossom.proto.Transaction)6 TransactionInput (snowblossom.proto.TransactionInput)6 TransactionOutput (snowblossom.proto.TransactionOutput)6 ByteString (com.google.protobuf.ByteString)3 TreeMap (java.util.TreeMap)3 Map (java.util.Map)2 TrieDBMem (snowblossom.lib.trie.TrieDBMem)2 TimeRecord (duckutil.TimeRecord)1 TimeRecordAuto (duckutil.TimeRecordAuto)1 File (java.io.File)1 Random (java.util.Random)1 TrieDBMap (snowblossom.lib.trie.TrieDBMap)1 TrieDBRocks (snowblossom.lib.trie.TrieDBRocks)1