예제 #1
0
        public void DecodeStructRef(ref Rlp.ValueDecoderContext decoderContext, RlpBehaviors rlpBehaviors, out TxReceiptStructRef item)
        {
            item = new TxReceiptStructRef();

            if (decoderContext.IsNextItemNull())
            {
                decoderContext.ReadByte();
                return;
            }

            bool isStorage = (rlpBehaviors & RlpBehaviors.Storage) != 0;

            decoderContext.ReadSequenceLength();
            Span <byte> firstItem = decoderContext.DecodeByteArraySpan();

            if (firstItem.Length == 1)
            {
                item.StatusCode = firstItem[0];
            }
            else
            {
                item.PostTransactionState = firstItem.Length == 0 ? new ValueKeccak() : new ValueKeccak(firstItem);
            }

            if (isStorage)
            {
                decoderContext.DecodeValueKeccak(out item.BlockHash);
            }
            if (isStorage)
            {
                item.BlockNumber = (long)decoderContext.DecodeUInt256();
            }
            if (isStorage)
            {
                item.Index = decoderContext.DecodeInt();
            }
            if (isStorage)
            {
                decoderContext.DecodeAddressStructRef(out item.Sender);
            }
            if (isStorage)
            {
                decoderContext.DecodeAddressStructRef(out item.Recipient);
            }
            if (isStorage)
            {
                decoderContext.DecodeAddressStructRef(out item.ContractAddress);
            }
            if (isStorage)
            {
                item.GasUsed = (long)decoderContext.DecodeUBigInt();
            }
            item.GasUsedTotal = (long)decoderContext.DecodeUBigInt();
            decoderContext.DecodeBloomStructRef(out item.Bloom);

            var peekPrefixAndContentLength = decoderContext.PeekPrefixAndContentLength();
            var logsBytes = peekPrefixAndContentLength.ContentLength + peekPrefixAndContentLength.PrefixLength;

            item.LogsRlp = decoderContext.Data.Slice(decoderContext.Position, logsBytes);
            decoderContext.SkipItem();

            bool allowExtraData = (rlpBehaviors & RlpBehaviors.AllowExtraData) != 0;

            if (!allowExtraData)
            {
                if (isStorage && _supportTxHash)
                {
                    // since txHash was added later and may not be in rlp, we provide special mark byte that it will be next
                    if (decoderContext.PeekByte() == MarkTxHashByte)
                    {
                        decoderContext.ReadByte();
                        decoderContext.DecodeValueKeccak(out item.TxHash);
                    }
                }

                // since error was added later we can only rely on it in cases where we read receipt only and no data follows, empty errors might not be serialized
                if (decoderContext.Position != decoderContext.Length)
                {
                    item.Error = decoderContext.DecodeString();
                }
            }
        }
예제 #2
0
        public TxReceipt Decode(ref Rlp.ValueDecoderContext decoderContext, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            if (decoderContext.IsNextItemNull())
            {
                decoderContext.ReadByte();
                return(null);
            }

            bool      isStorage = (rlpBehaviors & RlpBehaviors.Storage) != 0;
            TxReceipt txReceipt = new TxReceipt();

            decoderContext.ReadSequenceLength();
            byte[] firstItem = decoderContext.DecodeByteArray();
            if (firstItem.Length == 1)
            {
                txReceipt.StatusCode = firstItem[0];
            }
            else
            {
                txReceipt.PostTransactionState = firstItem.Length == 0 ? null : new Keccak(firstItem);
            }

            if (isStorage)
            {
                txReceipt.BlockHash = decoderContext.DecodeKeccak();
            }
            if (isStorage)
            {
                txReceipt.BlockNumber = (long)decoderContext.DecodeUInt256();
            }
            if (isStorage)
            {
                txReceipt.Index = decoderContext.DecodeInt();
            }
            if (isStorage)
            {
                txReceipt.Sender = decoderContext.DecodeAddress();
            }
            if (isStorage)
            {
                txReceipt.Recipient = decoderContext.DecodeAddress();
            }
            if (isStorage)
            {
                txReceipt.ContractAddress = decoderContext.DecodeAddress();
            }
            if (isStorage)
            {
                txReceipt.GasUsed = (long)decoderContext.DecodeUBigInt();
            }
            txReceipt.GasUsedTotal = (long)decoderContext.DecodeUBigInt();
            txReceipt.Bloom        = decoderContext.DecodeBloom();

            int             lastCheck  = decoderContext.ReadSequenceLength() + decoderContext.Position;
            List <LogEntry> logEntries = new List <LogEntry>();

            while (decoderContext.Position < lastCheck)
            {
                logEntries.Add(Rlp.Decode <LogEntry>(ref decoderContext, RlpBehaviors.AllowExtraData));
            }

            bool allowExtraData = (rlpBehaviors & RlpBehaviors.AllowExtraData) != 0;

            if (!allowExtraData)
            {
                decoderContext.Check(lastCheck);
            }

            if (!allowExtraData)
            {
                if (isStorage && _supportTxHash)
                {
                    // since txHash was added later and may not be in rlp, we provide special mark byte that it will be next
                    if (decoderContext.PeekByte() == MarkTxHashByte)
                    {
                        decoderContext.ReadByte();
                        txReceipt.TxHash = decoderContext.DecodeKeccak();
                    }
                }

                // since error was added later we can only rely on it in cases where we read receipt only and no data follows, empty errors might not be serialized
                if (decoderContext.Position != decoderContext.Length)
                {
                    txReceipt.Error = decoderContext.DecodeString();
                }
            }

            txReceipt.Logs = logEntries.ToArray();
            return(txReceipt);
        }