Пример #1
0
        private static ParityTraceAction DecodeAction(Rlp.DecoderContext context)
        {
            ParityTraceAction action = new ParityTraceAction();
            int sequenceLength       = context.ReadSequenceLength();

            if (context.ReadNumberOfItemsRemaining(context.Position + sequenceLength) == 3)
            {
                action.CallType     = "reward";
                action.RewardType   = context.DecodeString();
                action.Author       = context.DecodeAddress();
                action.Value        = context.DecodeUInt256();
                action.TraceAddress = Array.Empty <int>();
            }
            else
            {
                action.CallType       = context.DecodeString();
                action.From           = context.DecodeAddress();
                action.To             = context.DecodeAddress();
                action.Value          = context.DecodeUInt256();
                action.Gas            = context.DecodeLong();
                action.Input          = context.DecodeByteArray();
                action.Result         = new ParityTraceResult();
                action.Result.Output  = context.DecodeByteArray();
                action.Result.GasUsed = context.DecodeLong();
                action.TraceAddress   = context.DecodeArray(c => c.DecodeInt());
                int subtracesCount = context.DecodeInt();
                action.Subtraces = new List <ParityTraceAction>(subtracesCount);
                for (int i = 0; i < subtracesCount; i++)
                {
                    action.Subtraces.Add(DecodeAction(context));
                }
            }

            return(action);
        }
Пример #2
0
 public StatusMessage Deserialize(byte[] bytes)
 {
     try
     {
         StatusMessage      statusMessage = new StatusMessage();
         Rlp.DecoderContext context       = bytes.AsRlpContext();
         context.ReadSequenceLength();
         statusMessage.ProtocolVersion = context.DecodeByte();
         statusMessage.ChainId         = context.DecodeUInt256();
         statusMessage.TotalDifficulty = context.DecodeUInt256();
         statusMessage.BestHash        = context.DecodeKeccak();
         statusMessage.GenesisHash     = context.DecodeKeccak();
         return(statusMessage);
     }
     catch (Exception)
     {
         // TODO: still to be explained...
         StatusMessage      statusMessage = new StatusMessage();
         Rlp.DecoderContext context       = bytes.AsSpan(3).ToArray().AsRlpContext();
         context.ReadSequenceLength();
         statusMessage.ProtocolVersion = context.DecodeByte();
         statusMessage.ChainId         = context.DecodeUInt256();
         statusMessage.TotalDifficulty = context.DecodeUInt256();
         statusMessage.BestHash        = context.DecodeKeccak();
         statusMessage.GenesisHash     = context.DecodeKeccak();
         statusMessage.StrangePrefix   = bytes.AsSpan(0, 3).ToArray().ToHexString();
         return(statusMessage);
     }
 }
Пример #3
0
        public StatusMessage Deserialize(byte[] bytes)
        {
            StatusMessage statusMessage = new StatusMessage();

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            statusMessage.ProtocolVersion = context.DecodeByte();
            statusMessage.ChainId         = context.DecodeUInt256();
            statusMessage.TotalDifficulty = context.DecodeUInt256();
            statusMessage.BestHash        = context.DecodeKeccak();
            statusMessage.GenesisHash     = context.DecodeKeccak();
            return(statusMessage);
        }
Пример #4
0
        public BlockInfo Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            int lastCheck = context.ReadSequenceLength() + context.Position;

            BlockInfo blockInfo = new BlockInfo();

            blockInfo.BlockHash         = context.DecodeKeccak();
            blockInfo.WasProcessed      = context.DecodeBool();
            blockInfo.TotalDifficulty   = context.DecodeUInt256();
            blockInfo.TotalTransactions = context.DecodeUInt256();

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

            return(blockInfo);
        }
Пример #5
0
        private ParityStateChange <UInt256> DecodeChange(Rlp.DecoderContext context)
        {
            int sequenceLength = context.ReadSequenceLength();

            if (sequenceLength == 0)
            {
                return(null);
            }

            ParityStateChange <UInt256> change = new ParityStateChange <UInt256>(context.DecodeUInt256(), context.DecodeUInt256());

            return(change);
        }
