Skip to content

Commit

Permalink
feat: read replays from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Jan 24, 2024
1 parent 0689f63 commit 7c8f889
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import java.util.List;

import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.plugin.data.BlockBody;

public record BlockSnapshot(BlockHeaderSnapshot header, List<TransactionSnapshot> txs) {
public static BlockSnapshot of(final BlockHeader header, final BlockBody body) {
return new BlockSnapshot(
BlockHeaderSnapshot.from(header),
body.getTransactions().stream().map(TransactionSnapshot::of).toList());
body.getTransactions().stream().map(t -> TransactionSnapshot.of((Transaction) t)).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,41 @@

package net.consensys.linea.zktracer;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.zktracer.testing.ToyExecutionEnvironment;
import org.junit.jupiter.api.Test;

@Slf4j
public class ReplaysTests {
public static void replay(String filename) {
ToyExecutionEnvironment environment = ToyExecutionEnvironment.builder().build();
environment.replay(filename);
final InputStream fileStream =
ReplaysTests.class.getClassLoader().getResourceAsStream("replays/%s".formatted(filename));
if (fileStream == null) {
fail("unable to find %s in replay resources".formatted(filename));
}

final InputStream stream;
try {
stream = filename.toLowerCase().endsWith("gz") ? new GZIPInputStream(fileStream) : fileStream;
} catch (IOException e) {
log.error("while loading {}: {}", filename, e.getMessage());
throw new RuntimeException(e);
}
ToyExecutionEnvironment.builder()
.build()
.replay(new BufferedReader(new InputStreamReader(stream)));
}

@Test
void replayOneFile() {
replay("/home/franklin/asdf.json");
void traceTxStartNotTheSameAsTxPrepare() {
replay("start-vs-prepare-tx.json.gz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import static net.consensys.linea.zktracer.runtime.stack.Stack.MAX_STACK_SIZE;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -124,11 +124,11 @@ public void run() {
*
* @param replayFile the file containing the conflation
*/
public void replay(final String replayFile) {
public void replay(final Reader replayFile) {
Gson gson = new Gson();
ConflationSnapshot conflation;
try {
conflation = gson.fromJson(new FileReader(replayFile), ConflationSnapshot.class);
conflation = gson.fromJson(replayFile, ConflationSnapshot.class);
} catch (Exception e) {
log.error(e.getMessage());
return;
Expand Down
Binary file not shown.

0 comments on commit 7c8f889

Please sign in to comment.