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

Allow deploy native contracts in any block (only once) #2056

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ private static Transaction DeployNativeContracts()
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy);
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray());
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray());
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray());
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray());
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray());

script = sb.ToArray();
}
return new Transaction
Expand Down
25 changes: 12 additions & 13 deletions src/neo/SmartContract/ApplicationEngine.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ namespace Neo.SmartContract
{
partial class ApplicationEngine
{
public static readonly InteropDescriptor Neo_Native_Deploy = Register("Neo.Native.Deploy", nameof(DeployNativeContracts), 0, CallFlags.AllowModifyStates, false);
public static readonly InteropDescriptor Neo_Native_Deploy = Register("Neo.Native.Deploy", nameof(DeployNativeContract), 0, CallFlags.AllowModifyStates, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need Neo.Native.Update ? How do we update native contract?

Copy link
Member Author

@shargon shargon Dec 3, 2020

Choose a reason for hiding this comment

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

I think that we should do a condition inside the method, and change the method hash in order to include the arguments, in this way we can have method overloads (or just use other name).

public static readonly InteropDescriptor Neo_Native_Call = Register("Neo.Native.Call", nameof(CallNativeContract), 0, CallFlags.None, false);

protected internal void DeployNativeContracts()
protected internal void DeployNativeContract(UInt160 hash)
{
if (Snapshot.PersistingBlock.Index != 0)
throw new InvalidOperationException();
foreach (NativeContract contract in NativeContract.Contracts)
NativeContract contract = NativeContract.GetContract(hash);
if (contract == null) throw new Exception($"Can't find a native contract with the hash: {hash}");
if (Snapshot.Contracts.Contains(hash)) throw new Exception($"{hash} already deployed");

Snapshot.Contracts.Add(contract.Hash, new ContractState
{
Snapshot.Contracts.Add(contract.Hash, new ContractState
{
Id = contract.Id,
Script = contract.Script,
Manifest = contract.Manifest
});
contract.Initialize(this);
}
Id = contract.Id,
Script = contract.Script,
Manifest = contract.Manifest
});
contract.Initialize(this);
}

protected internal void CallNativeContract(string name)
Expand Down
3 changes: 3 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ protected internal void AddGas(long gas)
{
GasConsumed = checked(GasConsumed + gas);
if (GasConsumed > gas_amount)
{
if (Snapshot?.PersistingBlock?.Index == 0) return;
shargon marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidOperationException("Insufficient GAS.");
}
}

protected override void OnFault(Exception e)
Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public void TestContainsTransaction()
[TestMethod]
public void TestGetCurrentBlockHash()
{
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8"));
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3"));
}

[TestMethod]
public void TestGetCurrentHeaderHash()
{
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8"));
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3"));
}

[TestMethod]
Expand All @@ -91,7 +91,7 @@ public void TestGetBlock()
[TestMethod]
public void TestGetBlockHash()
{
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8"));
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3"));
Blockchain.Singleton.GetBlockHash(10).Should().BeNull();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Ledger/UT_TransactionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void TestDeserialize()
[TestMethod]
public void TestGetSize()
{
((ISerializable)origin).Size.Should().Be(63);
((ISerializable)origin).Size.Should().Be(193);
}
}
}