private bool Contract_Storage_Put() { if (!(CurrentContext.EvaluationStack.Pop() is InteropInterface _interface)) { return(false); } TestStorageContext context = _interface.GetInterface <TestStorageContext>(); byte[] key = CurrentContext.EvaluationStack.Pop().GetByteArray(); byte[] value = CurrentContext.EvaluationStack.Pop().GetByteArray(); return(PutEx(context, key, value, StorageFlags.None)); }
private bool PutEx(TestStorageContext context, byte[] key, byte[] value, StorageFlags flags) { if (key.Length > MaxStorageKeySize) { return(false); } if (value.Length > MaxStorageValueSize) { return(false); } if (context.IsReadOnly) { return(false); } StorageKey skey = new StorageKey { ScriptHash = context.ScriptHash, Key = key }; if (Storages.TryGetValue(skey, out var item) && item.IsConstant == true) { return(false); } if (value.Length == 0 && !flags.HasFlag(StorageFlags.Constant)) { // If put 'value' is empty (and non-const), we remove it (implicit `Storage.Delete`) Storages.Remove(skey); } else { item = Storages[skey] = new StorageItem(); item.Value = value; item.IsConstant = flags.HasFlag(StorageFlags.Constant); } return(true); }
private bool Contract_Storage_Get() { if (CurrentContext.EvaluationStack.Pop() is InteropInterface _interface) { TestStorageContext context = _interface.GetInterface <TestStorageContext>(); byte[] key = CurrentContext.EvaluationStack.Pop().GetByteArray(); if (Storages.TryGetValue(new StorageKey { ScriptHash = context.ScriptHash, Key = key }, out var item)) { CurrentContext.EvaluationStack.Push(item.Value); } else { CurrentContext.EvaluationStack.Push(new byte[0]); } return(true); } return(false); }
private bool Contract_Storage_Delete() { if (CurrentContext.EvaluationStack.Pop() is InteropInterface _interface) { TestStorageContext context = _interface.GetInterface <TestStorageContext>(); if (context.IsReadOnly) { return(false); } StorageKey key = new StorageKey { ScriptHash = context.ScriptHash, Key = CurrentContext.EvaluationStack.Pop().GetByteArray() }; if (Storages.TryGetValue(key, out var item) && item.IsConstant == true) { return(false); } Storages.Remove(key); return(true); } return(false); }