Skip to content

Commit 5dec50e

Browse files
authored
release 0.7.8, Backwards compatible trielog metadata api (#61)
* Revert "revert zkTrielogPlugin changes for older versions of shomei (#60)" * add backward-compatible api for trielog construction Signed-off-by: garyschulte <[email protected]>
1 parent fdf2da8 commit 5dec50e

File tree

10 files changed

+410
-20
lines changed

10 files changed

+410
-20
lines changed

CHANGELOG.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# Changelog
22

3-
## unreleased
4-
5-
## version 0.7.7
6-
* revert trielog metadata changes for trielogs until a new shomei release is tested [#60](https://github.com/Consensys/besu-shomei-plugin/pull/60)
7-
83
## version 0.7.6
94
* update tracer to rc18, besu to 25.11.0-RC1-linea2 [#59](https://github.com/Consensys/besu-shomei-plugin/pull/59)
105
* safe tracer for the besu-shomei-plugin by matkt [#58](https://github.com/Consensys/besu-shomei-plugin/pull/58)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
releaseVersion=0.7.7-SNAPSHOT
1+
releaseVersion=0.7.8-SNAPSHOT
22
besuVersion=25.10.0-RC1-linea2
33
arithmetizationVersion=beta-v4.0-rc18

src/main/java/net/consensys/shomei/rpc/BesuShomeiRpcPlugin.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.consensys.shomei.context.ShomeiContext;
1818
import net.consensys.shomei.context.ShomeiContext.ShomeiContextImpl;
1919
import net.consensys.shomei.rpc.methods.ShomeiGetTrieLog;
20+
import net.consensys.shomei.rpc.methods.ShomeiGetTrieLogMetadata;
2021
import net.consensys.shomei.rpc.methods.ShomeiGetTrieLogsByRange;
2122

2223
import java.util.List;
@@ -36,7 +37,11 @@ public class BesuShomeiRpcPlugin implements BesuPlugin {
3637
@Override
3738
public void register(final ServiceManager serviceManager) {
3839
LOG.debug("Registering RPC plugins");
39-
var methods = List.of(new ShomeiGetTrieLogsByRange(ctx), new ShomeiGetTrieLog(ctx));
40+
var methods =
41+
List.of(
42+
new ShomeiGetTrieLogsByRange(ctx),
43+
new ShomeiGetTrieLog(ctx),
44+
new ShomeiGetTrieLogMetadata(ctx));
4045
serviceManager
4146
.getService(RpcEndpointService.class)
4247
.ifPresent(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright ConsenSys 2023
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
package net.consensys.shomei.rpc.methods;
16+
17+
import net.consensys.shomei.context.ShomeiContext;
18+
import net.consensys.shomei.trielog.PluginTrieLogLayer;
19+
20+
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest;
21+
22+
public class ShomeiGetTrieLogMetadata implements PluginRpcMethod {
23+
private final ShomeiContext ctx;
24+
25+
public ShomeiGetTrieLogMetadata(ShomeiContext ctx) {
26+
this.ctx = ctx;
27+
}
28+
29+
@Override
30+
public String getNamespace() {
31+
return "shomei";
32+
}
33+
34+
@Override
35+
public String getName() {
36+
return "getTrieLogMetadata";
37+
}
38+
39+
@Override
40+
public Object execute(PluginRpcRequest rpcRequest) {
41+
var params = rpcRequest.getParams();
42+
Long blockNumber = Long.parseLong(params[0].toString());
43+
44+
return ctx.getTrieLogService()
45+
.getTrieLogProvider()
46+
.getTrieLogLayer(blockNumber)
47+
.filter(t -> t instanceof PluginTrieLogLayer)
48+
.map(t -> (PluginTrieLogLayer) t)
49+
.orElse(null);
50+
}
51+
}

src/main/java/net/consensys/shomei/trielog/PluginTrieLogLayer.java

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@
1414
*/
1515
package net.consensys.shomei.trielog;
1616

17+
import net.consensys.shomei.cli.ShomeiCliOptions.ZkTraceComparisonFeature;
18+
1719
import java.util.HashMap;
20+
import java.util.List;
1821
import java.util.Map;
1922
import java.util.Objects;
2023
import java.util.Optional;
24+
import java.util.stream.Collectors;
2125

26+
import com.fasterxml.jackson.annotation.JsonGetter;
27+
import com.fasterxml.jackson.annotation.JsonIgnore;
28+
import com.fasterxml.jackson.annotation.JsonInclude;
2229
import org.apache.commons.lang3.builder.EqualsBuilder;
2330
import org.apache.commons.lang3.builder.HashCodeBuilder;
2431
import org.apache.tuweni.bytes.Bytes;
@@ -37,18 +44,37 @@
3744
* trie changes as well.
3845
*/
3946
@SuppressWarnings("unchecked")
47+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
4048
public record PluginTrieLogLayer(
41-
Hash blockHash,
42-
Optional<Long> blockNumber,
43-
Map<Address, TrieLog.LogTuple<AccountValue>> accounts,
44-
Map<Address, TrieLog.LogTuple<Bytes>> code,
45-
Map<Address, Map<StorageSlotKey, LogTuple<UInt256>>> storage,
46-
boolean frozen)
49+
@JsonIgnore Hash blockHash,
50+
@JsonIgnore Optional<Long> blockNumber,
51+
@JsonIgnore Map<Address, TrieLog.LogTuple<AccountValue>> accounts,
52+
@JsonIgnore Map<Address, TrieLog.LogTuple<Bytes>> code,
53+
@JsonIgnore Map<Address, Map<StorageSlotKey, LogTuple<UInt256>>> storage,
54+
@JsonIgnore boolean frozen,
55+
@JsonIgnore Optional<Integer> zkTraceComparisonFeature)
4756
implements TrieLog {
4857

58+
public PluginTrieLogLayer(
59+
Hash blockHash,
60+
Optional<Long> blockNumber,
61+
Map<Address, TrieLog.LogTuple<AccountValue>> accounts,
62+
Map<Address, TrieLog.LogTuple<Bytes>> code,
63+
Map<Address, Map<StorageSlotKey, LogTuple<UInt256>>> storage,
64+
boolean frozen) {
65+
this(blockHash, blockNumber, accounts, code, storage, frozen, Optional.empty());
66+
}
67+
4968
/** Creates a new PluginTrieLogLayer with blockhash and empty maps to deserialize into. */
5069
public PluginTrieLogLayer(final Hash blockHash) {
51-
this(blockHash, Optional.empty(), new HashMap<>(), new HashMap<>(), new HashMap<>(), true);
70+
this(
71+
blockHash,
72+
Optional.empty(),
73+
new HashMap<>(),
74+
new HashMap<>(),
75+
new HashMap<>(),
76+
true,
77+
Optional.empty());
5278
}
5379

5480
/** Locks the layer so no new changes can be added; */
@@ -128,6 +154,45 @@ public Optional<AccountValue> getAccount(final Address address) {
128154
return Optional.ofNullable(accounts.get(address)).map(LogTuple::getUpdated);
129155
}
130156

157+
// JSON serialization methods for metadata
158+
@JsonGetter("blockHash")
159+
public String getBlockHashHex() {
160+
return blockHash.toHexString();
161+
}
162+
163+
@JsonGetter("blockNumber")
164+
public Long getBlockNumberValue() {
165+
return blockNumber.orElse(null);
166+
}
167+
168+
@JsonGetter("zkTraceComparisonFeatures")
169+
public List<String> getZkTraceComparisonFeatures() {
170+
return zkTraceComparisonFeature
171+
.map(ZkTraceComparisonFeature::fromMask)
172+
.map(enumSet -> enumSet.stream().map(Enum::name).collect(Collectors.toList()))
173+
.orElse(null);
174+
}
175+
176+
@JsonGetter("zkTraceComparisonFeatureMask")
177+
public Integer getZkTraceComparisonFeatureMask() {
178+
return zkTraceComparisonFeature.orElse(null);
179+
}
180+
181+
@JsonGetter("accountChangesCount")
182+
public int getAccountChangesCount() {
183+
return accounts.size();
184+
}
185+
186+
@JsonGetter("codeChangesCount")
187+
public int getCodeChangesCount() {
188+
return code.size();
189+
}
190+
191+
@JsonGetter("storageChangesCount")
192+
public int getStorageChangesCount() {
193+
return storage.values().stream().mapToInt(Map::size).sum();
194+
}
195+
131196
public String dump() {
132197
final StringBuilder sb = new StringBuilder();
133198
sb.append("TrieLog{" + "blockHash=").append(blockHash).append(frozen).append('}');

src/main/java/net/consensys/shomei/trielog/ZkTrieLogFactory.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public TrieLog create(final TrieLogAccumulator accumulator, final BlockHeader bl
109109
(Map<Address, LogTuple<AccountValue>>) accountsToUpdate,
110110
(Map<Address, LogTuple<Bytes>>) codeToUpdate,
111111
(Map<Address, Map<StorageSlotKey, LogTuple<UInt256>>>) storageToUpdate,
112-
true);
112+
true,
113+
Optional.of(comparisonFeatureMask.get()));
113114
} finally {
114115
ctx.getBlockImportTraceProvider().clear();
115116
}
@@ -281,7 +282,6 @@ public static void writeTo(final TrieLog layer, final RLPOutput output) {
281282
output.writeBytes(layer.getBlockHash());
282283
// optionally write block number
283284
layer.getBlockNumber().ifPresent(output::writeLongScalar);
284-
285285
for (final Address address : addresses) {
286286
output.startList(); // this change
287287
output.writeBytes(address);
@@ -327,6 +327,10 @@ public static void writeTo(final TrieLog layer, final RLPOutput output) {
327327
output.endList(); // this change
328328
}
329329

330+
// optionally write zkTraceComparisonFeature
331+
if (layer instanceof PluginTrieLogLayer pluginLayer) {
332+
pluginLayer.zkTraceComparisonFeature().ifPresent(output::writeInt);
333+
}
330334
output.endList(); // container
331335
}
332336

@@ -412,9 +416,17 @@ public static PluginTrieLogLayer readFrom(final RLPInput input) {
412416
// lenient leave list for forward compatible additions.
413417
input.leaveListLenient();
414418
}
419+
420+
// zkTraceComparisonFeature is optional (read as last element in container, before leaving)
421+
Optional<Integer> zkTraceComparisonFeature =
422+
Optional.of(!input.isEndOfCurrentList())
423+
.filter(isPresent -> isPresent)
424+
.map(__ -> input.readInt());
425+
415426
input.leaveListLenient();
416427

417-
return new PluginTrieLogLayer(blockHash, blockNumber, accounts, code, storage, true);
428+
return new PluginTrieLogLayer(
429+
blockHash, blockNumber, accounts, code, storage, true, zkTraceComparisonFeature);
418430
}
419431

420432
protected static <T> T nullOrValue(final RLPInput input, final Function<RLPInput, T> reader) {

0 commit comments

Comments
 (0)