Search in sources :

Example 91 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class PrevMinGasPriceValidatorTest method outOfValidMGPRangeBlock.

@Test
public void outOfValidMGPRangeBlock() {
    Block block = Mockito.mock(Block.class);
    Block parent = Mockito.mock(Block.class);
    Mockito.when(block.getParentHash()).thenReturn(new Keccak256(PARENT_HASH));
    Mockito.when(block.getMinimumGasPrice()).thenReturn(BLOCK_MGP);
    Mockito.when(parent.getMinimumGasPrice()).thenReturn(Coin.valueOf(10L));
    PrevMinGasPriceRule pmgpv = new PrevMinGasPriceRule();
    Assert.assertFalse(pmgpv.isValid(block, parent));
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 92 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class TransactionTest method testTransactionFromNew2.

@Ignore
@Test
public void testTransactionFromNew2() throws MissingPrivateKeyException {
    byte[] privKeyBytes = Hex.decode("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4");
    String RLP_TX_UNSIGNED = "eb8085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc1000080808080";
    String RLP_TX_SIGNED = "f86b8085e8d4a510008227109413978aee95f38490e9769c39b2773ed763d9cd5f872386f26fc10000801ba0eab47c1a49bf2fe5d40e01d313900e19ca485867d462fe06e139e3a536c6d4f4a014a569d327dcda4b29f74f93c0e9729d2f49ad726e703f9cd90dbb0fbf6649f1";
    Keccak256 HASH_TX_UNSIGNED = new Keccak256("328ea6d24659dec48adea1aced9a136e5ebdf40258db30d1b1d97ed2b74be34e");
    byte[] nonce = BigIntegers.asUnsignedByteArray(BigInteger.ZERO);
    // 1000000000000
    byte[] gasPrice = Hex.decode("e8d4a51000");
    // 10000
    byte[] gas = Hex.decode("2710");
    byte[] recieveAddress = Hex.decode("13978aee95f38490e9769c39b2773ed763d9cd5f");
    // 10000000000000000"
    byte[] value = Hex.decode("2386f26fc10000");
    byte[] data = new byte[0];
    Transaction tx = new Transaction(nonce, gasPrice, gas, recieveAddress, value, data);
    // Testing unsigned
    String encodedUnsigned = Hex.toHexString(tx.getEncoded());
    assertEquals(RLP_TX_UNSIGNED, encodedUnsigned);
    assertEquals(HASH_TX_UNSIGNED, tx.getHash());
    // Testing signed
    tx.sign(privKeyBytes);
    String encodedSigned = Hex.toHexString(tx.getEncoded());
    assertEquals(RLP_TX_SIGNED, encodedSigned);
    assertEquals(HASH_TX_UNSIGNED, tx.getHash());
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 93 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithOneSiblingBrokenSelectionRule.

private void processMinersFeesWithOneSiblingBrokenSelectionRule(String reasonForBrokenSelectionRule) throws IOException, BlockStoreException {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
    final long NUMBER_OF_TXS_WITH_FEES = 3;
    List<Block> blocks = createSimpleBlocks(this.genesisBlock, 4);
    Block blockWithOneTxA;
    Block blockWithOneTxB;
    if ("higherFees".equals(reasonForBrokenSelectionRule)) {
        blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseB, null, minerFee * 3, 0, txValue, cowKey);
    } else {
        Keccak256 blockWithOneTxBHash = PegTestUtils.createHash3();
        Keccak256 blockWithOneTxAHash = PegTestUtils.createHash3();
        blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxAHash, coinbaseA, null, minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxBHash, coinbaseB, null, (long) (minerFee * 1.5), 0, txValue, cowKey);
    }
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Lists.newArrayList(blockWithOneTxB.getHeader()), minerFee, 1, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blockThatIncludesUncleC, PegTestUtils.createHash3(), coinbaseD, null, minerFee, 2, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    blocks.addAll(createSimpleBlocks(blockWithOneTxD, 7));
    BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (Block b : blocks) {
        blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
        blockchain.tryToConnect(b);
    }
    // validate that the blockchain's and REMASC's initial states are correct
    Coin cowRemainingBalance = cowInitialBalance.subtract(Coin.valueOf(minerFee * NUMBER_OF_TXS_WITH_FEES + txValue * NUMBER_OF_TXS_WITH_FEES));
    List<Long> otherAccountsBalance = new ArrayList<>(Arrays.asList(null, null, null, null));
    this.validateAccountsCurrentBalanceIsCorrect(blockchain.getRepository(), cowRemainingBalance, minerFee * NUMBER_OF_TXS_WITH_FEES, null, this.getAccountsWithExpectedBalance(otherAccountsBalance));
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), Coin.ZERO, Coin.ZERO, 1L);
    // add block to pay fees of blocks on blockchain's height 5
    Block blockToPayFeesOnHeightFive = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFive, blockchain.getBestBlock());
    blockchain.tryToConnect(blockToPayFeesOnHeightFive);
    // -- After executing REMASC's contract for paying height 5 blocks
    // validate that account's balances are correct
    long blockRewardOnHeightFive = minerFee / remascConfig.getSyntheticSpan();
    long remascCurrentBalance = minerFee * 3 - blockRewardOnHeightFive;
    long rskReward = blockRewardOnHeightFive / remascConfig.getRskLabsDivisor();
    long rskCurrentBalance = rskReward;
    blockRewardOnHeightFive -= rskReward;
    long federationReward = blockRewardOnHeightFive / remascConfig.getFederationDivisor();
    blockRewardOnHeightFive -= federationReward;
    long publisherReward = blockRewardOnHeightFive / remascConfig.getPublishersDivisor();
    blockRewardOnHeightFive -= publisherReward;
    long minerRewardOnHeightFive = blockRewardOnHeightFive / 2;
    List<Long> otherAccountsBalanceOnHeightFive = new ArrayList<>(Arrays.asList(minerRewardOnHeightFive, minerRewardOnHeightFive, publisherReward, null));
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
    // TODO review value + 1
    this.validateAccountsCurrentBalanceIsCorrect(blockchain.getRepository(), cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightFive));
    // validate that REMASC's state is correct
    blockRewardOnHeightFive = minerFee / remascConfig.getSyntheticSpan();
    Coin expectedRewardBalance = Coin.valueOf(minerFee - blockRewardOnHeightFive);
    // TODO review burned value 1
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, Coin.valueOf(1), 0L);
    // add block to pay fees of blocks on blockchain's height 6
    Block blockToPayFeesOnHeightSix = RemascTestRunner.createBlock(this.genesisBlock, blockToPayFeesOnHeightFive, PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightSix, blockchain.getBestBlock());
    blockchain.tryToConnect(blockToPayFeesOnHeightSix);
    // -- After executing REMASC's contract for paying height 6 blocks
    // validate that account's balances are correct
    long rewardBalance = minerFee - blockRewardOnHeightFive + minerFee;
    long blockRewardOnHeightSix = rewardBalance / remascConfig.getSyntheticSpan();
    rewardBalance -= blockRewardOnHeightSix;
    rskReward = blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    long blockRewardWithoutRskFee = blockRewardOnHeightSix - rskReward;
    long federationReward2 = blockRewardWithoutRskFee / remascConfig.getFederationDivisor();
    long blockRewardWithoutRskAndFederationFee = blockRewardWithoutRskFee - federationReward2;
    long burnedBalance = blockRewardWithoutRskAndFederationFee / remascConfig.getPunishmentDivisor();
    remascCurrentBalance = minerFee * NUMBER_OF_TXS_WITH_FEES - blockRewardOnHeightFive - blockRewardOnHeightSix + burnedBalance;
    rskCurrentBalance += blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getFederationDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getPunishmentDivisor();
    List<Long> otherAccountsBalanceOnHeightSix = new ArrayList<>(Arrays.asList(minerRewardOnHeightFive, minerRewardOnHeightFive, publisherReward + blockRewardOnHeightSix, null));
    // TODO review + 1
    this.validateAccountsCurrentBalanceIsCorrect(blockchain.getRepository(), cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightSix));
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward + federationReward2);
    // TODO review + 1
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), Coin.valueOf(rewardBalance), Coin.valueOf(burnedBalance + 1), 0L);
}
Also used : Coin(co.rsk.core.Coin) BlockExecutor(co.rsk.core.bc.BlockExecutor) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder)

