Search in sources :

Example 21 with Trie

use of co.rsk.trie.Trie in project rskj by rsksmart.

the class BlockTest method testPremineFromJSON.

@Test
public void testPremineFromJSON() throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject genesisMap = (JSONObject) parser.parse(TEST_GENESIS);
    Set keys = genesisMap.keySet();
    Trie state = new TrieImpl(null, true);
    for (Object key : keys) {
        JSONObject val = (JSONObject) genesisMap.get(key);
        String denom = (String) val.keySet().toArray()[0];
        String value = (String) val.values().toArray()[0];
        BigInteger wei = Denomination.valueOf(denom.toUpperCase()).value().multiply(new BigInteger(value));
        AccountState accountState = new AccountState(BigInteger.ZERO, new Coin(wei));
        byte[] encodedAccountState = accountState.getEncoded();
        byte[] accountKey = Hex.decode(key.toString());
        state = state.put(accountKey, encodedAccountState);
        Assert.assertArrayEquals(encodedAccountState, state.get(accountKey));
    }
    logger.info("root: {}", state.getHash());
    assertEquals(GENESIS_STATE_ROOT, state.getHash());
}
Also used : Coin(co.rsk.core.Coin) TrieImpl(co.rsk.trie.TrieImpl) Set(java.util.Set) JSONObject(org.json.simple.JSONObject) BigInteger(java.math.BigInteger) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 22 with Trie

use of co.rsk.trie.Trie in project rskj by rsksmart.

the class StateTest method generateGenesisState.

private Trie generateGenesisState() {
    Trie trie = new TrieImpl();
    Genesis genesis = (Genesis) Genesis.getInstance(new RskSystemProperties());
    for (RskAddress addr : genesis.getPremine().keySet()) {
        trie = trie.put(addr.getBytes(), genesis.getPremine().get(addr).getAccountState().getEncoded());
    }
    return trie;
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) RskAddress(co.rsk.core.RskAddress) Trie(co.rsk.trie.Trie) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 23 with Trie

use of co.rsk.trie.Trie in project rskj by rsksmart.

the class SecureTrieImplHashTest method removeOrNeverInsertShouldBringSameHashWithSecureTrie.

@Test
public void removeOrNeverInsertShouldBringSameHashWithSecureTrie() {
    Trie trie1 = new TrieImpl(true).put("roosevalt", "So, first of all, let me assert my firm belief that".getBytes()).put("roosevelt", "the only thing we have to fear is... fear itself ".getBytes()).put("roosevilt", "42".getBytes()).delete("roosevelt");
    Trie trie2 = new TrieImpl(true).put("roosevalt", "So, first of all, let me assert my firm belief that".getBytes()).put("roosevilt", "42".getBytes());
    Assert.assertTrue(Arrays.equals(trie1.get("roosevalt"), "So, first of all, let me assert my firm belief that".getBytes()));
    Assert.assertTrue(Arrays.equals(trie1.get("roosevilt"), "42".getBytes()));
    Assert.assertNull(trie1.get("roosevelt"));
    Assert.assertEquals(trie1.getHash(), trie2.getHash());
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 24 with Trie

use of co.rsk.trie.Trie in project rskj by rsksmart.

the class SecureTrieImplKeyValueTest method zeroKeyWhenTwoKeysHasNoSharedPath.

@Test
public void zeroKeyWhenTwoKeysHasNoSharedPath() {
    byte[] zeroKey = "0".getBytes();
    byte[] oneKey = "1".getBytes();
    Trie trie = new TrieImpl(true).put(zeroKey, "So, first of all, let me assert my firm belief that".getBytes()).put(oneKey, "the only thing we have to fear is... fear itself ".getBytes());
    trie = trie.delete(oneKey);
    Assert.assertTrue(Arrays.equals(trie.get(zeroKey), "So, first of all, let me assert my firm belief that".getBytes()));
    Assert.assertNull(trie.get(oneKey));
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 25 with Trie

use of co.rsk.trie.Trie in project rskj by rsksmart.

the class SecureTrieImplKeyValueTest method zeroKeyWhenTwoKeysHasSharedPathAndOneIsPrefixOfTheOther.

@Test
public void zeroKeyWhenTwoKeysHasSharedPathAndOneIsPrefixOfTheOther() {
    byte[] zeroKey = "0".getBytes();
    byte[] oneKey = "012345678910".getBytes();
    Trie trie = new TrieImpl(true).put(zeroKey, "So, first of all, let me assert my firm belief that".getBytes()).put(oneKey, "the only thing we have to fear is... fear itself ".getBytes());
    trie = trie.delete(zeroKey);
    Assert.assertTrue(Arrays.equals(trie.get(oneKey), "the only thing we have to fear is... fear itself ".getBytes()));
    Assert.assertNull(trie.get(zeroKey));
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

Trie (co.rsk.trie.Trie)29 Test (org.junit.Test)24 TrieImpl (co.rsk.trie.TrieImpl)22 DataWord (org.ethereum.vm.DataWord)8 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)7 TrieStore (co.rsk.trie.TrieStore)4 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 Coin (co.rsk.core.Coin)2 Keccak256 (co.rsk.crypto.Keccak256)2 BigInteger (java.math.BigInteger)2 Ignore (org.junit.Ignore)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 RskAddress (co.rsk.core.RskAddress)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 Set (java.util.Set)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1 Transaction (org.ethereum.core.Transaction)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1