Skip to content

Commit

Permalink
tx.GasPrice should return effectiveGasPrice or feeCap for 1559 txs (#…
Browse files Browse the repository at this point in the history
…3216)

* transaction.GasPrice for 1559 tx should return effective gas price or MaxFeePerGas

* add tests

* fix test

* fix NDM build
  • Loading branch information
MarekM25 authored Jul 12, 2021
1 parent a7d672e commit 3161505
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Task<UInt256> GetNonceAsync(Address address)

public Task<NdmTransaction?> GetTransactionAsync(Keccak transactionHash)
{
(TxReceipt receipt, Transaction transaction) = _blockchainBridge.GetTransaction(transactionHash);
(TxReceipt receipt, Transaction transaction, UInt256? baseFee) = _blockchainBridge.GetTransaction(transactionHash);
if (transaction is null)
{
return Task.FromResult<NdmTransaction?>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ public BlockHeader FindBestSuggestedHeader()
throw new NotImplementedException();
}

public (TxReceipt Receipt, Transaction Transaction) GetTransaction(Keccak txHash)
public (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash)
{
return (new TxReceipt(), new Transaction
{
Hash = txHash
});
}, null);
}

private BlockReceiptsTracer _receiptsTracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.DataMarketplace.Core.Domain;
using Nethermind.DataMarketplace.Core.Services;
using Nethermind.Int256;
using Nethermind.Facade;
Expand Down Expand Up @@ -55,16 +56,16 @@ public void Setup()
[Test]
public async Task get_latest_block_number_should_return_0_if_head_is_null()
{
var result = await _ndmBridge.GetLatestBlockNumberAsync();
long result = await _ndmBridge.GetLatestBlockNumberAsync();
result.Should().Be(0);
}

[Test]
public async Task get_latest_block_number_should_return_head_number()
{
var header = Build.A.Block.TestObject;
Block header = Build.A.Block.TestObject;
_blockFinder.Head.Returns(header);
var result = await _ndmBridge.GetLatestBlockNumberAsync();
long result = await _ndmBridge.GetLatestBlockNumberAsync();
result.Should().Be(_blockFinder.Head.Number);
}

Expand All @@ -73,49 +74,49 @@ public async Task get_latest_block_number_should_return_head_number()
[Test]
public async Task get_code_should_invoke_blockchain_bridge_get_code()
{
var code = new byte[] {0, 1, 2};
var address = TestItem.AddressA;
byte[] code = new byte[] {0, 1, 2};
Address address = TestItem.AddressA;
_stateReader.GetCode(Arg.Any<Keccak>(), address).Returns(code);
_blockFinder.Head.Returns(_anyBlock);
var result = await _ndmBridge.GetCodeAsync(address);
byte[] result = await _ndmBridge.GetCodeAsync(address);
_stateReader.Received().GetCode(_anyBlock.StateRoot, address);
result.Should().BeSameAs(code);
}

[Test]
public async Task find_block_by_hash_should_invoke_blockchain_bridge_find_block_by_hash()
{
var block = Build.A.Block.TestObject;
Block block = Build.A.Block.TestObject;
_blockFinder.FindBlock(block.Hash).Returns(block);
var result = await _ndmBridge.FindBlockAsync(block.Hash);
Block? result = await _ndmBridge.FindBlockAsync(block.Hash);
_blockFinder.Received().FindBlock(block.Hash);
result.Should().Be(block);
}

[Test]
public async Task find_block_by_number_should_invoke_blockchain_bridge_find_block_by_number()
{
var block = Build.A.Block.TestObject;
Block block = Build.A.Block.TestObject;
_blockFinder.FindBlock(block.Number).Returns(block);
var result = await _ndmBridge.FindBlockAsync(block.Number);
Block? result = await _ndmBridge.FindBlockAsync(block.Number);
_blockFinder.Received().FindBlock(block.Number);
result.Should().Be(block);
}

