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

Perform native contract initialization for the set of hardforks #3202

Merged
merged 19 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
15 changes: 13 additions & 2 deletions src/Neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
{
foreach (NativeContract contract in Contracts)
{
if (contract.IsInitializeBlock(engine.ProtocolSettings, engine.PersistingBlock.Index, out Hardfork? hf))
if (contract.IsInitializeBlock(engine.ProtocolSettings, engine.PersistingBlock.Index, out var hfs))
{
ContractState contractState = contract.GetContractState(engine.ProtocolSettings, engine.PersistingBlock.Index);
StorageItem state = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Contract).Add(contract.Hash));
Expand All @@ -80,6 +80,9 @@
// Create the contract state
engine.Snapshot.Add(CreateStorageKey(Prefix_Contract).Add(contract.Hash), new StorageItem(contractState));
engine.Snapshot.Add(CreateStorageKey(Prefix_ContractHash).AddBigEndian(contract.Id), new StorageItem(contract.Hash.ToArray()));

// Initialize the native smart contract
await contract.Initialize(engine, null);

Check failure on line 85 in src/Neo/SmartContract/Native/ContractManagement.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NativeContract' does not contain a definition for 'Initialize' and no accessible extension method 'Initialize' accepting a first argument of type 'NativeContract' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 85 in src/Neo/SmartContract/Native/ContractManagement.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NativeContract' does not contain a definition for 'Initialize' and no accessible extension method 'Initialize' accepting a first argument of type 'NativeContract' could be found (are you missing a using directive or an assembly reference?)
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
Expand All @@ -92,7 +95,15 @@
oldContract.Manifest = contractState.Manifest;
}

await contract.InitializeAsync(engine, hf);
// Initialize native contract for all hardforks
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (hfs?.Length > 0)
{
foreach (var hf in hfs)
{
await contract.Initialize(engine, hf);

Check failure on line 103 in src/Neo/SmartContract/Native/ContractManagement.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NativeContract' does not contain a definition for 'Initialize' and no accessible extension method 'Initialize' accepting a first argument of type 'NativeContract' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 103 in src/Neo/SmartContract/Native/ContractManagement.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NativeContract' does not contain a definition for 'Initialize' and no accessible extension method 'Initialize' accepting a first argument of type 'NativeContract' could be found (are you missing a using directive or an assembly reference?)
shargon marked this conversation as resolved.
Show resolved Hide resolved
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Emit native contract notification
engine.SendNotification(Hash, state is null ? "Deploy" : "Update", new VM.Types.Array(engine.ReferenceCounter) { contract.Hash.ToArray() });
}
Expand Down
34 changes: 21 additions & 13 deletions src/Neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,14 @@ protected virtual void OnManifestCompose(ContractManifest manifest) { }
/// </summary>
/// <param name="settings">The <see cref="ProtocolSettings"/> where the HardForks are configured.</param>
/// <param name="index">Block index</param>
/// <param name="hardfork">Active hardfork</param>
/// <param name="hardforks">Active hardforks</param>
/// <returns>True if the native contract must be initialized</returns>
internal bool IsInitializeBlock(ProtocolSettings settings, uint index, out Hardfork? hardfork)
internal bool IsInitializeBlock(ProtocolSettings settings, uint index, out Hardfork[] hardforks)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
// If is not configured, the Genesis is the a initialized block
if (index == 0 && ActiveIn is null)
{
hardfork = null;
return true;
}
var hfs = new List<Hardfork>();

// If is in the hardfork height, return true
foreach (Hardfork hf in usedHardforks)
// If is in the hardfork height, add them to return array
foreach (var hf in usedHardforks)
{
if (!settings.Hardforks.TryGetValue(hf, out var activeIn))
{
Expand All @@ -288,13 +283,26 @@ internal bool IsInitializeBlock(ProtocolSettings settings, uint index, out Hardf

if (activeIn == index)
{
hardfork = hf;
return true;
hfs.Add(hf);
}
}
shargon marked this conversation as resolved.
Show resolved Hide resolved

// Return all initialize hardforks
if (hfs.Count > 0)
{
hardforks = hfs.ToArray();
return true;
}

// If is not configured, the Genesis is an initialization block.
if (index == 0 && ActiveIn is null)
{
hardforks = hfs.ToArray();
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
hardforks = hfs.ToArray();
hardforks = [];

Plus move this above the if statement above it; line:90.

AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

// Initialized not required
hardfork = null;
hardforks = null;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public void TestIsInitializeBlock()
Assert.IsNull(hf);

Assert.IsTrue(NativeContract.CryptoLib.IsInitializeBlock(settings, 20, out hf));
Assert.AreEqual(Hardfork.HF_Cockatrice, hf);
Assert.AreEqual(1, hf.Length);
Assert.AreEqual(Hardfork.HF_Cockatrice, hf[0]);
}

[TestMethod]
Expand Down
Loading