forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Designate contract (neo-project#1950)
* add designate contract * format * fix * Some checks * reorganize file * Remove empty line * Update DesignateContract.cs * ArgumentOutOfRangeException * Update DesignateContract.cs * Remove OracleContract.Lists.cs Co-authored-by: Shargon <shargon@gmail.com> Co-authored-by: erikzhang <erik@neo.org>
- Loading branch information
1 parent
488eab3
commit 8546f3c
Showing
6 changed files
with
100 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/neo/SmartContract/Native/Designate/DesignateContract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma warning disable IDE0051 | ||
|
||
using Neo.Cryptography; | ||
using Neo.Cryptography.ECC; | ||
using Neo.IO; | ||
using Neo.Ledger; | ||
using Neo.Persistence; | ||
using Neo.SmartContract.Manifest; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Neo.SmartContract.Native.Designate | ||
{ | ||
public sealed class DesignateContract : NativeContract | ||
{ | ||
public override string Name => "Designation"; | ||
public override int Id => -5; | ||
|
||
internal DesignateContract() | ||
{ | ||
Manifest.Features = ContractFeatures.HasStorage; | ||
} | ||
|
||
internal override void Initialize(ApplicationEngine engine) | ||
{ | ||
foreach (byte role in Enum.GetValues(typeof(Role))) | ||
{ | ||
engine.Snapshot.Storages.Add(CreateStorageKey(role), new StorageItem(new NodeList())); | ||
} | ||
} | ||
|
||
[ContractMethod(0_01000000, CallFlags.AllowStates)] | ||
public ECPoint[] GetDesignatedByRole(StoreView snapshot, Role role) | ||
{ | ||
if (!Enum.IsDefined(typeof(Role), role)) | ||
throw new ArgumentOutOfRangeException(nameof(role)); | ||
return snapshot.Storages[CreateStorageKey((byte)role)].GetInteroperable<NodeList>().ToArray(); | ||
} | ||
|
||
[ContractMethod(0, CallFlags.AllowModifyStates)] | ||
private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes) | ||
{ | ||
if (nodes.Length == 0) throw new ArgumentException(nameof(nodes)); | ||
if (!Enum.IsDefined(typeof(Role), role)) | ||
throw new ArgumentOutOfRangeException(nameof(role)); | ||
if (!CheckCommittee(engine)) throw new InvalidOperationException(); | ||
NodeList list = engine.Snapshot.Storages.GetAndChange(CreateStorageKey((byte)role)).GetInteroperable<NodeList>(); | ||
list.Clear(); | ||
list.AddRange(nodes); | ||
list.Sort(); | ||
} | ||
|
||
private class NodeList : List<ECPoint>, IInteroperable | ||
{ | ||
public void FromStackItem(StackItem stackItem) | ||
{ | ||
foreach (StackItem item in (VM.Types.Array)stackItem) | ||
Add(ECPoint.DecodePoint(item.GetSpan(), ECCurve.Secp256r1)); | ||
} | ||
|
||
public StackItem ToStackItem(ReferenceCounter referenceCounter) | ||
{ | ||
return new VM.Types.Array(referenceCounter, this.Select(p => (StackItem)p.ToArray())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Neo.SmartContract.Native.Designate | ||
{ | ||
public enum Role : byte | ||
{ | ||
StateValidator = 4, | ||
Oracle = 8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
src/neo/SmartContract/Native/Oracle/OracleContract.Lists.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters