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); } }
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); }
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 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); }
public DisconnectMessage Deserialize(byte[] bytes) { Rlp.DecoderContext context = bytes.AsRlpContext(); context.ReadSequenceLength(); int reason = context.DecodeInt(); DisconnectMessage disconnectMessage = new DisconnectMessage(reason); return(disconnectMessage); }
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); }
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); }
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); }
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)); }
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); }
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); }
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); }
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 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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public DisconnectMessage Deserialize(byte[] bytes) { if (bytes.Length == 1) { return(new DisconnectMessage((DisconnectReason)bytes[0])); } if (bytes.SequenceEqual(strangeBytes)) { return(new DisconnectMessage(DisconnectReason.StrangeBytes)); } Rlp.DecoderContext context = bytes.AsRlpContext(); context.ReadSequenceLength(); int reason = context.DecodeInt(); DisconnectMessage disconnectMessage = new DisconnectMessage(reason); return(disconnectMessage); }
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); }
public HelloMessage Deserialize(byte[] bytes) { Rlp.DecoderContext context = bytes.AsRlpContext(); context.ReadSequenceLength(); HelloMessage helloMessage = new HelloMessage(); helloMessage.P2PVersion = context.DecodeByte(); helloMessage.ClientId = context.DecodeString(); helloMessage.Capabilities = context.DecodeArray(ctx => { ctx.ReadSequenceLength(); string protocolCode = ctx.DecodeString(); int version = ctx.DecodeByte(); return(new Capability(protocolCode, version)); }).ToList(); helloMessage.ListenPort = context.DecodeInt(); helloMessage.NodeId = new PublicKey(context.DecodeByteArray()); return(helloMessage); }
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); }
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); }
protected override void Decode(IChannelHandlerContext context, byte[] input, List <object> output) { if (_logger.IsTraceEnabled) { _logger.Trace("Merging frames"); } Rlp.DecoderContext headerBodyItems = input.Slice(3, 13).AsRlpContext(); int headerDataEnd = headerBodyItems.ReadSequenceLength() + headerBodyItems.Position; int numberOfItems = headerBodyItems.ReadNumberOfItemsRemaining(headerDataEnd); int protocolType = headerBodyItems.DecodeInt(); // not needed - adaptive IDs int?contextId = numberOfItems > 1 ? headerBodyItems.DecodeInt() : (int?)null; int?totalPacketSize = numberOfItems > 2 ? headerBodyItems.DecodeInt() : (int?)null; bool isChunked = totalPacketSize.HasValue || contextId.HasValue && _currentSizes.ContainsKey(contextId.Value); if (isChunked) { if (_logger.IsTraceEnabled) { _logger.Trace("Merging chunked packet"); } bool isFirstChunk = totalPacketSize.HasValue; if (isFirstChunk) { _currentSizes[contextId.Value] = 0; _totalPayloadSizes[contextId.Value] = totalPacketSize.Value - 1; // packet type data size _packets[contextId.Value] = new Packet("???", GetPacketType(input), new byte[_totalPayloadSizes[contextId.Value]]); // adaptive IDs _payloads[contextId.Value] = new List <byte[]>(); } int packetTypeDataSize = isFirstChunk ? 1 : 0; int frameSize = input.Length - HeaderSize - FrameMacSize - packetTypeDataSize; _payloads[contextId.Value].Add(input.Slice(32 + packetTypeDataSize, frameSize)); _currentSizes[contextId.Value] += frameSize; if (_currentSizes[contextId.Value] >= _totalPayloadSizes[contextId.Value]) { int padding = _currentSizes[contextId.Value] - _totalPayloadSizes[contextId.Value]; Packet packet = _packets[contextId.Value]; int offset = 0; int frameCount = _payloads[contextId.Value].Count; for (int i = 0; i < frameCount; i++) { int length = _payloads[contextId.Value][i].Length - (i == frameCount - 1 ? padding : 0); Buffer.BlockCopy(_payloads[contextId.Value][i], 0, packet.Data, offset, length); offset += length; } output.Add(packet); _currentSizes.Remove(contextId.Value); _totalPayloadSizes.Remove(contextId.Value); _payloads.Remove(contextId.Value); _packets.Remove(contextId.Value); } } else { int totalBodySize = input[0] & 0xFF; totalBodySize = (totalBodySize << 8) + (input[1] & 0xFF); totalBodySize = (totalBodySize << 8) + (input[2] & 0xFF); if (_logger.IsTraceEnabled) { _logger.Trace($"Merging single frame packet of length {totalBodySize - 1}"); } output.Add(new Packet("???", GetPacketType(input), input.Slice(1 + 32, totalBodySize - 1))); // ??? protocol because of adaptive IDs } }