Beispiel #1
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);
     }
 }
 public BlockHeadersMessage Deserialize(byte[] bytes)
 {
     BlockHeadersMessage message = new BlockHeadersMessage();
     Rlp.DecoderContext context = bytes.AsRlpContext();
     message.BlockHeaders = Rlp.DecodeArray<BlockHeader>(context);
     return message;
 }
Beispiel #3
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);
        }
Beispiel #4
0
        public GetReceiptsMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext decoderContext = bytes.AsRlpContext();
            var hashes = decoderContext.DecodeArray(itemContext => itemContext.DecodeKeccak());

            return(new GetReceiptsMessage(hashes));
        }
Beispiel #5
0
        public NodeDataFeed(ISnapshotableDb codeDb, ISnapshotableDb stateDb, ILogManager logManager)
        {
            _codeDb  = codeDb ?? throw new ArgumentNullException(nameof(codeDb));
            _stateDb = stateDb ?? throw new ArgumentNullException(nameof(stateDb));
            _logger  = logManager.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

            byte[] progress = _codeDb.Get(_fastSyncProgressKey);
            if (progress != null)
            {
                Rlp.DecoderContext context = new Rlp.DecoderContext(progress);
                context.ReadSequenceLength();
                _consumedNodesCount  = context.DecodeLong();
                _savedStorageCount   = context.DecodeLong();
                _savedStateCount     = context.DecodeLong();
                _savedNodesCount     = context.DecodeLong();
                _savedAccounts       = context.DecodeLong();
                _savedCode           = context.DecodeLong();
                _requestedNodesCount = context.DecodeLong();
                _dbChecks            = context.DecodeLong();
                _stateWasThere       = context.DecodeLong();
                _stateWasNotThere    = context.DecodeLong();
                if (context.Position != context.Length)
                {
                    _dataSize = context.DecodeLong();
                }
            }
        }
        public GetNodeDataMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext decoderContext = bytes.AsRlpContext();
            var keys = decoderContext.DecodeArray(itemContext => itemContext.DecodeKeccak());

            return(new GetNodeDataMessage(keys));
        }
        public static Signature DecodeSignature(Rlp.DecoderContext context)
        {
            Span <byte> vBytes = context.DecodeByteArraySpan();
            Span <byte> rBytes = context.DecodeByteArraySpan();
            Span <byte> sBytes = context.DecodeByteArraySpan();

            if (vBytes[0] == 0 || rBytes[0] == 0 || sBytes[0] == 0)
            {
                throw new RlpException("VRS starting with 0");
            }

            if (rBytes.Length > 32 || sBytes.Length > 32)
            {
                throw new RlpException("R and S lengths expected to be less or equal 32");
            }

            int v = vBytes.ToInt32();

            if (rBytes.SequenceEqual(Bytes.Zero32) && sBytes.SequenceEqual(Bytes.Zero32))
            {
                throw new RlpException("Both 'r' and 's' are zero when decoding a transaction.");
            }

            Signature signature = new Signature(rBytes, sBytes, v);

            return(signature);
        }
Beispiel #8
0
        public void Long_negative()
        {
            Rlp  output  = Rlp.Encode(-1L);
            var  context = new Rlp.DecoderContext(output.Bytes);
            long value   = context.DecodeLong();

            Assert.AreEqual(-1L, value);
        }
        public DisconnectMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            int reason = context.DecodeInt();
            DisconnectMessage disconnectMessage = new DisconnectMessage(reason);

            return(disconnectMessage);
        }
Beispiel #10
0
        public NewBlockMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext context = bytes.AsRlpContext();
            NewBlockMessage    message = new NewBlockMessage();

            context.ReadSequenceLength();
            message.Block           = Rlp.Decode <Block>(context);
            message.TotalDifficulty = context.DecodeUBigInt();
            return(message);
        }
Beispiel #11
0
        public AckEip8Message Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext context         = bytes.AsRlpContext();
            AckEip8Message     authEip8Message = new AckEip8Message();

            context.ReadSequenceLength();
            authEip8Message.EphemeralPublicKey = new PublicKey(context.DecodeByteArray());
            authEip8Message.Nonce = context.DecodeByteArray();
            return(authEip8Message);
        }
