Skip to content

Commit

Permalink
Fix a couple of bugs. Tests fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashatyrev committed Sep 13, 2015
1 parent fc10ab2 commit 7fcea37
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.spongycastle.util.encoders.Hex;

import java.io.*;
import java.math.BigInteger;
import java.util.*;

/**
Expand Down Expand Up @@ -155,7 +156,7 @@ public int count() {
public int compareTo(PathElement o) {
if (type != o.type) return type.compareTo(o.type);
if (type == Type.Offset || type == Type.StorageIndex) {
return new Integer(key).compareTo(new Integer(o.key));
return new BigInteger(key, 16).compareTo(new BigInteger(o.key, 16));
}
return key.compareTo(o.key);
}
Expand Down Expand Up @@ -236,6 +237,14 @@ public String serializeToJson() throws JsonProcessingException {
public static StorageDictionary deserializeFromJson(String json) throws IOException {
ObjectMapper om = new ObjectMapper();
StorageDictionary.PathElement root = om.readValue(json, StorageDictionary.PathElement.class);
installRoots(root);
return new StorageDictionary(root);
}

private static void installRoots(PathElement el) {
for (PathElement element : el.children.values()) {
element.parent = el;
installRoots(element);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
//@Component
public class StorageDictionaryDb {
private static final Logger logger = LoggerFactory.getLogger("repository");
public static StorageDictionaryDb INST = new StorageDictionaryDb().init();

private static final Serializer<StorageDictionary> SERIALIZER = new Serializer<StorageDictionary>() {
@Override
public void serialize(DataOutput out, StorageDictionary value) throws IOException {
Expand All @@ -46,14 +44,19 @@ public StorageDictionary deserialize(DataInput in, int available) throws IOExcep
}
};

// @Autowired
public static StorageDictionaryDb INST = new StorageDictionaryDb().init();

// @Autowired
MapDBFactory mapDBFactory = new MapDBFactoryImpl();

private DB storagekeysDb;
private HTreeMap<ByteArrayWrapper, StorageDictionary> contracts;

private StorageDictionaryDb() {
}

@PostConstruct
public StorageDictionaryDb init() {
StorageDictionaryDb init() {
storagekeysDb = mapDBFactory.createTransactionalDB("metadata/keydictionary");
contracts = storagekeysDb.hashMapCreate("contracts")
.valueSerializer(SERIALIZER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public void dbTest() throws Exception {
kp.addPath(new DataWord("1114"), createPath("a/b2/c1"));
System.out.println(kp.dump());

StorageDictionaryDb db = new StorageDictionaryDb();
db.mapDBFactory = new MapDBFactoryImpl();
db.init();
StorageDictionaryDb db = StorageDictionaryDb.INST;
// StorageDictionaryDb db = new StorageDictionaryDb();
// db.mapDBFactory = new MapDBFactoryImpl();
// db.init();

byte[] contractAddr = Hex.decode("abcdef");
Assert.assertFalse(kp.isValid());
Expand All @@ -83,6 +84,7 @@ public void dbTest() throws Exception {

Assert.assertEquals(kp.root, kp1.root);
db.close();
db.init();
}

static StorageDictionary.PathElement[] createPath(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.ethereum.vm;

import org.junit.Test;
import org.spongycastle.util.encoders.Hex;

/**
* Created by Anton Nashatyrev on 10.09.2015.
*/
public class StorageDictionaryHandlerTest {

@Test
public void test1() {
StorageDictionaryHandler sk = new StorageDictionaryHandler(new DataWord(0xff));
sk.vmStartPlayNotify();
sk.vmSha3Notify(Hex.decode("0000000000000000000000000000000000000000000000000000000000000001" +
"0000000000000000000000000000000000000000000000000000000000000004"),
new DataWord(Hex.decode("abd6e7cb50984ff9c2f3e18a2660c3353dadf4e3291deeb275dae2cd1e44fe05")));
sk.vmSha3Notify(Hex.decode("abd6e7cb50984ff9c2f3e18a2660c3353dadf4e3291deeb275dae2cd1e44fe05"),
new DataWord(Hex.decode("210afe6ebef982fa193bb4e17f9f236cdf09af7788627b5d54d9e3e4b100021b")));

sk.vmSStoreNotify(new DataWord(Hex.decode("210afe6ebef982fa193bb4e17f9f236cdf09af7788627b5d54d9e3e4b100021b")),
new DataWord(Hex.decode("3137326142713267314b38327174626745755876384b63443342453531346258")));
sk.vmSStoreNotify(new DataWord(Hex.decode("210afe6ebef982fa193bb4e17f9f236cdf09af7788627b5d54d9e3e4b100021c")),
new DataWord(Hex.decode("3772000000000000000000000000000000000000000000000000000000000000")));

sk.vmSStoreNotify(new DataWord(Hex.decode("abd6e7cb50984ff9c2f3e18a2660c3353dadf4e3291deeb275dae2cd1e44fe05")),
new DataWord(Hex.decode("0000000000000000000000000000000000000000000000000000000000000022")));
sk.vmSStoreNotify(new DataWord(Hex.decode("0000000000000000000000000000000000000000000000000000000000000003")),
new DataWord(Hex.decode("0000000000000000000000000000000000000000000000000000000000000002")));

sk.vmSStoreNotify(new DataWord(Hex.decode("abd6e7cb50984ff9c2f3e18a2660c3353dadf4e3291deeb275dae2cd1e44fe06")),
new DataWord(Hex.decode("000000000000000000000000000000000000000000000000016345785d8a0000")));
sk.vmSStoreNotify(new DataWord(Hex.decode("abd6e7cb50984ff9c2f3e18a2660c3353dadf4e3291deeb275dae2cd1e44fe07")),
new DataWord(Hex.decode("0000000000000000000000000d82cd113dc35ddda93f38166cd5cde8b88e36a1")));
sk.vmEndPlayNotify(null);

System.out.println(sk.hashes.values());

System.out.println(sk.keysPath.dump());
}

@Test
public void test2() {
StorageDictionaryHandler sk = new StorageDictionaryHandler(new DataWord(0xff));
sk.vmStartPlayNotify();
sk.vmSha3Notify(Hex.decode("2153b9b2b56bb29c21cf47fe64fd2abdd7171b14cca48560f6f6cf294a1a5c52" +
"0000000000000000000000000000000000000000000000000000000000000103"),
new DataWord(Hex.decode("ffd874e59055f4f3dfa2a72e56b6998ed34dc01ebcf35d9ab673308b9f41fc81")));

sk.vmSStoreNotify(new DataWord(Hex.decode("ffd874e59055f4f3dfa2a72e56b6998ed34dc01ebcf35d9ab673308b9f41fc7f")),
new DataWord(Hex.decode("3137326142713267314b38327174626745755876384b63443342453531346258")));
sk.vmSStoreNotify(new DataWord(Hex.decode("ffd874e59055f4f3dfa2a72e56b6998ed34dc01ebcf35d9ab673308b9f41fc80")),
new DataWord(Hex.decode("3137326142713267314b38327174626745755876384b63443342453531346258")));
sk.vmSStoreNotify(new DataWord(Hex.decode("ffd874e59055f4f3dfa2a72e56b6998ed34dc01ebcf35d9ab673308b9f41fc81")),
new DataWord(Hex.decode("3137326142713267314b38327174626745755876384b63443342453531346258")));
sk.vmEndPlayNotify(null);

System.out.println(sk.keysPath.dump());
}
}

0 comments on commit 7fcea37

Please sign in to comment.