Skip to content

Commit

Permalink
feat:raw to span
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <281165273grape@gmail.com>
  • Loading branch information
GrapeBaBa committed Jan 24, 2024
1 parent d3f3284 commit d420a58
Show file tree
Hide file tree
Showing 11 changed files with 887 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static List<Batch> decodeBatches(Channel channel) {
byte[] batchData = ArrayUtils.subarray(
((RlpString) rlpType).getBytes(), 1, ((RlpString) rlpType).getBytes().length);
RlpList rlpBatchData =
(RlpList) RlpDecoder.decode(batchData).getValues().get(0);
(RlpList) RlpDecoder.decode(batchData).getValues().getFirst();
return Batch.decode(rlpBatchData, channel.l1InclusionBlock());
})
.collect(Collectors.toList());
Expand Down
3 changes: 3 additions & 0 deletions hildr-utilities/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ dependencies {
api 'io.opentelemetry:opentelemetry-sdk'
api 'io.opentelemetry:opentelemetry-sdk-logs'

implementation 'com.fasterxml.jackson:jackson-bom:2.15.2'
implementation 'com.fasterxml.jackson.core:jackson-core'

implementation 'ch.qos.logback:logback-core:1.4.7'
implementation 'ch.qos.logback:logback-classic:1.4.7'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import io.netty.buffer.ByteBuf;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.web3j.utils.Numeric;

/**
* The type RawSpanBatch.
Expand All @@ -11,8 +14,16 @@
* @since 0.2.4
*/
public class RawSpanBatch implements IBatch {
private final SpanBatchPrefix spanbatchPrefix;
private final SpanBatchPayload spanbatchPayload;
private SpanBatchPrefix spanbatchPrefix;
private SpanBatchPayload spanbatchPayload;

/**
* Instantiates a new Raw span batch.
*/
public RawSpanBatch() {
this.spanbatchPrefix = new SpanBatchPrefix();
this.spanbatchPayload = new SpanBatchPayload();
}

/**
* Instantiates a new Raw span batch.
Expand Down Expand Up @@ -83,4 +94,66 @@ public void decode(ByteBuf source) {
this.spanbatchPrefix.decode(source);
this.spanbatchPayload.decode(source);
}

/**
* Derive span batch.
*
* @param blockTime the block time
* @param genesisTimestamp the genesis timestamp
* @param chainID the chain id
* @return the span batch
*/
public SpanBatch derive(BigInteger blockTime, BigInteger genesisTimestamp, BigInteger chainID) {
if (this.spanbatchPayload.blockCount() == 0) {
throw new IllegalArgumentException("SpanBatch block count cannot be zero");
}

BigInteger[] blockOriginNums = new BigInteger[(int) this.spanbatchPayload.blockCount()];

BigInteger l1OriginBlockNumber = this.spanbatchPrefix.l1OriginNum();

for (int i = (int) (this.spanbatchPayload.blockCount() - 1); i >= 0; i--) {
blockOriginNums[i] = l1OriginBlockNumber;
if (this.spanbatchPayload.originBits().testBit(i) && i > 0) {
l1OriginBlockNumber = l1OriginBlockNumber.subtract(BigInteger.ONE);
}
}

this.spanbatchPayload.txs().recoverV(chainID);
List<byte[]> fullTxs = this.spanbatchPayload.txs().fullTxs(chainID);

SpanBatch spanBatch = new SpanBatch();
spanBatch.setParentCheck(this.spanbatchPrefix.parentCheck());
spanBatch.setL1OriginCheck(this.spanbatchPrefix.l1OriginCheck());

int txIndex = 0;
for (int i = 0; i < this.spanbatchPayload.blockCount(); i++) {
SpanBatchElement spanBatchElement = new SpanBatchElement();
spanBatchElement.setTimestamp(genesisTimestamp
.add(this.spanbatchPrefix.relTimestamp())
.add(blockTime.multiply(BigInteger.valueOf(i))));
spanBatchElement.setEpochNum(blockOriginNums[i]);
List<String> txs = new ArrayList<>();
for (int j = 0; j < this.spanbatchPayload.blockTxCounts().get(i); j++) {
txs.add(Numeric.toHexString(fullTxs.get(txIndex)));
txIndex++;
}
spanBatchElement.setTransactions(txs);
spanBatch.getBatches().add(spanBatchElement);
}

return spanBatch;
}

/**
* To span batch span batch.
*
* @param blockTime the block time
* @param genesisTimestamp the genesis timestamp
* @param chainID the chain id
* @return the span batch
*/
public SpanBatch toSpanBatch(BigInteger blockTime, BigInteger genesisTimestamp, BigInteger chainID) {
return derive(blockTime, genesisTimestamp, chainID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.optimism.type.BlockId;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
Expand All @@ -12,17 +13,38 @@
/**
* 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<String> transactions)
implements IBatch {
public class SingularBatch implements IBatch {
private String parentHash;
private BigInteger epochNum;
private String epochHash;
private BigInteger timestamp;
private List<String> transactions;

/**
* Instantiates a new Singular batch.
*/
public SingularBatch() {}

/**
* Instantiates a new Singular batch.
*
* @param parentHash the parent hash
* @param epochNum the epoch num
* @param epochHash the epoch hash
* @param timestamp the timestamp
* @param transactions the transactions
*/
public SingularBatch(
String parentHash, BigInteger epochNum, String epochHash, BigInteger timestamp, List<String> transactions) {
this.parentHash = parentHash;
this.epochNum = epochNum;
this.epochHash = epochHash;
this.timestamp = timestamp;
this.transactions = transactions;
}

/**
* Epoch block id.
Expand Down Expand Up @@ -86,4 +108,125 @@ public BigInteger getTimestamp() {
public BigInteger getEpochNum() {
return epochNum();
}

/**
* To span batch element span batch element.
*
* @return the span batch element
*/
public SpanBatchElement toSpanBatchElement() {
return new SpanBatchElement(epochNum(), timestamp(), transactions());
}

/**
* Parent hash string.
*
* @return the string
*/
public String parentHash() {
return parentHash;
}

/**
* Epoch num big integer.
*
* @return the big integer
*/
public BigInteger epochNum() {
return epochNum;
}

/**
* Epoch hash string.
*
* @return the string
*/
public String epochHash() {
return epochHash;
}

/**
* Timestamp big integer.
*
* @return the big integer
*/
public BigInteger timestamp() {
return timestamp;
}

/**
* Transactions list.
*
* @return the list
*/
public List<String> transactions() {
return transactions;
}

/**
* Sets parent hash.
*
* @param parentHash the parent hash
*/
public void setParentHash(String parentHash) {
this.parentHash = parentHash;
}

/**
* Sets epoch num.
*
* @param epochNum the epoch num
*/
public void setEpochNum(BigInteger epochNum) {
this.epochNum = epochNum;
}

/**
* Sets epoch hash.
*
* @param epochHash the epoch hash
*/
public void setEpochHash(String epochHash) {
this.epochHash = epochHash;
}

/**
* Sets timestamp.
*
* @param timestamp the timestamp
*/
public void setTimestamp(BigInteger timestamp) {
this.timestamp = timestamp;
}

/**
* Sets transactions.
*
* @param transactions the transactions
*/
public void setTransactions(List<String> transactions) {
this.transactions = transactions;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SingularBatch that)) return false;
return Objects.equals(parentHash, that.parentHash)
&& Objects.equals(epochNum, that.epochNum)
&& Objects.equals(epochHash, that.epochHash)
&& Objects.equals(timestamp, that.timestamp)
&& Objects.equals(transactions, that.transactions);
}

@Override
public int hashCode() {
return Objects.hash(parentHash, epochNum, epochHash, timestamp, transactions);
}

@Override
public String toString() {
return "SingularBatch[parentHash=%s, epochNum=%s, epochHash=%s, timestamp=%s, transactions=%s]"
.formatted(parentHash, epochNum, epochHash, timestamp, transactions);
}
}
Loading

0 comments on commit d420a58

Please sign in to comment.