Пример #6
0
        private SortedList <Address, UInt256> DecodeSigners(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            SortedList <Address, UInt256> signers = new SortedList <Address, UInt256>(CliqueAddressComparer.Instance);
            int length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address signer   = context.DecodeAddress();
                UInt256 signedAt = context.DecodeUInt256();
                signers.Add(signer, signedAt);
            }

            return(signers);
        }
Пример #7
0
        private List <Vote> DecodeVotes(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            List <Vote> votes  = new List <Vote>();
            int         length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address signer    = context.DecodeAddress();
                UInt256 block     = context.DecodeUInt256();
                Address address   = context.DecodeAddress();
                bool    authorize = context.DecodeBool();
                Vote    vote      = new Vote(signer, block, address, authorize);
                votes.Add(vote);
            }
            return(votes);
        }
Пример #8
0
        public ParityLikeTxTrace Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            ParityLikeTxTrace trace = new ParityLikeTxTrace();

            context.ReadSequenceLength();
            trace.BlockHash       = context.DecodeKeccak();
            trace.BlockNumber     = context.DecodeUInt256();
            trace.TransactionHash = context.DecodeKeccak();
            byte[] txPosBytes = context.DecodeByteArray();
            trace.TransactionPosition = txPosBytes.Length == 0 ? (int?)null : txPosBytes.ToInt32();
            context.ReadSequenceLength();
            trace.Action       = DecodeAction(context);
            trace.StateChanges = DecodeStateDiff(context);
            // stateChanges

            return(trace);
        }
Пример #9
0
        private Dictionary <UInt256, ParityStateChange <byte[]> > DecodeStorageChange(Rlp.DecoderContext context)
        {
            int checkpoint = context.ReadSequenceLength();
            var change     = new Dictionary <UInt256, ParityStateChange <byte[]> >();
            int itemsCount = context.ReadNumberOfItemsRemaining(context.Position + checkpoint);

            if (itemsCount == 0)
            {
                return(null);
            }

            for (int i = 0; i < itemsCount; i = i + 2)
            {
                change[context.DecodeUInt256()] = DecodeByteChange(context);
            }

            return(change);
        }
Пример #10
0
        public Snapshot Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            context.ReadSequenceLength();

            // Block number
            UInt256 number = context.DecodeUInt256();
            // Hash
            Keccak hash = context.DecodeKeccak();
            // Signers
            SortedList <Address, UInt256> signers = DecodeSigners(context);
            // Votes
            List <Vote> votes = DecodeVotes(context);
            // Tally
            Dictionary <Address, Tally> tally = DecodeTally(context);
            Snapshot snapshot = new Snapshot(number, hash, signers, tally);

            snapshot.Votes = votes;

            return(snapshot);
        }
Пример #11
0
        public GetBlockHeadersMessage Deserialize(byte[] bytes)
        {
            GetBlockHeadersMessage message = new GetBlockHeadersMessage();

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            int position = context.Position;

            byte[] startingBytes = context.DecodeByteArray();
            context.Position = position;
            if (startingBytes.Length == 32)
            {
                message.StartingBlockHash = context.DecodeKeccak();
            }
            else
            {
                message.StartingBlockNumber = (long)context.DecodeUInt256();
            }

            message.MaxHeaders = context.DecodeInt();
            message.Skip       = context.DecodeInt();
            message.Reverse    = context.DecodeByte();
            return(message);
        }
Пример #12
0
        public Snapshot Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            context.ReadSequenceLength();

            // Config
            CliqueConfig config = new CliqueConfig(15, 30000);
            // Signature cache
            LruCache <Keccak, Address> sigCache = new LruCache <Keccak, Address>(Clique.InMemorySignatures);
            // Block number
            UInt256 number = context.DecodeUInt256();
            // Hash
            Keccak hash = context.DecodeKeccak();
            // Signers
            SortedList <Address, UInt256> signers = DecodeSigners(context);
            // Votes
            List <Vote> votes = DecodeVotes(context);
            // Tally
            Dictionary <Address, Tally> tally = DecodeTally(context);
            Snapshot snapshot = new Snapshot(config, sigCache, number, hash, signers, tally);

            snapshot.Votes = votes;

            return(snapshot);
        }