Skip to content

Commit f831462

Browse files
committed
update tests
1 parent e297c8e commit f831462

File tree

7 files changed

+148
-107
lines changed

7 files changed

+148
-107
lines changed

core/src/main/java/net/consensys/shomei/storage/ZkWorldStateArchive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void cacheSnapshot(TrieLogIdentifier trieLogIdentifier, WorldStateStorage storag
165165
}
166166

167167
@VisibleForTesting
168-
void applyTrieLog(
168+
public void applyTrieLog(
169169
final long newBlockNumber, final boolean generateTrace, final TrieLogLayer trieLogLayer) {
170170
headWorldState.getAccumulator().rollForward(trieLogLayer);
171171
headWorldState.commit(newBlockNumber, trieLogLayer.getBlockHash(), generateTrace);

core/src/main/java/net/consensys/shomei/trielog/TrieLogLayerConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.hyperledger.besu.datatypes.Address;
2121
import org.hyperledger.besu.datatypes.Hash;
22-
import org.hyperledger.besu.datatypes.Wei;
2322
import org.hyperledger.besu.ethereum.rlp.RLPInput;
2423

2524
import java.util.Objects;
@@ -192,7 +191,7 @@ public PriorAccount preparePriorTrieLogAccount(final AccountKey accountKey, fina
192191
in.enterList();
193192

194193
final UInt256 nonce = UInt256.valueOf(in.readLongScalar());
195-
final Wei balance = Wei.of(in.readUInt256Scalar());
194+
final UInt256 balance = in.readUInt256Scalar();
196195
final Hash evmStorageRoot;
197196
if (in.nextIsNull()) {
198197
evmStorageRoot = Hash.EMPTY_TRIE_HASH;
@@ -204,7 +203,8 @@ public PriorAccount preparePriorTrieLogAccount(final AccountKey accountKey, fina
204203
in.leaveList();
205204

206205
LOG.atTrace()
207-
.setMessage("prior account entry ({}) : expected old value ({},{},{}) and found ({},{})")
206+
.setMessage(
207+
"prior account entry ({}) : expected old value ({},{},{}) and found ({},{},{})")
208208
.addArgument(accountKey)
209209
.addArgument(flatLeaf)
210210
.addArgument(nonce)

core/src/test/java/net/consensys/shomei/TrieLogShippingTests.java

Lines changed: 114 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -12,131 +12,145 @@
1212
*/
1313
package net.consensys.shomei;
1414

15-
// import static net.consensys.shomei.util.TestFixtureGenerator.getContractStorageTrie;
16-
// import static net.consensys.shomei.util.bytes.MimcSafeBytes.safeUInt256;
17-
// import static org.assertj.core.api.Assertions.assertThat;
18-
//
19-
// import org.hyperledger.besu.datatypes.Hash;
20-
// import org.hyperledger.besu.datatypes.StorageSlotKey;
21-
// import org.hyperledger.besu.ethereum.rlp.RLP;
22-
// import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue;
23-
//
24-
// import net.consensys.shomei.exception.MissingTrieLogException;
25-
// import net.consensys.shomei.storage.ZkWorldStateArchive;
26-
// import net.consensys.shomei.trie.ZKTrie;
27-
// import net.consensys.shomei.trielog.TrieLogLayer;
28-
// import net.consensys.shomei.trielog.TrieLogLayerConverter;
29-
// import net.consensys.shomei.util.TestFixtureGenerator;
30-
// import net.consensys.shomei.util.bytes.MimcSafeBytes;
31-
// import org.apache.tuweni.bytes.Bytes;
32-
// import org.apache.tuweni.bytes.Bytes32;
33-
// import org.apache.tuweni.units.bigints.UInt256;
34-
// import org.junit.Test;
35-
36-
@SuppressWarnings("unused")
15+
import static net.consensys.shomei.util.TestFixtureGenerator.getContractStorageTrie;
16+
import static net.consensys.shomei.util.bytes.PoseidonSafeBytesUtils.safeUInt256;
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
import org.hyperledger.besu.datatypes.Hash;
20+
import org.hyperledger.besu.datatypes.StorageSlotKey;
21+
import org.hyperledger.besu.datatypes.Wei;
22+
import org.hyperledger.besu.ethereum.rlp.RLP;
23+
import org.hyperledger.besu.ethereum.trie.common.PmtStateTrieAccountValue;
24+
25+
import net.consensys.shomei.context.ShomeiContext;
26+
import net.consensys.shomei.storage.InMemoryStorageProvider;
27+
import net.consensys.shomei.storage.StorageProvider;
28+
import net.consensys.shomei.storage.ZkWorldStateArchive;
29+
import net.consensys.shomei.storage.worldstate.InMemoryWorldStateStorage;
30+
import net.consensys.shomei.storage.worldstate.WorldStateStorage;
31+
import net.consensys.shomei.trie.ZKTrie;
32+
import net.consensys.shomei.trie.storage.InMemoryStorage;
33+
import net.consensys.shomei.trielog.TrieLogLayer;
34+
import net.consensys.shomei.trielog.TrieLogLayerConverter;
35+
import net.consensys.shomei.trielog.ZkTrieLogFactory;
36+
import net.consensys.shomei.util.TestFixtureGenerator;
37+
import net.consensys.shomei.util.bytes.PoseidonSafeBytes;
38+
import org.apache.tuweni.bytes.Bytes;
39+
import org.apache.tuweni.bytes.Bytes32;
40+
import org.apache.tuweni.units.bigints.UInt256;
41+
import org.junit.jupiter.api.Test;
42+
3743
public class TrieLogShippingTests {
38-
/*
39-
// TODO activate when ZkTrieLogFactoryImpl will be available
40-
@Test
41-
public void testTrielogShippingWithNewContractUpdate() throws MissingTrieLogException {
4244

43-
ZKTrie accountStateTrieOne =
44-
ZKTrie.createTrie(new WorldStateStorageProxy(new InMemoryWorldStateStorage()));
45+
@Test
46+
public void testTrielogShippingWithNewContractUpdate() {
47+
// Initialize in-memory storage and state trie
48+
final InMemoryStorage storage = new InMemoryStorage();
49+
ZKTrie accountStateTrie = ZKTrie.createTrie(storage);
4550

46-
// add contract with storage
51+
// Create a contract with initial storage
4752
MutableZkAccount contract = TestFixtureGenerator.getAccountTwo();
48-
StorageSlotKey storageSlotKey = new StorageSlotKey(UInt256.valueOf(14));
49-
MimcSafeBytes<UInt256> slotValue = safeUInt256(UInt256.valueOf(12));
53+
PoseidonSafeBytes<UInt256> slotKey = safeUInt256(UInt256.valueOf(14));
54+
PoseidonSafeBytes<UInt256> slotValue = safeUInt256(UInt256.valueOf(12));
5055
ZKTrie contractStorageTrie = getContractStorageTrie(contract);
51-
contractStorageTrie.putAndProve(storageSlotKey.slotHash(), storageSlotKey.slotKey(), slotValue);
52-
contract.setStorageRoot(Hash.wrap(contractStorageTrie.getTopRootHash()));
5356

54-
accountStateTrieOne.putAndProve(
57+
// Update contract storage and state trie
58+
contractStorageTrie.putWithTrace(slotKey.hash(), slotKey, slotValue);
59+
contract.setStorageRoot(Hash.wrap(contractStorageTrie.getTopRootHash()));
60+
accountStateTrie.putWithTrace(
5561
contract.getHkey(), contract.getAddress(), contract.getEncodedBytes());
5662

57-
Hash topRootHashBeforeUpdate = Hash.wrap(accountStateTrieOne.getTopRootHash());
63+
// Save the root hash before updating the storage
64+
Hash topRootHashBeforeUpdate = Hash.wrap(accountStateTrie.getTopRootHash());
5865

59-
// change storage
60-
final MimcSafeBytes<UInt256> newStorageValue = safeUInt256(UInt256.valueOf(22));
61-
contractStorageTrie.putAndProve(
62-
storageSlotKey.slotHash(), storageSlotKey.slotKey(), newStorageValue);
66+
// Update storage with new value
67+
final PoseidonSafeBytes<UInt256> newStorageValue = safeUInt256(UInt256.valueOf(22));
68+
contractStorageTrie.putWithTrace(slotKey.hash(), slotKey, newStorageValue);
6369
contract.setStorageRoot(Hash.wrap(contractStorageTrie.getTopRootHash()));
64-
accountStateTrieOne.putAndProve(
70+
accountStateTrie.putWithTrace(
6571
contract.getHkey(), contract.getAddress(), contract.getEncodedBytes());
6672

67-
Hash topRootHashAfterUpdate = Hash.wrap(accountStateTrieOne.getTopRootHash());
73+
// Save the root hash after updating the storage
74+
Hash topRootHashAfterUpdate = Hash.wrap(accountStateTrie.getTopRootHash());
6875

69-
// simulate trielog from Besu before update
70-
org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogLayer trieLogLayer =
71-
new org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogLayer();
72-
trieLogLayer.addAccountChange(
76+
// Simulate TrieLogLayer from Besu before the update
77+
org.hyperledger.besu.ethereum.trie.pathbased.common.trielog.TrieLogLayer trieLogLayerBefore =
78+
new org.hyperledger.besu.ethereum.trie.pathbased.common.trielog.TrieLogLayer();
79+
trieLogLayerBefore.addAccountChange(
7380
contract.getAddress().getOriginalUnsafeValue(),
7481
null,
75-
new StateTrieAccountValue(
76-
contract.nonce.toLong(),
77-
contract.balance,
78-
Hash.wrap(
79-
Bytes32.random()), // change storage root to simulate evm storage root sent by Besu
82+
new PmtStateTrieAccountValue(
83+
contract.nonce.getOriginalUnsafeValue().toLong(),
84+
Wei.of(contract.balance.getOriginalUnsafeValue()),
85+
Hash.wrap(Bytes32.random()), // Simulate initial storage root
8086
Hash.wrap(contract.keccakCodeHash.getOriginalUnsafeValue())));
81-
trieLogLayer.setBlockHash(Hash.wrap(Bytes32.random()));
82-
trieLogLayer.addStorageChange(
87+
trieLogLayerBefore.setBlockHash(Hash.wrap(Bytes32.random()));
88+
trieLogLayerBefore.setBlockNumber(0);
89+
trieLogLayerBefore.addStorageChange(
8390
contract.getAddress().getOriginalUnsafeValue(),
84-
new org.hyperledger.besu.ethereum.bonsai.worldview.StorageSlotKey(
85-
storageSlotKey.slotKey().getOriginalUnsafeValue()),
91+
new StorageSlotKey(slotKey.getOriginalUnsafeValue()),
8692
null,
8793
slotValue.getOriginalUnsafeValue());
8894

89-
// simulate trielog from Besu after update
90-
org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogLayer trieLogLayer2 =
91-
new org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogLayer();
92-
trieLogLayer2.addAccountChange(
95+
// Simulate TrieLogLayer from Besu after the update
96+
org.hyperledger.besu.ethereum.trie.pathbased.common.trielog.TrieLogLayer trieLogLayerAfter =
97+
new org.hyperledger.besu.ethereum.trie.pathbased.common.trielog.TrieLogLayer();
98+
trieLogLayerAfter.addAccountChange(
9399
contract.getAddress().getOriginalUnsafeValue(),
94-
new StateTrieAccountValue(
95-
contract.nonce.toLong(),
96-
contract.balance,
97-
Hash.wrap(
98-
Bytes32.random()), // change storage root to simulate evm storage root sent by Besu
99-
Hash.wrap(
100-
contract.keccakCodeHash
101-
.getOriginalUnsafeValue())), // get update of the first trielog
102-
new StateTrieAccountValue(
103-
contract.nonce.toLong(),
104-
contract.balance,
105-
Hash.wrap(
106-
Bytes32.random()), // change storage root to simulate evm storage root sent by Besu
100+
new PmtStateTrieAccountValue(
101+
contract.nonce.getOriginalUnsafeValue().toLong(),
102+
Wei.of(contract.balance.getOriginalUnsafeValue()),
103+
Hash.wrap(Bytes32.random()), // Simulate updated storage root
104+
Hash.wrap(contract.keccakCodeHash.getOriginalUnsafeValue())),
105+
new PmtStateTrieAccountValue(
106+
contract.nonce.getOriginalUnsafeValue().toLong(),
107+
Wei.of(contract.balance.getOriginalUnsafeValue()),
108+
Hash.wrap(Bytes32.random()), // Simulate further storage root update
107109
Hash.wrap(contract.keccakCodeHash.getOriginalUnsafeValue())));
108-
trieLogLayer2.setBlockHash(Hash.wrap(Bytes32.random()));
109-
trieLogLayer2.addStorageChange(
110+
trieLogLayerAfter.setBlockHash(Hash.wrap(Bytes32.random()));
111+
trieLogLayerAfter.setBlockNumber(1);
112+
trieLogLayerAfter.addStorageChange(
110113
contract.getAddress().getOriginalUnsafeValue(),
111-
new org.hyperledger.besu.ethereum.bonsai.worldview.StorageSlotKey(
112-
storageSlotKey.slotKey().getOriginalUnsafeValue()),
114+
new StorageSlotKey(slotKey.getOriginalUnsafeValue()),
113115
slotValue.getOriginalUnsafeValue(),
114116
newStorageValue.getOriginalUnsafeValue());
115117

116-
ZkTrieLogFactoryImpl zkTrieLogFactory = new ZkTrieLogFactoryImpl();
117-
118-
// init the worldstate entrypoint with empty worldstate
119-
InMemoryWorldStateStorage storage = new InMemoryWorldStateStorage();
120-
ZkWorldStateArchive evmWorldStateEntryPoint = new ZkWorldStateArchive(storage);
121-
assertThat(evmWorldStateEntryPoint.getCurrentRootHash()).isEqualTo(ZKTrie.EMPTY_TRIE_ROOT);
122-
123-
// decode trielog from Besu
124-
TrieLogLayer decodedLayer =
125-
new TrieLogLayerConverter(storage)
126-
.decodeTrieLog(RLP.input(Bytes.wrap(zkTrieLogFactory.serialize(trieLogLayer))));
127-
128-
// move head with the new trielog
129-
evmWorldStateEntryPoint.applyTrieLog(0, decodedLayer);
130-
assertThat(evmWorldStateEntryPoint.getCurrentRootHash()).isEqualTo(topRootHashBeforeUpdate);
131-
132-
// decode second trielog from Besu
133-
TrieLogLayer decodedLayer2 =
134-
new TrieLogLayerConverter(storage)
135-
.decodeTrieLog(RLP.input(Bytes.wrap(zkTrieLogFactory.serialize(trieLogLayer2))));
136-
137-
// move head with the second trielog
138-
evmWorldStateEntryPoint.applyTrieLog(1, decodedLayer2);
139-
assertThat(evmWorldStateEntryPoint.getCurrentRootHash()).isEqualTo(topRootHashAfterUpdate);
118+
// Initialize the ShomeiContext and world state entry point
119+
ZkTrieLogFactory zkTrieLogFactory =
120+
new ZkTrieLogFactory(ShomeiContext.ShomeiContextImpl.getOrCreate());
121+
InMemoryWorldStateStorage worldStateStorage = new InMemoryWorldStateStorage();
122+
StorageProvider inMemoryStorageProvider =
123+
new InMemoryStorageProvider() {
124+
@Override
125+
public WorldStateStorage getWorldStateStorage() {
126+
return worldStateStorage;
127+
}
128+
};
129+
130+
ZkWorldStateArchive worldStateArchive = new ZkWorldStateArchive(inMemoryStorageProvider);
131+
132+
// Verify initial world state matches the default state root hash
133+
assertThat(worldStateArchive.getHeadWorldState().getStateRootHash())
134+
.isEqualTo(ZKTrie.DEFAULT_TRIE_ROOT);
135+
136+
// Decode and apply TrieLogLayer before the update
137+
TrieLogLayer decodedLayerBefore =
138+
new TrieLogLayerConverter(inMemoryStorageProvider.getWorldStateStorage())
139+
.decodeTrieLog(RLP.input(Bytes.wrap(zkTrieLogFactory.serialize(trieLogLayerBefore))));
140+
worldStateArchive.applyTrieLog(0, false, decodedLayerBefore);
141+
142+
// Assert world state root matches before update
143+
assertThat(worldStateArchive.getHeadWorldState().getStateRootHash())
144+
.isEqualTo(topRootHashBeforeUpdate);
145+
146+
// Decode and apply TrieLogLayer after the update
147+
TrieLogLayer decodedLayerAfter =
148+
new TrieLogLayerConverter(inMemoryStorageProvider.getWorldStateStorage())
149+
.decodeTrieLog(RLP.input(Bytes.wrap(zkTrieLogFactory.serialize(trieLogLayerAfter))));
150+
worldStateArchive.applyTrieLog(1, false, decodedLayerAfter);
151+
152+
// Assert world state root matches after update
153+
assertThat(worldStateArchive.getHeadWorldState().getStateRootHash())
154+
.isEqualTo(topRootHashAfterUpdate);
140155
}
141-
*/
142156
}

core/src/test/java/net/consensys/shomei/WorldstateTraceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public void testTraceStateWithAnAccount() throws IOException {
114114
Trace trace =
115115
accountStateTrie.putWithTrace(
116116
account.getHkey(), account.getAddress(), account.getEncodedBytes());
117-
118117
assertThat(JSON_OBJECT_MAPPER.writeValueAsString(trace))
119118
.isEqualToIgnoringWhitespace(getResources("testTraceStateWithAnAccount.json"));
120119
}
@@ -210,7 +209,9 @@ public void testWorldStateWithUpdateContractStorage() throws IOException {
210209
final PoseidonSafeBytes<Bytes32> slotKey = safeByte32(createDumFullBytes(14));
211210
final Hash slotKeyHash = slotKey.hash();
212211
final PoseidonSafeBytes<Bytes32> slotValue = safeByte32(createDumFullBytes(18));
212+
213213
final Trace trace3 = account2Storage.putWithTrace(slotKeyHash, slotKey, slotValue);
214+
trace3.setLocation(zkAccount2.getAddress().getOriginalUnsafeValue());
214215

215216
zkAccount2.setStorageRoot(Hash.wrap(account2Storage.getTopRootHash()));
216217
final Trace trace4 =

core/src/test/java/net/consensys/shomei/ZkAccountTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
package net.consensys.shomei;
1414

1515
import static net.consensys.shomei.util.bytes.PoseidonSafeBytesUtils.safeByte32;
16+
import static net.consensys.shomei.util.bytes.PoseidonSafeBytesUtils.safeCode;
1617
import static net.consensys.shomei.util.bytes.PoseidonSafeBytesUtils.safeUInt256;
1718
import static org.assertj.core.api.Assertions.assertThat;
1819

1920
import org.hyperledger.besu.datatypes.Address;
2021
import org.hyperledger.besu.datatypes.Hash;
2122

23+
import net.consensys.shomei.trie.ZKTrie;
2224
import net.consensys.shomei.trielog.AccountKey;
2325
import net.consensys.shomei.util.bytes.PoseidonSafeBytes;
26+
import net.consensys.zkevm.HashProvider;
2427
import org.apache.tuweni.bytes.Bytes;
2528
import org.apache.tuweni.bytes.Bytes32;
2629
import org.apache.tuweni.units.bigints.UInt256;
@@ -30,13 +33,14 @@ public class ZkAccountTest {
3033

3134
@Test
3235
public void testHashZeroAccount() {
36+
3337
final ZkAccount zkAccount =
3438
new ZkAccount(
3539
new AccountKey(Hash.ZERO, Address.ZERO),
3640
safeUInt256(UInt256.valueOf(0L)),
3741
safeUInt256(UInt256.valueOf(0L)),
3842
Hash.ZERO,
39-
Hash.ZERO,
43+
ZKTrie.DEFAULT_TRIE_ROOT,
4044
safeByte32(Hash.ZERO),
4145
safeUInt256(UInt256.valueOf(0L)));
4246

@@ -111,4 +115,25 @@ public void testEncodedBytesSerialization() {
111115

112116
assertThat(deserializedAccount).isEqualToComparingFieldByField(originalAccount);
113117
}
118+
119+
@Test
120+
public void testContractCodeEncoding() {
121+
final Bytes code =
122+
Bytes.fromHexString("0x495340db00ecc17b5cb435d5731f8d6635e6b3ef42507a8303a068d178a95d22");
123+
;
124+
final PoseidonSafeBytes<Bytes> safeCode = safeCode(code);
125+
final ZkAccount zkAccount =
126+
new ZkAccount(
127+
new AccountKey(Hash.ZERO, Address.ZERO),
128+
safeUInt256(UInt256.valueOf(65L)),
129+
safeUInt256(UInt256.valueOf(835L)),
130+
ZKTrie.DEFAULT_TRIE_ROOT,
131+
safeCode.hash(),
132+
safeByte32(HashProvider.keccak256(code)),
133+
safeUInt256(UInt256.valueOf(0L)));
134+
assertThat(zkAccount.getEncodedBytes().hash())
135+
.isEqualTo(
136+
Bytes32.fromHexString(
137+
"0x5b4f38da3b5579846022b90a4a9ca1096653055722e75ebc0d4f68ba22d712c9"));
138+
}
114139
}

core/src/test/resources/testTraceStateWithAnAccount.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"0x639bef4d255b33103543742212d4e7710dbd582b47ece1c76a481f3e7dea1639"
139139
]
140140
},
141+
141142
"key":"0x2400000000000000000000000000000000000000",
142143
"value":"0x000000000000000000000000000000000000000000000000000000000000004100000000000000000000000000000000000000000000000000000000000003432fa0344a2fab2b310d2af3155c330261263f887379aef18b4941e3ea1cc59df70656ab853b3f52840362a8177e217b630c3f876b11e848365145aa24220647fcc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4700000000000000000000000000000000000000000000000000000000000000000",
143144
"priorLeftLeaf":{

core/src/test/resources/testWorldStateWithUpdateContractStorage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
"type":2
313313
},
314314
{
315-
"location":"0x",
315+
"location":"0x2f00000000000000000000000000000000000000",
316316
"newNextFreeNode":3,
317317
"oldSubRoot":"0x3a00a8e34a16f8a1225fee734816edb326f783bd6678d793345a28f046586ba6",
318318
"newSubRoot":"0x7a47390f07bccddb52ea3dd3746e54c14c79361d08cf35f35f293e7d5a85db92",

0 commit comments

Comments
 (0)