Skip to content

Commit

Permalink
Classes related to Neo.SmartContract types should not be internal (#1785
Browse files Browse the repository at this point in the history
)

* Classes related to Neo.SmartContract types should not be internal
Fixes #1782

* public callbacks + protected internal service methods

* Add AsReadOnly()

Co-authored-by: Harry Pierson <[email protected]>
Co-authored-by: Shargon <[email protected]>
  • Loading branch information
3 people authored Jul 22, 2020
1 parent d542ac3 commit 54a1094
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 72 deletions.
8 changes: 4 additions & 4 deletions src/neo/SmartContract/ApplicationEngine.Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Binary_Base64Encode = Register("System.Binary.Base64Encode", nameof(Base64Encode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base64Decode = Register("System.Binary.Base64Decode", nameof(Base64Decode), 0_00100000, CallFlags.None, true);

internal byte[] BinarySerialize(StackItem item)
protected internal byte[] BinarySerialize(StackItem item)
{
return BinarySerializer.Serialize(item, MaxItemSize);
}

internal StackItem BinaryDeserialize(byte[] data)
protected internal StackItem BinaryDeserialize(byte[] data)
{
return BinarySerializer.Deserialize(data, MaxStackSize, MaxItemSize, ReferenceCounter);
}

internal string Base64Encode(byte[] data)
protected internal string Base64Encode(byte[] data)
{
return ToBase64String(data);
}

internal byte[] Base64Decode(string s)
protected internal byte[] Base64Decode(string s)
{
return FromBase64String(s);
}
Expand Down
12 changes: 6 additions & 6 deletions src/neo/SmartContract/ApplicationEngine.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Blockchain_GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", nameof(GetTransactionFromBlock), 0_01000000, CallFlags.AllowStates, true);
public static readonly InteropDescriptor System_Blockchain_GetContract = Register("System.Blockchain.GetContract", nameof(GetContract), 0_01000000, CallFlags.AllowStates, true);

internal uint GetBlockchainHeight()
protected internal uint GetBlockchainHeight()
{
return Snapshot.Height;
}

internal Block GetBlock(byte[] indexOrHash)
protected internal Block GetBlock(byte[] indexOrHash)
{
UInt256 hash;
if (indexOrHash.Length < UInt256.Length)
Expand All @@ -47,22 +47,22 @@ internal Block GetBlock(byte[] indexOrHash)
return block;
}

internal Transaction GetTransaction(UInt256 hash)
protected internal Transaction GetTransaction(UInt256 hash)
{
TransactionState state = Snapshot.Transactions.TryGet(hash);
if (state != null && !IsTraceableBlock(Snapshot, state.BlockIndex)) state = null;
return state?.Transaction;
}

internal int GetTransactionHeight(UInt256 hash)
protected internal int GetTransactionHeight(UInt256 hash)
{
TransactionState state = Snapshot.Transactions.TryGet(hash);
if (state is null) return -1;
if (!IsTraceableBlock(Snapshot, state.BlockIndex)) return -1;
return (int)state.BlockIndex;
}

internal Transaction GetTransactionFromBlock(byte[] blockIndexOrHash, int txIndex)
protected internal Transaction GetTransactionFromBlock(byte[] blockIndexOrHash, int txIndex)
{
UInt256 hash;
if (blockIndexOrHash.Length < UInt256.Length)
Expand All @@ -89,7 +89,7 @@ internal Transaction GetTransactionFromBlock(byte[] blockIndexOrHash, int txInde
return Snapshot.GetTransaction(block.Hashes[txIndex + 1]);
}

internal ContractState GetContract(UInt160 hash)
protected internal ContractState GetContract(UInt160 hash)
{
return Snapshot.Contracts.TryGet(hash);
}
Expand Down
8 changes: 4 additions & 4 deletions src/neo/SmartContract/ApplicationEngine.Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Callback_CreateFromSyscall = Register("System.Callback.CreateFromSyscall", nameof(CreateCallbackFromSyscall), 0_00000400, CallFlags.None, false);
public static readonly InteropDescriptor System_Callback_Invoke = Register("System.Callback.Invoke", nameof(InvokeCallback), 0_01000000, CallFlags.None, false);

internal void InvokeCallback(CallbackBase callback, Array args)
protected internal void InvokeCallback(CallbackBase callback, Array args)
{
if (args.Count != callback.ParametersCount)
throw new InvalidOperationException();
Expand All @@ -21,17 +21,17 @@ internal void InvokeCallback(CallbackBase callback, Array args)
OnSysCall(syscallCallback.Method);
}

internal PointerCallback CreateCallback(Pointer pointer, int parcount)
protected internal PointerCallback CreateCallback(Pointer pointer, int parcount)
{
return new PointerCallback(CurrentContext, pointer, parcount);
}

internal MethodCallback CreateCallbackFromMethod(UInt160 hash, string method)
protected internal MethodCallback CreateCallbackFromMethod(UInt160 hash, string method)
{
return new MethodCallback(this, hash, method);
}

internal SyscallCallback CreateCallbackFromSyscall(uint method)
protected internal SyscallCallback CreateCallbackFromSyscall(uint method)
{
return new SyscallCallback(method);
}
Expand Down
16 changes: 8 additions & 8 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ partial class ApplicationEngine
/// </summary>
public static readonly InteropDescriptor System_Contract_CreateStandardAccount = Register("System.Contract.CreateStandardAccount", nameof(CreateStandardAccount), 0_00010000, CallFlags.None, true);

internal ContractState CreateContract(byte[] script, byte[] manifest)
protected internal ContractState CreateContract(byte[] script, byte[] manifest)
{
if (script.Length == 0 || script.Length > MaxContractLength || manifest.Length == 0 || manifest.Length > ContractManifest.MaxLength)
throw new ArgumentException();
Expand All @@ -51,7 +51,7 @@ internal ContractState CreateContract(byte[] script, byte[] manifest)
return contract;
}

internal void UpdateContract(byte[] script, byte[] manifest)
protected internal void UpdateContract(byte[] script, byte[] manifest)
{
AddGas(StoragePrice * (script?.Length ?? 0 + manifest?.Length ?? 0));

Expand Down Expand Up @@ -88,7 +88,7 @@ internal void UpdateContract(byte[] script, byte[] manifest)
}
}

internal void DestroyContract()
protected internal void DestroyContract()
{
UInt160 hash = CurrentScriptHash;
ContractState contract = Snapshot.Contracts.TryGet(hash);
Expand All @@ -99,12 +99,12 @@ internal void DestroyContract()
Snapshot.Storages.Delete(key);
}

internal void CallContract(UInt160 contractHash, string method, Array args)
protected internal void CallContract(UInt160 contractHash, string method, Array args)
{
CallContractInternal(contractHash, method, args, CallFlags.All);
}

internal void CallContractEx(UInt160 contractHash, string method, Array args, CallFlags callFlags)
protected internal void CallContractEx(UInt160 contractHash, string method, Array args, CallFlags callFlags)
{
if ((callFlags & ~CallFlags.All) != 0)
throw new ArgumentOutOfRangeException(nameof(callFlags));
Expand Down Expand Up @@ -162,7 +162,7 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg
if (md != null) LoadClonedContext(md.Offset);
}

internal bool IsStandardContract(UInt160 hash)
protected internal bool IsStandardContract(UInt160 hash)
{
ContractState contract = Snapshot.Contracts.TryGet(hash);

Expand All @@ -188,13 +188,13 @@ internal bool IsStandardContract(UInt160 hash)
return false;
}

internal CallFlags GetCallFlags()
protected internal CallFlags GetCallFlags()
{
var state = CurrentContext.GetState<ExecutionContextState>();
return state.CallFlags;
}

internal UInt160 CreateStandardAccount(ECPoint pubKey)
protected internal UInt160 CreateStandardAccount(ECPoint pubKey)
{
return Contract.CreateSignatureRedeemScript(pubKey).ToScriptHash();
}
Expand Down
12 changes: 6 additions & 6 deletions src/neo/SmartContract/ApplicationEngine.Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ partial class ApplicationEngine
public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256r1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256r1", nameof(CheckMultisigWithECDsaSecp256r1), 0, CallFlags.None, true);
public static readonly InteropDescriptor Neo_Crypto_CheckMultisigWithECDsaSecp256k1 = Register("Neo.Crypto.CheckMultisigWithECDsaSecp256k1", nameof(CheckMultisigWithECDsaSecp256k1), 0, CallFlags.None, true);

internal byte[] RIPEMD160(StackItem item)
protected internal byte[] RIPEMD160(StackItem item)
{
ReadOnlySpan<byte> value = item switch
{
Expand All @@ -29,7 +29,7 @@ internal byte[] RIPEMD160(StackItem item)
return value.RIPEMD160();
}

internal byte[] Sha256(StackItem item)
protected internal byte[] Sha256(StackItem item)
{
ReadOnlySpan<byte> value = item switch
{
Expand All @@ -40,12 +40,12 @@ internal byte[] Sha256(StackItem item)
return value.Sha256();
}

internal bool VerifyWithECDsaSecp256r1(StackItem item, byte[] pubkey, byte[] signature)
protected internal bool VerifyWithECDsaSecp256r1(StackItem item, byte[] pubkey, byte[] signature)
{
return VerifyWithECDsa(item, pubkey, signature, ECCurve.Secp256r1);
}

internal bool VerifyWithECDsaSecp256k1(StackItem item, byte[] pubkey, byte[] signature)
protected internal bool VerifyWithECDsaSecp256k1(StackItem item, byte[] pubkey, byte[] signature)
{
return VerifyWithECDsa(item, pubkey, signature, ECCurve.Secp256k1);
}
Expand All @@ -68,12 +68,12 @@ private bool VerifyWithECDsa(StackItem item, byte[] pubkey, byte[] signature, EC
}
}

internal bool CheckMultisigWithECDsaSecp256r1(StackItem item, byte[][] pubkeys, byte[][] signatures)
protected internal bool CheckMultisigWithECDsaSecp256r1(StackItem item, byte[][] pubkeys, byte[][] signatures)
{
return CheckMultiSigWithECDsa(item, pubkeys, signatures, ECCurve.Secp256r1);
}

internal bool CheckMultisigWithECDsaSecp256k1(StackItem item, byte[][] pubkeys, byte[][] signatures)
protected internal bool CheckMultisigWithECDsaSecp256k1(StackItem item, byte[][] pubkeys, byte[][] signatures)
{
return CheckMultiSigWithECDsa(item, pubkeys, signatures, ECCurve.Secp256k1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/neo/SmartContract/ApplicationEngine.Enumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Enumerator_Value = Register("System.Enumerator.Value", nameof(EnumeratorValue), 0_00000400, CallFlags.None, false);
public static readonly InteropDescriptor System_Enumerator_Concat = Register("System.Enumerator.Concat", nameof(ConcatEnumerators), 0_00000400, CallFlags.None, false);

internal IEnumerator CreateEnumerator(StackItem item)
protected internal IEnumerator CreateEnumerator(StackItem item)
{
return item switch
{
Expand All @@ -24,17 +24,17 @@ internal IEnumerator CreateEnumerator(StackItem item)
};
}

internal bool EnumeratorNext(IEnumerator enumerator)
protected internal bool EnumeratorNext(IEnumerator enumerator)
{
return enumerator.Next();
}

internal StackItem EnumeratorValue(IEnumerator enumerator)
protected internal StackItem EnumeratorValue(IEnumerator enumerator)
{
return enumerator.Value();
}

internal IEnumerator ConcatEnumerators(IEnumerator first, IEnumerator second)
protected internal IEnumerator ConcatEnumerators(IEnumerator first, IEnumerator second)
{
return new ConcatenatedEnumerator(first, second);
}
Expand Down
10 changes: 5 additions & 5 deletions src/neo/SmartContract/ApplicationEngine.Iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Iterator_Values = Register("System.Iterator.Values", nameof(IteratorValues), 0_00000400, CallFlags.None, false);
public static readonly InteropDescriptor System_Iterator_Concat = Register("System.Iterator.Concat", nameof(ConcatIterators), 0_00000400, CallFlags.None, false);

internal IIterator CreateIterator(StackItem item)
protected internal IIterator CreateIterator(StackItem item)
{
return item switch
{
Expand All @@ -26,22 +26,22 @@ internal IIterator CreateIterator(StackItem item)
};
}

internal PrimitiveType IteratorKey(IIterator iterator)
protected internal PrimitiveType IteratorKey(IIterator iterator)
{
return iterator.Key();
}

internal IEnumerator IteratorKeys(IIterator iterator)
protected internal IEnumerator IteratorKeys(IIterator iterator)
{
return new IteratorKeysWrapper(iterator);
}

internal IEnumerator IteratorValues(IIterator iterator)
protected internal IEnumerator IteratorValues(IIterator iterator)
{
return new IteratorValuesWrapper(iterator);
}

internal IIterator ConcatIterators(IIterator first, IIterator second)
protected internal IIterator ConcatIterators(IIterator first, IIterator second)
{
return new ConcatenatedIterator(first, second);
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Json_Serialize = Register("System.Json.Serialize", nameof(JsonSerialize), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Json_Deserialize = Register("System.Json.Deserialize", nameof(JsonDeserialize), 0_00500000, CallFlags.None, true);

internal byte[] JsonSerialize(StackItem item)
protected internal byte[] JsonSerialize(StackItem item)
{
return JsonSerializer.SerializeToByteArray(item, MaxItemSize);
}

internal StackItem JsonDeserialize(byte[] json)
protected internal StackItem JsonDeserialize(byte[] json)
{
return JsonSerializer.Deserialize(JObject.Parse(json, 10), ReferenceCounter);
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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_Call = Register("Neo.Native.Call", nameof(CallNativeContract), 0, CallFlags.None, false);

internal void DeployNativeContracts()
protected internal void DeployNativeContracts()
{
if (Snapshot.PersistingBlock.Index != 0)
throw new InvalidOperationException();
Expand All @@ -25,7 +25,7 @@ internal void DeployNativeContracts()
}
}

internal void CallNativeContract(string name)
protected internal void CallNativeContract(string name)
{
NativeContract.GetContract(name).Invoke(this);
}
Expand Down
20 changes: 10 additions & 10 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ private static bool CheckItemForNotification(StackItem state)
}
}

internal string GetPlatform()
protected internal string GetPlatform()
{
return "NEO";
}

internal ulong GetTime()
protected internal ulong GetTime()
{
return Snapshot.PersistingBlock.Timestamp;
}

internal IInteroperable GetScriptContainer()
protected internal IInteroperable GetScriptContainer()
{
return ScriptContainer as IInteroperable;
}

internal bool CheckWitness(byte[] hashOrPubkey)
protected internal bool CheckWitness(byte[] hashOrPubkey)
{
UInt160 hash = hashOrPubkey.Length switch
{
Expand All @@ -100,7 +100,7 @@ internal bool CheckWitness(byte[] hashOrPubkey)
return CheckWitnessInternal(hash);
}

internal bool CheckWitnessInternal(UInt160 hash)
protected internal bool CheckWitnessInternal(UInt160 hash)
{
if (ScriptContainer is Transaction tx)
{
Expand Down Expand Up @@ -133,36 +133,36 @@ internal bool CheckWitnessInternal(UInt160 hash)
return hashes_for_verifying.Contains(hash);
}

internal int GetInvocationCounter()
protected internal int GetInvocationCounter()
{
if (!invocationCounter.TryGetValue(CurrentScriptHash, out var counter))
throw new InvalidOperationException();
return counter;
}

internal void RuntimeLog(byte[] state)
protected internal void RuntimeLog(byte[] state)
{
if (state.Length > MaxNotificationSize) throw new ArgumentException();
string message = Utility.StrictUTF8.GetString(state);
Log?.Invoke(this, new LogEventArgs(ScriptContainer, CurrentScriptHash, message));
}

internal void RuntimeNotify(byte[] eventName, Array state)
protected internal void RuntimeNotify(byte[] eventName, Array state)
{
if (eventName.Length > MaxEventName) throw new ArgumentException();
if (!CheckItemForNotification(state)) throw new ArgumentException();
SendNotification(CurrentScriptHash, Utility.StrictUTF8.GetString(eventName), state);
}

internal void SendNotification(UInt160 hash, string eventName, Array state)
protected internal void SendNotification(UInt160 hash, string eventName, Array state)
{
NotifyEventArgs notification = new NotifyEventArgs(ScriptContainer, hash, eventName, (Array)state.DeepCopy());
Notify?.Invoke(this, notification);
notifications ??= new List<NotifyEventArgs>();
notifications.Add(notification);
}

internal NotifyEventArgs[] GetNotifications(UInt160 hash)
protected internal NotifyEventArgs[] GetNotifications(UInt160 hash)
{
IEnumerable<NotifyEventArgs> notifications = Notifications;
if (hash != null) // must filter by scriptHash
Expand Down
Loading

0 comments on commit 54a1094

Please sign in to comment.