Example #1
0
        public TransactionReceipt Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            TransactionReceipt receipt = new TransactionReceipt();

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

            receipt.GasUsed = (long)context.DecodeUBigInt(); // TODO: review
            receipt.Bloom   = context.DecodeBloom();

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

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

            if (!rlpBehaviors.HasFlag(RlpBehaviors.AllowExtraData))
            {
                context.Check(lastCheck);
            }

            receipt.Logs = logEntries.ToArray();
            return(receipt);
        }
Example #2
0
        public BlockHeader Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            if (context.Length == 1 && context.Data[0] == 192)
            {
                return(null);
            }

            var headerRlp = context.PeekNextItem();

            int headerSequenceLength = context.ReadSequenceLength();
            int headerCheck          = context.Position + headerSequenceLength;

            Keccak  parentHash       = context.DecodeKeccak();
            Keccak  ommersHash       = context.DecodeKeccak();
            Address beneficiary      = context.DecodeAddress();
            Keccak  stateRoot        = context.DecodeKeccak();
            Keccak  transactionsRoot = context.DecodeKeccak();
            Keccak  receiptsRoot     = context.DecodeKeccak();
            Bloom   bloom            = context.DecodeBloom();
            UInt256 difficulty       = context.DecodeUInt256();
            UInt256 number           = context.DecodeUInt256();
            UInt256 gasLimit         = context.DecodeUInt256();
            UInt256 gasUsed          = context.DecodeUInt256();
            UInt256 timestamp        = context.DecodeUInt256();

            byte[]     extraData = context.DecodeByteArray();
            Keccak     mixHash   = context.DecodeKeccak();
            BigInteger nonce     = context.DecodeUBigInt();

            if (!rlpBehaviors.HasFlag(RlpBehaviors.AllowExtraData))
            {
                context.Check(headerCheck);
            }

            BlockHeader blockHeader = new BlockHeader(
                parentHash,
                ommersHash,
                beneficiary,
                difficulty,
                number,
                (long)gasLimit,
                timestamp,
                extraData);

            blockHeader.StateRoot        = stateRoot;
            blockHeader.TransactionsRoot = transactionsRoot;
            blockHeader.ReceiptsRoot     = receiptsRoot;
            blockHeader.Bloom            = bloom;
            blockHeader.GasUsed          = (long)gasUsed;
            blockHeader.MixHash          = mixHash;
            blockHeader.Nonce            = (ulong)nonce;
            blockHeader.Hash             = Keccak.Compute(headerRlp);
            return(blockHeader);
        }
        public TxReceipt Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            bool      isStorage = (rlpBehaviors & RlpBehaviors.Storage) != 0;
            TxReceipt txReceipt = new TxReceipt();

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

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

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

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

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

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

            // since error was added later we can only rely on it in cases where we read receipt only and no data follows
            if (isStorage && !allowExtraData && context.Position != context.Length)
            {
                txReceipt.Error = context.DecodeString();
            }

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