[Test]
public async Task get_latest_block_number_should_return_null_if_head_is_null()
{
var result = await _ndmBridge.GetLatestBlockAsync();
Block? result = await _ndmBridge.GetLatestBlockAsync();
result.Should().BeNull();
}

[Test]
public async Task get_latest_block_should_return_head_number()
{
var block = Build.A.Block.TestObject;
Block block = Build.A.Block.TestObject;
_blockchainBridge.BeamHead.Returns(block);
_blockFinder.FindBlock(block.Hash).Returns(block);
var result = await _ndmBridge.GetLatestBlockAsync();
Block? result = await _ndmBridge.GetLatestBlockAsync();
result.Should().Be(block);
_blockFinder.Received().FindBlock(block.Hash);
}
Expand All @@ -124,32 +125,33 @@ public async Task get_latest_block_should_return_head_number()
public async Task get_nonce_should_invoke_blockchain_bridge_get_nonce()
{
UInt256 nonce = 1;
var address = TestItem.AddressA;
Address address = TestItem.AddressA;
_blockchainBridge.BeamHead.Returns(_anyBlock);
_stateReader.GetAccount(_anyBlock.StateRoot, address).Returns(Account.TotallyEmpty.WithChangedNonce(nonce));
var result = await _ndmBridge.GetNonceAsync(address);
UInt256 result = await _ndmBridge.GetNonceAsync(address);
_stateReader.Received().GetNonce(_anyBlock.StateRoot, address);
result.Should().Be(nonce);
}

[Test]
public async Task get_transaction_should_return_null_if_receipt_or_transaction_is_null()
{
var hash = TestItem.KeccakA;
var result = await _ndmBridge.GetTransactionAsync(hash);
Keccak hash = TestItem.KeccakA;
NdmTransaction? result = await _ndmBridge.GetTransactionAsync(hash);
result.Should().BeNull();
_blockchainBridge.Received().GetTransaction(hash);
}

