Skip to content

Commit

Permalink
[besu] refactor: drop unused template
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Nov 18, 2023
1 parent 6900f80 commit ae4cb1a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 129 deletions.
14 changes: 0 additions & 14 deletions src/exporters/besu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use serde::Serialize;
use super::reg_to_string;

const TRACE_COLUMNS_TEMPLATE: &str = include_str!("besu_trace_columns.java");
const TRACE_MODULE_TEMPLATE: &str = include_str!("besu_module_trace.java");

#[derive(Serialize)]
struct BesuColumn {
Expand Down Expand Up @@ -205,10 +204,6 @@ pub fn render(cs: &ConstraintSet, package: &str, output_path: Option<&String>) -
columns,
};

let trace_module_render = handlebars
.render_template(TRACE_MODULE_TEMPLATE, &template_data)
.expect("error rendering trace module java template for Besu");

let trace_columns_render = handlebars
.render_template(TRACE_COLUMNS_TEMPLATE, &template_data)
.expect("error rendering trace columns java template for Besu");
Expand All @@ -218,22 +213,13 @@ pub fn render(cs: &ConstraintSet, package: &str, output_path: Option<&String>) -
if !Path::new(f).is_dir() {
bail!("{} is not a directory", f.bold().yellow());
}
let trace_module_java_filepath = {
let m = format!("{}{}", template_data.module_prefix, "Trace.java");
Path::new(f).join(m)
};

let trace_columns_java_filepath = Path::new(f).join("Trace.java");

fill_file(trace_module_java_filepath, trace_module_render)
.expect("error creating trace module java file for Besu");

fill_file(trace_columns_java_filepath, trace_columns_render)
.expect("error creating trace columns java file for Besu");
}
None => {
println!("{trace_module_render}");
println!("=========================================================================");
println!("{trace_columns_render}");
}
}
Expand Down
37 changes: 0 additions & 37 deletions src/exporters/besu_module_trace.java

This file was deleted.

131 changes: 57 additions & 74 deletions src/exporters/besu_trace_columns.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,113 +17,96 @@

import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import net.consensys.linea.zktracer.ColumnHeader;
import net.consensys.linea.zktracer.types.UnsignedByte;
import org.apache.tuweni.units.bigints.UInt256;


/**
* WARNING: This code is generated automatically.
* Any modifications to this code may be overwritten and could lead to unexpected behavior.
*
* <p>Any modifications to this code may be overwritten and could lead to unexpected behavior.
* Please DO NOT ATTEMPT TO MODIFY this code directly.
*/
public record Trace(
{{#each registers}}
@JsonProperty("{{ this.corset_name }}") List<{{ this.tupe }}> {{ this.java_name }}{{#unless @last}},{{/unless}}{{#if @last}}) { {{/if}}
public class Trace {
{{#each constants}}
static final {{ this.tupe }} {{ this.name }} = {{ this.value }};
{{/each}}
static TraceBuilder builder(int length) {
return new TraceBuilder(length);
}

public int size() {
return this.{{ registers.0.java_name }}.size();
}

public static List<ColumnHeader> headers(int size) {
return List.of(
{{#each registers}}
new ColumnHeader("{{ this.corset_name }}", {{ this.bytes_width }}, size){{ #unless @last}},{{/unless}}
{{/each}}
);
}

private final BitSet filled = new BitSet();
private int currentLine = 0;

{{#each registers}}
private final MappedByteBuffer {{ this.java_name}};
{{/each}}

static class TraceBuilder {
private final BitSet filled = new BitSet();
private int currentLine = 0;

{{#each registers}}
private MappedByteBuffer {{ this.java_name}};
{{/each}}

private TraceBuilder(int length) {
}
static List<ColumnHeader> headers(int length) {
return List.of(
{{ #each registers }}
new ColumnHeader("{{ this.corset_name }}", {{ this.bytes_width }}, length){{ #if @last }});{{ else }},{{ /if }}
{{ /each }}
}

public int size() {
if (!filled.isEmpty()) {
throw new RuntimeException("Cannot measure a trace with a non-validated row.");
}
public Trace(List<MappedByteBuffer> buffers) {
{{ #each registers }}
this.{{ java_name }} = buffers.get({{ @index }});
{{ /each }}
}

return this.currentLine;
public int size() {
if (!filled.isEmpty()) {
throw new RuntimeException("Cannot measure a trace with a non-validated row.");
}

public void setBuffers(List<MappedByteBuffer> buffers) {
{{ #each registers }}
this.{{ java_name }} = buffers.get({{ @index }});
{{ /each }}
}
return this.currentLine;
}

public void releaseBuffers() {
{{ #each registers }}
this.{{ java_name }} = null;
{{ /each }}
{{#each columns}}
public Trace {{ this.appender }}(final {{ this.tupe }} b) {
if (filled.get({{ this.reg_id }})) {
throw new IllegalStateException("{{ this.corset_name }} already set");
} else {
filled.set({{ this.reg_id }});
}

{{#each columns}}
public TraceBuilder {{ this.appender }}(final {{ this.tupe }} b) {
if (filled.get({{ this.reg_id }})) {
throw new IllegalStateException("{{ this.corset_name }} already set");
} else {
filled.set({{ this.reg_id }});
}
{{ this.register }}.{{ this.putter }};

{{ this.register }}.{{ this.putter }};
return this;
}

return this;
{{/each}}
public Trace validateRow() {
{{#each registers}}
if (!filled.get({{ this.id }})) {
throw new IllegalStateException("{{ this.corset_name }} has not been filled");
}

{{/each}}
public TraceBuilder validateRow() {
{{#each registers}}
if (!filled.get({{ this.id }})) {
throw new IllegalStateException("{{ this.corset_name }} has not been filled");
}
filled.clear();
this.currentLine++;

{{/each}}
filled.clear();
this.currentLine++;
return this;
}

return this;
public Trace fillAndValidateRow() {
{{#each registers}}
if (!filled.get({{ this.id }})) {
{{ this.java_name }}.position({{ this.java_name }}.position() + {{ this.bytes_width }});
}

public TraceBuilder fillAndValidateRow() {
filled.clear();
this.currentLine++;
{{/each}}
filled.clear();
this.currentLine++;

return this;
}
return this;
}

public Trace build() {
if (!filled.isEmpty()) {
throw new IllegalStateException("Cannot build trace with a non-validated row.");
}
return null;
public Trace build() {
if (!filled.isEmpty()) {
throw new IllegalStateException("Cannot build trace with a non-validated row.");
}
return null;
}
}
8 changes: 4 additions & 4 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn parse_flat_trace(tracefile: &str, cs: &mut ConstraintSet) -> Result<()> {
let mut columns_reader = ColumnsReader::from(BufReader::new(File::open(tracefile)?));
let map = columns_reader.map()?;

for column in dbg!(map).into_iter() {
for column in map.into_iter() {
let column_ref: ColumnRef = column.handle.clone().into();
let mut xs = std::iter::once(Ok(CValue::zero()))
.chain((0..column.length).map(|_| {
Expand All @@ -107,9 +107,6 @@ pub fn parse_flat_trace(tracefile: &str, cs: &mut ConstraintSet) -> Result<()> {
}))
.collect::<Result<Vec<_>>>()?;

let module_spilling = cs
.spilling_for_column(&column_ref)
.ok_or_else(|| anyhow!("no spilling found for {}", column.handle.pretty()))?;
let module_min_len = cs
.columns
.min_len
Expand All @@ -119,6 +116,9 @@ pub fn parse_flat_trace(tracefile: &str, cs: &mut ConstraintSet) -> Result<()> {

if let Some(Register { magma, .. }) = cs.columns.register(&column_ref) {
debug!("Importing {}", column.handle.pretty());
let module_spilling = cs
.spilling_for_column(&column_ref)
.ok_or_else(|| anyhow!("no spilling found for {}", column.handle.pretty()))?;
// If the parsed column is not long enought w.r.t. the
// minimal module length, prepend it with as many zeroes as
// required.
Expand Down

0 comments on commit ae4cb1a

Please sign in to comment.