Search in sources :

Example 1 with TestTransaction

use of io.nuls.consensus.poc.TestTransaction in project nuls by nuls-io.

the class TxMemoryPoolTest method test.

@Test
public void test() {
    assertNotNull(txMemoryPool);
    Transaction tx = new TestTransaction();
    boolean success = txMemoryPool.add(tx, false);
    assertTrue(success);
    Transaction tempTx = txMemoryPool.get();
    assertEquals(tx, tempTx);
    tempTx = txMemoryPool.get();
    assertNull(tempTx);
    // tempTx = txMemoryPool.get(tx.getTx().getHash());
    // assertNull(tempTx);
    // 
    // txMemoryPool.add(tx, false);
    // tempTx = txMemoryPool.get(tx.getTx().getHash());
    // assertEquals(tempTx, tx);
    // 
    // tempTx = txMemoryPool.get(tx.getTx().getHash());
    // assertEquals(tempTx, tx);
    Transaction tx2 = new TestTransaction();
    txMemoryPool.add(tx2, true);
    tempTx = txMemoryPool.get();
    assertNotNull(tempTx);
    assertEquals(tempTx, tx);
    tempTx = txMemoryPool.get();
    assertNotNull(tempTx);
    assertEquals(tempTx, tx2);
    tempTx = txMemoryPool.get();
    assertNull(tempTx);
    txMemoryPool.add(tx2, true);
    List<Transaction> list = txMemoryPool.getAll();
    assertEquals(list.size(), 0);
    list = txMemoryPool.getAllOrphan();
    assertEquals(list.size(), 1);
    success = txMemoryPool.exist(tx.getHash());
    assertFalse(success);
    success = txMemoryPool.exist(tx2.getHash());
    assertTrue(success);
    success = txMemoryPool.remove(tx2.getHash());
    assertTrue(success);
    success = txMemoryPool.exist(tx2.getHash());
    assertFalse(success);
}
Also used : TestTransaction(io.nuls.consensus.poc.TestTransaction) Transaction(io.nuls.kernel.model.Transaction) TestTransaction(io.nuls.consensus.poc.TestTransaction) Test(org.junit.Test)

Example 2 with TestTransaction

use of io.nuls.consensus.poc.TestTransaction in project nuls by nuls-io.

the class ConsensusPocServiceTest method getMemoryTxs.

@Test
public void getMemoryTxs() throws Exception {
    assertNotNull(service);
    Transaction tx = new TestTransaction();
    tx.setTime(0l);
    assertEquals(tx.getHash().getDigestHex(), "0020c7f397ae78f2c1d12b3edc916e8112bcac576a98444c4c26034c207c9a7ad281");
    Result result = service.newTx(tx);
    assertNotNull(result);
    assertTrue(result.isSuccess());
    assertFalse(result.isFailed());
    List<Transaction> memoryTxs = service.getMemoryTxs();
    assertNotNull(memoryTxs);
    assertEquals(1, memoryTxs.size());
    tx = memoryTxs.get(0);
    assertNotNull(tx);
    assertEquals(tx.getHash().getDigestHex(), "0020c7f397ae78f2c1d12b3edc916e8112bcac576a98444c4c26034c207c9a7ad281");
}
Also used : TestTransaction(io.nuls.consensus.poc.TestTransaction) TestTransaction(io.nuls.consensus.poc.TestTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

Example 3 with TestTransaction

use of io.nuls.consensus.poc.TestTransaction in project nuls by nuls-io.

the class ConsensusPocServiceTest method newTx.

@Test
public void newTx() throws Exception {
    assertNotNull(service);
    // new a tx
    Transaction tx = new TestTransaction();
    CoinData coinData = new CoinData();
    List<Coin> fromList = new ArrayList<>();
    fromList.add(new Coin(new byte[20], Na.NA, 0L));
    coinData.setFrom(fromList);
    tx.setCoinData(coinData);
    tx.setTime(1l);
    assertNotNull(tx);
    assertNotNull(tx.getHash());
    assertEquals(tx.getHash().getDigestHex(), "00204a54f8b12b75c3c1fe5f261416adaf1a1b906ccf5673bb7a133ede5a0a4c56f8");
    Result result = service.newTx(tx);
    assertNotNull(result);
    assertTrue(result.isSuccess());
    assertFalse(result.isFailed());
    // test orphan
    NulsDataValidator<TestTransaction> testValidator = new NulsDataValidator<TestTransaction>() {

        @Override
        public ValidateResult validate(TestTransaction data) {
            if (data.getHash().getDigestHex().equals("0020e27ee243921bf482d7b62b6ee63c7ab1938953c834318b79fa3204c5c869e26b")) {
                return ValidateResult.getFailedResult("test.transaction", TransactionErrorCode.ORPHAN_TX);
            } else {
                return ValidateResult.getSuccessResult();
            }
        }
    };
    ValidatorManager.addValidator(TestTransaction.class, testValidator);
    tx = new TestTransaction();
    tx.setTime(2l);
    assertEquals(tx.getHash().getDigestHex(), "0020e27ee243921bf482d7b62b6ee63c7ab1938953c834318b79fa3204c5c869e26b");
    result = service.newTx(tx);
    assertNotNull(result);
    assertTrue(result.isSuccess());
    assertFalse(result.isFailed());
    List<Transaction> list = TxMemoryPool.getInstance().getAll();
    assertNotNull(list);
    assertEquals(list.size(), 1);
    List<Transaction> orphanList = TxMemoryPool.getInstance().getAllOrphan();
    assertNotNull(orphanList);
    assertEquals(orphanList.size(), 1);
}
Also used : TestTransaction(io.nuls.consensus.poc.TestTransaction) NulsDataValidator(io.nuls.kernel.validate.NulsDataValidator) TestTransaction(io.nuls.consensus.poc.TestTransaction) ArrayList(java.util.ArrayList) ValidateResult(io.nuls.kernel.validate.ValidateResult) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

Aggregations

TestTransaction (io.nuls.consensus.poc.TestTransaction)3 Test (org.junit.Test)3 BaseTest (io.nuls.consensus.poc.BaseTest)2 ValidateResult (io.nuls.kernel.validate.ValidateResult)2 Transaction (io.nuls.kernel.model.Transaction)1 NulsDataValidator (io.nuls.kernel.validate.NulsDataValidator)1 ArrayList (java.util.ArrayList)1