Пример #1
0
        public byte[] ReadStorage(byte[] key)
        {
            // If we already have cached storage data, return it. Otherwise we'll need to cache some.
            if (!StorageCache.TryGetValue(key, out var val))
            {
                // We obtain the value from our storage trie, cache it, and decode it, as it's RLP encoded.
                byte[] value = StorageTrie.Get(key);
                if (value == null)
                {
                    val = null;
                }
                else
                {
                    val = RLP.Decode(value);
                }

                StorageCache[key] = val;
            }

            // Return our storage key.
            return(val);
        }
Пример #2
0
        public void CommitStorageChanges()
        {
            // For each storage cache item, we want to flush those changes to the main trie/database.
            foreach (var key in StorageCache.Keys)
            {
                // If the value is null, it means the value is non existent anymore, so we remove the key value pair
                if (StorageCache[key] == null)
                {
                    StorageTrie.Remove(key);
                }
                else
                {
                    // Otherwise we set the new value.
                    StorageTrie.Set(key, RLP.Encode(StorageCache[key]));
                }
            }

            // Now we clear our cache.
            StorageCache.Clear();

            // And update our account's storage root hash.
            StorageRoot = StorageTrie.GetRootNodeHash();
        }