Esempio n. 1
0
        public void Encode(RlpStream stream, AccessList?item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            if (item is null)
            {
                stream.WriteByte(Rlp.NullObjectByte);
            }
            else
            {
                int contentLength = GetContentLength(item);
                stream.StartSequence(contentLength);

                if (!item.IsNormalized)
                {
                    AccessListItem?currentItem = default;

                    void SerializeCurrent()
                    {
                        if (currentItem is not null)
                        {
                            AccessListItem toEncode = currentItem.Value;
                            EncodeListItem(stream, toEncode.Address, toEncode.Indexes, toEncode.Indexes.Count);
                        }
                    }

                    foreach (object accessListEntry in item.OrderQueue !)
                    {
                        if (accessListEntry is Address address)
                        {
                            // serialize any element that is not the last
                            SerializeCurrent();
                            currentItem = new AccessListItem(address, new List <UInt256>());
                        }
                        else
                        {
                            if (currentItem is null)
                            {
                                throw new InvalidDataException(
                                          $"{nameof(AccessList)} order looks corrupted - processing index ahead of address");
                            }

                            currentItem.Value.Indexes.Add((UInt256)accessListEntry);
                        }
                    }

                    // serialize the last element
                    SerializeCurrent();
                }
                else
                {
                    foreach ((Address address, IReadOnlySet <UInt256> indexes) in item.Data)
                    {
                        EncodeListItem(stream, address, indexes, indexes.Count);
                    }
                }
            }
        }