Recalculate trie only when necessary

This commit is contained in:
nicksavers 2014-08-03 18:35:20 +02:00
parent 31fddb591b
commit 59d8dfbed0
1 changed files with 7 additions and 9 deletions

View File

@ -52,7 +52,7 @@ public class ContractDetails {
} }
} else{ } else{
storageTrie.update(key.getData(), RLP.encodeElement( value.getNoLeadZeroesData() )); storageTrie.update(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
int index = storageKeys.indexOf(key); int index = storageKeys.indexOf(key);
if (index != -1) { if (index != -1) {
storageKeys.remove(index); storageKeys.remove(index);
@ -94,7 +94,11 @@ public class ContractDetails {
public byte[] getStorageHash() { public byte[] getStorageHash() {
getEncoded(); storageTrie = new Trie(null);
// calc the trie for root hash
for (int i = 0; i < storageKeys.size(); ++i){
storageTrie.update(storageKeys.get(i).getData(), RLP.encodeElement( storageValues.get(i).getNoLeadZeroesData() ));
}
return storageTrie.getRootHash(); return storageTrie.getRootHash();
} }
@ -124,7 +128,7 @@ public class ContractDetails {
for (int i = 0; i < keys.size(); ++i) { for (int i = 0; i < keys.size(); ++i) {
DataWord key = storageKeys.get(i); DataWord key = storageKeys.get(i);
DataWord value = storageValues.get(i); DataWord value = storageValues.get(i);
storageTrie.update(key.getData(), RLP.encodeElement( value.getNoLeadZeroesData() )); storageTrie.update(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
} }
this.code = code.getRLPData(); this.code = code.getRLPData();
@ -149,12 +153,6 @@ public class ContractDetails {
values[i] = RLP.encodeElement(value.getNoLeadZeroesData()); values[i] = RLP.encodeElement(value.getNoLeadZeroesData());
} }
storageTrie = new Trie(null);
// calc the trie for root hash
for (int i = 0; i < storageKeys.size(); ++i){
storageTrie.update(storageKeys.get(i).getData(), values[i]);
}
byte[] rlpKeysList = RLP.encodeList(keys); byte[] rlpKeysList = RLP.encodeList(keys);
byte[] rlpValuesList = RLP.encodeList(values); byte[] rlpValuesList = RLP.encodeList(values);
byte[] rlpCode = RLP.encodeElement(code); byte[] rlpCode = RLP.encodeElement(code);