Skip to content

Commit

Permalink
new approach to calculate blobBaseFee
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Feb 16, 2024
1 parent a87c302 commit 7b9bc40
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 43 deletions.
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
"io.github.adraffy:ens-normalize:$ensAdraffyVersion",
"io.tmio:tuweni-bytes:$tuweniVersion",
"io.tmio:tuweni-units:$tuweniVersion"
"io.vertx:vertx-core:YourVertxVersion:4.4.6"
testImplementation project(path: ':crypto', configuration: 'testArtifacts'),
"nl.jqno.equalsverifier:equalsverifier:$equalsverifierVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/web3j/protocol/Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import java.util.concurrent.ScheduledExecutorService;

import org.web3j.protocol.core.Batcher;
import org.web3j.protocol.core.BlobFee;
import org.web3j.protocol.core.Ethereum;
import org.web3j.protocol.core.JsonRpc2_0Web3j;
import org.web3j.protocol.rx.Web3jRx;

/** JSON-RPC Request object building factory. */
public interface Web3j extends Ethereum, Web3jRx, Batcher {
public interface Web3j extends Ethereum, Web3jRx, Batcher, BlobFee {

/**
* Construct a new Web3j instance.
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/org/web3j/protocol/core/BlobFee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 Web3 Labs Ltd.
*
* 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 org.web3j.protocol.core;

import java.math.BigInteger;

public interface BlobFee {
/**
* Calculating Base Fee per Blob Gas
*
* @return baseFee value.
*/
BigInteger ethGetBaseFeePerBlobGas();
}
30 changes: 30 additions & 0 deletions core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
public class JsonRpc2_0Web3j implements Web3j {

public static final int DEFAULT_BLOCK_TIME = 15 * 1000;
private static final BigInteger MIN_BLOB_BASE_FEE = new BigInteger("1");
private static final BigInteger BLOB_BASE_FEE_UPDATE_FRACTION = new BigInteger("3338477");

protected final Web3jService web3jService;
private final JsonRpc2_0Rx web3jRx;
Expand Down Expand Up @@ -876,4 +878,32 @@ public void shutdown() {
public BatchRequest newBatch() {
return new BatchRequest(web3jService);
}

@Override
public BigInteger ethGetBaseFeePerBlobGas() {
try {
EthBlock ethBlock =
web3jService.send(
ethGetBlockByNumber(DefaultBlockParameter.valueOf("latest"), false),

Check warning on line 887 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L885-L887

Added lines #L885 - L887 were not covered by tests
EthBlock.class);
return fakeExponential(ethBlock.getBlock().getExcessBlobGas());
} catch (IOException e) {
throw new RuntimeException("Failed to get baseFeePerBlobGas value", e);

Check warning on line 891 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L889-L891

Added lines #L889 - L891 were not covered by tests
}
}

private static BigInteger fakeExponential(BigInteger numerator) {
BigInteger i = BigInteger.ONE;
BigInteger output = BigInteger.ZERO;
BigInteger numeratorAccum = MIN_BLOB_BASE_FEE.multiply(BLOB_BASE_FEE_UPDATE_FRACTION);

Check warning on line 898 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L896-L898

Added lines #L896 - L898 were not covered by tests
while (numeratorAccum.compareTo(BigInteger.ZERO) > 0) {
output = output.add(numeratorAccum);
numeratorAccum =

Check warning on line 901 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L900-L901

Added lines #L900 - L901 were not covered by tests
numeratorAccum
.multiply(numerator)
.divide(BLOB_BASE_FEE_UPDATE_FRACTION.multiply(i));
i = i.add(BigInteger.ONE);

Check warning on line 905 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L903-L905

Added lines #L903 - L905 were not covered by tests
}
return output.divide(BLOB_BASE_FEE_UPDATE_FRACTION);

Check warning on line 907 in core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java#L907

Added line #L907 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
package org.web3j.protocol.core.methods.request;

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

import com.fasterxml.jackson.annotation.JsonInclude;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.utils.Numeric;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.core.Response;
import org.web3j.utils.Numeric;
import org.apache.tuweni.bytes.Bytes;

/**
* Block object returned by:
Expand Down Expand Up @@ -147,6 +146,59 @@ public Block(
this.excessBlobGas = excessBlobGas;
}

public Block(
String number,
String hash,
String parentHash,
String nonce,
String sha3Uncles,
String logsBloom,
String transactionsRoot,
String stateRoot,
String receiptsRoot,
String author,
String miner,
String mixHash,
String difficulty,
String totalDifficulty,
String extraData,
String size,
String gasLimit,
String gasUsed,
String timestamp,
List<TransactionResult> transactions,
List<String> uncles,
List<String> sealFields,
String baseFeePerGas,
String withdrawalsRoot,
List<Withdrawal> withdrawals) {
this.number = number;
this.hash = hash;
this.parentHash = parentHash;
this.nonce = nonce;
this.sha3Uncles = sha3Uncles;
this.logsBloom = logsBloom;
this.transactionsRoot = transactionsRoot;
this.stateRoot = stateRoot;
this.receiptsRoot = receiptsRoot;
this.author = author;
this.miner = miner;
this.mixHash = mixHash;
this.difficulty = difficulty;
this.totalDifficulty = totalDifficulty;
this.extraData = extraData;
this.size = size;
this.gasLimit = gasLimit;
this.gasUsed = gasUsed;
this.timestamp = timestamp;
this.transactions = transactions;
this.uncles = uncles;
this.sealFields = sealFields;
this.baseFeePerGas = baseFeePerGas;
this.withdrawalsRoot = withdrawalsRoot;
this.withdrawals = withdrawals;
}

public BigInteger getNumber() {
return Numeric.decodeQuantity(number);
}
Expand Down Expand Up @@ -556,8 +608,6 @@ public boolean equals(Object o) {
return getWithdrawals() != null
? getWithdrawals().equals(block.getWithdrawals())
: block.getWithdrawals() == null;


}

@Override
Expand Down Expand Up @@ -601,8 +651,14 @@ public int hashCode() {
31 * result
+ (getWithdrawalsRoot() != null ? getWithdrawalsRoot().hashCode() : 0);
result = 31 * result + (getWithdrawals() != null ? getWithdrawals().hashCode() : 0);
result = 31 * result + (getBlobGasUsedRaw() != null ? getBlobGasUsedRaw().hashCode() : 0);
result = 31 * result + (getExcessBlobGasRaw() != null ? getExcessBlobGasRaw().hashCode() : 0);
result =
31 * result
+ (getBlobGasUsedRaw() != null ? getBlobGasUsedRaw().hashCode() : 0);
result =
31 * result
+ (getExcessBlobGasRaw() != null
? getExcessBlobGasRaw().hashCode()
: 0);
return result;
}
}
Expand Down Expand Up @@ -776,7 +832,7 @@ public TransactionObject(
String maxPriorityFeePerGas,
List<AccessListObject> accessList,
String maxFeePerBlobGas,
List<Bytes> versionedHashes) {
List<String> versionedHashes) {
super(

Check warning on line 836 in core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java#L836

Added line #L836 was not covered by tests
hash,
nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.math.BigInteger;
import java.util.List;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.crypto.TransactionUtils;
import org.web3j.utils.Numeric;

Expand Down Expand Up @@ -45,7 +44,7 @@ public class Transaction {
private String maxPriorityFeePerGas;
private List<AccessListObject> accessList;
private String maxFeePerBlobGas;
private List<Bytes> versionedHashes;
private List<String> versionedHashes;

public Transaction() {}

Expand Down Expand Up @@ -170,7 +169,7 @@ public Transaction(
String maxPriorityFeePerGas,
List accessList,
String maxFeePerBlobGas,
List<Bytes> versionedHashes) {
List versionedHashes) {
this.hash = hash;
this.nonce = nonce;
this.blockHash = blockHash;
Expand Down Expand Up @@ -450,11 +449,11 @@ public void setMaxFeePerBlobGas(String maxFeePerBlobGas) {
this.maxFeePerBlobGas = maxFeePerBlobGas;
}

Check warning on line 450 in core/src/main/java/org/web3j/protocol/core/methods/response/Transaction.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/Transaction.java#L449-L450

Added lines #L449 - L450 were not covered by tests

public List<Bytes> getVersionedHashes() {
public List<String> getVersionedHashes() {
return versionedHashes;
}

public void setVersionedHashes(List<Bytes> versionedHashes) {
public void setVersionedHashes(List<String> versionedHashes) {
this.versionedHashes = versionedHashes;
}

Check warning on line 458 in core/src/main/java/org/web3j/protocol/core/methods/response/Transaction.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/Transaction.java#L457-L458

Added lines #L457 - L458 were not covered by tests

Expand Down Expand Up @@ -611,7 +610,11 @@ public int hashCode() {
+ (getMaxPriorityFeePerGasRaw() != null
? getMaxPriorityFeePerGasRaw().hashCode()
: 0);
result = 31 * result + (getMaxFeePerBlobGasRaw() != null ? getMaxFeePerBlobGasRaw().hashCode() : 0);
result =
31 * result
+ (getMaxFeePerBlobGasRaw() != null
? getMaxFeePerBlobGasRaw().hashCode()
: 0);
result = 31 * result + (getVersionedHashes() != null ? getVersionedHashes().hashCode() : 0);
result = 31 * result + (getAccessList() != null ? getAccessList().hashCode() : 0);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class TransactionReceipt {
private String revertReason;
private String type;
private String effectiveGasPrice;
private String blobGasPrice;
private String blobGasUsed;

public TransactionReceipt() {}

Expand Down Expand Up @@ -75,6 +77,45 @@ public TransactionReceipt(
this.effectiveGasPrice = effectiveGasPrice;
}

public TransactionReceipt(
String transactionHash,
String transactionIndex,
String blockHash,
String blockNumber,
String cumulativeGasUsed,
String gasUsed,
String contractAddress,
String root,
String status,
String from,
String to,
List<Log> logs,
String logsBloom,
String revertReason,
String type,
String effectiveGasPrice,
String blobGasPrice,
String blobGasUsed) {
this.transactionHash = transactionHash;
this.transactionIndex = transactionIndex;
this.blockHash = blockHash;
this.blockNumber = blockNumber;
this.cumulativeGasUsed = cumulativeGasUsed;
this.gasUsed = gasUsed;
this.contractAddress = contractAddress;
this.root = root;
this.status = status;
this.from = from;
this.to = to;
this.logs = logs;
this.logsBloom = logsBloom;
this.revertReason = revertReason;
this.type = type;
this.effectiveGasPrice = effectiveGasPrice;
this.blobGasPrice = blobGasPrice;
this.blobGasUsed = blobGasUsed;
}

Check warning on line 117 in core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java#L98-L117

Added lines #L98 - L117 were not covered by tests

public String getTransactionHash() {
return transactionHash;
}
Expand Down Expand Up @@ -227,6 +268,22 @@ public void setEffectiveGasPrice(String effectiveGasPrice) {
this.effectiveGasPrice = effectiveGasPrice;
}

public String getBlobGasPrice() {
return blobGasPrice;

Check warning on line 272 in core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java#L272

Added line #L272 was not covered by tests
}

public void setBlobGasPrice(String blobGasPrice) {
this.blobGasPrice = blobGasPrice;
}

Check warning on line 277 in core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java#L276-L277

Added lines #L276 - L277 were not covered by tests

public String getBlobGasUsed() {
return blobGasUsed;

Check warning on line 280 in core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java#L280

Added line #L280 was not covered by tests
}

public void setBlobGasUsed(String blobGasUsed) {
this.blobGasUsed = blobGasUsed;
}

Check warning on line 285 in core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java#L284-L285

Added lines #L284 - L285 were not covered by tests

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -302,6 +359,18 @@ public boolean equals(Object o) {
: that.getEffectiveGasPrice() != null) {
return false;
}

if (blobGasPrice != null
? !blobGasPrice.equals(that.blobGasPrice)
: that.blobGasPrice != null) {
return false;
}

if (blobGasUsed != null
? !blobGasUsed.equals(that.blobGasUsed)
: that.blobGasUsed != null) {
return false;
}
return getRevertReason() != null
? getRevertReason().equals(that.getRevertReason())
: that.getRevertReason() == null;
Expand All @@ -327,6 +396,8 @@ public int hashCode() {
result =
31 * result
+ (getEffectiveGasPrice() != null ? getEffectiveGasPrice().hashCode() : 0);
result = 31 * result + (blobGasPrice != null ? blobGasPrice.hashCode() : 0);
result = 31 * result + (blobGasUsed != null ? blobGasUsed.hashCode() : 0);
return result;
}

Expand Down Expand Up @@ -380,6 +451,12 @@ public String toString() {
+ ", effectiveGasPrice='"
+ effectiveGasPrice
+ '\''
+ ", blobGasPrice='"
+ blobGasPrice
+ '\''
+ ", blobGasused='"
+ blobGasUsed
+ '\''
+ '}';
}
}
Loading

0 comments on commit 7b9bc40

Please sign in to comment.