Example 94 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class RemascTestRunner method createBlock.

public static Block createBlock(Block genesis, Block parentBlock, Keccak256 blockHash, RskAddress coinbase, List<BlockHeader> uncles, Long difficulty, Transaction... txsToInlcude) {
    List<Transaction> txs = new ArrayList<>();
    if (txsToInlcude != null) {
        for (Transaction tx : txsToInlcude) {
            txs.add(tx);
        }
    }
    Transaction remascTx = new RemascTransaction(parentBlock.getNumber() + 1);
    txs.add(remascTx);
    long difficultyAsLong = difficulty == null ? parentBlock.getDifficulty().asBigInteger().longValue() : difficulty;
    if (difficultyAsLong == 0)
        difficultyAsLong = 1;
    byte[] diffBytes = BigInteger.valueOf(difficultyAsLong).toByteArray();
    Coin paidFees = Coin.ZERO;
    for (Transaction tx : txs) {
        BigInteger gasLimit = new BigInteger(1, tx.getGasLimit());
        Coin gasPrice = tx.getGasPrice();
        paidFees = paidFees.add(gasPrice.multiply(gasLimit));
    }
    Block block = new Block(// parent hash
    parentBlock.getHash().getBytes(), // uncle hash
    EMPTY_LIST_HASH, // coinbase
    coinbase.getBytes(), // logs bloom
    new Bloom().getData(), // difficulty
    diffBytes, parentBlock.getNumber() + 1, parentBlock.getGasLimit(), parentBlock.getGasUsed(), parentBlock.getTimestamp(), // extraData
    new byte[0], // mixHash
    new byte[0], // provisory nonce
    BigInteger.ZERO.toByteArray(), // receipts root
    HashUtil.EMPTY_TRIE_HASH, // transaction root
    BlockChainImpl.calcTxTrie(txs), // EMPTY_TRIE_HASH,   // state root
    genesis.getStateRoot(), // transaction list
    txs, // uncle list
    uncles, BigInteger.TEN.toByteArray(), paidFees) {

        private BlockHeader harcodedHashHeader;

        @Override
        public BlockHeader getHeader() {
            if (harcodedHashHeader == null) {
                harcodedHashHeader = new BlockHeader(super.getHeader().getEncoded(), false) {

                    @Override
                    public Keccak256 getHash() {
                        return blockHash;
                    }
                };
            }
            return harcodedHashHeader;
        }

        @Override
        public Keccak256 getHash() {
            return blockHash;
        }

        @Override
        public void flushRLP() {
            if (harcodedHashHeader != null)
                super.getHeader().setPaidFees(harcodedHashHeader.getPaidFees());
            super.flushRLP();
            harcodedHashHeader = null;
        }
    };
    return block;
}
Also used : ArrayList(java.util.ArrayList) Keccak256(co.rsk.crypto.Keccak256) Coin(co.rsk.core.Coin) BigInteger(java.math.BigInteger)

