forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow plugins to send custom messages (neo-project#2101)
- Loading branch information
Showing
11 changed files
with
193 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using Neo.IO; | ||
using Neo.Ledger; | ||
using Neo.Persistence; | ||
using Neo.SmartContract; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Neo.Network.P2P.Payloads | ||
{ | ||
public class ExtensiblePayload : IInventory | ||
{ | ||
public string Category; | ||
public uint ValidBlockStart; | ||
public uint ValidBlockEnd; | ||
public UInt160 Sender; | ||
public byte[] Data; | ||
public Witness Witness; | ||
|
||
private UInt256 _hash = null; | ||
public UInt256 Hash | ||
{ | ||
get | ||
{ | ||
if (_hash == null) | ||
{ | ||
_hash = this.CalculateHash(); | ||
} | ||
return _hash; | ||
} | ||
} | ||
|
||
InventoryType IInventory.InventoryType => InventoryType.Extensible; | ||
|
||
public int Size => | ||
Category.GetVarSize() + //Category | ||
sizeof(uint) + //ValidBlockStart | ||
sizeof(uint) + //ValidBlockEnd | ||
UInt160.Length + //Sender | ||
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) | ||
{ | ||
Category = reader.ReadVarString(32); | ||
ValidBlockStart = reader.ReadUInt32(); | ||
ValidBlockEnd = reader.ReadUInt32(); | ||
if (ValidBlockStart > ValidBlockEnd) throw new FormatException(); | ||
Sender = reader.ReadSerializable<UInt160>(); | ||
Data = reader.ReadVarBytes(ushort.MaxValue); | ||
} | ||
|
||
UInt160[] IVerifiable.GetScriptHashesForVerifying(StoreView snapshot) | ||
{ | ||
return new[] { Sender }; // This address should be checked by consumer | ||
} | ||
|
||
void ISerializable.Serialize(BinaryWriter writer) | ||
{ | ||
((IVerifiable)this).SerializeUnsigned(writer); | ||
writer.Write((byte)1); writer.Write(Witness); | ||
} | ||
|
||
void IVerifiable.SerializeUnsigned(BinaryWriter writer) | ||
{ | ||
writer.WriteVarString(Category); | ||
writer.Write(ValidBlockStart); | ||
writer.Write(ValidBlockEnd); | ||
writer.Write(Sender); | ||
writer.WriteVarBytes(Data); | ||
} | ||
|
||
public bool Verify(StoreView snapshot) | ||
{ | ||
if (snapshot.PersistingBlock.Index < ValidBlockStart || snapshot.PersistingBlock.Index > ValidBlockEnd) return false; | ||
if (!Blockchain.Singleton.IsExtensibleWitnessWhiteListed(Sender)) return false; | ||
return this.VerifyWitnesses(snapshot, 0_02000000); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/neo.UnitTests/Network/P2P/Payloads/UT_ExtensiblePayload.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.IO; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.SmartContract; | ||
using System; | ||
|
||
namespace Neo.UnitTests.Network.P2P.Payloads | ||
{ | ||
[TestClass] | ||
public class UT_ExtensiblePayload | ||
{ | ||
[TestMethod] | ||
public void Size_Get() | ||
{ | ||
var test = new ExtensiblePayload() | ||
{ | ||
Sender = Array.Empty<byte>().ToScriptHash(), | ||
Category = "123", | ||
Data = new byte[] { 1, 2, 3 }, | ||
Witness = new Witness() { InvocationScript = new byte[] { 3, 5, 6 }, VerificationScript = Array.Empty<byte>() } | ||
}; | ||
test.Size.Should().Be(42); | ||
} | ||
|
||
[TestMethod] | ||
public void DeserializeAndSerialize() | ||
{ | ||
var test = new ExtensiblePayload() | ||
{ | ||
Category = "123", | ||
ValidBlockStart = 456, | ||
ValidBlockEnd = 789, | ||
Sender = Array.Empty<byte>().ToScriptHash(), | ||
Data = new byte[] { 1, 2, 3 }, | ||
Witness = new Witness() { InvocationScript = new byte[] { 3, 5, 6 }, VerificationScript = Array.Empty<byte>() } | ||
}; | ||
var clone = test.ToArray().AsSerializable<ExtensiblePayload>(); | ||
|
||
Assert.AreEqual(test.Sender, clone.Witness.ScriptHash); | ||
Assert.AreEqual(test.Hash, clone.Hash); | ||
Assert.AreEqual(test.ValidBlockStart, clone.ValidBlockStart); | ||
Assert.AreEqual(test.ValidBlockEnd, clone.ValidBlockEnd); | ||
Assert.AreEqual(test.Category, clone.Category); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters