Search in sources :

Example 31 with Keccak256

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

the class TrieImpl method retrieveNode.

/**
 * retrieveNode get the subnode at position n. If it is not present but its hash is known,
 * the node is retrieved from the store
 *
 * @param n position of subnode (0 to arity - 1)
 *
 * @return  the node or null if no subnode at position
 */
private Trie retrieveNode(int n) {
    Trie node = this.getNode(n);
    if (node != null) {
        return node;
    }
    if (this.hashes == null) {
        return null;
    }
    Keccak256 localHash = this.hashes[n];
    if (localHash == null) {
        return null;
    }
    node = this.store.retrieve(localHash.getBytes());
    if (node == null) {
        String strHash = localHash.toHexString();
        logger.error(ERROR_NON_EXISTENT_TRIE_LOGGER, strHash);
        panicProcessor.panic(PANIC_TOPIC, ERROR_NON_EXISTENT_TRIE + " " + strHash);
        throw new TrieSerializationException(ERROR_NON_EXISTENT_TRIE + " " + strHash, null);
    }
    if (this.nodes == null) {
        this.nodes = new TrieImpl[ARITY];
    }
    this.nodes[n] = (TrieImpl) node;
    return node;
}
Also used : Keccak256(co.rsk.crypto.Keccak256)

Example 32 with Keccak256

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

the class TrieImpl method getHash.

/**
 * getHash get hash associated to subnode at positin n. If the hash is known
 * because it is in the internal hash cache, no access to subnode is needed.
 *
 * @param n     subnode position
 *
 * @return  node hash or null if no node is present
 */
@Nullable
private Keccak256 getHash(int n) {
    if (this.hashes != null && this.hashes[n] != null) {
        return this.hashes[n];
    }
    if (this.nodes == null || this.nodes[n] == null) {
        return null;
    }
    TrieImpl node = this.nodes[n];
    if (isEmptyTrie(node.value, node.nodes, node.hashes)) {
        return null;
    }
    Keccak256 localHash = node.getHash();
    this.setHash(n, localHash);
    return localHash;
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Nullable(javax.annotation.Nullable)

Example 33 with Keccak256

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

the class ReceiptStoreImpl method get.

@Override
public TransactionInfo get(byte[] transactionHash, byte[] blockHash, BlockStore store) {
    List<TransactionInfo> txsInfo = getAll(transactionHash);
    if (txsInfo.isEmpty()) {
        return null;
    }
    Block block = null;
    Map<Keccak256, Block> tiblocks = new HashMap();
    if (store != null) {
        block = store.getBlockByHash(blockHash);
        for (TransactionInfo ti : txsInfo) {
            byte[] bhash = ti.getBlockHash();
            Keccak256 key = new Keccak256(bhash);
            tiblocks.put(key, store.getBlockByHash(bhash));
        }
    }
    while (true) {
        int nless = 0;
        for (TransactionInfo ti : txsInfo) {
            byte[] hash = ti.getBlockHash();
            Keccak256 key = new Keccak256(hash);
            Block tiblock = tiblocks.get(key);
            if (tiblock != null && block != null) {
                if (tiblock.getNumber() > block.getNumber()) {
                    nless++;
                    continue;
                }
                if (tiblock.getNumber() < block.getNumber()) {
                    continue;
                }
            }
            if (Arrays.equals(ti.getBlockHash(), blockHash)) {
                return ti;
            }
        }
        if (nless >= txsInfo.size()) {
            return null;
        }
        if (store == null) {
            return null;
        }
        if (block == null) {
            block = store.getBlockByHash(blockHash);
            if (block == null) {
                return null;
            }
        }
        if (block.isGenesis()) {
            return null;
        }
        block = store.getBlockByHash(block.getParentHash().getBytes());
        if (block == null) {
            return null;
        }
        blockHash = block.getHash().getBytes();
    }
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 34 with Keccak256

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

the class TransactionTest method test3.

@Test
public /* achieve public key of the sender when signature manually set */
void test3() throws Exception {
    if (config.getBlockchainConfig().getCommonConstants().getChainId() != 0)
        return;
    // cat --> 79b08ad8787060333663d19704909ee7b1903e58
    // cow --> cd2a3d9f938e13cd947ec05abc7fe734df8dd826
    BigInteger value = new BigInteger("1000000000000000000000");
    byte[] privKey = HashUtil.keccak256("cat".getBytes());
    ECKey ecKey = ECKey.fromPrivate(privKey);
    byte[] senderPrivKey = HashUtil.keccak256("cow".getBytes());
    byte[] gasPrice = Hex.decode("09184e72a000");
    byte[] gas = Hex.decode("4255");
    // Tn (nonce); Tp(pgas); Tg(gaslimi); Tt(value); Tv(value); Ti(sender);  Tw; Tr; Ts
    Transaction tx = new Transaction(null, gasPrice, gas, ecKey.getAddress(), value.toByteArray(), null);
    Keccak256 hash = tx.getRawHash();
    ECKey.ECDSASignature signature = ECKey.fromPrivate(senderPrivKey).sign(hash.getBytes());
    tx.setSignature(signature);
    System.out.println("v\t\t\t: " + Hex.toHexString(new byte[] { tx.getSignature().v }));
    System.out.println("r\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().r)));
    System.out.println("s\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().s)));
    System.out.println("RLP encoded tx\t\t: " + Hex.toHexString(tx.getEncoded()));
    // retrieve the signer/sender of the transaction
    ECKey key = ECKey.signatureToKey(tx.getHash().getBytes(), tx.getSignature().toBase64());
    System.out.println("Tx unsigned RLP\t\t: " + Hex.toHexString(tx.getEncodedRaw()));
    System.out.println("Tx signed   RLP\t\t: " + Hex.toHexString(tx.getEncoded()));
    System.out.println("Signature public key\t: " + Hex.toHexString(key.getPubKey()));
    System.out.println("Sender is\t\t: " + Hex.toHexString(key.getAddress()));
    assertEquals("cd2a3d9f938e13cd947ec05abc7fe734df8dd826", Hex.toHexString(key.getAddress()));
    System.out.println(tx.toString());
}
Also used : BigInteger(java.math.BigInteger) ECKey(org.ethereum.crypto.ECKey) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 35 with Keccak256

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

the class BlockUtilsTest method unknowAncestorsHashes.

@Test
public void unknowAncestorsHashes() {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    BlockStore store = new BlockStore();
    Block genesis = new BlockGenerator().getGenesisBlock();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    Block block1 = new BlockBuilder().difficulty(2l).parent(genesis).build();
    Block block1b = new BlockBuilder().difficulty(1l).parent(genesis).build();
    Block block2 = new BlockBuilder().parent(block1).build();
    Block block3 = new BlockBuilder().parent(block2).build();
    store.saveBlock(block3);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockChain.tryToConnect(block1b));
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) 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