Search in sources :

Example 1 with ConsensusRule

use of org.ethereum.config.blockchain.upgrades.ConsensusRule 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 2 with ConsensusRule

use of org.ethereum.config.blockchain.upgrades.ConsensusRule in project rskj by rsksmart.

the class BridgeSupportTest method assertRefundInProcessPegInVersion1.

private void assertRefundInProcessPegInVersion1(TxSenderAddressType lockSenderAddressType, Optional<Address> btcRefundAddress, List<ConsensusRule> consensusRules) throws IOException, RegisterBtcTransactionException, PeginInstructionsException {
    // Arrange
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    for (ConsensusRule consensusRule : consensusRules) {
        when(activations.isActive(consensusRule)).thenReturn(true);
    }
    Repository repository = createRepository();
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Address btcSenderAddress = null;
    if (lockSenderAddressType != TxSenderAddressType.UNKNOWN) {
        btcSenderAddress = srcKey1.toAddress(btcParams);
    }
    BtcTransaction btcTx = new BtcTransaction(btcParams);
    btcTx.addOutput(Coin.COIN.multiply(10), bridgeConstants.getGenesisFederation().getAddress());
    btcTx.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
    when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
    when(provider.getLockingCap()).thenReturn(Coin.COIN.multiply(1));
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(lockSenderAddressType, btcSenderAddress, rskAddress);
    if (!btcRefundAddress.isPresent() && btcSenderAddress != null) {
        btcRefundAddress = Optional.of(btcSenderAddress);
    }
    PeginInstructionsProvider peginInstructionsProvider = getPeginInstructionsProviderForVersion1(rskAddress, btcRefundAddress);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, peginInstructionsProvider, mock(Block.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
    // Act
    bridgeSupport.processPegIn(btcTx, mock(Transaction.class), 0, mock(Sha256Hash.class));
    // Assert
    if (lockSenderAddressType == TxSenderAddressType.UNKNOWN && !btcRefundAddress.isPresent()) {
        // Unknown sender and no refund address. Can't refund
        Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
    } else {
        Assert.assertEquals(1, releaseTransactionSet.getEntries().size());
        // Check rejection tx input was created from btc tx and sent to the btc refund address indicated by the user
        boolean successfulRejection = false;
        for (ReleaseTransactionSet.Entry e : releaseTransactionSet.getEntries()) {
            BtcTransaction refundTx = e.getTransaction();
            if (refundTx.getInput(0).getOutpoint().getHash() == btcTx.getHash() && refundTx.getOutput(0).getScriptPubKey().getToAddress(btcParams).equals(btcRefundAddress.get())) {
                successfulRejection = true;
                break;
            }
        }
        Assert.assertTrue(successfulRejection);
    }
}
Also used : Script(co.rsk.bitcoinj.script.Script) RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block)

Example 3 with ConsensusRule

use of org.ethereum.config.blockchain.upgrades.ConsensusRule in project rskj by rsksmart.

the class BlockExecutor method maintainPrecompiledContractStorageRoots.

/**
 * Precompiled contracts storage is setup like any other contract for consistency. Here, we apply this logic on the
 * exact activation block.
 * This method is called automatically for every block except for the Genesis (which makes an explicit call).
 */
public static void maintainPrecompiledContractStorageRoots(Repository track, ActivationConfig.ForBlock activations) {
    if (activations.isActivating(RSKIP126)) {
        for (RskAddress addr : PrecompiledContracts.GENESIS_ADDRESSES) {
            if (!track.isExist(addr)) {
                track.createAccount(addr);
            }
            track.setupContract(addr);
        }
    }
    for (Map.Entry<RskAddress, ConsensusRule> e : PrecompiledContracts.CONSENSUS_ENABLED_ADDRESSES.entrySet()) {
        ConsensusRule contractActivationRule = e.getValue();
        if (activations.isActivating(contractActivationRule)) {
            RskAddress addr = e.getKey();
            track.createAccount(addr);
            track.setupContract(addr);
        }
    }
}
Also used : ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) RskAddress(co.rsk.core.RskAddress)

Aggregations

RskAddress (co.rsk.core.RskAddress)3 ConsensusRule (org.ethereum.config.blockchain.upgrades.ConsensusRule)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2 Script (co.rsk.bitcoinj.script.Script)1 TestSystemProperties (co.rsk.config.TestSystemProperties)1 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)1 RepositorySnapshot (co.rsk.db.RepositorySnapshot)1 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)1 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)1 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)1 Block (org.ethereum.core.Block)1 Blockchain (org.ethereum.core.Blockchain)1 Repository (org.ethereum.core.Repository)1 Transaction (org.ethereum.core.Transaction)1 ECKey (org.ethereum.crypto.ECKey)1 MutableRepository (org.ethereum.db.MutableRepository)1 RskTestFactory (org.ethereum.util.RskTestFactory)1 InternalTransaction (org.ethereum.vm.program.InternalTransaction)1 Test (org.junit.Test)1