Skip to content

Commit

Permalink
Create ManagementContract (neo-project#2119)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent fd0ebee commit e59cf38
Show file tree
Hide file tree
Showing 50 changed files with 592 additions and 691 deletions.
51 changes: 4 additions & 47 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -53,7 +52,7 @@ private class UnverifiedBlocksList { public LinkedList<Block> Blocks = new Linke
PrimaryIndex = 0,
Nonce = 2083236893
},
Transactions = new[] { DeployNativeContracts() }
Transactions = Array.Empty<Transaction>()
};

private readonly static Script onPersistScript, postPersistScript;
Expand Down Expand Up @@ -92,20 +91,12 @@ static Blockchain()

using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (NativeContract contract in new NativeContract[] { NativeContract.GAS, NativeContract.NEO })
{
sb.EmitAppCall(contract.Hash, "onPersist");
sb.Emit(OpCode.DROP);
}
sb.EmitSysCall(ApplicationEngine.System_Contract_NativeOnPersist);
onPersistScript = sb.ToArray();
}
using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (NativeContract contract in new NativeContract[] { NativeContract.NEO, NativeContract.Oracle })
{
sb.EmitAppCall(contract.Hash, "postPersist");
sb.Emit(OpCode.DROP);
}
sb.EmitSysCall(ApplicationEngine.System_Contract_NativePostPersist);
postPersistScript = sb.ToArray();
}
}
Expand Down Expand Up @@ -165,39 +156,6 @@ public bool ContainsTransaction(UInt256 hash)
return View.ContainsTransaction(hash);
}

private static Transaction DeployNativeContracts()
{
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy);
script = sb.ToArray();
}
return new Transaction
{
Version = 0,
Script = script,
SystemFee = 0,
Signers = new[]
{
new Signer
{
Account = (new[] { (byte)OpCode.PUSH1 }).ToScriptHash(),
Scopes = WitnessScope.None
}
},
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = new[]
{
new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = new[] { (byte)OpCode.PUSH1 }
}
}
};
}

public Block GetBlock(uint index)
{
if (index == 0) return GenesisBlock;
Expand Down Expand Up @@ -437,9 +395,8 @@ private void Persist(Block block)
}
List<ApplicationExecuted> all_application_executed = new List<ApplicationExecuted>();
snapshot.PersistingBlock = block;
if (block.Index > 0)
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot))
{
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot);
engine.LoadScript(onPersistScript);
if (engine.Execute() != VMState.HALT) throw new InvalidOperationException();
ApplicationExecuted application_executed = new ApplicationExecuted(engine);
Expand Down
37 changes: 0 additions & 37 deletions src/neo/Ledger/ContractIdState.cs

This file was deleted.

95 changes: 0 additions & 95 deletions src/neo/Ledger/ContractState.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/neo/Ledger/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public byte[] Value
return value ??= cache switch
{
BigInteger bi => bi.ToByteArrayStandard(),
IInteroperable interoperable => BinarySerializer.Serialize(interoperable.ToStackItem(null), 4096),
IInteroperable interoperable => BinarySerializer.Serialize(interoperable.ToStackItem(null), 1024 * 1024),
IReadOnlyCollection<ISerializable> list => list.ToByteArray(),
null => null,
_ => throw new InvalidCastException()
Expand Down
4 changes: 0 additions & 4 deletions src/neo/Persistence/ClonedView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ internal class ClonedView : StoreView
{
public override DataCache<UInt256, TrimmedBlock> Blocks { get; }
public override DataCache<UInt256, TransactionState> Transactions { get; }
public override DataCache<UInt160, ContractState> Contracts { get; }
public override DataCache<StorageKey, StorageItem> Storages { get; }
public override DataCache<SerializableWrapper<uint>, HeaderHashList> HeaderHashList { get; }
public override MetaDataCache<HashIndexState> BlockHashIndex { get; }
public override MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public override MetaDataCache<ContractIdState> ContractId { get; }

public ClonedView(StoreView view)
{
this.PersistingBlock = view.PersistingBlock;
this.Blocks = view.Blocks.CreateSnapshot();
this.Transactions = view.Transactions.CreateSnapshot();
this.Contracts = view.Contracts.CreateSnapshot();
this.Storages = view.Storages.CreateSnapshot();
this.HeaderHashList = view.HeaderHashList.CreateSnapshot();
this.BlockHashIndex = view.BlockHashIndex.CreateSnapshot();
this.HeaderHashIndex = view.HeaderHashIndex.CreateSnapshot();
this.ContractId = view.ContractId.CreateSnapshot();
}
}
}
2 changes: 0 additions & 2 deletions src/neo/Persistence/Prefixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ internal static class Prefixes
public const byte DATA_Block = 0x01;
public const byte DATA_Transaction = 0x02;

public const byte ST_Contract = 0x50;
public const byte ST_Storage = 0x70;

public const byte IX_HeaderHashList = 0x80;
public const byte IX_CurrentBlock = 0xc0;
public const byte IX_CurrentHeader = 0xc1;
public const byte IX_ContractId = 0xc2;

