private void BotPackageReceivedEventArgs(object sender, PackageReceivedEventArgs <BotHeader> e) { var header = e.Proto; var botId = header.BotId; var meta = _messageIdMap[e.Proto.MessageId]; if (!IsExpectedMessage(header)) { Logger.Warn("[R] <--- Unexpected message from {0,-22} {1}", meta.Type.Name, botId); _peerManager.Punish(botId); } var message = DecodeMessage(header.MessageId, e.Payload, (BotHeader.Size + header.Padding), header.PayloadSize); LogMessaging(message, botId, false); var botMessage = new BotMessage { Header = header, Message = message }; if (!PoW.IsEnough(e.Payload, 0, header.PayloadSize + header.Padding + BotHeader.Size, meta.RequiredWork)) { Logger.Warn("[R] <--- Insufficient work for {0,-22} {1}", meta.Type.Name, botId); _peerManager.Ban(e.Proto.EndPoint); } meta.MessageHandler.Handle(botMessage); }
public JsonResult Mine() { var proof = PoW.ProofOfWork(_blockchain.LastBlock.Proof); _blockchain.NewTransaction("0", _nodeId, 1); var block = _blockchain.NewBlock(proof); return(Json(new { Message = "New Block Forged", Index = block.Index, block.Transactions, block.Proof, block.PrevHash })); }
internal void Send(MessageMetadata metadata, ulong correlationId, short ttl, byte[] payload, BotIdentifier peerBotId) { if (!_peerList.IsRegisteredBot(peerBotId) && (metadata.MessageId != MessageCode.Syn || metadata.MessageId != MessageCode.AckSyn)) { Logger.Verbose("Cannot send message to unkown {0} bot", peerBotId); return; } var peerInfo = _peerList[peerBotId]; byte[] message; BotHeader header; do { var padding = RandomUtils.NextPadding(); header = new BotHeader { CorrelationId = correlationId == 0 ? RandomUtils.NextCorrelationId() : correlationId, MessageId = (short)metadata.MessageId, PayloadSize = (short)payload.Length, Padding = (short)padding.Length, Ttl = ttl == 0 ? RandomUtils.NextTtl() : ttl }; var preambule = BufferUtils.Concat(header.Encode(), padding); message = BufferUtils.Concat(preambule, payload); } while (!PoW.IsEnough(message, 0, message.Length, metadata.RequiredWork)); if (peerInfo.Handshaked) { message = Aes.Encrypt(message, 0, message.Length, peerInfo.EncryptionKey); } var endPoint = peerInfo.EndPoint; Logger.Verbose("{0}@{1} {2}", header.BotId, endPoint, header.CorrelationId); _communicationManager.Send(endPoint, message); if (correlationId == 0) { WaitingForReply.Add(new Package(endPoint, message, message.Length), header.CorrelationId); } }