Skip to content

Commit

Permalink
datagas limit checking, and test coverage on pricing up to 1000
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Florentine <justin+github@florentine.us>
  • Loading branch information
jflo committed Jun 16, 2023
1 parent b18b5d4 commit 51d9ae5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

public interface GasLimitCalculator {

static final long DATA_GAS_LIMIT = 786432;

long nextGasLimit(long currentGasLimit, long targetGasLimit, long newBlockNumber);

static GasLimitCalculator constant() {
return (currentGasLimit, targetGasLimit, newBlockNumber) -> currentGasLimit;
}

default long currentDataGasLimit() {
return 1 << 19;
return DATA_GAS_LIMIT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ public ValidationResult<TransactionInvalidReason> validateTransactionsBlobs(

BlobsWithCommitments blobsWithCommitments = transaction.getBlobsWithCommitments().get();

final long blobsLimit = gasLimitCalculator.currentDataGasLimit() / gasCalculator.dataGasUsed(1);
final long blobsLimit =
gasLimitCalculator.currentDataGasLimit()
/ gasCalculator.dataGasUsed(blobsWithCommitments.getBlobs().size());
if (blobsWithCommitments.getBlobs().size() > blobsLimit) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_BLOBS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,49 @@

import org.hyperledger.besu.datatypes.DataGas;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Test;

class CancunFeeMarketTest {

private static final int DATA_GAS_PER_BLOB = 131072;

@Test
void dataPricePerGas() {
CancunFeeMarket cancunFeeMarket = new CancunFeeMarket(0, Optional.empty());
// when no excess data gas, data price per gas is 1
assertEquals(1, cancunFeeMarket.dataPricePerGas(DataGas.ZERO).getAsBigInteger().intValue());

// when excess data doubles, price is log
assertEquals(
2,
cancunFeeMarket
.dataPricePerGas(DataGas.fromHexString("0x180000"))
.getAsBigInteger()
.intValue());
assertEquals(
3,
cancunFeeMarket
.dataPricePerGas(DataGas.fromHexString("0x280000"))
.getAsBigInteger()
.intValue());
assertEquals(
5,
cancunFeeMarket
.dataPricePerGas(DataGas.fromHexString("0x380000"))
.getAsBigInteger()
.intValue());
assertEquals(
8,
cancunFeeMarket
.dataPricePerGas(DataGas.fromHexString("0x480000"))
.getAsBigInteger()
.intValue());
record DataGasPricing(long excess, long price) {}
List<DataGasPricing> testVector = new ArrayList<>();

int numBlobs = 1;
long price = 1;
while (price <= 1000) {
price = dataGasPrice(DataGas.of(numBlobs * DATA_GAS_PER_BLOB));
var testCase = new DataGasPricing(numBlobs * DATA_GAS_PER_BLOB, price);
testVector.add(testCase);
numBlobs++;
}

testVector.stream()
.forEach(
dataGasPricing -> {
assertEquals(
dataGasPricing.price,
cancunFeeMarket
.dataPricePerGas(DataGas.of(dataGasPricing.excess))
.getAsBigInteger()
.intValue());
});
}

private long dataGasPrice(final DataGas excess) {
double dgufDenominator = 3338477;
double fakeExpo = excess.getValue().longValue() / dgufDenominator;
return (long) (1 * Math.exp(fakeExpo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ private void handlePacket(final DatagramPacket datagram) {
final Endpoint endpoint = new Endpoint(host, port, Optional.empty());
handleIncomingPacket(endpoint, event.result());
} else {
if (event.cause() instanceof PeerDiscoveryPacketDecodingException || event.cause() instanceof DecodeException) {
if (event.cause() instanceof PeerDiscoveryPacketDecodingException
|| event.cause() instanceof DecodeException) {
LOG.debug(
"Discarding invalid peer discovery packet: {}, {}",
event.cause().getMessage(),
Expand Down

0 comments on commit 51d9ae5

Please sign in to comment.