Exemple #1
0
        private bool PutEx(StorageContext context, byte[] key, byte[] value, StorageFlags flags)
        {
            if (Trigger != TriggerType.Application && Trigger != TriggerType.ApplicationR)
            {
                return(false);
            }
            if (key.Length > 1024)
            {
                return(false);
            }
            if (context.IsReadOnly)
            {
                return(false);
            }
            if (!CheckStorageContext(context))
            {
                return(false);
            }
            StorageKey skey = new StorageKey
            {
                ScriptHash = context.ScriptHash,
                Key        = key
            };
            StorageItem item = Snapshot.Storages.GetAndChange(skey, () => new StorageItem());

            if (item.IsConstant)
            {
                return(false);
            }
            item.Value      = value;
            item.IsConstant = flags.HasFlag(StorageFlags.Constant);
            return(true);
        }
Exemple #2
0
        private static bool PutEx(ApplicationEngine engine, StorageContext context, byte[] key, byte[] value, StorageFlags flags)
        {
            if (engine.Trigger != TriggerType.Application)
            {
                return(false);
            }
            if (key.Length > MaxStorageKeySize)
            {
                return(false);
            }
            if (value.Length > MaxStorageValueSize)
            {
                return(false);
            }
            if (context.IsReadOnly)
            {
                return(false);
            }
            if (!CheckStorageContext(engine, context))
            {
                return(false);
            }

            StorageKey skey = new StorageKey
            {
                ScriptHash = context.ScriptHash,
                Key        = key
            };

            if (engine.Snapshot.Storages.TryGet(skey)?.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`)
                engine.Snapshot.Storages.Delete(skey);
            }
            else
            {
                StorageItem item = engine.Snapshot.Storages.GetAndChange(skey, () => new StorageItem());
                item.Value      = value;
                item.IsConstant = flags.HasFlag(StorageFlags.Constant);
            }
            return(true);
        }
Exemple #3
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);
        }
Exemple #4
0
        private void PutExInternal(StorageContext context, byte[] key, byte[] value, StorageFlags flags)
        {
            if (key.Length > MaxStorageKeySize || value.Length > MaxStorageValueSize || context.IsReadOnly)
            {
                throw new ArgumentException();
            }

            int        newDataSize;
            StorageKey skey = new StorageKey
            {
                Id  = context.Id,
                Key = key
            };
            StorageItem item = Snapshot.Storages.GetAndChange(skey);

            if (item is null)
            {
                newDataSize = key.Length + value.Length;
                Snapshot.Storages.Add(skey, item = new StorageItem());
            }
            else
            {
                if (item.IsConstant)
                {
                    throw new InvalidOperationException();
                }
                if (value.Length == 0)
                {
                    newDataSize = 1;
                }
                else if (value.Length <= item.Value.Length)
                {
                    newDataSize = (value.Length - 1) / 4 + 1;
                }
                else
                {
                    newDataSize = (item.Value.Length - 1) / 4 + 1 + value.Length - item.Value.Length;
                }
            }
            AddGas(newDataSize * StoragePrice);

            item.Value      = value;
            item.IsConstant = flags.HasFlag(StorageFlags.Constant);
        }
Exemple #5
0
        private static bool PutEx(ApplicationEngine engine, StorageContext context, byte[] key, byte[] value, StorageFlags flags)
        {
            if (engine.Trigger != TriggerType.Application)
            {
                return(false);
            }
            if (key.Length > MaxStorageKeySize)
            {
                return(false);
            }
            if (value.Length > MaxStorageValueSize)
            {
                return(false);
            }
            if (context.IsReadOnly)
            {
                return(false);
            }
            if (!CheckStorageContext(engine, context))
            {
                return(false);
            }
            StorageKey skey = new StorageKey
            {
                ScriptHash = context.ScriptHash,
                Key        = key
            };
            StorageItem item = engine.Snapshot.Storages.GetAndChange(skey, () => new StorageItem());

            if (item.IsConstant)
            {
                return(false);
            }
            item.Value      = value;
            item.IsConstant = flags.HasFlag(StorageFlags.Constant);
            return(true);
        }