Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }