Skip to content

Commit

Permalink
feat:span batch txs/tx
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 3, 2024
1 parent bdb579b commit c7a388d
Show file tree
Hide file tree
Showing 19 changed files with 1,123 additions and 713 deletions.
4 changes: 2 additions & 2 deletions hildr-node/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ dependencies {
implementation('tech.pegasys.teku.internal:serviceutils:23.10.0')
implementation('tech.pegasys.teku.internal:unsigned:23.10.0')
implementation('tech.pegasys.teku.internal:statetransition:23.10.0')
implementation('org.hyperledger.besu.internal:metrics-core:22.10.4')
implementation('org.hyperledger.besu:plugin-api:22.10.4')
implementation('org.hyperledger.besu.internal:metrics-core:23.10.2')
implementation('org.hyperledger.besu:plugin-api:23.10.2')
implementation('io.libp2p:jvm-libp2p:1.0.1-RELEASE')
implementation 'io.tmio:tuweni-units:2.4.2'
implementation('io.tmio:tuweni-crypto:2.4.2'){
Expand Down
13 changes: 9 additions & 4 deletions hildr-utilities/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {

api("com.squareup.okhttp3:okhttp:5.0.0-alpha.2")
api("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2")
api('org.web3j:core:4.9.8') {
api('org.web3j:core:4.10.3') {
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
exclude group: 'com.squareup.okhttp3', module: 'logging-interceptor'
Expand All @@ -79,16 +79,21 @@ dependencies {
implementation 'ch.qos.logback:logback-core:1.4.7'
implementation 'ch.qos.logback:logback-classic:1.4.7'


implementation 'com.google.protobuf:protobuf-java:3.25.1'
implementation 'io.tmio:tuweni-units:2.4.2'
implementation 'io.tmio:tuweni-rlp:2.4.2'
implementation 'org.bouncycastle:bcprov-jdk18on:1.76'
implementation 'org.slf4j:slf4j-api:2.0.7'
implementation 'io.libp2p:jvm-libp2p:1.0.1-RELEASE'
implementation('org.hyperledger.besu.internal:core:23.10.2')
implementation('org.hyperledger.besu.internal:algorithms:23.10.2')
implementation('org.hyperledger.besu.internal:rlp:23.10.2')
implementation('org.hyperledger.besu:besu-datatypes:23.10.2')
implementation('org.hyperledger.besu:plugin-api:23.10.2')

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'

testImplementation 'io.tmio:tuweni-ssz:2.4.2'
testImplementation 'io.tmio:tuweni-units:2.4.2'
testImplementation('io.tmio:tuweni-crypto:2.4.2'){
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.optimism.utilities.derive.stages;

public record RawSpanBatch(SpanBatchPrefix spanbatchPrefix, SpanBatchPayload spanbatchPayload) {}
// package io.optimism.utilities.derive.stages;
//
// public record RawSpanBatch(SpanBatchPrefix spanbatchPrefix, SpanBatchPayload spanbatchPayload) {}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public int getBlockCount() {
* @param singularBatch SingularBatch
*/
public void AppendSingularBatch(SingularBatch singularBatch) {
if (batches.size() == 0) {
if (batches.isEmpty()) {
this.parentCheck = singularBatch.parentHash().substring(0, 20);
}
this.batches.add(SpanBatchElement.singularBatchToElement(singularBatch)); // add the batch to the list
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
package io.optimism.utilities.derive.stages;

import java.math.BigInteger;
import java.util.ArrayList;
import static org.hyperledger.besu.ethereum.core.encoding.AccessListTransactionEncoder.writeAccessList;

import java.util.List;
import java.util.stream.Collectors;
import org.web3j.crypto.AccessListObject;
import org.web3j.crypto.transaction.type.TransactionType;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
import org.web3j.rlp.RlpType;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.AccessListEntry;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLPInput;

/**
* EIP-2930.
* The type SpanBatchAccessListTxData.
*
* @author grapebaba
* @since 0.2.4
*/
public record SpanBatchAccessListTxData(
BigInteger value, BigInteger gasPrice, String data, List<AccessListObject> accessList)
public record SpanBatchAccessListTxData(Wei value, Wei gasPrice, Bytes data, List<AccessListEntry> accessList)
implements SpanBatchTxData {

@Override
public byte txType() {
return TransactionType.EIP2930.getRlpType();
public TransactionType txType() {
return TransactionType.ACCESS_LIST;
}

/**
* Encode byte [ ].
*
* @return the byte [ ]
*/
public byte[] encode() {
List<RlpType> rlpList = new ArrayList<>();
for (AccessListObject access : accessList) {
rlpList.add(RlpString.create(access.getAddress()));
rlpList.add(new RlpList(
access.getStorageKeys().stream().map(RlpString::create).collect(Collectors.toList())));
}
return RlpEncoder.encode(new RlpList(
RlpString.create(value()),
RlpString.create(gasPrice()),
RlpString.create(data()),
new RlpList(rlpList)));
BytesValueRLPOutput out = new BytesValueRLPOutput();
out.writeByte(txType().getEthSerializedType());
out.startList();
out.writeUInt256Scalar(value());
out.writeUInt256Scalar(gasPrice());
out.writeBytes(data());
writeAccessList(out, Optional.ofNullable(accessList()));
out.endList();
return out.encoded().toArrayUnsafe();
}

public static SpanBatchAccessListTxData decode(RlpList rlp) {
BigInteger value = ((RlpString) rlp.getValues().get(0)).asPositiveBigInteger();
BigInteger gasPrice = ((RlpString) rlp.getValues().get(1)).asPositiveBigInteger();
String data = ((RlpString) rlp.getValues().get(2)).asString();
List<AccessListObject> accessObjList = new ArrayList<>();
((RlpList) rlp.getValues().get(3)).getValues().forEach(rlpType -> {
RlpList rlpList = (RlpList) rlpType;
String address = ((RlpString) rlpList.getValues().get(0)).asString();
List<String> storageKeys = ((RlpList) rlpList.getValues().get(1))
.getValues().stream()
.map(stKey -> ((RlpString) stKey).asString())
.collect(Collectors.toList());
accessObjList.add(new AccessListObject(address, storageKeys));
/**
* Decode span batch access list tx data.
*
* @param input the input
* @return the span batch access list tx data
*/
public static SpanBatchAccessListTxData decode(RLPInput input) {
input.enterList();
Wei value = Wei.of(input.readUInt256Scalar());
Wei gasPrice = Wei.of(input.readUInt256Scalar());
Bytes data = input.readBytes();
List<AccessListEntry> accessList = input.readList(accessListEntryRLPInput -> {
accessListEntryRLPInput.enterList();
final AccessListEntry accessListEntry = new AccessListEntry(
Address.wrap(accessListEntryRLPInput.readBytes()),
accessListEntryRLPInput.readList(RLPInput::readBytes32));
accessListEntryRLPInput.leaveList();
return accessListEntry;
});
return new SpanBatchAccessListTxData(value, gasPrice, data, accessObjList);
input.leaveList();
return new SpanBatchAccessListTxData(value, gasPrice, data, accessList);
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
package io.optimism.utilities.derive.stages;

import java.math.BigInteger;
import java.util.ArrayList;
import static org.hyperledger.besu.ethereum.core.encoding.AccessListTransactionEncoder.writeAccessList;

import java.util.List;
import java.util.stream.Collectors;
import org.web3j.crypto.AccessListObject;
import org.web3j.crypto.transaction.type.TransactionType;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
import org.web3j.rlp.RlpType;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.AccessListEntry;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLPInput;

/**
* The type SpanBatchDynamicFeeTxData.
*
* @author grapebaba
* @since 0.2.4
*/
public record SpanBatchDynamicFeeTxData(
BigInteger value, BigInteger gasTipCap, BigInteger gasFeeCap, String data, List<AccessListObject> accessList)
Wei value, Wei gasTipCap, Wei gasFeeCap, Bytes data, List<AccessListEntry> accessList)
implements SpanBatchTxData {
@Override
public byte txType() {
return TransactionType.EIP1559.getRlpType();
public TransactionType txType() {
return TransactionType.EIP1559;
}

/**
* Encode byte [ ].
*
* @return the byte [ ]
*/
public byte[] encode() {
List<RlpType> rlpList = new ArrayList<>();
for (AccessListObject access : accessList) {
rlpList.add(RlpString.create(access.getAddress()));
rlpList.add(new RlpList(
access.getStorageKeys().stream().map(RlpString::create).collect(Collectors.toList())));
}
return RlpEncoder.encode(new RlpList(
RlpString.create(value()),
RlpString.create(gasTipCap()),
RlpString.create(gasFeeCap()),
RlpString.create(data()),
new RlpList(rlpList)));
BytesValueRLPOutput out = new BytesValueRLPOutput();
out.writeByte(txType().getEthSerializedType());
out.startList();
out.writeUInt256Scalar(value());
out.writeUInt256Scalar(gasTipCap());
out.writeUInt256Scalar(gasFeeCap());
out.writeBytes(data());
writeAccessList(out, Optional.ofNullable(accessList()));
out.endList();
return out.encoded().toArrayUnsafe();
}

public static SpanBatchDynamicFeeTxData decode(RlpList rlp) {
BigInteger value = ((RlpString) rlp.getValues().get(0)).asPositiveBigInteger();
BigInteger gasTipCap = ((RlpString) rlp.getValues().get(1)).asPositiveBigInteger();
BigInteger gasFeeCap = ((RlpString) rlp.getValues().get(2)).asPositiveBigInteger();
String data = ((RlpString) rlp.getValues().get(2)).asString();
List<AccessListObject> accessObjList = new ArrayList<>();
((RlpList) rlp.getValues().get(3)).getValues().forEach(rlpType -> {
RlpList rlpList = (RlpList) rlpType;
String address = ((RlpString) rlpList.getValues().get(0)).asString();
List<String> storageKeys = ((RlpList) rlpList.getValues().get(1))
.getValues().stream()
.map(stKey -> ((RlpString) stKey).asString())
.collect(Collectors.toList());
accessObjList.add(new AccessListObject(address, storageKeys));
/**
* Decode span batch dynamic fee tx data.
*
* @param input the input
* @return the span batch dynamic fee tx data
*/
public static SpanBatchDynamicFeeTxData decode(RLPInput input) {
input.enterList();
Wei value = Wei.of(input.readUInt256Scalar());
Wei gasTipCap = Wei.of(input.readUInt256Scalar());
Wei gasFeeCap = Wei.of(input.readUInt256Scalar());
Bytes data = input.readBytes();
List<AccessListEntry> accessList = input.readList(accessListEntryRLPInput -> {
accessListEntryRLPInput.enterList();
final AccessListEntry accessListEntry = new AccessListEntry(
Address.wrap(accessListEntryRLPInput.readBytes()),
accessListEntryRLPInput.readList(RLPInput::readBytes32));
accessListEntryRLPInput.leaveList();
return accessListEntry;
});
return new SpanBatchDynamicFeeTxData(value, gasTipCap, gasFeeCap, data, accessObjList);

input.leaveList();
return new SpanBatchDynamicFeeTxData(value, gasTipCap, gasFeeCap, data, accessList);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
/*
* Copyright 2023 q315xia@163.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.optimism.utilities.derive.stages;

import java.math.BigInteger;
import org.web3j.rlp.RlpEncoder;
import org.web3j.rlp.RlpList;
import org.web3j.rlp.RlpString;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLPInput;

public record SpanBatchLegacyTxData(BigInteger value, BigInteger gasPrice, String data) implements SpanBatchTxData {
/**
* The type SpanBatchLegacyTxData.
*
* @author zhouop0
* @since 0.2.4
*/
public record SpanBatchLegacyTxData(Wei value, Wei gasPrice, Bytes data) implements SpanBatchTxData {

@Override
public byte txType() {
return 0x00;
public TransactionType txType() {
return TransactionType.FRONTIER;
}

/**
* Encode span batch legacy tx data.
*
* @return the span batch legacy tx data
*/
public byte[] encode() {
return RlpEncoder.encode(
new RlpList(RlpString.create(value()), RlpString.create(gasPrice()), RlpString.create(data())));
BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
out.writeUInt256Scalar(value());
out.writeUInt256Scalar(gasPrice());
out.writeBytes(data());
out.endList();
return out.encoded().toArrayUnsafe();
}

public static SpanBatchLegacyTxData decode(RlpList rlp) {
BigInteger value = ((RlpString) rlp.getValues().get(0)).asPositiveBigInteger();
BigInteger gasPrice = ((RlpString) rlp.getValues().get(1)).asPositiveBigInteger();
String data = ((RlpString) rlp.getValues().get(2)).asString();
/**
* Decode span batch legacy tx data.
*
* @param input the rlp
* @return the span batch legacy tx data
*/
public static SpanBatchLegacyTxData decode(RLPInput input) {
input.enterList();
Wei value = Wei.of(input.readUInt256Scalar());
Wei gasPrice = Wei.of(input.readUInt256Scalar());
Bytes data = input.readBytes();
input.leaveList();
return new SpanBatchLegacyTxData(value, gasPrice, data);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.optimism.utilities.derive.stages;

import java.math.BigInteger;
import java.util.List;

public record SpanBatchPayload(
BigInteger blockCount, BigInteger originBits, List<BigInteger> blockTxCounts, SpanBatchTxs txs) {}
// package io.optimism.utilities.derive.stages;
//
// import java.math.BigInteger;
// import java.util.List;
//
// public record SpanBatchPayload(
// BigInteger blockCount, BigInteger originBits, List<BigInteger> blockTxCounts, SpanBatchTxs txs) {}
Loading

0 comments on commit c7a388d

Please sign in to comment.