/* Prefixes 0xf0 to 0xff are reserved for external use.
*
Expand Down
2 changes: 0 additions & 2 deletions src/neo/Persistence/ReadOnlyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ public class ReadOnlyView : StoreView

public override DataCache<UInt256, TrimmedBlock> Blocks => new StoreDataCache<UInt256, TrimmedBlock>(store, Prefixes.DATA_Block);
public override DataCache<UInt256, TransactionState> Transactions => new StoreDataCache<UInt256, TransactionState>(store, Prefixes.DATA_Transaction);
public override DataCache<UInt160, ContractState> Contracts => new StoreDataCache<UInt160, ContractState>(store, Prefixes.ST_Contract);
public override DataCache<StorageKey, StorageItem> Storages => new StoreDataCache<StorageKey, StorageItem>(store, Prefixes.ST_Storage);
public override DataCache<SerializableWrapper<uint>, HeaderHashList> HeaderHashList => new StoreDataCache<SerializableWrapper<uint>, HeaderHashList>(store, Prefixes.IX_HeaderHashList);
public override MetaDataCache<HashIndexState> BlockHashIndex => new StoreMetaDataCache<HashIndexState>(store, Prefixes.IX_CurrentBlock);
public override MetaDataCache<HashIndexState> HeaderHashIndex => new StoreMetaDataCache<HashIndexState>(store, Prefixes.IX_CurrentHeader);
public override MetaDataCache<ContractIdState> ContractId => new StoreMetaDataCache<ContractIdState>(store, Prefixes.IX_ContractId);

public ReadOnlyView(IReadOnlyStore store)
{
Expand Down
4 changes: 0 additions & 4 deletions src/neo/Persistence/SnapshotView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ public class SnapshotView : StoreView, IDisposable

public override DataCache<UInt256, TrimmedBlock> Blocks { get; }
public override DataCache<UInt256, TransactionState> Transactions { get; }
public override DataCache<UInt160, ContractState> Contracts { get; }
public override DataCache<StorageKey, StorageItem> Storages { get; }
public override DataCache<SerializableWrapper<uint>, HeaderHashList> HeaderHashList { get; }
public override MetaDataCache<HashIndexState> BlockHashIndex { get; }
public override MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public override MetaDataCache<ContractIdState> ContractId { get; }

public SnapshotView(IStore store)
{
this.snapshot = store.GetSnapshot();
Blocks = new StoreDataCache<UInt256, TrimmedBlock>(snapshot, Prefixes.DATA_Block);
Transactions = new StoreDataCache<UInt256, TransactionState>(snapshot, Prefixes.DATA_Transaction);
Contracts = new StoreDataCache<UInt160, ContractState>(snapshot, Prefixes.ST_Contract);
Storages = new StoreDataCache<StorageKey, StorageItem>(snapshot, Prefixes.ST_Storage);
HeaderHashList = new StoreDataCache<SerializableWrapper<uint>, HeaderHashList>(snapshot, Prefixes.IX_HeaderHashList);
BlockHashIndex = new StoreMetaDataCache<HashIndexState>(snapshot, Prefixes.IX_CurrentBlock);
HeaderHashIndex = new StoreMetaDataCache<HashIndexState>(snapshot, Prefixes.IX_CurrentHeader);
ContractId = new StoreMetaDataCache<ContractIdState>(snapshot, Prefixes.IX_ContractId);
}

public override void Commit()
Expand Down
4 changes: 0 additions & 4 deletions src/neo/Persistence/StoreView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ public abstract class StoreView
public Block PersistingBlock { get; internal set; }
public abstract DataCache<UInt256, TrimmedBlock> Blocks { get; }
public abstract DataCache<UInt256, TransactionState> Transactions { get; }
public abstract DataCache<UInt160, ContractState> Contracts { get; }
public abstract DataCache<StorageKey, StorageItem> Storages { get; }
public abstract DataCache<SerializableWrapper<uint>, HeaderHashList> HeaderHashList { get; }
public abstract MetaDataCache<HashIndexState> BlockHashIndex { get; }
public abstract MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public abstract MetaDataCache<ContractIdState> ContractId { get; }

public uint Height => BlockHashIndex.Get().Index;
public uint HeaderHeight => HeaderHashIndex.Get().Index;
Expand All @@ -34,12 +32,10 @@ public virtual void Commit()
{
Blocks.Commit();
Transactions.Commit();
Contracts.Commit();
Storages.Commit();
HeaderHashList.Commit();
BlockHashIndex.Commit();
HeaderHashIndex.Commit();
ContractId.Commit();
}

public bool ContainsBlock(UInt256 hash)
Expand Down
6 changes: 0 additions & 6 deletions src/neo/SmartContract/ApplicationEngine.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Blockchain_GetTransaction = Register("System.Blockchain.GetTransaction", nameof(GetTransaction), 0_01000000, CallFlags.ReadStates, true);
public static readonly InteropDescriptor System_Blockchain_GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", nameof(GetTransactionHeight), 0_01000000, CallFlags.ReadStates, true);
public static readonly InteropDescriptor System_Blockchain_GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", nameof(GetTransactionFromBlock), 0_01000000, CallFlags.ReadStates, true);
public static readonly InteropDescriptor System_Blockchain_GetContract = Register("System.Blockchain.GetContract", nameof(GetContract), 0_01000000, CallFlags.ReadStates, true);

protected internal uint GetBlockchainHeight()
{
Expand Down Expand Up @@ -87,11 +86,6 @@ protected internal Transaction GetTransactionFromBlock(byte[] blockIndexOrHash,
return Snapshot.GetTransaction(block.Hashes[txIndex + 1]);
}

protected internal ContractState GetContract(UInt160 hash)
{
return Snapshot.Contracts.TryGet(hash);
}

private static bool IsTraceableBlock(StoreView snapshot, uint index)
{
if (index > snapshot.Height) return false;
Expand Down
Loading

0 comments on commit e59cf38

Please sign in to comment.