diff --git a/hildr-utilities/build.gradle b/hildr-utilities/build.gradle index b26ed923..291b4d7b 100644 --- a/hildr-utilities/build.gradle +++ b/hildr-utilities/build.gradle @@ -12,7 +12,8 @@ version = '0.1.1' repositories { // Use Maven Central for resolving dependencies. mavenCentral() - maven { url "https://artifacts.consensys.net/public/teku/maven/" + maven { + url "https://artifacts.consensys.net/public/teku/maven/" } maven { url "https://dl.cloudsmith.io/public/libp2p/jvm-libp2p/maven/" @@ -93,7 +94,7 @@ dependencies { testImplementation platform('org.junit:junit-bom:5.9.1') testImplementation 'org.junit.jupiter:junit-jupiter' - testImplementation('io.tmio:tuweni-crypto:2.4.2'){ + testImplementation('io.tmio:tuweni-crypto:2.4.2') { exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on' } @@ -165,4 +166,20 @@ spotless { removeUnusedImports() } + + +} + +javadoc { + if (JavaVersion.current().isJava9Compatible()) { + options.addBooleanOption('html5', true) + } + options.addBooleanOption('-enable-preview', true) + options.addStringOption('-release', '21') +// options.addStringOption('-add-modules', 'jdk.incubator.concurrent') +} + +java { + withJavadocJar() + withSourcesJar() } diff --git a/hildr-utilities/src/main/java/io/optimism/type/DepositTransaction.java b/hildr-utilities/src/main/java/io/optimism/type/DepositTransaction.java index 69031231..a8f20e27 100644 --- a/hildr-utilities/src/main/java/io/optimism/type/DepositTransaction.java +++ b/hildr-utilities/src/main/java/io/optimism/type/DepositTransaction.java @@ -71,8 +71,23 @@ public class DepositTransaction { */ private String data; + /** + * Instantiates a new Deposit transaction. + */ public DepositTransaction() {} + /** + * Instantiates a new Deposit transaction. + * + * @param sourceHash the source hash + * @param from the from + * @param to the to + * @param mint the mint + * @param value the value + * @param gas the gas + * @param isSystemTransaction the is system transaction + * @param data the data + */ public DepositTransaction( String sourceHash, String from, @@ -92,38 +107,83 @@ public DepositTransaction( this.data = data; } + /** + * Gets source hash. + * + * @return the source hash + */ public String getSourceHash() { return sourceHash; } + /** + * Gets from. + * + * @return the from + */ public String getFrom() { return from; } + /** + * Gets to. + * + * @return the to + */ public String getTo() { return to; } + /** + * Gets mint. + * + * @return the mint + */ public BigInteger getMint() { return mint; } + /** + * Gets value. + * + * @return the value + */ public BigInteger getValue() { return value; } + /** + * Gets gas. + * + * @return the gas + */ public BigInteger getGas() { return gas; } + /** + * Is system transaction boolean. + * + * @return the boolean + */ public boolean isSystemTransaction() { return isSystemTransaction; } + /** + * Gets data. + * + * @return the data + */ public String getData() { return data; } + /** + * As rlp values list. + * + * @return the list + */ public List asRlpValues() { List result = new ArrayList<>(); result.add(RlpString.create(getSourceHash())); diff --git a/hildr-utilities/src/main/java/io/optimism/type/RollupConfigResult.java b/hildr-utilities/src/main/java/io/optimism/type/RollupConfigResult.java index a4c57ef8..42d60309 100644 --- a/hildr-utilities/src/main/java/io/optimism/type/RollupConfigResult.java +++ b/hildr-utilities/src/main/java/io/optimism/type/RollupConfigResult.java @@ -98,90 +98,205 @@ public class RollupConfigResult { @JsonProperty("l1_system_config_address") private String l1SystemConfigAddress; + /** + * Instantiates a new Rollup config result. + */ + public RollupConfigResult() {} + + /** + * Gets genesis. + * + * @return the genesis + */ public Genesis getGenesis() { return genesis; } + /** + * Sets genesis. + * + * @param genesis the genesis + */ public void setGenesis(Genesis genesis) { this.genesis = genesis; } + /** + * Gets block time. + * + * @return the block time + */ public BigInteger getBlockTime() { return blockTime; } + /** + * Sets block time. + * + * @param blockTime the block time + */ public void setBlockTime(BigInteger blockTime) { this.blockTime = blockTime; } + /** + * Gets max sequencer drift. + * + * @return the max sequencer drift + */ public BigInteger getMaxSequencerDrift() { return maxSequencerDrift; } + /** + * Sets max sequencer drift. + * + * @param maxSequencerDrift the max sequencer drift + */ public void setMaxSequencerDrift(BigInteger maxSequencerDrift) { this.maxSequencerDrift = maxSequencerDrift; } + /** + * Gets seq window size. + * + * @return the seq window size + */ public BigInteger getSeqWindowSize() { return seqWindowSize; } + /** + * Sets seq window size. + * + * @param seqWindowSize the seq window size + */ public void setSeqWindowSize(BigInteger seqWindowSize) { this.seqWindowSize = seqWindowSize; } + /** + * Gets channel timeout. + * + * @return the channel timeout + */ public BigInteger getChannelTimeout() { return channelTimeout; } + /** + * Sets channel timeout. + * + * @param channelTimeout the channel timeout + */ public void setChannelTimeout(BigInteger channelTimeout) { this.channelTimeout = channelTimeout; } + /** + * Gets l 1 chain id. + * + * @return the l 1 chain id + */ public BigInteger getL1ChainId() { return l1ChainId; } + /** + * Sets l 1 chain id. + * + * @param l1ChainId the l 1 chain id + */ public void setL1ChainId(BigInteger l1ChainId) { this.l1ChainId = l1ChainId; } + /** + * Gets l 2 chain id. + * + * @return the l 2 chain id + */ public BigInteger getL2ChainId() { return l2ChainId; } + /** + * Sets l 2 chain id. + * + * @param l2ChainId the l 2 chain id + */ public void setL2ChainId(BigInteger l2ChainId) { this.l2ChainId = l2ChainId; } + /** + * Gets regolith time. + * + * @return the regolith time + */ public BigInteger getRegolithTime() { return regolithTime; } + /** + * Sets regolith time. + * + * @param regolithTime the regolith time + */ public void setRegolithTime(BigInteger regolithTime) { this.regolithTime = regolithTime; } + /** + * Gets batch inbox address. + * + * @return the batch inbox address + */ public String getBatchInboxAddress() { return batchInboxAddress; } + /** + * Sets batch inbox address. + * + * @param batchInboxAddress the batch inbox address + */ public void setBatchInboxAddress(String batchInboxAddress) { this.batchInboxAddress = batchInboxAddress; } + /** + * Gets deposit contract address. + * + * @return the deposit contract address + */ public String getDepositContractAddress() { return depositContractAddress; } + /** + * Sets deposit contract address. + * + * @param depositContractAddress the deposit contract address + */ public void setDepositContractAddress(String depositContractAddress) { this.depositContractAddress = depositContractAddress; } + /** + * Gets l 1 system config address. + * + * @return the l 1 system config address + */ public String getL1SystemConfigAddress() { return l1SystemConfigAddress; } + /** + * Sets l 1 system config address. + * + * @param l1SystemConfigAddress the l 1 system config address + */ public void setL1SystemConfigAddress(String l1SystemConfigAddress) { this.l1SystemConfigAddress = l1SystemConfigAddress; } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/TxDecoder.java b/hildr-utilities/src/main/java/io/optimism/utilities/TxDecoder.java index ccbd7f51..f06e44e6 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/TxDecoder.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/TxDecoder.java @@ -32,6 +32,17 @@ */ public class TxDecoder { + /** + * Instantiates a new Tx decoder. + */ + public TxDecoder() {} + + /** + * Decode to deposit deposit transaction. + * + * @param hexTransaction the hex transaction + * @return the deposit transaction + */ public static DepositTransaction decodeToDeposit(final String hexTransaction) { final byte[] transaction = Numeric.hexStringToByteArray(hexTransaction); if (transaction.length > 0 && transaction[0] != ((byte) 0x7E)) { diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/BatchType.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/BatchType.java index 5fd92940..5da1cab9 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/BatchType.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/BatchType.java @@ -1,7 +1,16 @@ package io.optimism.utilities.derive.stages; +/** + * The enum Batch type. + */ public enum BatchType { + /** + * Singular batch type batch type. + */ SINGULAR_BATCH_TYPE(0, "SingularBatchType"), + /** + * Span batch type batch type. + */ SPAN_BATCH_TYPE(1, "SpanBatchType"); private final int code; private final String name; @@ -11,14 +20,30 @@ public enum BatchType { this.name = name; } + /** + * Gets code. + * + * @return the code + */ public int getCode() { return code; } + /** + * Gets name. + * + * @return the name + */ public String getName() { return name; } + /** + * From batch type. + * + * @param code the code + * @return the batch type + */ public static BatchType from(int code) { for (BatchType batchType : BatchType.values()) { if (batchType.getCode() == code) { diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/IBatch.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/IBatch.java index 8b60ba96..05e7c93b 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/IBatch.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/IBatch.java @@ -3,16 +3,26 @@ import java.math.BigInteger; /** - * Batch contains information to build one or multiple L2 blocks. - * Batcher converts L2 blocks into Batch and writes encoded bytes to Channel. - * Derivation pipeline decodes Batch from Channel, and converts to one or multiple payload attributes. + * Batch contains information to build one or multiple L2 blocks. + * Batcher converts L2 blocks into Batch and writes encoded bytes to Channel. + * Derivation pipeline decodes Batch from Channel, and converts to one or multiple payload attributes. * - * @author zhouop0 - * @since 0.1.0 + * @author zhouop0 + * @since 0.1.0 */ public interface IBatch { - int getBatchType(); + /** + * Gets batch type. + * + * @return the batch type + */ + BatchType getBatchType(); + /** + * Gets timestamp. + * + * @return the timestamp + */ BigInteger getTimestamp(); } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/RawSpanBatch.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/RawSpanBatch.java index 51eabae9..c73779b2 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/RawSpanBatch.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/RawSpanBatch.java @@ -1,3 +1,86 @@ -// package io.optimism.utilities.derive.stages; -// -// public record RawSpanBatch(SpanBatchPrefix spanbatchPrefix, SpanBatchPayload spanbatchPayload) {} +package io.optimism.utilities.derive.stages; + +import io.netty.buffer.ByteBuf; +import java.math.BigInteger; +import java.util.Objects; + +/** + * The type RawSpanBatch. + * + * @author grapebaba + * @since 0.2.4 + */ +public class RawSpanBatch implements IBatch { + private final SpanBatchPrefix spanbatchPrefix; + private final SpanBatchPayload spanbatchPayload; + + /** + * Instantiates a new Raw span batch. + * + * @param spanbatchPrefix the spanbatch prefix + * @param spanbatchPayload the spanbatch payload + */ + public RawSpanBatch(SpanBatchPrefix spanbatchPrefix, SpanBatchPayload spanbatchPayload) { + this.spanbatchPrefix = spanbatchPrefix; + this.spanbatchPayload = spanbatchPayload; + } + + /** + * Spanbatch prefix span batch prefix. + * + * @return the span batch prefix + */ + public SpanBatchPrefix spanbatchPrefix() { + return spanbatchPrefix; + } + + /** + * Spanbatch payload span batch payload. + * + * @return the span batch payload + */ + public SpanBatchPayload spanbatchPayload() { + return spanbatchPayload; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RawSpanBatch that)) return false; + return Objects.equals(spanbatchPrefix, that.spanbatchPrefix) + && Objects.equals(spanbatchPayload, that.spanbatchPayload); + } + + @Override + public int hashCode() { + return Objects.hash(spanbatchPrefix, spanbatchPayload); + } + + @Override + public String toString() { + return "RawSpanBatch[spanbatchPrefix=%s, spanbatchPayload=%s]".formatted(spanbatchPrefix, spanbatchPayload); + } + + @Override + public BatchType getBatchType() { + return BatchType.SPAN_BATCH_TYPE; + } + + @Override + public BigInteger getTimestamp() { + return null; + } + + /** + * Decode. + * + * @param source the source + */ + public void decode(ByteBuf source) { + if (source.readableBytes() > SpanBatchUtils.MaxSpanBatchSize) { + throw new IllegalArgumentException("SpanBatch is too large"); + } + this.spanbatchPrefix.decode(source); + this.spanbatchPayload.decode(source); + } +} diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SingularBatch.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SingularBatch.java index 9c0da606..4d25bed1 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SingularBatch.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SingularBatch.java @@ -9,14 +9,35 @@ import org.web3j.rlp.RlpString; import org.web3j.rlp.RlpType; +/** + * The type SingularBatch. + * + * @param epochHash the epoch hash + * @param epochNum the epoch num + * @param parentHash the parent hash + * @param timestamp the timestamp + * @param transactions the transactions + * @author grapebaba + * @since 0.2.4 + */ public record SingularBatch( String parentHash, BigInteger epochNum, String epochHash, BigInteger timestamp, List transactions) implements IBatch { + /** + * Epoch block id. + * + * @return the block id + */ public BlockId epoch() { return new BlockId(epochHash(), epochNum()); } + /** + * Encode byte [ ]. + * + * @return the byte [ ] + */ public byte[] encode() { List collect = transactions().stream() .map(tx -> (RlpType) RlpString.create(tx)) @@ -48,8 +69,8 @@ public static SingularBatch decode(RlpList rlp) { } @Override - public int getBatchType() { - return BatchType.SINGULAR_BATCH_TYPE.getCode(); + public BatchType getBatchType() { + return BatchType.SINGULAR_BATCH_TYPE; } @Override @@ -57,6 +78,11 @@ public BigInteger getTimestamp() { return timestamp(); } + /** + * Gets epoch num. + * + * @return the epoch num + */ public BigInteger getEpochNum() { return epochNum(); } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatch.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatch.java index 803e9560..73536b68 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatch.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatch.java @@ -4,11 +4,11 @@ import java.util.List; /** - * SpanBatch is an implementation of Batch interface, - * containing the input to build a span of L2 blocks in derived form (SpanBatchElement) + * SpanBatch is an implementation of Batch interface, + * containing the input to build a span of L2 blocks in derived form (SpanBatchElement) * - * @author zhouop0 - * @since 0.1.0 + * @author zhouop0 + * @since 0.1.0 */ public class SpanBatch implements IBatch { @@ -19,21 +19,41 @@ public class SpanBatch implements IBatch { // List of block input in derived form private List batches; + /** + * Instantiates a new Span batch. + */ + public SpanBatch() {} + + /** + * Gets parent check. + * + * @return the parent check + */ public String getParentCheck() { return parentCheck; } + /** + * Gets l 1 origin check. + * + * @return the l 1 origin check + */ public String getL1OriginCheck() { return l1OriginCheck; } + /** + * Gets batches. + * + * @return the batches + */ public List getBatches() { return batches; } @Override - public int getBatchType() { - return BatchType.SPAN_BATCH_TYPE.getCode(); + public BatchType getBatchType() { + return BatchType.SPAN_BATCH_TYPE; } @Override @@ -43,6 +63,8 @@ public BigInteger getTimestamp() { /** * GetStartEpochNum returns epoch number(L1 origin block number) of the first block in the span. + * + * @return the epoch number(L1 origin block number) of the first block in the span. */ public BigInteger getStartEpochNum() { return this.batches.getFirst().epochNum(); @@ -50,8 +72,9 @@ public BigInteger getStartEpochNum() { /** * checks if the parentCheck matches the first 20 bytes of given hash, probably the current L2 safe head. + * * @param hash the first 20 bytes of given hash. - * @return boolean. + * @return boolean. boolean */ public boolean checkOriginHash(String hash) { return this.l1OriginCheck.equals(hash); @@ -59,8 +82,9 @@ public boolean checkOriginHash(String hash) { /** * checks if the parentCheck matches the first 20 bytes of given hash, probably the current L2 safe head. + * * @param hash the first 20 bytes of given hash. - * @return boolean. + * @return boolean. boolean */ public boolean checkParentHash(String hash) { return this.parentCheck.equals(hash); @@ -68,6 +92,7 @@ public boolean checkParentHash(String hash) { /** * GetBlockEpochNum + * * @param index batches index. * @return the epoch number(L1 origin block number) of the block at the given index in the span. */ @@ -77,6 +102,7 @@ public BigInteger getBlockEpochNum(int index) { /** * GetBlockTimestamp + * * @param index batches index. * @return the timestamp of the block at the given index in the span. */ @@ -86,6 +112,7 @@ public BigInteger getBlockTimestamp(int index) { /** * GetBlockCount + * * @return the number of blocks in the span. */ public int getBlockCount() { diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchAccessListTxData.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchAccessListTxData.java index 4115ef49..c61b3ac6 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchAccessListTxData.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchAccessListTxData.java @@ -15,6 +15,10 @@ /** * The type SpanBatchAccessListTxData. * + * @param value the value + * @param gasPrice the gas price + * @param data the data + * @param accessList the access list * @author grapebaba * @since 0.2.4 */ diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchDynamicFeeTxData.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchDynamicFeeTxData.java index ee3b63a6..d2ab4e5d 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchDynamicFeeTxData.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchDynamicFeeTxData.java @@ -15,6 +15,11 @@ /** * The type SpanBatchDynamicFeeTxData. * + * @param value the value + * @param gasTipCap the gas tip cap + * @param gasFeeCap the gas fee cap + * @param data the data + * @param accessList the access list * @author grapebaba * @since 0.2.4 */ diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchElement.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchElement.java index 7ba0e588..21b252fe 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchElement.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchElement.java @@ -3,8 +3,23 @@ import java.math.BigInteger; import java.util.List; +/** + * The type SpanBatchElement. + * + * @param epochNum the epoch num + * @param timestamp the timestamp + * @param transactions the transactions + * @author grapebaba + * @since 0.2.4 + */ public record SpanBatchElement(BigInteger epochNum, BigInteger timestamp, List transactions) { + /** + * Singular batch to element span batch element. + * + * @param singularBatch the singular batch + * @return the span batch element + */ public static SpanBatchElement singularBatchToElement(SingularBatch singularBatch) { return new SpanBatchElement(singularBatch.epochNum(), singularBatch.timestamp(), singularBatch.transactions()); } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchLegacyTxData.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchLegacyTxData.java index 9ecc5fd2..7eabb0c1 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchLegacyTxData.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchLegacyTxData.java @@ -25,6 +25,9 @@ /** * The type SpanBatchLegacyTxData. * + * @param value the value + * @param gasPrice the gas price + * @param data the data * @author zhouop0 * @since 0.2.4 */ diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPayload.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPayload.java index 66521afb..458a1c76 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPayload.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPayload.java @@ -1,7 +1,248 @@ -// package io.optimism.utilities.derive.stages; -// -// import java.math.BigInteger; -// import java.util.List; -// -// public record SpanBatchPayload( -// BigInteger blockCount, BigInteger originBits, List blockTxCounts, SpanBatchTxs txs) {} +package io.optimism.utilities.derive.stages; + +import static io.optimism.utilities.derive.stages.SpanBatchUtils.putVarLong; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.PooledByteBufAllocator; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * The type SpanBatchPayload. + * + * @since 0.2.4 + * @author grapebaba + */ +public class SpanBatchPayload { + private long blockCount; + private BigInteger originBits; + private List blockTxCounts; + private SpanBatchTxs txs; + + /** + * Instantiates a new Span batch payload. + * + * @param blockCount the block count + * @param originBits the origin bits + * @param blockTxCounts the block tx counts + * @param txs the txs + */ + public SpanBatchPayload(long blockCount, BigInteger originBits, List blockTxCounts, SpanBatchTxs txs) { + this.blockCount = blockCount; + this.originBits = originBits; + this.blockTxCounts = blockTxCounts; + this.txs = txs; + } + + /** + * Block count long. + * + * @return the long + */ + public long blockCount() { + return blockCount; + } + + /** + * Origin bits big integer. + * + * @return the big integer + */ + public BigInteger originBits() { + return originBits; + } + + /** + * Block tx counts list. + * + * @return the list + */ + public List blockTxCounts() { + return blockTxCounts; + } + + /** + * Txs span batch txs. + * + * @return the span batch txs + */ + public SpanBatchTxs txs() { + return txs; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpanBatchPayload that)) return false; + return blockCount == that.blockCount + && Objects.equals(originBits, that.originBits) + && Objects.equals(blockTxCounts, that.blockTxCounts) + && Objects.equals(txs, that.txs); + } + + @Override + public int hashCode() { + return Objects.hash(blockCount, originBits, blockTxCounts, txs); + } + + @Override + public String toString() { + return "SpanBatchPayload[blockCount=%d, originBits=%s, blockTxCounts=%s, txs=%s]" + .formatted(blockCount, originBits, blockTxCounts, txs); + } + + /** + * Decode origin bits. + * + * @param source the source + */ + public void decodeOriginBits(ByteBuf source) { + this.originBits = SpanBatchUtils.decodeSpanBatchBits(source, (int) this.blockCount); + } + + /** + * Encode origin bits byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeOriginBits() { + return SpanBatchUtils.encodeSpanBatchBits((int) this.blockCount, this.originBits); + } + + /** + * Decode block count. + * + * @param source the source + */ + public void decodeBlockCount(ByteBuf source) { + var blockCount = SpanBatchUtils.getVarLong(source); + if (blockCount > SpanBatchUtils.MaxSpanBatchSize) { + throw new RuntimeException("span batch size limit reached"); + } + + if (blockCount == 0) { + throw new RuntimeException("block count cannot be zero"); + } + + this.blockCount = blockCount; + } + + /** + * Encode block count byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeBlockCount() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10); + putVarLong(this.blockCount, buffer); + + return ByteBufUtil.getBytes(buffer); + } + + /** + * Decode block tx counts. + * + * @param source the source + */ + public void decodeBlockTxCounts(ByteBuf source) { + List blockTxCounts = new ArrayList<>(); + for (int i = 0; i < this.blockCount; i++) { + var txCount = SpanBatchUtils.getVarLong(source); + if (txCount > SpanBatchUtils.MaxSpanBatchSize) { + throw new RuntimeException("span batch size limit reached"); + } + blockTxCounts.add(txCount); + } + this.blockTxCounts = blockTxCounts; + } + + /** + * Encode block tx counts byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeBlockTxCounts() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10 * this.blockTxCounts.size()); + for (Long blockTxCount : this.blockTxCounts) { + putVarLong(blockTxCount, buffer); + } + + return ByteBufUtil.getBytes(buffer); + } + + /** + * Decode txs. + * + * @param source the source + */ + public void decodeTxs(ByteBuf source) { + if (this.txs == null) { + this.txs = new SpanBatchTxs(); + } + + if (this.blockTxCounts == null || this.blockTxCounts.isEmpty()) { + throw new RuntimeException("blockTxCounts cannot be null"); + } + + long totalBlockTxCount = 0; + for (int i = 0; i < this.blockCount; i++) { + long total; + try { + total = Math.addExact(totalBlockTxCount, this.blockTxCounts.get(i)); + } catch (ArithmeticException e) { + throw new RuntimeException("totalBlockTxCount overflow"); + } + totalBlockTxCount = total; + } + + if (totalBlockTxCount > SpanBatchUtils.MaxSpanBatchSize) { + throw new RuntimeException("span batch size limit reached"); + } + + this.txs.setTotalBlockTxCount(totalBlockTxCount); + this.txs.decode(source); + } + + /** + * Encode txs byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeTxs() { + if (this.txs == null) { + throw new RuntimeException("txs cannot be null"); + } + + return this.txs.encode(); + } + + /** + * Decode. + * + * @param source the source + */ + public void decode(ByteBuf source) { + decodeBlockCount(source); + decodeOriginBits(source); + decodeBlockTxCounts(source); + decodeTxs(source); + } + + /** + * Encode byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encode() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); + buffer.writeBytes(encodeBlockCount()); + buffer.writeBytes(encodeOriginBits()); + buffer.writeBytes(encodeBlockTxCounts()); + buffer.writeBytes(encodeTxs()); + + return ByteBufUtil.getBytes(buffer); + } +} diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPrefix.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPrefix.java index 7a2c99d8..20a2e0e9 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPrefix.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchPrefix.java @@ -1,5 +1,202 @@ package io.optimism.utilities.derive.stages; +import static io.optimism.utilities.derive.stages.SpanBatchUtils.putVarLong; + +import com.google.common.primitives.Longs; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.PooledByteBufAllocator; import java.math.BigInteger; +import java.util.Objects; +import org.apache.tuweni.bytes.Bytes; +import org.web3j.utils.Numeric; + +/** + * The type SpanBatchPrefix. + * + * @author grapebaba + * @since 0.2.4 + */ +public class SpanBatchPrefix { + private BigInteger relTimestamp; + private BigInteger l1Origin; + private Bytes parentCheck; + private Bytes l1OriginCheck; + + /** + * Instantiates a new Span batch prefix. + * + * @param relTimestamp the rel timestamp + * @param l1Origin the l 1 origin + * @param parentCheck the parent check + * @param l1OriginCheck the l 1 origin check + */ + public SpanBatchPrefix(BigInteger relTimestamp, BigInteger l1Origin, Bytes parentCheck, Bytes l1OriginCheck) { + this.relTimestamp = relTimestamp; + this.l1Origin = l1Origin; + this.parentCheck = parentCheck; + this.l1OriginCheck = l1OriginCheck; + } + + /** + * Rel timestamp big integer. + * + * @return the big integer + */ + public BigInteger relTimestamp() { + return relTimestamp; + } + + /** + * L 1 origin big integer. + * + * @return the big integer + */ + public BigInteger l1Origin() { + return l1Origin; + } + + /** + * Parent check bytes. + * + * @return the bytes + */ + public Bytes parentCheck() { + return parentCheck; + } + + /** + * L 1 origin check bytes. + * + * @return the bytes + */ + public Bytes l1OriginCheck() { + return l1OriginCheck; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpanBatchPrefix that)) return false; + return Objects.equals(relTimestamp, that.relTimestamp) + && Objects.equals(l1Origin, that.l1Origin) + && Objects.equals(parentCheck, that.parentCheck) + && Objects.equals(l1OriginCheck, that.l1OriginCheck); + } + + @Override + public int hashCode() { + return Objects.hash(relTimestamp, l1Origin, parentCheck, l1OriginCheck); + } + + @Override + public String toString() { + return "SpanBatchPrefix[relTimestamp=%s, l1Origin=%s, parentCheck=%s, l1OriginCheck=%s]" + .formatted(relTimestamp, l1Origin, parentCheck, l1OriginCheck); + } + + /** + * Decode rel timestamp. + * + * @param source the source + */ + public void decodeRelTimestamp(ByteBuf source) { + this.relTimestamp = Numeric.toBigInt(Longs.toByteArray(SpanBatchUtils.getVarLong(source))); + } + + /** + * Encode rel timestamp byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeRelTimestamp() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10); + putVarLong(this.relTimestamp.longValue(), buffer); + + return ByteBufUtil.getBytes(buffer); + } + + /** + * Decode l 1 origin num. + * + * @param source the source + */ + public void decodeL1OriginNum(ByteBuf source) { + this.l1Origin = Numeric.toBigInt(Longs.toByteArray(SpanBatchUtils.getVarLong(source))); + } + + /** + * Encode l 1 origin num byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeL1OriginNum() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10); + putVarLong(this.l1Origin.longValue(), buffer); + + return ByteBufUtil.getBytes(buffer); + } + + /** + * Decode parent check. + * + * @param source the source + */ + public void decodeParentCheck(ByteBuf source) { + this.parentCheck = Bytes.wrapByteBuf(source.readBytes(20)); + } + + /** + * Encode parent check byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeParentCheck() { + return this.parentCheck.toArrayUnsafe(); + } + + /** + * Decode l 1 origin check. + * + * @param source the source + */ + public void decodeL1OriginCheck(ByteBuf source) { + this.l1OriginCheck = Bytes.wrapByteBuf(source.readBytes(20)); + } + + /** + * Encode l 1 origin check byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encodeL1OriginCheck() { + return this.l1OriginCheck.toArrayUnsafe(); + } + + /** + * Decode. + * + * @param source the source + */ + public void decode(ByteBuf source) { + decodeRelTimestamp(source); + decodeL1OriginNum(source); + decodeParentCheck(source); + decodeL1OriginCheck(source); + } + + /** + * Encode byte [ ]. + * + * @return the byte [ ] + */ + public byte[] encode() { + ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); + buffer.writeBytes(encodeRelTimestamp()); + buffer.writeBytes(encodeL1OriginNum()); + buffer.writeBytes(encodeParentCheck()); + buffer.writeBytes(encodeL1OriginCheck()); -public record SpanBatchPrefix(BigInteger relTimestamp, BigInteger l1Origin, String parentCheck, String l1OriginCheck) {} + return ByteBufUtil.getBytes(buffer); + } +} diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchSignature.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchSignature.java index 54332c08..141aea2c 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchSignature.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchSignature.java @@ -3,37 +3,80 @@ import java.math.BigInteger; import java.util.Objects; +/** + * The type SpanBatchSignature. + * + * @author grapebaba + * @since 0.2.4 + */ public class SpanBatchSignature { private BigInteger v; private BigInteger r; private BigInteger s; + /** + * Instantiates a new Span batch signature. + * + * @param v the v + * @param r the r + * @param s the s + */ public SpanBatchSignature(BigInteger v, BigInteger r, BigInteger s) { this.v = v; this.r = r; this.s = s; } + /** + * V big integer. + * + * @return the big integer + */ public BigInteger v() { return v; } + /** + * R big integer. + * + * @return the big integer + */ public BigInteger r() { return r; } + /** + * S big integer. + * + * @return the big integer + */ public BigInteger s() { return s; } + /** + * Sets v. + * + * @param v the v + */ public void setV(BigInteger v) { this.v = v; } + /** + * Sets r. + * + * @param r the r + */ public void setR(BigInteger r) { this.r = r; } + /** + * Sets s. + * + * @param s the s + */ public void setS(BigInteger s) { this.s = s; } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTx.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTx.java index f4cce16b..6278eaef 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTx.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTx.java @@ -101,19 +101,22 @@ public static SpanBatchTx newSpanBatchTx(Transaction tx) { * @return the span batch tx data */ public byte[] marshalBinary() { - if (TransactionType.FRONTIER == spanBatchTxData.txType()) { - SpanBatchLegacyTxData spanBatchLegacyTxData = (SpanBatchLegacyTxData) this.spanBatchTxData; - return spanBatchLegacyTxData.encode(); - } - if (TransactionType.EIP1559 == spanBatchTxData.txType()) { - SpanBatchDynamicFeeTxData spanBatchDynamicFeeTxData = (SpanBatchDynamicFeeTxData) this.spanBatchTxData; - return spanBatchDynamicFeeTxData.encode(); - } - if (TransactionType.ACCESS_LIST == spanBatchTxData.txType()) { - SpanBatchAccessListTxData spanBatchAccessListTxData = (SpanBatchAccessListTxData) this.spanBatchTxData; - return spanBatchAccessListTxData.encode(); + switch (txType()) { + case FRONTIER -> { + SpanBatchLegacyTxData spanBatchLegacyTxData = (SpanBatchLegacyTxData) this.spanBatchTxData; + return spanBatchLegacyTxData.encode(); + } + case EIP1559 -> { + SpanBatchDynamicFeeTxData spanBatchDynamicFeeTxData = (SpanBatchDynamicFeeTxData) this.spanBatchTxData; + return spanBatchDynamicFeeTxData.encode(); + } + case ACCESS_LIST -> { + SpanBatchAccessListTxData spanBatchAccessListTxData = (SpanBatchAccessListTxData) this.spanBatchTxData; + return spanBatchAccessListTxData.encode(); + } + case BLOB -> throw new RuntimeException("blob tx not supported"); + default -> throw new IllegalStateException("unexpected value: %s".formatted(txType())); } - throw new RuntimeException("invalid typed transaction type"); } /** @@ -143,10 +146,27 @@ public static SpanBatchTx unmarshalBinary(byte[] b) { throw new RuntimeException("invalid typed transaction type"); } + /** + * Gets span batch tx data. + * + * @return the span batch tx data + */ public SpanBatchTxData getSpanBatchTxData() { return spanBatchTxData; } + /** + * Convert to full tx transaction. + * + * @param nonce the nonce + * @param gas the gas + * @param to the to + * @param chainId the chain id + * @param v the v + * @param r the r + * @param s the s + * @return the transaction + */ public Transaction convertToFullTx( BigInteger nonce, BigInteger gas, String to, BigInteger chainId, BigInteger v, BigInteger r, BigInteger s) { diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxData.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxData.java index 35f8402a..14c1b0b0 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxData.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxData.java @@ -2,7 +2,18 @@ import org.hyperledger.besu.datatypes.TransactionType; +/** + * The interface SpanBatchTxData. + * + * @author grapebaba + * @since 0.2.4 + */ public interface SpanBatchTxData { + /** + * Tx type transaction type. + * + * @return the transaction type + */ TransactionType txType(); } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxs.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxs.java index 52d9541e..4ffc270e 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxs.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchTxs.java @@ -1,5 +1,7 @@ package io.optimism.utilities.derive.stages; +import static io.optimism.utilities.derive.stages.SpanBatchUtils.getVarLong; +import static io.optimism.utilities.derive.stages.SpanBatchUtils.putVarLong; import static org.hyperledger.besu.ethereum.core.Transaction.REPLAY_PROTECTED_V_BASE; import static org.hyperledger.besu.ethereum.core.Transaction.REPLAY_UNPROTECTED_V_BASE; @@ -23,9 +25,14 @@ import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.web3j.utils.Numeric; +/** + * The type SpanBatchTxs. + * + * @author grapebaba + * @since 0.2.4 + */ public class SpanBatchTxs { - private static final long MaxSpanBatchSize = 10000000L; private long totalBlockTxCount; private BigInteger contractCreationBits; private BigInteger yParityBits; @@ -40,8 +47,26 @@ public class SpanBatchTxs { private BigInteger protectedBits; + /** + * Instantiates a new Span batch txs. + */ public SpanBatchTxs() {} + /** + * Instantiates a new Span batch txs. + * + * @param totalBlockTxCount the total block tx count + * @param contractCreationBits the contract creation bits + * @param yParityBits the y parity bits + * @param txSigs the tx sigs + * @param txNonces the tx nonces + * @param txGases the tx gases + * @param txTos the tx tos + * @param txDatas the tx datas + * @param txTypes the tx types + * @param totalLegacyTxCount the total legacy tx count + * @param protectedBits the protected bits + */ public SpanBatchTxs( Long totalBlockTxCount, BigInteger contractCreationBits, @@ -73,7 +98,7 @@ public SpanBatchTxs( * @return encoded contract creation bits. */ public byte[] encodeContractCreationBits() { - return encodeSpanBatchBits((int) this.totalBlockTxCount, this.contractCreationBits); + return SpanBatchUtils.encodeSpanBatchBits((int) this.totalBlockTxCount, this.contractCreationBits); } /** @@ -82,7 +107,8 @@ public byte[] encodeContractCreationBits() { * @param contractCreationBit encoded contract creation bits. */ public void decodeContractCreationBits(ByteBuf contractCreationBit) { - this.contractCreationBits = decodeSpanBatchBits(contractCreationBit, (int) this.totalBlockTxCount); + this.contractCreationBits = + SpanBatchUtils.decodeSpanBatchBits(contractCreationBit, (int) this.totalBlockTxCount); } /** @@ -91,7 +117,7 @@ public void decodeContractCreationBits(ByteBuf contractCreationBit) { * @return encoded protected bits. */ public byte[] encodeProtectedBits() { - return encodeSpanBatchBits((int) this.totalLegacyTxCount, this.protectedBits); + return SpanBatchUtils.encodeSpanBatchBits((int) this.totalLegacyTxCount, this.protectedBits); } /** @@ -100,7 +126,7 @@ public byte[] encodeProtectedBits() { * @param protectedBit encoded protected bits. */ public void decodeProtectedBits(ByteBuf protectedBit) { - this.protectedBits = decodeSpanBatchBits(protectedBit, (int) this.totalLegacyTxCount); + this.protectedBits = SpanBatchUtils.decodeSpanBatchBits(protectedBit, (int) this.totalLegacyTxCount); } /** @@ -127,7 +153,7 @@ public long contractCreationCount() { * @return encoded y parity bits. */ public byte[] encodeYParityBits() { - return encodeSpanBatchBits((int) this.totalBlockTxCount, this.yParityBits); + return SpanBatchUtils.encodeSpanBatchBits((int) this.totalBlockTxCount, this.yParityBits); } /** @@ -136,9 +162,14 @@ public byte[] encodeYParityBits() { * @param yParityBit encoded y parity bits. */ public void decodeYParityBits(ByteBuf yParityBit) { - this.yParityBits = decodeSpanBatchBits(yParityBit, (int) this.totalBlockTxCount); + this.yParityBits = SpanBatchUtils.decodeSpanBatchBits(yParityBit, (int) this.totalBlockTxCount); } + /** + * Encode tx sigs rs byte [ ]. + * + * @return the byte [ ] + */ public byte[] encodeTxSigsRS() { ByteBuf result = PooledByteBufAllocator.DEFAULT.buffer(64 * txSigs.size()); for (SpanBatchSignature txSig : txSigs) { @@ -150,6 +181,11 @@ public byte[] encodeTxSigsRS() { return ByteBufUtil.getBytes(result); } + /** + * Decode tx sigs rs. + * + * @param txSigsBuffer the tx sigs buffer + */ public void decodeTxSigsRS(ByteBuf txSigsBuffer) { List txSigs = new ArrayList<>(); for (int i = 0; i < totalBlockTxCount; i++) { @@ -163,6 +199,11 @@ public void decodeTxSigsRS(ByteBuf txSigsBuffer) { this.txSigs = txSigs; } + /** + * Encode tx nonces byte [ ]. + * + * @return the byte [ ] + */ public byte[] encodeTxNonces() { ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10 * txNonces.size()); for (BigInteger txNonce : txNonces) { @@ -171,6 +212,11 @@ public byte[] encodeTxNonces() { return ByteBufUtil.getBytes(buffer); } + /** + * Decode tx nonces. + * + * @param txNoncesBuffer the tx nonces buffer + */ public void decodeTxNonces(ByteBuf txNoncesBuffer) { List txNonces = new ArrayList<>(); @@ -181,6 +227,11 @@ public void decodeTxNonces(ByteBuf txNoncesBuffer) { this.txNonces = txNonces; } + /** + * Encode tx gases byte [ ]. + * + * @return the byte [ ] + */ public byte[] encodeTxGases() { ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(10 * txGases.size()); for (BigInteger txGas : txGases) { @@ -189,6 +240,11 @@ public byte[] encodeTxGases() { return ByteBufUtil.getBytes(buffer); } + /** + * Decode tx gases. + * + * @param gases the gases + */ public void decodeTxGases(ByteBuf gases) { List txNonces = new ArrayList<>(); @@ -199,6 +255,11 @@ public void decodeTxGases(ByteBuf gases) { this.txGases = txNonces; } + /** + * Encode tx tos byte [ ]. + * + * @return the byte [ ] + */ public byte[] encodeTxTos() { ByteBuf result = PooledByteBufAllocator.DEFAULT.buffer(20 * txTos.size()); for (String txTo : txTos) { @@ -208,6 +269,11 @@ public byte[] encodeTxTos() { return ByteBufUtil.getBytes(result); } + /** + * Decode tx tos. + * + * @param txTosBuffer the tx tos buffer + */ public void decodeTxTos(ByteBuf txTosBuffer) { List txTos = new ArrayList<>(); var contractCreationCount = contractCreationCount(); @@ -219,6 +285,11 @@ public void decodeTxTos(ByteBuf txTosBuffer) { this.txTos = txTos; } + /** + * Encode tx datas byte [ ]. + * + * @return the byte [ ] + */ public byte[] encodeTxDatas() { ByteBuf result = PooledByteBufAllocator.DEFAULT.buffer(); for (Bytes txData : txDatas) { @@ -227,22 +298,22 @@ public byte[] encodeTxDatas() { return ByteBufUtil.getBytes(result); } + /** + * Decode tx datas. + * + * @param txDatasBuffer the tx datas buffer + */ public void decodeTxDatas(ByteBuf txDatasBuffer) { List txDatas = new ArrayList<>(); List txTypes = new ArrayList<>(); - System.out.println(txDatasBuffer.readerIndex()); for (int i = 0; i < totalBlockTxCount; i++) { Bytes remaining = Bytes.wrapByteBuf(txDatasBuffer, txDatasBuffer.readerIndex(), txDatasBuffer.readableBytes()); - System.out.println(i); - if (i == 1886) { - System.out.println("here"); - } + RLPInput input = new BytesValueRLPInput(remaining, false, false); var tx = readTxData(input, i == totalBlockTxCount - 1); Bytes txData = tx.getLeft(); - System.out.println(txData.toHexString()); TransactionType txType = tx.getRight(); txDatas.add(txData); txTypes.add(txType); @@ -250,14 +321,17 @@ public void decodeTxDatas(ByteBuf txDatasBuffer) { totalLegacyTxCount++; } txDatasBuffer.readerIndex(txDatasBuffer.readerIndex() + txData.size()); - System.out.println(txDatasBuffer.readerIndex()); } - System.out.println(txDatasBuffer.readerIndex()); this.txDatas = txDatas; this.txTypes = txTypes; } + /** + * Recover v. + * + * @param chainId the chain id + */ public void recoverV(BigInteger chainId) { if (this.txTypes.size() != this.txSigs.size()) { throw new RuntimeException("tx type length and tx sigs length mismatch"); @@ -291,6 +365,11 @@ public void recoverV(BigInteger chainId) { } } + /** + * Encode byte [ ]. + * + * @return the byte [ ] + */ public byte[] encode() { ByteBuf result = PooledByteBufAllocator.DEFAULT.buffer(); result.writeBytes(encodeContractCreationBits()); @@ -304,6 +383,11 @@ public byte[] encode() { return ByteBufUtil.getBytes(result); } + /** + * Decode. + * + * @param buffer the buffer + */ public void decode(ByteBuf buffer) { decodeContractCreationBits(buffer); decodeYParityBits(buffer); @@ -315,6 +399,12 @@ public void decode(ByteBuf buffer) { decodeProtectedBits(buffer); } + /** + * Full txs list. + * + * @param chainId the chain id + * @return the list + */ public List fullTxs(BigInteger chainId) { List fullTxs = new ArrayList<>(); int toIdx = 0; @@ -341,6 +431,13 @@ public List fullTxs(BigInteger chainId) { return fullTxs; } + /** + * New span batch txs span batch txs. + * + * @param txs the txs + * @param chainId the chain id + * @return the span batch txs + */ public static SpanBatchTxs newSpanBatchTxs(List txs, BigInteger chainId) { long totalBlockTxCount = txs.size(); BigInteger contractCreationBits = BigInteger.ZERO; @@ -413,90 +510,201 @@ public static SpanBatchTxs newSpanBatchTxs(List txs, BigInteger chainId) protectedBits); } + /** + * Gets contract creation bits. + * + * @return the contract creation bits + */ public BigInteger getContractCreationBits() { return contractCreationBits; } + /** + * Sets contract creation bits. + * + * @param contractCreationBits the contract creation bits + */ public void setContractCreationBits(BigInteger contractCreationBits) { this.contractCreationBits = contractCreationBits; } + /** + * Gets parity bits. + * + * @return the parity bits + */ public BigInteger getyParityBits() { return yParityBits; } + /** + * Sets parity bits. + * + * @param yParityBits the y parity bits + */ public void setyParityBits(BigInteger yParityBits) { this.yParityBits = yParityBits; } + /** + * Gets tx sigs. + * + * @return the tx sigs + */ public List getTxSigs() { return txSigs; } + /** + * Sets tx sigs. + * + * @param txSigs the tx sigs + */ public void setTxSigs(List txSigs) { this.txSigs = txSigs; } + /** + * Gets tx nonces. + * + * @return the tx nonces + */ public List getTxNonces() { return txNonces; } + /** + * Sets tx nonces. + * + * @param txNonces the tx nonces + */ public void setTxNonces(List txNonces) { this.txNonces = txNonces; } + /** + * Gets tx gases. + * + * @return the tx gases + */ public List getTxGases() { return txGases; } + /** + * Sets tx gases. + * + * @param txGases the tx gases + */ public void setTxGases(List txGases) { this.txGases = txGases; } + /** + * Gets tx tos. + * + * @return the tx tos + */ public List getTxTos() { return txTos; } + /** + * Sets tx tos. + * + * @param txTos the tx tos + */ public void setTxTos(List txTos) { this.txTos = txTos; } + /** + * Gets tx datas. + * + * @return the tx datas + */ public List getTxDatas() { return txDatas; } + /** + * Sets tx datas. + * + * @param txDatas the tx datas + */ public void setTxDatas(List txDatas) { this.txDatas = txDatas; } + /** + * Gets tx types. + * + * @return the tx types + */ public List getTxTypes() { return txTypes; } + /** + * Sets tx types. + * + * @param txTypes the tx types + */ public void setTxTypes(List txTypes) { this.txTypes = txTypes; } + /** + * Sets total block tx count. + * + * @param totalBlockTxCount the total block tx count + */ public void setTotalBlockTxCount(long totalBlockTxCount) { this.totalBlockTxCount = totalBlockTxCount; } + /** + * Gets total block tx count. + * + * @return the total block tx count + */ public long getTotalBlockTxCount() { return totalBlockTxCount; } + /** + * Gets protected bits. + * + * @return the protected bits + */ public BigInteger getProtectedBits() { return protectedBits; } + /** + * Gets total legacy tx count. + * + * @return the total legacy tx count + */ public long getTotalLegacyTxCount() { return totalLegacyTxCount; } + /** + * Sets total legacy tx count. + * + * @param totalLegacyTxCount the total legacy tx count + */ public void setTotalLegacyTxCount(long totalLegacyTxCount) { this.totalLegacyTxCount = totalLegacyTxCount; } + /** + * Convert v to y parity big integer. + * + * @param transaction the transaction + * @return the big integer + */ public static BigInteger convertVToYParity(Transaction transaction) { switch (transaction.getType()) { case FRONTIER -> { @@ -513,43 +721,13 @@ public static BigInteger convertVToYParity(Transaction transaction) { } } - public static BigInteger decodeSpanBatchBits(ByteBuf source, int bitLength) { - var bufLen = bitLength / 8; - if (bitLength % 8 != 0) { - bufLen++; - } - if (bufLen > MaxSpanBatchSize) { - throw new RuntimeException("span batch size limit reached"); - } - byte[] buf = new byte[(int) bufLen]; - try { - source.readBytes(buf); - } catch (Exception e) { - throw new RuntimeException("read error"); - } - var res = Numeric.toBigInt(buf); - if (res.bitLength() > bitLength) { - throw new RuntimeException("invalid bit length"); - } - return res; - } - - public static byte[] encodeSpanBatchBits(int bitLength, BigInteger bits) { - if (bits.bitLength() > bitLength) { - throw new RuntimeException( - "bitfield is larger than bitLength: %d > %d".formatted(bits.bitLength(), bitLength)); - } - - var bufLen = bitLength / 8; - if (bitLength % 8 != 0) { - bufLen++; - } - if (bufLen > MaxSpanBatchSize) { - throw new RuntimeException("span batch size limit reached"); - } - return Numeric.toBytesPadded(bits, bufLen); - } - + /** + * Read tx data pair. + * + * @param input the input + * @param isLast the is last + * @return the pair + */ public static Pair readTxData(RLPInput input, boolean isLast) { if (isTypedTransaction(input)) { final Bytes typedTransactionBytes = input.readBytes(); @@ -562,6 +740,12 @@ public static Pair readTxData(RLPInput input, boolean is } } + /** + * Last current list as bytes bytes. + * + * @param input the input + * @return the bytes + */ public static Bytes lastCurrentListAsBytes(RLPInput input) { if (!input.nextIsList()) { throw new RuntimeException("Cannot read list, current item is not a list list"); @@ -609,83 +793,4 @@ private static Optional getTransactionType(final Bytes opaqueBy return Optional.empty(); } } - - /** - * Reads an up to 64 bit long varint from the current position of the - * given ByteBuffer and returns the decoded value as long. - * - *

The position of the buffer is advanced to the first byte after the - * decoded varint. - * - * @param src the ByteBuffer to get the var int from - * @return The integer value of the decoded long varint - */ - public static long getVarLong(ByteBuf src) { - long tmp; - if ((tmp = src.readByte()) >= 0) { - return tmp; - } - long result = tmp & 0x7f; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 7; - } else { - result |= (tmp & 0x7f) << 7; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 14; - } else { - result |= (tmp & 0x7f) << 14; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 21; - } else { - result |= (tmp & 0x7f) << 21; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 28; - } else { - result |= (tmp & 0x7f) << 28; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 35; - } else { - result |= (tmp & 0x7f) << 35; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 42; - } else { - result |= (tmp & 0x7f) << 42; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 49; - } else { - result |= (tmp & 0x7f) << 49; - if ((tmp = src.readByte()) >= 0) { - result |= tmp << 56; - } else { - result |= (tmp & 0x7f) << 56; - result |= ((long) src.readByte()) << 63; - } - } - } - } - } - } - } - } - return result; - } - - /** - * Encodes a long integer in a variable-length encoding, 7 bits per byte, to a - * ByteBuffer sink. - * - * @param v the value to encode - * @param sink the ByteBuffer to add the encoded value - */ - public static void putVarLong(long v, ByteBuf sink) { - while (true) { - int bits = ((int) v) & 0x7f; - v >>>= 7; - if (v == 0) { - sink.writeByte((byte) bits); - return; - } - sink.writeByte((byte) (bits | 0x80)); - } - } } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchUtils.java b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchUtils.java new file mode 100644 index 00000000..9caa9e79 --- /dev/null +++ b/hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchUtils.java @@ -0,0 +1,154 @@ +package io.optimism.utilities.derive.stages; + +import io.netty.buffer.ByteBuf; +import java.math.BigInteger; +import org.web3j.utils.Numeric; + +/** + * The type SpanBatchUtils. + * + * @author grapebaba + * @since 0.2.4 + */ +public class SpanBatchUtils { + + /** + * The constant MaxSpanBatchSize. + */ + public static final long MaxSpanBatchSize = 10000000L; + + /** + * Instantiates a new Span batch utils. + */ + public SpanBatchUtils() {} + + /** + * Decode span batch bits big integer. + * + * @param source the source + * @param bitLength the bit length + * @return the big integer + */ + public static BigInteger decodeSpanBatchBits(ByteBuf source, int bitLength) { + var bufLen = bitLength / 8; + if (bitLength % 8 != 0) { + bufLen++; + } + if (bufLen > MaxSpanBatchSize) { + throw new RuntimeException("span batch size limit reached"); + } + byte[] buf = new byte[(int) bufLen]; + try { + source.readBytes(buf); + } catch (Exception e) { + throw new RuntimeException("read error"); + } + var res = Numeric.toBigInt(buf); + if (res.bitLength() > bitLength) { + throw new RuntimeException("invalid bit length"); + } + return res; + } + + /** + * Encode span batch bits byte [ ]. + * + * @param bitLength the bit length + * @param bits the bits + * @return the byte [ ] + */ + public static byte[] encodeSpanBatchBits(int bitLength, BigInteger bits) { + if (bits.bitLength() > bitLength) { + throw new RuntimeException( + "bitfield is larger than bitLength: %d > %d".formatted(bits.bitLength(), bitLength)); + } + + var bufLen = bitLength / 8; + if (bitLength % 8 != 0) { + bufLen++; + } + if (bufLen > MaxSpanBatchSize) { + throw new RuntimeException("span batch size limit reached"); + } + return Numeric.toBytesPadded(bits, bufLen); + } + + /** + * Reads an up to 64 bit long varint from the current position of the + * given ByteBuffer and returns the decoded value as long. + * + *

The position of the buffer is advanced to the first byte after the + * decoded varint. + * + * @param src the ByteBuffer to get the var int from + * @return The integer value of the decoded long varint + */ + public static long getVarLong(ByteBuf src) { + long tmp; + if ((tmp = src.readByte()) >= 0) { + return tmp; + } + long result = tmp & 0x7f; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 7; + } else { + result |= (tmp & 0x7f) << 7; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 14; + } else { + result |= (tmp & 0x7f) << 14; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 21; + } else { + result |= (tmp & 0x7f) << 21; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 28; + } else { + result |= (tmp & 0x7f) << 28; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 35; + } else { + result |= (tmp & 0x7f) << 35; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 42; + } else { + result |= (tmp & 0x7f) << 42; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 49; + } else { + result |= (tmp & 0x7f) << 49; + if ((tmp = src.readByte()) >= 0) { + result |= tmp << 56; + } else { + result |= (tmp & 0x7f) << 56; + result |= ((long) src.readByte()) << 63; + } + } + } + } + } + } + } + } + return result; + } + + /** + * Encodes a long integer in a variable-length encoding, 7 bits per byte, to a + * ByteBuffer sink. + * + * @param v the value to encode + * @param sink the ByteBuffer to add the encoded value + */ + public static void putVarLong(long v, ByteBuf sink) { + while (true) { + int bits = ((int) v) & 0x7f; + v >>>= 7; + if (v == 0) { + sink.writeByte((byte) bits); + return; + } + sink.writeByte((byte) (bits | 0x80)); + } + } +} diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/gas/GasCalculator.java b/hildr-utilities/src/main/java/io/optimism/utilities/gas/GasCalculator.java index 26c97721..8ca45111 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/gas/GasCalculator.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/gas/GasCalculator.java @@ -32,16 +32,34 @@ public class GasCalculator { */ private GasCalculator() {} + /** + * The constant TX_GAS_CONTRACT_CREATION. + */ public static final long TX_GAS_CONTRACT_CREATION = 53000L; + /** + * The constant TX_GAS. + */ public static final long TX_GAS = 21000L; + /** + * The constant TX_DATA_NON_ZERO_GAS_FRONTIER. + */ public static final long TX_DATA_NON_ZERO_GAS_FRONTIER = 68L; + /** + * The constant TX_DATA_NON_ZERO_GAS_EIP2028. + */ public static final long TX_DATA_NON_ZERO_GAS_EIP2028 = 16L; + /** + * The constant TX_DATA_ZERO_GAS. + */ public static final long TX_DATA_ZERO_GAS = 4L; + /** + * The constant INIT_CODE_WORD_GAS. + */ public static final long INIT_CODE_WORD_GAS = 2L; /** diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/MetricsServer.java b/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/MetricsServer.java index 4a6a4830..0cdb74af 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/MetricsServer.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/MetricsServer.java @@ -56,7 +56,8 @@ public static MeterRegistry createPrometheusRegistry() { /** * start a http server for prometheus to access. * - * @param port custom http server port + * @param registry prometheus registry instance + * @param port custom http server port */ public static void start(MeterRegistry registry, int port) { if (!(registry instanceof PrometheusMeterRegistry)) { @@ -90,7 +91,9 @@ public static void start(MeterRegistry registry, int port) { } } - /** stop the http server. */ + /** + * stop the http server. + */ public static void stop() { if (!isActive()) { return; @@ -117,6 +120,11 @@ public static void stop() { } } + /** + * Is active boolean. + * + * @return the boolean + */ public static boolean isActive() { return serverFuture != null && !(serverFuture.isDone() || serverFuture.isCancelled()); } diff --git a/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/TracerTaskWrapper.java b/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/TracerTaskWrapper.java index 50179bd3..5d7663be 100644 --- a/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/TracerTaskWrapper.java +++ b/hildr-utilities/src/main/java/io/optimism/utilities/telemetry/TracerTaskWrapper.java @@ -33,9 +33,16 @@ public class TracerTaskWrapper { private static Function tracerSupplier; - /** Instantiates a new Tracer task wrapper. */ + /** + * Instantiates a new Tracer task wrapper. + */ private TracerTaskWrapper() {} + /** + * Sets tracer supplier. + * + * @param supplier the supplier + */ public static void setTracerSupplier(Function supplier) { TracerTaskWrapper.tracerSupplier = supplier; } @@ -43,7 +50,7 @@ public static void setTracerSupplier(Function supplier) { /** * Wrap callable. It Will use default tracer name for tracer. * - * @param the type parameter + * @param the type parameter * @param call the call * @return the callable */ @@ -54,8 +61,9 @@ public static Callable wrap(Callable call) { /** * Wrap callable. * - * @param the type parameter - * @param call the call + * @param the type parameter + * @param tracerName the tracer name + * @param call the call * @return the callable */ public static Callable wrap(final String tracerName, final Callable call) { diff --git a/hildr-utilities/src/test/java/io/optimism/utilities/derive/stages/SpanBatchTxsTest.java b/hildr-utilities/src/test/java/io/optimism/utilities/derive/stages/SpanBatchTxsTest.java index 9c7cc43d..0dd6125e 100644 --- a/hildr-utilities/src/test/java/io/optimism/utilities/derive/stages/SpanBatchTxsTest.java +++ b/hildr-utilities/src/test/java/io/optimism/utilities/derive/stages/SpanBatchTxsTest.java @@ -22,9 +22,9 @@ public class SpanBatchTxsTest { void decodeAndEncodeSpanBatchBits() { String test = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - BigInteger res = SpanBatchTxs.decodeSpanBatchBits( + BigInteger res = SpanBatchUtils.decodeSpanBatchBits( Unpooled.wrappedBuffer(Bytes.fromHexString(test).toArray()), 544); - var res1 = SpanBatchTxs.encodeSpanBatchBits(544, res); + var res1 = SpanBatchUtils.encodeSpanBatchBits(544, res); assertArrayEquals(Numeric.hexStringToByteArray(test), res1); }