Search in sources :

Example 1 with BlockChainStatus

use of co.rsk.core.bc.BlockChainStatus in project rskj by rsksmart.

the class DslFilesTest method runLogs01Resource.

@Test
public void runLogs01Resource() throws FileNotFoundException, DslProcessorException {
    DslParser parser = DslParser.fromResource("dsl/logs01.txt");
    World world = new World();
    WorldDslProcessor processor = new WorldDslProcessor(world);
    processor.processCommands(parser);
    // the transaction receipt should have three logs
    BlockChainStatus status = world.getBlockChain().getStatus();
    Assert.assertEquals(1, status.getBestBlockNumber());
    Block block = status.getBestBlock();
    Assert.assertEquals(1, block.getTransactionsList().size());
    byte[] txhash = block.getTransactionsList().get(0).getHash().getBytes();
    TransactionInfo txinfo = world.getBlockChain().getTransactionInfo(txhash);
    // only three events, raised by
    // Counter constructor
    // Counter getValue
    // Creator constructor
    Assert.assertEquals(3, txinfo.getReceipt().getLogInfoList().size());
    // only one topic in each event
    Assert.assertEquals(1, txinfo.getReceipt().getLogInfoList().get(0).getTopics().size());
    Assert.assertEquals(1, txinfo.getReceipt().getLogInfoList().get(1).getTopics().size());
    Assert.assertEquals(1, txinfo.getReceipt().getLogInfoList().get(2).getTopics().size());
    // the topics are different
    DataWord topic1 = txinfo.getReceipt().getLogInfoList().get(0).getTopics().get(0);
    DataWord topic2 = txinfo.getReceipt().getLogInfoList().get(1).getTopics().get(0);
    DataWord topic3 = txinfo.getReceipt().getLogInfoList().get(2).getTopics().get(0);
    Assert.assertNotEquals(topic1, topic2);
    Assert.assertNotEquals(topic1, topic3);
    Assert.assertNotEquals(topic2, topic3);
    // only the third log was directly produced by the created contract
    byte[] contractAddress = txinfo.getReceipt().getTransaction().getContractAddress().getBytes();
    Assert.assertFalse(Arrays.equals(contractAddress, txinfo.getReceipt().getLogInfoList().get(0).getAddress()));
    Assert.assertFalse(Arrays.equals(contractAddress, txinfo.getReceipt().getLogInfoList().get(1).getAddress()));
    Assert.assertTrue(Arrays.equals(contractAddress, txinfo.getReceipt().getLogInfoList().get(2).getAddress()));
}
Also used : WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) DslParser(co.rsk.test.dsl.DslParser) Block(org.ethereum.core.Block) TransactionInfo(org.ethereum.db.TransactionInfo) BlockChainStatus(co.rsk.core.bc.BlockChainStatus) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 2 with BlockChainStatus

use of co.rsk.core.bc.BlockChainStatus in project rskj by rsksmart.

the class SnapshotManagerTest method revertToSnapshotClearingTransactionPool.

@Test
public void revertToSnapshotClearingTransactionPool() {
    Blockchain blockchain = createBlockchain();
    addBlocks(blockchain, 10);
    BlockChainStatus status = blockchain.getStatus();
    SnapshotManager manager = new SnapshotManager();
    int snapshotId = manager.takeSnapshot(blockchain);
    addBlocks(blockchain, 20);
    manager.takeSnapshot(blockchain);
    Assert.assertEquals(2, manager.getSnapshots().size());
    TransactionPool transactionPool = blockchain.getTransactionPool();
    Assert.assertNotNull(transactionPool);
    List<Transaction> txs = new ArrayList<>();
    txs.add(createSampleTransaction());
    txs.add(createSampleTransaction());
    transactionPool.addTransactions(txs);
    Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());
    Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());
    Assert.assertEquals(30, blockchain.getStatus().getBestBlockNumber());
    Assert.assertTrue(manager.revertToSnapshot(blockchain, snapshotId));
    BlockChainStatus newStatus = blockchain.getStatus();
    Assert.assertEquals(status.getBestBlockNumber(), newStatus.getBestBlockNumber());
    Assert.assertEquals(status.getTotalDifficulty(), newStatus.getTotalDifficulty());
    Assert.assertEquals(status.getBestBlock().getHash(), newStatus.getBestBlock().getHash());
    Assert.assertTrue(blockchain.getTransactionPool().getPendingTransactions().isEmpty());
    Assert.assertEquals(1, manager.getSnapshots().size());
    for (int k = 11; k <= 30; k++) Assert.assertTrue(blockchain.getBlocksByNumber(k).isEmpty());
}
Also used : ArrayList(java.util.ArrayList) BlockChainStatus(co.rsk.core.bc.BlockChainStatus) Test(org.junit.Test)

Example 3 with BlockChainStatus

use of co.rsk.core.bc.BlockChainStatus in project rskj by rsksmart.

the class NodeMessageHandler method sendStatusToAll.