Beispiel #12
0
        private ParityAccountStateChange DecodeAccountStateChange(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            ParityAccountStateChange change = new ParityAccountStateChange();

            change.Balance = DecodeChange(context);
            change.Code    = DecodeByteChange(context);
            change.Nonce   = DecodeChange(context);
            change.Storage = DecodeStorageChange(context);
            return(change);
        }
Beispiel #13
0
        public byte[] Get(UInt256 index)
        {
            byte[] key   = GetKey(index);
            byte[] value = Get(key);
            if (value == null)
            {
                return(new byte[] { 0 });
            }

            Rlp.DecoderContext rlp = value.AsRlpContext();
            return(rlp.DecodeByteArray());
        }
        private static HiMessage Deserialize(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            var protocolVersion = context.DecodeByte();
            var providerAddress = context.DecodeAddress();
            var consumerAddress = context.DecodeAddress();
            var nodeId          = new PublicKey(context.DecodeByteArray());
            var signature       = SignatureDecoder.DecodeSignature(context);

            return(new HiMessage(protocolVersion, providerAddress, consumerAddress,
                                 nodeId, signature));
        }
Beispiel #15
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);
        }
Beispiel #16
0
        private ParityStateChange <byte[]> DecodeByteChange(Rlp.DecoderContext context)
        {
            int sequenceLength = context.ReadSequenceLength();

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

            ParityStateChange <byte[]> change = new ParityStateChange <byte[]>(context.DecodeByteArray(), context.DecodeByteArray());

            return(change);
        }
Beispiel #17
0
        public StatusMessage Deserialize(byte[] bytes)
        {
            StatusMessage statusMessage = new StatusMessage();

            Rlp.DecoderContext context = bytes.AsRlpContext();
            context.ReadSequenceLength();
            statusMessage.ProtocolVersion = context.DecodeByte();
            statusMessage.ChainId         = context.DecodeInt();
            statusMessage.TotalDifficulty = context.DecodeUBigInt();
            statusMessage.BestHash        = context.DecodeKeccak();
            statusMessage.GenesisHash     = context.DecodeKeccak();
            return(statusMessage);
        }
Beispiel #18
0
        public NetworkNode Decode(Rlp.DecoderContext context, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            context.ReadSequenceLength();

            var publicKey   = new PublicKey(context.DecodeByteArray());
            var ip          = System.Text.Encoding.UTF8.GetString(context.DecodeByteArray());
            var port        = context.DecodeByteArray().ToInt32();
            var description = System.Text.Encoding.UTF8.GetString(context.DecodeByteArray());
            var reputation  = context.DecodeByteArray().ToInt64();

            var networkNode = new NetworkNode(publicKey, ip != string.Empty ? ip : null, port, description != string.Empty ? description : null, reputation);

            return(networkNode);
        }
        public NodeDataMessage Deserialize(byte[] bytes)
        {
            if (bytes.Length == 0 && bytes[0] == Rlp.OfEmptySequence[0])
            {
                return(new NodeDataMessage(null));
            }

            Rlp.DecoderContext decoderContext = bytes.AsRlpContext();

            var             data    = decoderContext.DecodeArray(itemContext => itemContext.DecodeByteArray());
            NodeDataMessage message = new NodeDataMessage(data);

            return(message);
        }
        public BlockBodiesMessage Deserialize(byte[] bytes)
        {
            Rlp.DecoderContext decoderContext = bytes.AsRlpContext();
            BlockBodiesMessage message        = new BlockBodiesMessage();

            message.Bodies = decoderContext.DecodeArray(ctx =>
            {
                decoderContext.ReadSequenceLength();
                Transaction[] transactions = decoderContext.DecodeArray(txCtx => Rlp.Decode <Transaction>(ctx));
                BlockHeader[] ommers       = decoderContext.DecodeArray(txCtx => Rlp.Decode <BlockHeader>(ctx));
                return(transactions, ommers);
            });

            return(message);
        }
