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
}
}
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();
}
}
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();
}
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);
}
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);
}
}
Aggregations