Skip to content

Commit

Permalink
Merge pull request #696 from rsksmart/copy_store_on_decode
Browse files Browse the repository at this point in the history
Copy trie store on ContractDetailsImpl.decode when it was saved without external storage
  • Loading branch information
aeidelman authored Nov 14, 2018
2 parents f6301f4 + 505440b commit 641281e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
33 changes: 30 additions & 3 deletions rskj-core/src/main/java/co/rsk/db/ContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import co.rsk.crypto.Keccak256;
import co.rsk.panic.PanicProcessor;
import co.rsk.trie.*;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.crypto.Keccak256Helper;
import org.ethereum.datasource.DataSourcePool;
import org.ethereum.db.ByteArrayWrapper;
Expand Down Expand Up @@ -83,7 +84,7 @@ private byte[] getCodeHash(byte[] code) {
return code == null ? EMPTY_DATA_HASH : Keccak256Helper.keccak256(code);
}

private Trie newTrie() {
private TrieImpl newTrie() {
TrieStore store = new ContractStorageStoreFactory(this.trieStorePool).getTrieStore(this.address);
return new TrieImpl(store, true);
}
Expand Down Expand Up @@ -195,8 +196,18 @@ private final void decode(byte[] rlpBytes) {

this.address = rlpAddress.getRLPData();

Keccak256 snapshotHash = new Keccak256(rlpStorage.getRLPData());
this.trie = this.newTrie().getSnapshotTo(snapshotHash);
byte[] root = rlpStorage.getRLPData();
byte[] external = rlpIsExternalStorage.getRLPData();

if (external != null && external.length > 0 && external[0] == 1) {
Keccak256 snapshotHash = new Keccak256(root);
this.trie = this.newTrie().getSnapshotTo(snapshotHash);
} else {
TrieImpl newTrie = this.newTrie();
TrieImpl tempTrie = (TrieImpl)TrieImpl.deserialize(root);
newTrie.getStore().copyFrom(tempTrie.getStore());
this.trie = newTrie.getSnapshotTo(tempTrie.getHash());
}

this.code = (rlpCode.getRLPData() == null) ? EMPTY_BYTE_ARRAY : rlpCode.getRLPData();
this.codeHash = Keccak256Helper.keccak256(code);
Expand Down Expand Up @@ -243,6 +254,22 @@ public byte[] getEncoded() {
return RLP.encodeList(rlpAddress, rlpIsExternalStorage, rlpStorage, rlpCode, rlpKeys);
}

@VisibleForTesting
public byte[] getEncodedOldFormat() {
logger.trace("getting contract details as bytes, hash {}, address {}, storage size {}, has external storage {}", this.getStorageHashAsString(), this.getAddressAsString(), this.getStorageSize(), this.hasExternalStorage());

byte[] rlpAddress = RLP.encodeElement(address);
byte[] rlpIsExternalStorage = RLP.encodeByte((byte) 0);

// Serialize the full trie
byte[] rlpStorage = RLP.encodeElement(this.trie.serialize());

byte[] rlpCode = RLP.encodeElement(this.code);
byte[] rlpKeys = RLP.encodeSet(this.keys);

return RLP.encodeList(rlpAddress, rlpIsExternalStorage, rlpStorage, rlpCode, rlpKeys);
}

@Override
public synchronized int getStorageSize() {
return keys.size();
Expand Down
80 changes: 80 additions & 0 deletions rskj-core/src/test/java/co/rsk/db/ContractDetailsImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,86 @@ public void getEncodedAndCreateClone() {
Assert.assertEquals(null, result.get(new DataWord(3)));
}

@Test
public void getOldEncodedAndCreateClone() {
HashMapDB store = new HashMapDB();
ContractDetailsImpl details = buildContractDetails(store);

List<DataWord> keys = new ArrayList<>();

keys.add(DataWord.ZERO);
keys.add(DataWord.ONE);

List<DataWord> values = new ArrayList<>();

values.add(new DataWord(42));
values.add(new DataWord(144));

details.setStorage(keys, values);

byte[] encoded = details.getEncodedOldFormat();

Assert.assertNotNull(encoded);

ContractDetailsImpl result = new ContractDetailsImpl(encoded, new TrieStorePoolOnMemory(), config.detailsInMemoryStorageLimit());

Assert.assertEquals(new DataWord(42), result.get(DataWord.ZERO));
Assert.assertEquals(new DataWord(144), result.get(DataWord.ONE));
Assert.assertEquals(null, result.get(new DataWord(2)));
Assert.assertEquals(null, result.get(new DataWord(3)));

Assert.assertArrayEquals(details.getStorageHash(), result.getStorageHash());
}

@Test
public void getOldEncodedWithEmptyStorageAndCreateClone() {
HashMapDB store = new HashMapDB();
ContractDetailsImpl details = buildContractDetails(store);

byte[] encoded = details.getEncodedOldFormat();

Assert.assertNotNull(encoded);

ContractDetailsImpl result = new ContractDetailsImpl(encoded, new TrieStorePoolOnMemory(), config.detailsInMemoryStorageLimit());

Assert.assertArrayEquals(details.getStorageHash(), result.getStorageHash());
}

@Test
public void getOldEncodedAndCreateCloneSettingStorage() {
HashMapDB store = new HashMapDB();
ContractDetailsImpl details = buildContractDetails(store);

DataWord value0 = new DataWord(42);
DataWord value1 = new DataWord(144);

details.put(DataWord.ZERO, value0);
details.put(DataWord.ONE, value1);

byte[] hash1 = details.getStorageHash();

details.put(new DataWord(10), value0);
details.put(new DataWord(11), value1);

byte[] hash2 = details.getStorageHash();

byte[] encoded = details.getEncodedOldFormat();

Assert.assertNotNull(encoded);

ContractDetailsImpl result = new ContractDetailsImpl(encoded, new TrieStorePoolOnMemory(), config.detailsInMemoryStorageLimit());

ContractDetails result1 = result.getSnapshotTo(hash1);

Assert.assertNotNull(result1);
Assert.assertArrayEquals(hash1, result1.getStorageHash());

ContractDetails result2 = result.getSnapshotTo(hash2);

Assert.assertNotNull(result2);
Assert.assertArrayEquals(hash2, result2.getStorageHash());
}

@Test
public void syncStorageInEmptyDetails() {
ContractDetailsImpl details = buildContractDetails(new HashMapDB());
Expand Down

0 comments on commit 641281e

Please sign in to comment.