Skip to content

Commit

Permalink
Merge pull request #305 from LATOKEN/gas_consumption
Browse files Browse the repository at this point in the history
Adjust gas consumption for external handlers
  • Loading branch information
tbssajal authored Jan 25, 2023
2 parents 159a47d + d967acf commit a381e5b
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/Lachain.Core/Blockchain/Hardfork/HardforkHeights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public static bool IsHardfork_15Active(ulong height)
return height >= Hardfork_15;
}

// adjust gas consumption
// allow contract balance transfer in ExternalHandler.cs
public static bool IsHardfork_16Active(ulong height)
{
return height >= Hardfork_16;
Expand Down
1 change: 1 addition & 0 deletions src/Lachain.Core/Blockchain/Operations/BlockManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ out Money totalFee
throw new InvalidBlockException(OperatingError.BlockGasOverflow);
Logger.LogWarning(
$"Unable to take transaction {txHash.ToHex()} with gas {receipt.GasUsed}, block gas limit overflowed {gasUsed}/{GasMetering.DefaultBlockGasLimit}");
gasUsed -= receipt.GasUsed;
continue;
}
else Logger.LogInformation($"Block gas limit after execution ok for tx : {txHash.ToHex()}");
Expand Down
191 changes: 143 additions & 48 deletions src/Lachain.Core/Blockchain/VM/ExternalHandler.cs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/Lachain.Core/Blockchain/VM/GasMetering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ public class GasMetering
public const ulong InputDataGasPerByte = 10;

public const ulong CopyFromMemoryGasPerByte = 10;
public const ulong NewCopyFromMemoryGasPerByte = 100;
public const ulong CopyToMemoryGasPerByte = 10;
public const ulong NewCopyToMemoryGasPerByte = 100;
public const ulong GetCallValueGasCost = 100;
public const ulong GetCallSizeGasCost = 10;
public const ulong GetReturnValueGasCost = 100;
public const ulong GetReturnSizeGasCost = 10;
public const ulong SetReturnGasCost = 100;
public const ulong GetGasLeftGasCost = 100;
public const ulong CopyCodeValueGasCost = 100;
public const ulong GetCodeSizeGasCost = 10;
public const ulong InvokeContractGasCost = 200_000;
public const ulong TransferFundsGasCost = 3_000_000;
public const ulong LoadStorageGasCost = 500_000;
public const ulong SaveStorageGasCost = 3_000_000;
Expand All @@ -25,7 +30,9 @@ public class GasMetering
public const ulong Ripemd160GasCost = 0;
public const ulong Ripemd160GasPerByte = 100_000;
public const ulong RecoverGasCost = 100_000;
public const ulong NewRecoverGasCost = 1_000_000;
public const ulong VerifyGasCost = 60_000;
public const ulong NewVerifyGasCost = 1_000_000;
public const ulong WriteEventPerByteGas = SaveStorageGasCost / 32;
public const ulong CallDataLoad = 1_000;
public const ulong MLoad = 1_000;
Expand Down
63 changes: 36 additions & 27 deletions test/Lachain.CoreTest/RPC/HTTP/Web3/Web3TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,41 @@ public void Test_EstimateGas()
{
_blockManager.TryBuildGenesisBlock();

var keyPair = _privateWallet!.EcdsaKeyPair;
GenerateBlocks(1, 1);

// after hardfork:
while (!HardforkHeights.IsHardfork_16Active(_blockManager.GetHeight()))
GenerateBlocks(_blockManager.GetHeight() + 1, _blockManager.GetHeight() + 1);

DeployAndTestContract("0x2e356e");

var receipt = TestUtils.GetRandomTransaction(false);
var opts = new JObject
{
["from"] = receipt.Transaction.From.ToHex(),
["to"] = receipt.Transaction.To.ToHex(),
};

var result = _apiService!.EstimateGas(opts);
Assert.AreNotEqual(result, "0x");
}

[Test]
/// Changed from private to public
[Ignore("fix it")]
public void Test_GetNetworkGasPrice()
{
var gasPrice_Expected = "0x1";

var gasPrice_Actual = _apiService!.GetNetworkGasPrice();

Assert.AreEqual(gasPrice_Expected, gasPrice_Actual);

}

private void DeployAndTestContract(string gasEstimate)
{
// Deploy contract
var keyPair = _privateWallet!.EcdsaKeyPair;
var byteCode = ByteCodeHex.HexToBytes();
Assert.That(VirtualMachine.VerifyContract(byteCode, true), "Unable to validate smart-contract code");
var from = keyPair.PublicKey.GetAddress();
Expand All @@ -352,7 +383,7 @@ public void Test_EstimateGas()
var tx = _transactionBuilder.DeployTransaction(from, byteCode);
var signedTx = _transactionSigner.Sign(tx, keyPair, HardforkHeights.IsHardfork_9Active(2));
Assert.That(_transactionPool.Add(signedTx) == OperatingError.Ok, "Can't add deploy tx to pool");
GenerateBlocks(2, 2);
GenerateBlocks(_blockManager.GetHeight() + 1, _blockManager.GetHeight() + 1);

// check contract is deployed
var contract = _stateManager.LastApprovedSnapshot.Contracts.GetContractByHash(contractHash);
Expand All @@ -369,30 +400,8 @@ public void Test_EstimateGas()
};

var result = _apiService!.EstimateGas(opts);
Assert.AreEqual(result, "0x2e1d22");

var receipt = TestUtils.GetRandomTransaction(false);
opts = new JObject
{
["from"] = receipt.Transaction.From.ToHex(),
["to"] = receipt.Transaction.To.ToHex(),
};

result = _apiService!.EstimateGas(opts);
Assert.AreNotEqual(result, "0x");
}

[Test]
/// Changed from private to public
[Ignore("fix it")]
public void Test_GetNetworkGasPrice()
{
var gasPrice_Expected = "0x1";

var gasPrice_Actual = _apiService!.GetNetworkGasPrice();

Assert.AreEqual(gasPrice_Expected, gasPrice_Actual);

System.Console.WriteLine(HardforkHeights.IsHardfork_16Active(_blockManager.GetHeight()));
Assert.AreEqual(result, gasEstimate);
}

// Below methods Execute a Transaction
Expand Down
3 changes: 2 additions & 1 deletion test/Lachain.CoreTest/Resources/config2.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"hardfork_12": 0,
"hardfork_13": 0,
"hardfork_14": 0,
"hardfork_15": 0
"hardfork_15": 0,
"hardfork_16": 10
},
"storage": {
"provider": "RocksDB",
Expand Down
3 changes: 3 additions & 0 deletions test/Lachain.StorageTest/StorageIntergrationTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.IO;
using System.Reflection;
using Lachain.Core.CLI;
using Lachain.Core.Config;
using Lachain.Core.DI;
using Lachain.Core.DI.Modules;
using Lachain.Core.DI.SimpleInjector;
using Lachain.Crypto;
using Lachain.Storage.State;
using Lachain.Utility.Serialization;
using Lachain.Utility.Utils;
using Lachain.UtilityTest;
using NUnit.Framework;
Expand Down

0 comments on commit a381e5b

Please sign in to comment.