Example 95 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class TrieImplSnapshotTest method getSnapshotToTrie.

@Test
public void getSnapshotToTrie() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    trie = trie.put("foo".getBytes(), "bar".getBytes());
    Keccak256 hash = trie.getHash();
    trie.save();
    trie = trie.put("bar".getBytes(), "foo".getBytes());
    Assert.assertNotNull(trie.get("foo".getBytes()));
    Assert.assertNotNull(trie.get("bar".getBytes()));
    Trie snapshot = trie.getSnapshotTo(hash);
    Assert.assertNotNull(snapshot);
    Assert.assertEquals(hash, snapshot.getHash());
    Assert.assertNotNull(snapshot.get("foo".getBytes()));
    Assert.assertNull(snapshot.get("bar".getBytes()));
}
Also used : Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Aggregations

Keccak256 (co.rsk.crypto.Keccak256)102 Test (org.junit.Test)53 Block (org.ethereum.core.Block)40 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BigInteger (java.math.BigInteger)14 RskSystemProperties (co.rsk.config.RskSystemProperties)8 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)8 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 HashMapDB (org.ethereum.datasource.HashMapDB)8 RepositoryImpl (co.rsk.db.RepositoryImpl)7 ArrayList (java.util.ArrayList)7 Blockchain (org.ethereum.core.Blockchain)7 BlockStore (org.ethereum.db.BlockStore)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 RLPList (org.ethereum.util.RLPList)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 Coin (co.rsk.core.Coin)5 IOException (java.io.IOException)5 BlockHeader (org.ethereum.core.BlockHeader)5