[Test]
public async Task get_transaction_should_invoke_blockchain_bridge_get_transaction_and_return_ndm_transaction()
{
var receipt = Build.A.Receipt.TestObject;
var transaction = Build.A.Transaction.TestObject;
var tuple = (receipt, transaction);
var hash = TestItem.KeccakA;
TxReceipt receipt = Build.A.Receipt.TestObject;
Transaction transaction = Build.A.Transaction.TestObject;
UInt256? baseFee = null;
(TxReceipt receipt, Transaction transaction, UInt256? baseFee) tuple = (receipt, transaction, baseFee);
Keccak hash = TestItem.KeccakA;
_blockchainBridge.GetTransaction(hash).Returns(tuple);
var result = await _ndmBridge.GetTransactionAsync(hash);
NdmTransaction? result = await _ndmBridge.GetTransactionAsync(hash);
result.Should().NotBeNull();
_blockchainBridge.Received().GetTransaction(hash);
result.Transaction.Should().Be(transaction);
Expand All @@ -163,21 +165,21 @@ public async Task get_network_id_should_invoke_blockchain_bridge_get_network_id(
{
const ulong networkId = 1;
_blockchainBridge.GetChainId().Returns(networkId);
var result = await _ndmBridge.GetNetworkIdAsync();
ulong result = await _ndmBridge.GetNetworkIdAsync();
_blockchainBridge.Received().GetChainId();
result.Should().Be(networkId);
}

[Test]
public async Task call_should_invoke_blockchain_bridge_call_and_return_data()
{
var head = Build.A.Block.TestObject;
var transaction = Build.A.Transaction.TestObject;
var data = new byte[] {0, 1, 2};
Block head = Build.A.Block.TestObject;
Transaction transaction = Build.A.Transaction.TestObject;
byte[] data = new byte[] {0, 1, 2};
_blockchainBridge.BeamHead.Returns(head);
var output = new BlockchainBridge.CallOutput(data, 0, null);
BlockchainBridge.CallOutput output = new BlockchainBridge.CallOutput(data, 0, null);
_blockchainBridge.Call(head?.Header, transaction, default).Returns(output);
var result = await _ndmBridge.CallAsync(transaction);
byte[] result = await _ndmBridge.CallAsync(transaction);
_blockchainBridge.Received().Call(head?.Header, transaction, default);
result.Should().BeSameAs(data);
}
Expand All @@ -186,8 +188,8 @@ public async Task call_should_invoke_blockchain_bridge_call_and_return_data()
public async Task call_with_transaction_number_for_invalid_block_should_invoke_blockchain_bridge_call_and_return_empty_data()
{
const int blockNumber = 1;
var transaction = Build.A.Transaction.TestObject;
var result = await _ndmBridge.CallAsync(transaction, blockNumber);
Transaction transaction = Build.A.Transaction.TestObject;
byte[] result = await _ndmBridge.CallAsync(transaction, blockNumber);
_blockFinder.Received().FindBlock(blockNumber);
_blockchainBridge.DidNotReceiveWithAnyArgs().Call(null, null, default);
result.Should().BeSameAs(Array.Empty<byte>());
Expand All @@ -196,13 +198,13 @@ public async Task call_with_transaction_number_for_invalid_block_should_invoke_b
[Test]
public async Task call_with_transaction_number_should_invoke_blockchain_bridge_call_and_return_data()
{
var block = Build.A.Block.TestObject;
var transaction = Build.A.Transaction.TestObject;
var data = new byte[] {0, 1, 2};
Block block = Build.A.Block.TestObject;
Transaction transaction = Build.A.Transaction.TestObject;
byte[] data = new byte[] {0, 1, 2};
_blockFinder.FindBlock(block.Number).Returns(block);
var output = new BlockchainBridge.CallOutput(data, 0, null);
BlockchainBridge.CallOutput output = new BlockchainBridge.CallOutput(data, 0, null);
_blockchainBridge.Call(block.Header, transaction, default).Returns(output);
var result = await _ndmBridge.CallAsync(transaction, block.Number);
byte[] result = await _ndmBridge.CallAsync(transaction, block.Number);
_blockFinder.Received().FindBlock(block.Number);
_blockchainBridge.Received().Call(block.Header, transaction, default);
result.Should().BeSameAs(data);
Expand All @@ -211,10 +213,10 @@ public async Task call_with_transaction_number_should_invoke_blockchain_bridge_c
[Test]
public async Task send_own_transaction_should_invoke_blockchain_bridge_send_transaction_and_return_hash()
{
var transaction = Build.A.Transaction.TestObject;
var hash = TestItem.KeccakA;
Transaction transaction = Build.A.Transaction.TestObject;
Keccak hash = TestItem.KeccakA;
_txSender.SendTransaction(transaction, TxHandlingOptions.PersistentBroadcast | TxHandlingOptions.ManagedNonce).Returns((hash, null));
var result = await _ndmBridge.SendOwnTransactionAsync(transaction);
Keccak? result = await _ndmBridge.SendOwnTransactionAsync(transaction);
await _txSender.Received().SendTransaction(transaction, TxHandlingOptions.PersistentBroadcast | TxHandlingOptions.ManagedNonce);
result.Should().Be(hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public async Task SetUp()
[Test]
public void get_transaction_returns_null_when_transaction_not_found()
{
_blockchainBridge.GetTransaction(TestItem.KeccakA).Should().Be((null, null));
_blockchainBridge.GetTransaction(TestItem.KeccakA).Should().Be((null, null, null));
}

[Test]
public void get_transaction_returns_null_when_block_not_found()
{
_receiptStorage.FindBlockHash(TestItem.KeccakA).Returns(TestItem.KeccakB);
_blockchainBridge.GetTransaction(TestItem.KeccakA).Should().Be((null, null));
_blockchainBridge.GetTransaction(TestItem.KeccakA).Should().Be((null, null, null));
}

[Test]
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ public Block BeamHead
return (null, null);
}

public (TxReceipt Receipt, Transaction Transaction) GetTransaction(Keccak txHash)
public (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash)
{
Keccak blockHash = _receiptFinder.FindBlockHash(txHash);
if (blockHash != null)
{
Block block = _blockTree.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
TxReceipt txReceipt = _receiptFinder.Get(block).ForTransaction(txHash);
return (txReceipt, block?.Transactions[txReceipt.Index]);
return (txReceipt, block?.Transactions[txReceipt.Index], block?.BaseFeePerGas);
}

if (_txPool.TryGetPendingTransaction(txHash, out Transaction? transaction))
{
return (null, transaction);
return (null, transaction, null);
}

return (null, null);
return (null, null, null);
}

public TxReceipt GetReceipt(Keccak txHash)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface IBlockchainBridge : ILogFinder
void RecoverTxSender(Transaction tx);
TxReceipt GetReceipt(Keccak txHash);
(TxReceipt Receipt, UInt256? EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash);
(TxReceipt Receipt, Transaction Transaction) GetTransaction(Keccak txHash);
(TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash);
BlockchainBridge.CallOutput Call(BlockHeader header, Transaction tx, CancellationToken cancellationToken);
BlockchainBridge.CallOutput EstimateGas(BlockHeader header, Transaction tx, CancellationToken cancellationToken);
BlockchainBridge.CallOutput CreateAccessList(BlockHeader header, Transaction tx, CancellationToken cancellationToken, bool optimize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public async Task Eth_pending_transactions()
string serialized = ctx._test.TestEthRpc("eth_pendingTransactions");
Assert.AreEqual("{\"jsonrpc\":\"2.0\",\"result\":[{\"hash\":\"0x190d9a78dbc61b1856162ab909976a1b28ba4a41ee041341576ea69686cd3b29\",\"nonce\":\"0x0\",\"blockHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"blockNumber\":null,\"transactionIndex\":null,\"from\":\"0x475674cb523a0a2736b7f7534390288fce16982c\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x1\",\"gas\":\"0x5208\",\"data\":\"0x\",\"input\":\"0x\",\"type\":\"0x0\",\"v\":\"0x26\",\"s\":\"0x2d04e55699fa32e6b65a22189f7571f5030d636d7d44a8b53fe016a2c3ecde24\",\"r\":\"0xda3978c3a1430bd902cf5bbca73c5a1eca019b3f003c95ee16657fd0bb89534c\"}],\"id\":67}", serialized, serialized.Replace("\"", "\\\""));
}

[Test]
public async Task Eth_pending_transactions_1559_tx()
{
using Context ctx = await Context.CreateWithLondonEnabled();
ctx._test.AddTransactions(Build.A.Transaction.WithMaxPriorityFeePerGas(6.GWei()).WithMaxFeePerGas(11.GWei()).WithType(TxType.EIP1559).SignedAndResolved(TestItem.PrivateKeyD).TestObject);
string serialized = ctx._test.TestEthRpc("eth_pendingTransactions");
Assert.AreEqual("{\"jsonrpc\":\"2.0\",\"result\":[{\"hash\":\"0x629190363a5457ff385cb4770426d98590446c08783f2c96961156351657be67\",\"nonce\":\"0x0\",\"blockHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"blockNumber\":null,\"transactionIndex\":null,\"from\":\"0x475674cb523a0a2736b7f7534390288fce16982c\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x28fa6ae00\",\"maxPriorityFeePerGas\":\"0x165a0bc00\",\"maxFeePerGas\":\"0x28fa6ae00\",\"gas\":\"0x5208\",\"data\":\"0x\",\"input\":\"0x\",\"chainId\":\"0x1\",\"type\":\"0x2\",\"v\":\"0x0\",\"s\":\"0x4d8e9a47d74a88cdc49d5b56cd0466e920c39f2d28a98be0f657c1884ad4b32d\",\"r\":\"0xcb08c119a76bdb58f29423ea7b084ea14be24cfbdc75300d3dab06b35a1a124b\"}],\"id\":67}", serialized, serialized.Replace("\"", "\\\""));
}

[Test]
public async Task Eth_get_transaction_by_block_number_and_index()
Expand Down Expand Up @@ -567,6 +576,19 @@ public async Task Eth_getTransactionReceipt_return_info_about_mined_1559tx()
string serialized = ctx._test.TestEthRpc("eth_getTransactionReceipt", tx.Hash.ToString());
Assert.AreEqual("{\"jsonrpc\":\"2.0\",\"result\":{\"transactionHash\":\"0x31501f80bf2ec493c368a519cb8ed6f132f0be26202304bbf1e1728642affb7f\",\"transactionIndex\":\"0x0\",\"blockHash\":\"0x691ca9ebaa6f2839593bcc22284277ff6e5d89d8dec2e40f9813ea109669be92\",\"blockNumber\":\"0x5\",\"cumulativeGasUsed\":\"0x521c\",\"gasUsed\":\"0x521c\",\"effectiveGasPrice\":\"0x5e91eb5d\",\"from\":\"0x723847c97bc651c7e8c013dbbe65a70712f02ad3\",\"to\":\"0x0000000000000000000000000000000000000000\",\"contractAddress\":null,\"logs\":[],\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"status\":\"0x1\",\"type\":\"0x2\"},\"id\":67}", serialized);
}

[Test]
public async Task Eth_getTransactionByHash_return_info_about_mined_1559tx()
{
using Context ctx = await Context.CreateWithLondonEnabled();
await ctx._test.AddFundsAfterLondon((new Address("0x723847c97bc651c7e8c013dbbe65a70712f02ad3"), 1.Ether()));
Transaction tx = Build.A.Transaction.WithData(new byte[]{0, 1})
.SignedAndResolved().WithChainId(1).WithGasPrice(0).WithValue(0).WithGasLimit(210200)
.WithType(TxType.EIP1559).WithMaxFeePerGas(20.GWei()).WithMaxPriorityFeePerGas(1.GWei()).TestObject;
await ctx._test.AddBlock(tx);
string serialized = ctx._test.TestEthRpc("eth_getTransactionByHash", tx.Hash.ToString());
Assert.AreEqual("{\"jsonrpc\":\"2.0\",\"result\":{\"hash\":\"0x31501f80bf2ec493c368a519cb8ed6f132f0be26202304bbf1e1728642affb7f\",\"nonce\":\"0x0\",\"blockHash\":\"0x691ca9ebaa6f2839593bcc22284277ff6e5d89d8dec2e40f9813ea109669be92\",\"blockNumber\":\"0x5\",\"transactionIndex\":\"0x0\",\"from\":\"0x723847c97bc651c7e8c013dbbe65a70712f02ad3\",\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x0\",\"gasPrice\":\"0x5e91eb5d\",\"maxPriorityFeePerGas\":\"0x3b9aca00\",\"maxFeePerGas\":\"0x4a817c800\",\"gas\":\"0x33518\",\"data\":\"0x0001\",\"input\":\"0x0001\",\"chainId\":\"0x1\",\"type\":\"0x2\",\"v\":\"0x0\",\"s\":\"0x6b82095065a599e6b5e52bed0043702baf3411418af679ac483f9fc75a8f6aef\",\"r\":\"0x8654517f7822e7a4e10e79f3f5a4136703c7d1b51d98e47686e201c3c2845f92\"},\"id\":67}", serialized);
}

[Test]
public async Task Eth_syncing()
Expand Down
Loading

0 comments on commit 3161505

Please sign in to comment.