Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no integration test for EIP-1559 feature #249

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions integration_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def test_events(cluster, suspend_capture):
def test_minimal_gas_price(cronos):
w3 = cronos.w3
gas_price = w3.eth.gas_price
assert gas_price == 5000000000000
tx = {
"to": "0x0000000000000000000000000000000000000000",
"value": 10000,
Expand Down Expand Up @@ -133,7 +132,7 @@ def test_statesync(cronos):
# Do an ethereum transfer
tx_value = 10000
gas_price = w3.eth.gas_price
initial_balance = 10000000000000000000000
initial_balance = w3.eth.get_balance(ADDRS["community"])
tx = {"to": ADDRS["community"], "value": tx_value, "gasPrice": gas_price}
txhash_0 = send_transaction(w3, tx, KEYS["validator"])["transactionHash"].hex()

Expand Down Expand Up @@ -226,11 +225,12 @@ def test_statesync(cronos):

def test_transaction(cronos):
w3 = cronos.w3
gas_price = w3.eth.gas_price

# send transaction
txhash_1 = send_transaction(
w3,
{"to": ADDRS["community"], "value": 10000, "gasPrice": w3.eth.gas_price},
{"to": ADDRS["community"], "value": 10000, "gasPrice": gas_price},
KEYS["validator"],
)["transactionHash"]
tx1 = w3.eth.get_transaction(txhash_1)
Expand All @@ -245,7 +245,7 @@ def test_transaction(cronos):
{
"to": ADDRS["community"],
"value": 10000,
"gasPrice": w3.eth.gas_price,
"gasPrice": gas_price,
"nonce": w3.eth.get_transaction_count(ADDRS["validator"]) - 1,
},
KEYS["validator"],
Expand Down Expand Up @@ -291,7 +291,8 @@ def test_transaction(cronos):
},
KEYS["validator"],
)["transactionHash"]
assert "insufficient fee" in str(exc)
# FIXME https://github.com/tharsis/ethermint/pull/911
assert "invalid base fee" in str(exc)

# check all failed transactions are not included in blockchain
assert w3.eth.get_block_number() == initial_block_number
Expand Down
67 changes: 67 additions & 0 deletions integration_tests/test_eip1559.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from .utils import ADDRS, KEYS, send_transaction, w3_wait_for_block


def adjust_base_fee(parent_fee, gas_limit, gas_used):
"spec: https://eips.ethereum.org/EIPS/eip-1559#specification"
change_denominator = 8
elasticity_multiplier = 2
gas_target = gas_limit // elasticity_multiplier

delta = parent_fee * (gas_target - gas_used) // gas_target // change_denominator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some explanation for parent_fee?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I linked the eip-1559 spec, it's basically the same code as the spec.

return parent_fee - delta


def test_dynamic_fee_tx(cluster):
"""
test basic eip-1559 tx works:
- tx fee calculation is compliant to go-ethereum
- base fee adjustment is compliant to go-ethereum
"""
w3 = cluster.w3
amount = 10000
before = w3.eth.get_balance(ADDRS["community"])
tip_price = 1
max_price = 1000000000000 + tip_price
tx = {
"to": "0x0000000000000000000000000000000000000000",
"value": amount,
"gas": 21000,
"maxFeePerGas": max_price,
"maxPriorityFeePerGas": tip_price,
}
txreceipt = send_transaction(w3, tx, KEYS["community"])
assert txreceipt.status == 1
blk = w3.eth.get_block(txreceipt.blockNumber)
assert txreceipt.effectiveGasPrice == blk.baseFeePerGas + tip_price

fee_expected = txreceipt.gasUsed * txreceipt.effectiveGasPrice
after = w3.eth.get_balance(ADDRS["community"])
fee_deducted = before - after - amount
assert fee_deducted == fee_expected

assert blk.gasUsed == txreceipt.gasUsed # we are the only tx in the block

# check the next block's base fee is adjusted accordingly
w3_wait_for_block(w3, txreceipt.blockNumber + 1)
next_base_price = w3.eth.get_block(txreceipt.blockNumber + 1).baseFeePerGas

assert next_base_price == adjust_base_fee(
blk.baseFeePerGas, blk.gasLimit, blk.gasUsed
)


def test_base_fee_adjustment(cluster):
"""
verify base fee adjustment of three continuous empty blocks
"""
w3 = cluster.w3
begin = w3.eth.block_number
w3_wait_for_block(w3, begin + 3)

blk = w3.eth.get_block(begin)
parent_fee = blk.baseFeePerGas

for i in range(3):
fee = w3.eth.get_block(begin + 1 + i).baseFeePerGas
assert fee == adjust_base_fee(parent_fee, blk.gasLimit, 0)
parent_fee = fee
4 changes: 3 additions & 1 deletion integration_tests/test_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def geth(tmp_path_factory):

@pytest.fixture(scope="module", params=[True, False])
def cronos(request, tmp_path_factory):
"start-cronos"
"""start-cronos
params: enable_auto_deployment
"""
yield from setup_cronos_experimental(
tmp_path_factory.mktemp("cronos_experimental"), 26700, request.param
)
Expand Down
24 changes: 24 additions & 0 deletions integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ def wait_for_ipc(path, timeout=40.0):
)


def w3_wait_for_block(w3, height, timeout=240):
for i in range(timeout * 2):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why multiply with 2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it sleep 0.5 seconds, multiply the loop count with 2 make total time closer to timeout as seconds.

try:
current_height = w3.eth.block_number
except AssertionError as e:
print(f"get current block number failed: {e}", file=sys.stderr)
else:
if current_height >= height:
break
print("current block height", current_height)
time.sleep(0.5)
else:
raise TimeoutError(f"wait for block {height} timeout")


def w3_wait_for_new_blocks(w3, n):
begin_height = w3.eth.block_number
while True:
time.sleep(0.5)
cur_height = w3.eth.block_number
if cur_height - begin_height >= n:
break


def get_ledger():
return ledger.Ledger()

Expand Down
7 changes: 7 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ in
import sources.nixpkgs {
overlays = [
(_: pkgs: dapptools) # use released version to hit the binary cache
(_: pkgs: {
go-ethereum = pkgs.callPackage ./go-ethereum.nix {
Copy link
Collaborator Author

@yihuang yihuang Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to replace to a newer version to test eip-1559 logic.

inherit (pkgs.darwin) libobjc;
inherit (pkgs.darwin.apple_sdk.frameworks) IOKit;
buildGoModule = pkgs.buildGo117Module;
};
}) # update to a version that supports eip-1559
(_: pkgs: rec {
buildGoApplication = pkgs.callPackage (import (sources.gomod2nix + "/builder")) {
go = pkgs.go_1_17;
Expand Down
60 changes: 60 additions & 0 deletions nix/go-ethereum.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{ lib, stdenv, buildGoModule, fetchFromGitHub, libobjc, IOKit }:

let
# A list of binaries to put into separate outputs
bins = [
"geth"
"clef"
];

in
buildGoModule rec {
pname = "go-ethereum";
version = "1.10.15";

src = fetchFromGitHub {
owner = "ethereum";
repo = pname;
rev = "v${version}";
sha256 = "0f6n9rg42ph47mvykc9f0lf99yzwqy4jm7mlzyks4l6i6fl1g3q1";
};

vendorSha256 = "1s5yfpk2yn7f3zwjl2fdrh6c63ki2b8rlmnlss27yxibsidaj0yd";

doCheck = false;

outputs = [ "out" ] ++ bins;

# Move binaries to separate outputs and symlink them back to $out
postInstall = lib.concatStringsSep "\n" (
builtins.map (bin: "mkdir -p \$${bin}/bin && mv $out/bin/${bin} \$${bin}/bin/ && ln -s \$${bin}/bin/${bin} $out/bin/") bins
);

subPackages = [
"cmd/abidump"
"cmd/abigen"
"cmd/bootnode"
"cmd/checkpoint-admin"
"cmd/clef"
"cmd/devp2p"
"cmd/ethkey"
"cmd/evm"
"cmd/faucet"
"cmd/geth"
"cmd/p2psim"
"cmd/puppeth"
"cmd/rlpdump"
"cmd/utils"
];

# Fix for usb-related segmentation faults on darwin
propagatedBuildInputs =
lib.optionals stdenv.isDarwin [ libobjc IOKit ];

meta = with lib; {
homepage = "https://geth.ethereum.org/";
description = "Official golang implementation of the Ethereum protocol";
license = with licenses; [ lgpl3Plus gpl3Plus ];
maintainers = with maintainers; [ adisbladis lionello RaghavSood ];
};
}
5 changes: 3 additions & 2 deletions scripts/cronos-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cronos_777-1:
cmd: cronosd
start-flags: "--trace"
app-config:
minimum-gas-prices: 5000000000000basetcro
minimum-gas-prices: 0basetcro
json-rpc:
address: "0.0.0.0:{EVMRPC_PORT}"
ws-address: "0.0.0.0:{EVMRPC_PORT_WS}"
Expand Down Expand Up @@ -53,4 +53,5 @@ cronos_777-1:
send_enabled: true
feemarket:
params:
no_base_fee: true
no_base_fee: false
initial_base_fee: 100000000000
3 changes: 3 additions & 0 deletions scripts/cronos-experimental-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ cronos_777-1:
evm:
params:
evm_denom: basetcro
feemarket:
params:
no_base_fee: true
cronos:
params:
cronos_admin: crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp
Expand Down
6 changes: 4 additions & 2 deletions scripts/geth-genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"muirGlacierBlock": 0,
"berlinBlock": 0,
"yoloV3Block": 0,
"londonBlock": 0,
"clique": {
"period": 2,
"epoch": 30000
Expand All @@ -21,7 +22,7 @@
"nonce": "0x0",
"timestamp": "0x60ed6d23",
"extraData": "0x000000000000000000000000000000000000000000000000000000000000000057f96e6b86cdefdb3d412547816a82e3e0ebf9d20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"gasLimit": "0x4db9760",
"difficulty": "0x1",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
Expand All @@ -35,5 +36,6 @@
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": "100000000000"
}