use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.
the class MemPoolTest method testBasicTxNoInput.
@Test
public void testBasicTxNoInput() 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 = UtxoUpdateBuffer.EMPTY;
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());
try {
mem_pool.addTransaction(tx, false);
Assert.fail();
} catch (ValidationException e) {
Assert.assertTrue(e.getMessage(), e.getMessage().startsWith("Unable to find source tx"));
}
}
use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.
the class MemPoolTest method testBasicTxNoDoubleInput.
@Test
public void testBasicTxNoDoubleInput() 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(200000L).build();
Transaction tx = TransactionUtil.createTransaction(ImmutableList.of(in, 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());
try {
mem_pool.addTransaction(tx, false);
Assert.fail();
} catch (ValidationException e) {
System.out.println(e);
}
}
use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.
the class MemPoolTest method testBasicTxYes.
@Test
public void testBasicTxYes() 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);
// Then has our transaction
Assert.assertEquals(1, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
mem_pool.rebuildPriorityMap(utxo_root);
// Still has our transaction
Assert.assertEquals(1, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
mem_pool.rebuildPriorityMap(UtxoUpdateBuffer.EMPTY);
// That transaction is impossible now
Assert.assertEquals(0, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
mem_pool.rebuildPriorityMap(utxo_root);
// And does not come back
Assert.assertEquals(0, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
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());
mem_pool.addTransaction(tx2, false);
// And the outputs are freed up
Assert.assertEquals(1, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
}
use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.
the class MemPool method rebuildPriorityMap.
public synchronized void rebuildPriorityMap(ChainHash new_utxo_root) {
logger.log(Level.FINE, String.format("Mempool.rebuildPriorityMap(%s)", new_utxo_root));
utxo_for_pri_map = new_utxo_root;
priority_map.clear();
LinkedList<ChainHash> remove_list = new LinkedList<>();
for (TransactionMempoolInfo info : known_transactions.values()) {
Transaction tx = info.tx;
TXCluster cluster;
try {
cluster = buildTXCluster(tx);
} catch (ValidationException e) {
cluster = null;
}
if (cluster == null) {
remove_list.add(new ChainHash(tx.getTxHash()));
TransactionInner inner = TransactionUtil.getInner(tx);
for (TransactionInput in : inner.getInputsList()) {
String key = HexUtil.getHexString(in.getSrcTxId()) + ":" + in.getSrcTxOutIdx();
claimed_outputs.remove(key);
}
} else {
double ratio = (double) cluster.total_fee / (double) cluster.total_size;
priority_map.put(ratio, cluster);
}
}
logger.log(Level.FINER, String.format("Removing %d transactions from mempool", remove_list.size()));
for (ChainHash h : remove_list) {
TransactionMempoolInfo info = known_transactions.remove(h);
for (AddressSpecHash spec_hash : info.involved_addresses) {
address_tx_map.remove(spec_hash, h);
}
}
logger.log(Level.FINER, String.format("Remaining in mempool: %d", known_transactions.size()));
}
use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.
the class MonitorTool method sendNotices.
private void sendNotices(AddressSpecHash hash, ByteString tx_hash) {
ByteString key = hash.getBytes().concat(tx_hash);
synchronized (processed_tx) {
if (processed_tx.contains(key))
return;
}
Transaction tx = stub_holder.getBlockingStub().getTransaction(RequestTransaction.newBuilder().setTxHash(tx_hash).build());
TransactionInner inner = TransactionUtil.getInner(tx);
int idx = 0;
for (TransactionInput in : inner.getInputsList()) {
if (hash.getBytes().equals(in.getSpecHash())) {
monitor_interface.onOutbound(tx, idx);
}
idx++;
}
idx = 0;
for (TransactionOutput out : inner.getOutputsList()) {
if (hash.getBytes().equals(out.getRecipientSpecHash())) {
monitor_interface.onInbound(tx, idx);
}
idx++;
}
synchronized (processed_tx) {
processed_tx.add(key);
}
}
Aggregations