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