private synchronized void sendStatusToAll() {
    BlockChainStatus blockChainStatus = this.blockProcessor.getBlockchain().getStatus();
    Block block = blockChainStatus.getBestBlock();
    BlockDifficulty totalDifficulty = blockChainStatus.getTotalDifficulty();
    Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), totalDifficulty);
    logger.trace("Sending status best block to all {} {}", status.getBestBlockNumber(), Hex.toHexString(status.getBestBlockHash()).substring(0, 8));
    this.channelManager.broadcastStatus(status);
}
Also used : BlockChainStatus(co.rsk.core.bc.BlockChainStatus) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockChainStatus(co.rsk.core.bc.BlockChainStatus)

Example 4 with BlockChainStatus

use of co.rsk.core.bc.BlockChainStatus in project rskj by rsksmart.

the class RskWireProtocol method sendStatus.

/**
 ***********************
 *    Message Sending    *
 ************************
 */
@Override
public void sendStatus() {
    byte protocolVersion = version.getCode();
    int networkId = config.networkId();
    BlockChainStatus blockChainStatus = this.blockchain.getStatus();
    Block bestBlock = blockChainStatus.getBestBlock();
    BlockDifficulty totalDifficulty = blockChainStatus.getTotalDifficulty();
    // Original status
    Genesis genesis = GenesisLoader.loadGenesis(config, config.genesisInfo(), config.getBlockchainConfig().getCommonConstants().getInitialNonce(), true);
    org.ethereum.net.eth.message.StatusMessage msg = new org.ethereum.net.eth.message.StatusMessage(protocolVersion, networkId, ByteUtil.bigIntegerToBytes(totalDifficulty.asBigInteger()), bestBlock.getHash().getBytes(), genesis.getHash().getBytes());
    sendMessage(msg);
    // RSK new protocol send status
    Status status = new Status(bestBlock.getNumber(), bestBlock.getHash().getBytes(), bestBlock.getParentHash().getBytes(), totalDifficulty);
    RskMessage rskmessage = new RskMessage(config, new StatusMessage(status));
    loggerNet.trace("Sending status best block {} to {}", status.getBestBlockNumber(), this.messageSender.getPeerNodeID().toString());
    sendMessage(rskmessage);
    ethState = EthState.STATUS_SENT;
}
Also used : BlockChainStatus(co.rsk.core.bc.BlockChainStatus) StatusMessage(co.rsk.net.messages.StatusMessage) BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainStatus(co.rsk.core.bc.BlockChainStatus) co.rsk.net(co.rsk.net)

Example 5 with BlockChainStatus

use of co.rsk.core.bc.BlockChainStatus in project rskj by rsksmart.

the class SnapshotManagerTest method resetSnapshotClearingTransactionPool.

@Test
public void resetSnapshotClearingTransactionPool() {
    Blockchain blockchain = createBlockchain();
    Block genesis = blockchain.getBestBlock();
    BlockDifficulty genesisDifficulty = blockchain.getStatus().getTotalDifficulty();
    addBlocks(blockchain, 10);
    BlockChainStatus status = blockchain.getStatus();
    Assert.assertEquals(10, status.getBestBlockNumber());
    TransactionPool transactionPool = blockchain.getTransactionPool();
    Assert.assertNotNull(transactionPool);
    List<Transaction> txs = new ArrayList<>();
    txs.add(createSampleTransaction());
    txs.add(createSampleTransaction());
    transactionPool.addTransactions(txs);
    Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());
    Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());
    SnapshotManager manager = new SnapshotManager();
    manager.takeSnapshot(blockchain);
    Assert.assertFalse(manager.getSnapshots().isEmpty());
    Assert.assertTrue(manager.resetSnapshots(blockchain));
    Assert.assertTrue(manager.getSnapshots().isEmpty());
    Assert.assertTrue(manager.resetSnapshots(blockchain));
    BlockChainStatus newStatus = blockchain.getStatus();
    Assert.assertEquals(0, newStatus.getBestBlockNumber());
    Assert.assertEquals(genesisDifficulty, newStatus.getTotalDifficulty());
    Assert.assertEquals(genesis.getHash(), newStatus.getBestBlock().getHash());
    Assert.assertTrue(blockchain.getTransactionPool().getPendingTransactions().isEmpty());
    Assert.assertTrue(manager.getSnapshots().isEmpty());
    for (int k = 1; k <= 10; k++) Assert.assertTrue(blockchain.getBlocksByNumber(k).isEmpty());
}
Also used : ArrayList(java.util.ArrayList) BlockChainStatus(co.rsk.core.bc.BlockChainStatus) Test(org.junit.Test)

Aggregations

BlockChainStatus (co.rsk.core.bc.BlockChainStatus)8 Test (org.junit.Test)6 BlockDifficulty (co.rsk.core.BlockDifficulty)2 World (co.rsk.test.World)2 ArrayList (java.util.ArrayList)2 Block (org.ethereum.core.Block)2 Blockchain (org.ethereum.core.Blockchain)2 co.rsk.net (co.rsk.net)1 StatusMessage (co.rsk.net.messages.StatusMessage)1 DslParser (co.rsk.test.dsl.DslParser)1 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)1 TransactionInfo (org.ethereum.db.TransactionInfo)1 DataWord (org.ethereum.vm.DataWord)1