Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins to send custom messages #2101

Merged
merged 36 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
30ca673
Allow plugins to send messages
shargon Nov 25, 2020
4d9c613
Any role
shargon Nov 25, 2020
0b632d2
Merge branch 'master' into plugin-msg
shargon Nov 26, 2020
f2c773b
Merge branch 'master' into plugin-msg
shargon Dec 1, 2020
ea0393c
Use a different enum
shargon Dec 7, 2020
2b126e6
Merge branch 'master' into plugin-msg
shargon Dec 7, 2020
b697823
Merge branch 'master' into plugin-msg
shargon Dec 9, 2020
08e1685
Merge branch 'master' into plugin-msg
vncoelho Dec 18, 2020
338face
Merge branch 'master' into plugin-msg
shargon Dec 21, 2020
e8650ad
Remove enum
shargon Dec 21, 2020
e7355d0
WhiteList p2p message
shargon Dec 21, 2020
0af7858
Clean
shargon Dec 21, 2020
4334d6e
Rename
shargon Dec 22, 2020
6d6bde2
Merge branch 'master' into plugin-msg
shargon Dec 22, 2020
7960e27
Rename
erikzhang Dec 22, 2020
8250aa1
Add single validators
shargon Dec 24, 2020
a8eca56
Add single validators
shargon Dec 24, 2020
9d2756a
Update Blockchain.cs
shargon Dec 24, 2020
c656f3b
IInventory
shargon Dec 24, 2020
7fd40ec
Use CreateBuilder
erikzhang Dec 27, 2020
58b5b0d
Merge branch 'master' into plugin-msg
shargon Dec 28, 2020
8338410
Merge branch 'master' into plugin-msg
shargon Dec 29, 2020
6c62a30
Add Height
shargon Dec 29, 2020
18d3e9a
Simplify
erikzhang Dec 30, 2020
16be616
Merge branch 'master' into plugin-msg
shargon Dec 30, 2020
4d8b0a2
Remove MessageType
shargon Dec 30, 2020
4c7fdaf
Merge branch 'master' into plugin-msg
shargon Dec 30, 2020
9c2a6f6
Update
shargon Dec 30, 2020
48a98a5
Fix UT
shargon Dec 30, 2020
eb41546
Rename
shargon Jan 1, 2021
5e1f684
Merge branch 'master' into plugin-msg
shargon Jan 3, 2021
98a7316
Add sender
shargon Jan 5, 2021
0b28d17
Update ExtensiblePayload.cs
erikzhang Jan 6, 2021
7f49339
Check format
erikzhang Jan 6, 2021
b6f505e
Fix UT
erikzhang Jan 6, 2021
e028b64
Merge branch 'master' into plugin-msg
erikzhang Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/neo/Network/P2P/MessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public enum MessageCommand : byte
Block = 0x2c,
[ReflectionCache(typeof(ConsensusPayload))]
Consensus = 0x2d,
[ReflectionCache(typeof(PluginPayload))]
Plugin = 0x2e,
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
Reject = 0x2f,

//SPV protocol
Expand Down
110 changes: 110 additions & 0 deletions src/neo/Network/P2P/Payloads/PluginPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Neo.IO;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.SmartContract.Native.Designate;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
public class PluginPayload : IVerifiable
{
public string Plugin;
shargon marked this conversation as resolved.
Show resolved Hide resolved
public byte MessageType;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
public byte[] Data;
public PluginPayloadRole WitnessRole;
public Witness Witness;

private UInt256 _hash = null;
public UInt256 Hash
{
get
{
if (_hash == null)
{
_hash = this.CalculateHash();
}
return _hash;
}
}

public int Size =>
Plugin.GetVarSize() + //Plugin
sizeof(byte) + //MessageType
sizeof(PluginPayloadRole) + //WitnessRole
Data.GetVarSize() + //Data
1 + Witness.Size; //Witness

Witness[] IVerifiable.Witnesses
{
get
{
return new[] { Witness };
}
set
{
if (value.Length != 1) throw new ArgumentException();
Witness = value[0];
}
}

void ISerializable.Deserialize(BinaryReader reader)
{
((IVerifiable)this).DeserializeUnsigned(reader);
if (reader.ReadByte() != 1) throw new FormatException();
Witness = reader.ReadSerializable<Witness>();
}

void IVerifiable.DeserializeUnsigned(BinaryReader reader)
{
Plugin = reader.ReadVarString(32);
MessageType = reader.ReadByte();
Data = reader.ReadVarBytes(ushort.MaxValue);
WitnessRole = (PluginPayloadRole)reader.ReadByte();
if (!Enum.IsDefined(typeof(PluginPayloadRole), WitnessRole)) throw new FormatException();
}

UInt160[] IVerifiable.GetScriptHashesForVerifying(StoreView snapshot)
{
switch (WitnessRole)
{
case PluginPayloadRole.Committee:
{
return new[] { NativeContract.NEO.GetCommitteeAddress(snapshot) };
}
case PluginPayloadRole.Validators:
{
return new[] { Blockchain.GetConsensusAddress(NativeContract.NEO.GetNextBlockValidators(snapshot)) };
}
case PluginPayloadRole.StateValidator:
case PluginPayloadRole.Oracle:
{
return new[] { Blockchain.GetConsensusAddress(NativeContract.Designate.GetDesignatedByRole(snapshot, (Role)WitnessRole, snapshot.Height)) };
}
}

throw new ArgumentException();
}

void ISerializable.Serialize(BinaryWriter writer)
{
((IVerifiable)this).SerializeUnsigned(writer);
writer.Write((byte)1); writer.Write(Witness);
}

void IVerifiable.SerializeUnsigned(BinaryWriter writer)
{
writer.WriteVarString(Plugin);
writer.Write(MessageType);
writer.WriteVarBytes(Data);
writer.Write((byte)WitnessRole);
}

public bool Verify(StoreView snapshot)
{
return this.VerifyWitnesses(snapshot, 0_02000000);
}
}
}
10 changes: 10 additions & 0 deletions src/neo/Network/P2P/Payloads/PluginPayloadRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Neo.Network.P2P.Payloads
{
public enum PluginPayloadRole : byte
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
Committee = 0,
Validators = 2,
StateValidator = 4,
Oracle = 8
}
}
14 changes: 14 additions & 0 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ protected override UInt256 GetKeyForItem((UInt256, DateTime) item)

private void OnMessage(Message msg)
{
if (msg.Command == MessageCommand.Plugin)
{
var payload = (PluginPayload)msg.Payload;
if (!knownHashes.Add(payload.Hash)) return;
if (!payload.Verify(Blockchain.Singleton.GetSnapshot())) return;
}
foreach (IP2PPlugin plugin in Plugin.P2PPlugins)
if (!plugin.OnP2PMessage(msg))
return;
Expand Down Expand Up @@ -107,6 +113,9 @@ private void OnMessage(Message msg)
if (msg.Payload.Size <= Transaction.MaxTransactionSize)
OnInventoryReceived((Transaction)msg.Payload);
break;
case MessageCommand.Plugin:
OnPluginMessage(msg);
break;
case MessageCommand.Verack:
case MessageCommand.Version:
throw new ProtocolViolationException();
Expand All @@ -119,6 +128,11 @@ private void OnMessage(Message msg)
}
}

private void OnPluginMessage(Message msg)
{
shargon marked this conversation as resolved.
Show resolved Hide resolved
system.LocalNode.Tell(msg);
}

private void OnAddrMessageReceived(AddrPayload payload)
{
ref bool sent = ref sentCommands[(byte)MessageCommand.GetAddr];
Expand Down