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

Fix contract balance transfer #338

Merged
merged 4 commits into from
Jan 25, 2023
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
1 change: 1 addition & 0 deletions src/Lachain.Core/Blockchain/Hardfork/HardforkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class HardforkConfig
[JsonProperty("hardfork_13")] public ulong? Hardfork_13 { get; set; }
[JsonProperty("hardfork_14")] public ulong? Hardfork_14 { get; set; }
[JsonProperty("hardfork_15")] public ulong? Hardfork_15 { get; set; }
[JsonProperty("hardfork_16")] public ulong? Hardfork_16 { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Lachain.Core/Blockchain/Hardfork/HardforkHeights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class HardforkHeights
private static ulong Hardfork_13;
private static ulong Hardfork_14;
private static ulong Hardfork_15;
private static ulong Hardfork_16;

//we need this value as default deploy height
public static ulong GetHardfork_3()
Expand Down Expand Up @@ -102,6 +103,11 @@ public static bool IsHardfork_15Active(ulong height)
{
return height >= Hardfork_15;
}

public static bool IsHardfork_16Active(ulong height)
{
return height >= Hardfork_16;
}

public static void SetHardforkHeights(HardforkConfig hardforkConfig)
{
Expand Down Expand Up @@ -169,6 +175,10 @@ public static void SetHardforkHeights(HardforkConfig hardforkConfig)
if(hardforkConfig.Hardfork_15 is null)
throw new Exception("hardfork_15 is null");
Hardfork_15 = (ulong) hardforkConfig.Hardfork_15;

if(hardforkConfig.Hardfork_16 is null)
throw new Exception("hardfork_16 is null");
Hardfork_16 = (ulong) hardforkConfig.Hardfork_16;
}
}
}
20 changes: 19 additions & 1 deletion src/Lachain.Core/Blockchain/VM/ExternalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,10 +1484,28 @@ private static bool TransferBalance(
UInt160 from, UInt160 to, Money value, IExecutionFrame frame
)
{
Logger.LogTrace($"Transfer balance of {value.ToString()} is requested from {from.ToHex()} to {to.ToHex()}");
var receipt = frame.InvocationContext.Receipt;
var snapshot = frame.InvocationContext.Snapshot;
var height = snapshot.Blocks.GetTotalBlockHeight();
if (HardforkHeights.IsHardfork_15Active(height))
if (HardforkHeights.IsHardfork_16Active(height))
{
var contract = snapshot.Contracts.GetContractByHash(from);
if (contract is null)
{
// balance transfer from plain address
return snapshot.Balances.TransferBalance(
from, to, value, receipt,
HardforkHeights.IsHardfork_15Active(height), HardforkHeights.IsHardfork_9Active(height)
);
}
else
{
// allow balance transfer from contract address
return snapshot.Balances.TransferContractBalance(from, to, value);
}
}
else if (HardforkHeights.IsHardfork_15Active(height))
{
var contract = snapshot.Contracts.GetContractByHash(from);
if (contract is null)
Expand Down
32 changes: 30 additions & 2 deletions src/Lachain.Core/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Lachain.Core.Config
{
public class ConfigManager : IConfigManager
{
private const ulong _CurrentVersion = 18;
private const ulong _CurrentVersion = 19;
private IDictionary<string, object> _config;
public string ConfigPath { get; }
public RunOptions CommandLineOptions { get; }
Expand Down Expand Up @@ -51,7 +51,7 @@ public void UpdateWalletPassword(string password)
private void _UpdateConfigVersion()
{
ulong version = 1;
version = GetConfig<VersionConfig>("version")?.Version ?? 1;
version = GetConfig<VersionConfig>("versionInfo")?.Version ?? 1;
if (version > _CurrentVersion)
throw new ApplicationException("Unknown config version");
if (version == _CurrentVersion)
Expand Down Expand Up @@ -90,6 +90,8 @@ private void _UpdateConfigVersion()
_UpdateConfigToV17();
if (version < 18)
_UpdateConfigToV18();
if (version < 19)
_UpdateConfigToV19();
version = GetConfig<VersionConfig>("version")?.Version ??
throw new ApplicationException("No version section in config");
if (version != _CurrentVersion)
Expand Down Expand Up @@ -547,6 +549,32 @@ private void _UpdateConfigToV18()

_SaveCurrentConfig();
}

// version 19 of config should contain hardfork_16
private void _UpdateConfigToV19()
{
var network = GetConfig<NetworkConfig>("network") ??
throw new ApplicationException("No network section in config");

var hardforks = GetConfig<HardforkConfig>("hardfork") ??
throw new ApplicationException("No hardfork section in config");
hardforks.Hardfork_16 ??= network.NetworkName switch
{
"mainnet" => 8028800,
"testnet" => 7751300,
"devnet" => 7887130,
_ => 0
};
_config["hardfork"] = JObject.FromObject(hardforks);

var version = GetConfig<VersionConfig>("version") ??
throw new ApplicationException("No version section in config");

version.Version = 19;
_config["version"] = JObject.FromObject(version);

_SaveCurrentConfig();
}

private void _SaveCurrentConfig()
{
Expand Down