Search in sources :

Example 26 with Keccak256

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

the class ProofOfWorkRuleTest method mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix.

private static Block mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix(Block block, byte[] compressed) {
    Keccak256 blockMergedMiningHash = new Keccak256(block.getHashForMergedMining());
    co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = co.rsk.bitcoinj.params.RegTestParams.get();
    co.rsk.bitcoinj.core.BtcTransaction bitcoinMergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(bitcoinNetworkParameters, blockMergedMiningHash.getBytes());
    co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(bitcoinNetworkParameters, bitcoinMergedMiningCoinbaseTransaction);
    BigInteger targetBI = DifficultyUtils.difficultyToTarget(block.getDifficulty());
    BlockMiner.findNonce(bitcoinMergedMiningBlock, targetBI);
    // We need to clone to allow modifications
    Block newBlock = new Block(block.getEncoded()).cloneBlock();
    newBlock.setBitcoinMergedMiningHeader(bitcoinMergedMiningBlock.cloneAsHeader().bitcoinSerialize());
    co.rsk.bitcoinj.core.PartialMerkleTree bitcoinMergedMiningMerkleBranch = getBitcoinMergedMerkleBranch(bitcoinMergedMiningBlock);
    newBlock.setBitcoinMergedMiningCoinbaseTransaction(org.spongycastle.util.Arrays.concatenate(compressed, blockMergedMiningHash.getBytes()));
    newBlock.setBitcoinMergedMiningMerkleProof(bitcoinMergedMiningMerkleBranch.bitcoinSerialize());
    return newBlock;
}
Also used : BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 27 with Keccak256

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

the class TrieImplSnapshotTest method getSnapshotToTrieWithLongValues.

@Test
public void getSnapshotToTrieWithLongValues() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    trie = trie.put("foo".getBytes(), TrieImplValueTest.makeValue(100));
    Keccak256 hash = trie.getHash();
    trie.save();
    trie = trie.put("bar".getBytes(), TrieImplValueTest.makeValue(200));
    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)

Example 28 with Keccak256

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

the class TrieImplSnapshotTest method getSnapshotToTrieUsingDeserializedTrie.

@Test
public void getSnapshotToTrieUsingDeserializedTrie() {
    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 = TrieImpl.deserialize(trie.serialize()).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)

Example 29 with Keccak256

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

the class TrieImplSnapshotTest method getSnapshotToTrieUsingDeserializedTrieWithLongValues.

@Test
public void getSnapshotToTrieUsingDeserializedTrieWithLongValues() {
    byte[] value1 = TrieImplValueTest.makeValue(100);
    byte[] value2 = TrieImplValueTest.makeValue(200);
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    trie = trie.put("foo".getBytes(), value1);
    Keccak256 hash = trie.getHash();
    trie.save();
    trie = trie.put("bar".getBytes(), value2);
    Assert.assertNotNull(trie.get("foo".getBytes()));
    Assert.assertArrayEquals(value1, trie.get("foo".getBytes()));
    Assert.assertNotNull(trie.get("bar".getBytes()));
    Assert.assertArrayEquals(value2, trie.get("bar".getBytes()));
    Trie snapshot = TrieImpl.deserialize(trie.serialize()).getSnapshotTo(hash);
    Assert.assertNotNull(snapshot);
    Assert.assertEquals(hash, snapshot.getHash());
    Assert.assertNotNull(snapshot.get("foo".getBytes()));
    Assert.assertArrayEquals(value1, 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)

Example 30 with Keccak256

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

the class TrieImpl method getNodeCount.

/**
 * getNodeCount the number of direct subnodes in this node
 *
 * @return the number of direct subnodes in this node
 *
 * Takes into account that a node could be not present as an object and
 * only be referenced via its unique hash
 */
private int getNodeCount() {
    int count = 0;
    for (int k = 0; k < ARITY; k++) {
        TrieImpl node = this.getNode(k);
        Keccak256 localHash = this.getHash(k);
        if (node != null && !isEmptyTrie(node.value, node.nodes, node.hashes) || localHash != null) {
            count++;
        }
    }
    return count;
}
Also used : Keccak256(co.rsk.crypto.Keccak256)

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