Search in sources :

Example 11 with TransactionInput

use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.

the class MemPoolTest method testSimpleChain.

@Test
public void testSimpleChain() throws Exception {
    HashedTrie utxo_trie = newMemoryTrie();
    KeyPair keys = KeyUtil.generateECCompressedKey();
    UtxoUpdateBuffer utxo_buffer = new UtxoUpdateBuffer(utxo_trie, UtxoUpdateBuffer.EMPTY);
    TransactionInput in_a = addUtxoToUseAtInput(utxo_buffer, keys, 100000L);
    ChainHash utxo_root = utxo_buffer.commit();
    TransactionOutput out_a = TransactionOutput.newBuilder().setRecipientSpecHash(in_a.getSpecHash()).setValue(100000L).build();
    Transaction tx_a = TransactionUtil.createTransaction(ImmutableList.of(in_a), ImmutableList.of(out_a), keys);
    MemPool mem_pool = new MemPool(utxo_trie, new DummyChainState(100));
    mem_pool.rebuildPriorityMap(utxo_root);
    Assert.assertEquals(0, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    mem_pool.addTransaction(tx_a, false);
    Assert.assertEquals(1, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    TransactionInput in_b = TransactionInput.newBuilder().setSpecHash(in_a.getSpecHash()).setSrcTxId(tx_a.getTxHash()).setSrcTxOutIdx(0).build();
    TransactionOutput out_b = TransactionOutput.newBuilder().setRecipientSpecHash(in_a.getSpecHash()).setValue(100000L).build();
    Transaction tx_b = TransactionUtil.createTransaction(ImmutableList.of(in_b), ImmutableList.of(out_b), keys);
    mem_pool.addTransaction(tx_b, false);
    Assert.assertEquals(2, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    TransactionInput in_c = TransactionInput.newBuilder().setSpecHash(in_a.getSpecHash()).setSrcTxId(tx_b.getTxHash()).setSrcTxOutIdx(0).build();
    TransactionOutput out_c = TransactionOutput.newBuilder().setRecipientSpecHash(in_a.getSpecHash()).setValue(100000L).build();
    Transaction tx_c = TransactionUtil.createTransaction(ImmutableList.of(in_c), ImmutableList.of(out_c), keys);
    mem_pool.addTransaction(tx_c, false);
    Assert.assertEquals(3, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
}
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)

Example 12 with TransactionInput

use of snowblossom.proto.TransactionInput in project snowblossom by snowblossomcoin.

the class MemPoolTest method testStormChain.

@Test
public void testStormChain() throws Exception {
    HashedTrie utxo_trie = newMemoryTrie();
    KeyPair keys = KeyUtil.generateECCompressedKey();
    UtxoUpdateBuffer utxo_buffer = new UtxoUpdateBuffer(utxo_trie, UtxoUpdateBuffer.EMPTY);
    Random rnd = new Random();
    TreeMap<Double, InputInfo> ready_inputs = new TreeMap<>();
    AddressSpecHash address = null;
    for (int i = 0; i < 100; i++) {
        TransactionInput in = addUtxoToUseAtInput(utxo_buffer, keys, 100000L);
        System.out.println("Source tx: " + new ChainHash(in.getSrcTxId()));
        InputInfo ii = new InputInfo();
        ii.in = in;
        ii.value = 100000L;
        ready_inputs.put(rnd.nextDouble(), ii);
        address = new AddressSpecHash(in.getSpecHash());
    }
    ChainHash utxo_root = utxo_buffer.commit();
    MemPool mem_pool = new MemPool(utxo_trie, new DummyChainState(100), 10000000);
    Assert.assertEquals(0, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    TimeRecord tr = new TimeRecord();
    TimeRecord.setSharedRecord(tr);
    for (int i = 0; i < 500; i++) {
        long t1 = System.nanoTime();
        InputInfo ia = ready_inputs.pollFirstEntry().getValue();
        InputInfo ib = ready_inputs.pollFirstEntry().getValue();
        InputInfo ic = ready_inputs.pollFirstEntry().getValue();
        TransactionOutput out = TransactionOutput.newBuilder().setRecipientSpecHash(address.getBytes()).setValue(100000L).build();
        System.out.println("Selected inputs: " + ia + " " + ib + " " + ic);
        long t3 = System.nanoTime();
        Transaction tx = TransactionUtil.createTransaction(ImmutableList.of(ia.in, ib.in, ic.in), ImmutableList.of(out, out, out), keys);
        TimeRecord.record(t3, "create_tx");
        System.out.println("Intermediate transaction: " + new ChainHash(tx.getTxHash()));
        long t2 = System.nanoTime();
        mem_pool.addTransaction(tx, false);
        TimeRecord.record(t2, "mem_pool");
        for (int j = 0; j < 3; j++) {
            TransactionInput in = TransactionInput.newBuilder().setSpecHash(address.getBytes()).setSrcTxId(tx.getTxHash()).setSrcTxOutIdx(j).build();
            System.out.println(String.format("Adding output %s:%d", new ChainHash(tx.getTxHash()).toString(), j));
            InputInfo ii = new InputInfo();
            ii.in = in;
            ii.value = 100000L;
            ready_inputs.put(rnd.nextDouble(), ii);
        }
        TimeRecord.record(t1, "tx");
    }
    long t4 = System.nanoTime();
    Assert.assertEquals(500, mem_pool.getTransactionsForBlock(utxo_root, 1048576).size());
    TimeRecord.record(t4, "get_block_tx_list");
    tr.printReport(System.out);
}
Also used : HashedTrie(snowblossom.lib.trie.HashedTrie) KeyPair(java.security.KeyPair) TransactionOutput(snowblossom.proto.TransactionOutput) TreeMap(java.util.TreeMap) TransactionInput(snowblossom.proto.TransactionInput) MemPool(snowblossom.node.MemPool) Random(java.util.Random) Transaction(snowblossom.proto.Transaction) TimeRecord(duckutil.TimeRecord) Test(org.junit.Test)

Aggregations

TransactionInput (snowblossom.proto.TransactionInput)12 TransactionOutput (snowblossom.proto.TransactionOutput)10 Transaction (snowblossom.proto.Transaction)9 KeyPair (java.security.KeyPair)6 Test (org.junit.Test)6 HashedTrie (snowblossom.lib.trie.HashedTrie)6 MemPool (snowblossom.node.MemPool)6 TransactionInner (snowblossom.proto.TransactionInner)5 ByteString (com.google.protobuf.ByteString)4 Random (java.util.Random)2 MetricLog (duckutil.MetricLog)1 TimeRecord (duckutil.TimeRecord)1 TreeMap (java.util.TreeMap)1 AddressSpec (snowblossom.proto.AddressSpec)1 BlockHeader (snowblossom.proto.BlockHeader)1 RequestTransaction (snowblossom.proto.RequestTransaction)1