Search in sources :

Example 11 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockchainLoaderTest method testLoadBlockchainEmptyBlockchain.

@Test
public void testLoadBlockchainEmptyBlockchain() {
    RskTestFactory objects = new RskTestFactory() {

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "blockchain_loader_genesis.json", BigInteger.ZERO, true, true, true);
        }
    };
    // calls loadBlockchain
    Blockchain blockchain = objects.getBlockchain();
    RepositorySnapshot repository = objects.getRepositoryLocator().snapshotAt(blockchain.getBestBlock().getHeader());
    TestSystemProperties testSystemProperties = new TestSystemProperties();
    ActivationConfig.ForBlock activations = testSystemProperties.getActivationConfig().forBlock(0);
    int enabledPCCs = PrecompiledContracts.GENESIS_ADDRESSES.size();
    for (ConsensusRule consensusRule : PrecompiledContracts.CONSENSUS_ENABLED_ADDRESSES.values()) {
        if (activations.isActive(consensusRule)) {
            enabledPCCs++;
        }
    }
    int testAccountsSize = 3;
    // PCCs + test accounts in blockchain_loader_genesis.json
    int genesisAccountKeysSize = enabledPCCs + testAccountsSize;
    Assert.assertEquals(genesisAccountKeysSize, repository.getAccountsKeys().size());
    RskAddress daba01 = new RskAddress("dabadabadabadabadabadabadabadabadaba0001");
    Assert.assertEquals(Coin.valueOf(2000), repository.getBalance(daba01));
    Assert.assertEquals(BigInteger.valueOf(24), repository.getNonce(daba01));
    RskAddress daba02 = new RskAddress("dabadabadabadabadabadabadabadabadaba0002");
    Assert.assertEquals(Coin.valueOf(1000), repository.getBalance(daba02));
    Assert.assertEquals(BigInteger.ZERO, repository.getNonce(daba02));
    RskAddress address = new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051");
    Assert.assertEquals(Coin.valueOf(10), repository.getBalance(address));
    Assert.assertEquals(BigInteger.valueOf(25), repository.getNonce(address));
    Assert.assertEquals(DataWord.ONE, repository.getStorageValue(address, DataWord.ZERO));
    Assert.assertEquals(DataWord.valueOf(3), repository.getStorageValue(address, DataWord.ONE));
    Assert.assertEquals(274, Objects.requireNonNull(repository.getCode(address)).length);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) Blockchain(org.ethereum.core.Blockchain) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) RskAddress(co.rsk.core.RskAddress) RskTestFactory(org.ethereum.util.RskTestFactory) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) TestSystemProperties(co.rsk.config.TestSystemProperties) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 12 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockToMineBuilder method getTransactions.

private List<Transaction> getTransactions(List<Transaction> txsToRemove, BlockHeader parentHeader, Coin minGasPrice) {
    logger.debug("getting transactions from pending state");
    List<Transaction> txs = minerUtils.getAllTransactions(transactionPool);
    logger.debug("{} transaction(s) collected from pending state", txs.size());
    Transaction remascTx = new RemascTransaction(parentHeader.getNumber() + 1);
    txs.add(remascTx);
    Map<RskAddress, BigInteger> accountNonces = new HashMap<>();
    RepositorySnapshot originalRepo = repositoryLocator.snapshotAt(parentHeader);
    return minerUtils.filterTransactions(txsToRemove, txs, accountNonces, originalRepo, minGasPrice);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 13 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class TransactionModuleTest method sendTransactionMustBeMined.

@Test
public void sendTransactionMustBeMined() {
    World world = new World();
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, null, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, null, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
    String tx = sendTransaction(web3, repository);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, tx);
    // Transaction tx must be in the block mined.
    Assert.assertEquals(tx, txInBlock.getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Test(org.junit.Test)

Example 14 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockchainVMTest method testSEND_1.

@Test
public void testSEND_1() {
    NewBlockChainInfo binfo = createNewBlockchain();
    Blockchain blockchain = binfo.blockchain;
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock(), Collections.emptyList(), blockchain.getBestBlock().getStateRoot());
    Coin transferAmount = Coin.valueOf(100L);
    // Add a single transaction paying to a new address
    byte[] dstAddress = randomAddress();
    BigInteger transactionGasLimit = new BigInteger("21000");
    Coin transactionGasPrice = Coin.valueOf(1);
    Transaction t = Transaction.builder().gasPrice(transactionGasPrice).gasLimit(transactionGasLimit).destination(dstAddress).chainId(Constants.REGTEST_CHAIN_ID).value(transferAmount).build();
    t.sign(binfo.faucetKey.getPrivKeyBytes());
    List<Transaction> txs = Collections.singletonList(t);
    Block block2 = blockGenerator.createChildBlock(block1, txs, blockchain.getBestBlock().getStateRoot());
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block1));
    MinerHelper mh = new MinerHelper(binfo.repository, binfo.repositoryLocator, binfo.blockchain);
    mh.completeBlock(block2, block1);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block2));
    RepositorySnapshot repository = binfo.repositoryLocator.snapshotAt(block2.getHeader());
    Assert.assertEquals(blockchain.getBestBlock(), block2);
    Assert.assertEquals(2, block2.getNumber());
    Coin srcAmount = faucetAmount.subtract(transferAmount);
    srcAmount = srcAmount.subtract(transactionGasPrice.multiply(transactionGasLimit));
    Assert.assertEquals(repository.getBalance(new RskAddress(binfo.faucetKey.getAddress())), srcAmount);
    Assert.assertEquals(repository.getBalance(new RskAddress(dstAddress)), transferAmount);
}
Also used : Coin(co.rsk.core.Coin) RepositorySnapshot(co.rsk.db.RepositorySnapshot) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 15 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockTxsValidationRuleTest method setup.

@Before
public void setup() {
    parent = mock(Block.class);
    BlockHeader parentHeader = mock(BlockHeader.class);
    when(parent.getHeader()).thenReturn(parentHeader);
    RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
    repositorySnapshot = mock(RepositorySnapshot.class);
    when(repositoryLocator.snapshotAt(parentHeader)).thenReturn(repositorySnapshot);
    rule = new BlockTxsValidationRule(repositoryLocator);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Before(org.junit.Before)

Aggregations

RepositorySnapshot (co.rsk.db.RepositorySnapshot)37 Test (org.junit.Test)25 RepositoryLocator (co.rsk.db.RepositoryLocator)18 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)13 RskAddress (co.rsk.core.RskAddress)12 BlockStore (org.ethereum.db.BlockStore)11 BigInteger (java.math.BigInteger)10 Coin (co.rsk.core.Coin)9 BlockExecutor (co.rsk.core.bc.BlockExecutor)8 World (co.rsk.test.World)7 TestSystemProperties (co.rsk.config.TestSystemProperties)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)5 Account (org.ethereum.core.Account)5 AccountInformationProvider (co.rsk.core.bc.AccountInformationProvider)3 TransactionGateway (co.rsk.net.TransactionGateway)3 DslParser (co.rsk.test.dsl.DslParser)3 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)3 TrieStore (co.rsk.trie.TrieStore)3 Block (org.ethereum.core.Block)3