Beispiel #21
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);
        }
Beispiel #22
0
        public ReceiptsMessage Deserialize(byte[] bytes)
        {
            if (bytes.Length == 0 && bytes[0] == Rlp.OfEmptySequence[0])
            {
                return(new ReceiptsMessage(null));
            }

            Rlp.DecoderContext decoderContext = bytes.AsRlpContext();

            var data = decoderContext.DecodeArray(itemContext =>
                                                  itemContext.DecodeArray(nestedContext => Rlp.Decode <TxReceipt>(nestedContext)) ?? new TxReceipt[0]);
            ReceiptsMessage message = new ReceiptsMessage(data);

            return(message);
        }
Beispiel #23
0
        private Node[] DeserializeNodes(Rlp.DecoderContext context)
        {
            return(context.DecodeArray(ctx =>
            {
                int lastPosition = ctx.ReadSequenceLength() + ctx.Position;
                int count = ctx.ReadNumberOfItemsRemaining(lastPosition);
                var address = GetAddress(ctx.DecodeByteArray(), ctx.DecodeInt());
                if (count > 3)
                {
                    ctx.DecodeInt();
                }

                byte[] id = ctx.DecodeByteArray();
                return NodeFactory.CreateNode(new PublicKey(id), address);
            }));
        }
        public FindNodeMessage Deserialize(byte[] msg)
        {
            var results = PrepareForDeserialization <FindNodeMessage>(msg);

            Rlp.DecoderContext context = results.Data.AsRlpContext();

            context.ReadSequenceLength();
            var searchedNodeId = context.DecodeByteArray();
            var expireTime     = context.DecodeLong();

            var message = results.Message;

            message.SearchedNodeId = searchedNodeId;
            message.ExpirationTime = expireTime;

            return(message);
        }
        public AuthEip8Message Deserialize(byte[] data)
        {
            // TODO: this would not be compatible with future versions... ? if the length of prefixes changes
            Rlp.DecoderContext context     = data.AsRlpContext();
            AuthEip8Message    authMessage = new AuthEip8Message();

            context.ReadSequenceLength();
            byte[]    sigAllBytes = context.DecodeByteArray();
            Signature signature   = new Signature(sigAllBytes.Slice(0, 64), sigAllBytes[64]); // since Signature class is Ethereum style it expects V as the 64th byte, hence we use RecoveryID constructor

            authMessage.Signature = signature;
            authMessage.PublicKey = new PublicKey(context.DecodeByteArray());
            authMessage.Nonce     = context.DecodeByteArray();
            int version = context.DecodeInt();

            return(authMessage);
        }
Beispiel #26
0
        private Dictionary <Address, Tally> DecodeTally(Rlp.DecoderContext context)
        {
            context.ReadSequenceLength();
            Dictionary <Address, Tally> tally = new Dictionary <Address, Tally>();
            int length = context.DecodeInt();

            for (int i = 0; i < length; i++)
            {
                Address address   = context.DecodeAddress();
                int     votes     = context.DecodeInt();
                bool    authorize = context.DecodeBool();
                Tally   tallyItem = new Tally(authorize);
                tallyItem.Votes = votes;
                tally[address]  = tallyItem;
            }
            return(tally);
        }
Beispiel #27
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);
        }
Beispiel #28
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);
        }
Beispiel #29
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.DecodeUBigInt();
            blockInfo.TotalTransactions = context.DecodeUBigInt();

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

            return(blockInfo);
        }
Beispiel #30
0
        private Dictionary <Address, ParityAccountStateChange> DecodeStateDiff(Rlp.DecoderContext context)
        {
            var accountStateChange = new Dictionary <Address, ParityAccountStateChange>();
            int checkpoint         = context.ReadSequenceLength();
            int items = context.ReadNumberOfItemsRemaining(context.Position + checkpoint);

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

            for (int i = 0; i < items; i = i + 2)
            {
                accountStateChange[context.DecodeAddress()] = DecodeAccountStateChange(context);
            }

            return(accountStateChange);
        }