Skip to content

Commit 159a47d

Browse files
authored
Merge pull request #338 from LATOKEN/fix-contract-balance-transfer
Fix contract balance transfer
2 parents 29ac2c5 + db57d86 commit 159a47d

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

src/Lachain.Core/Blockchain/Hardfork/HardforkConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public class HardforkConfig
1919
[JsonProperty("hardfork_13")] public ulong? Hardfork_13 { get; set; }
2020
[JsonProperty("hardfork_14")] public ulong? Hardfork_14 { get; set; }
2121
[JsonProperty("hardfork_15")] public ulong? Hardfork_15 { get; set; }
22+
[JsonProperty("hardfork_16")] public ulong? Hardfork_16 { get; set; }
2223
}
2324
}

src/Lachain.Core/Blockchain/Hardfork/HardforkHeights.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static class HardforkHeights
2020
private static ulong Hardfork_13;
2121
private static ulong Hardfork_14;
2222
private static ulong Hardfork_15;
23+
private static ulong Hardfork_16;
2324

2425
//we need this value as default deploy height
2526
public static ulong GetHardfork_3()
@@ -102,6 +103,11 @@ public static bool IsHardfork_15Active(ulong height)
102103
{
103104
return height >= Hardfork_15;
104105
}
106+
107+
public static bool IsHardfork_16Active(ulong height)
108+
{
109+
return height >= Hardfork_16;
110+
}
105111

106112
public static void SetHardforkHeights(HardforkConfig hardforkConfig)
107113
{
@@ -169,6 +175,10 @@ public static void SetHardforkHeights(HardforkConfig hardforkConfig)
169175
if(hardforkConfig.Hardfork_15 is null)
170176
throw new Exception("hardfork_15 is null");
171177
Hardfork_15 = (ulong) hardforkConfig.Hardfork_15;
178+
179+
if(hardforkConfig.Hardfork_16 is null)
180+
throw new Exception("hardfork_16 is null");
181+
Hardfork_16 = (ulong) hardforkConfig.Hardfork_16;
172182
}
173183
}
174184
}

src/Lachain.Core/Blockchain/VM/ExternalHandler.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,28 @@ private static bool TransferBalance(
14841484
UInt160 from, UInt160 to, Money value, IExecutionFrame frame
14851485
)
14861486
{
1487+
Logger.LogTrace($"Transfer balance of {value.ToString()} is requested from {from.ToHex()} to {to.ToHex()}");
14871488
var receipt = frame.InvocationContext.Receipt;
14881489
var snapshot = frame.InvocationContext.Snapshot;
14891490
var height = snapshot.Blocks.GetTotalBlockHeight();
1490-
if (HardforkHeights.IsHardfork_15Active(height))
1491+
if (HardforkHeights.IsHardfork_16Active(height))
1492+
{
1493+
var contract = snapshot.Contracts.GetContractByHash(from);
1494+
if (contract is null)
1495+
{
1496+
// balance transfer from plain address
1497+
return snapshot.Balances.TransferBalance(
1498+
from, to, value, receipt,
1499+
HardforkHeights.IsHardfork_15Active(height), HardforkHeights.IsHardfork_9Active(height)
1500+
);
1501+
}
1502+
else
1503+
{
1504+
// allow balance transfer from contract address
1505+
return snapshot.Balances.TransferContractBalance(from, to, value);
1506+
}
1507+
}
1508+
else if (HardforkHeights.IsHardfork_15Active(height))
14911509
{
14921510
var contract = snapshot.Contracts.GetContractByHash(from);
14931511
if (contract is null)

src/Lachain.Core/Config/ConfigManager.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Lachain.Core.Config
1414
{
1515
public class ConfigManager : IConfigManager
1616
{
17-
private const ulong _CurrentVersion = 18;
17+
private const ulong _CurrentVersion = 19;
1818
private IDictionary<string, object> _config;
1919
public string ConfigPath { get; }
2020
public RunOptions CommandLineOptions { get; }
@@ -51,7 +51,7 @@ public void UpdateWalletPassword(string password)
5151
private void _UpdateConfigVersion()
5252
{
5353
ulong version = 1;
54-
version = GetConfig<VersionConfig>("version")?.Version ?? 1;
54+
version = GetConfig<VersionConfig>("versionInfo")?.Version ?? 1;
5555
if (version > _CurrentVersion)
5656
throw new ApplicationException("Unknown config version");
5757
if (version == _CurrentVersion)
@@ -90,6 +90,8 @@ private void _UpdateConfigVersion()
9090
_UpdateConfigToV17();
9191
if (version < 18)
9292
_UpdateConfigToV18();
93+
if (version < 19)
94+
_UpdateConfigToV19();
9395
version = GetConfig<VersionConfig>("version")?.Version ??
9496
throw new ApplicationException("No version section in config");
9597
if (version != _CurrentVersion)
@@ -547,6 +549,32 @@ private void _UpdateConfigToV18()
547549

548550
_SaveCurrentConfig();
549551
}
552+
553+
// version 19 of config should contain hardfork_16
554+
private void _UpdateConfigToV19()
555+
{
556+
var network = GetConfig<NetworkConfig>("network") ??
557+
throw new ApplicationException("No network section in config");
558+
559+
var hardforks = GetConfig<HardforkConfig>("hardfork") ??
560+
throw new ApplicationException("No hardfork section in config");
561+
hardforks.Hardfork_16 ??= network.NetworkName switch
562+
{
563+
"mainnet" => 8028800,
564+
"testnet" => 7751300,
565+
"devnet" => 7887130,
566+
_ => 0
567+
};
568+
_config["hardfork"] = JObject.FromObject(hardforks);
569+
570+
var version = GetConfig<VersionConfig>("version") ??
571+
throw new ApplicationException("No version section in config");
572+
573+
version.Version = 19;
574+
_config["version"] = JObject.FromObject(version);
575+
576+
_SaveCurrentConfig();
577+
}
550578

551579
private void _SaveCurrentConfig()
552580
{

0 commit comments

Comments
 (0)