Skip to content

Commit

Permalink
Merge branch 'master' into fix/hotfixes-core
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Feb 6, 2025
2 parents 3fc7a66 + 038513a commit 3c567fd
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 30 deletions.
8 changes: 7 additions & 1 deletion src/Neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ public sealed class Block : IEquatable<Block>, IInventory
public Witness Witness => Header.Witness;

InventoryType IInventory.InventoryType => InventoryType.Block;

public int Size => Header.Size + Transactions.GetVarSize();
Witness[] IVerifiable.Witnesses { get => ((IVerifiable)Header).Witnesses; set => throw new NotSupportedException(); }

Witness[] IVerifiable.Witnesses
{
get => ((IVerifiable)Header).Witnesses;
set => throw new NotSupportedException();
}

public void Deserialize(ref MemoryReader reader)
{
Expand Down
20 changes: 12 additions & 8 deletions src/Neo/Network/P2P/Payloads/ExtensiblePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public UInt256 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
Category.GetVarSize() + // Category
sizeof(uint) + // ValidBlockStart
sizeof(uint) + // ValidBlockEnd
UInt160.Length + // Sender
Data.GetVarSize() + // Data
(Witness is null ? 1 : 1 + Witness.Size); // Witness, cannot be null for invalid payload

Witness[] IVerifiable.Witnesses
{
Expand All @@ -86,7 +86,10 @@ Witness[] IVerifiable.Witnesses
}
set
{
if (value.Length != 1) throw new ArgumentException($"Expected 1 witness, got {value.Length}.");
if (value is null)
throw new ArgumentNullException(nameof(IVerifiable.Witnesses));
if (value.Length != 1)
throw new ArgumentException($"Expected 1 witness, got {value.Length}.", nameof(IVerifiable.Witnesses));
Witness = value[0];
}
}
Expand Down Expand Up @@ -119,7 +122,8 @@ UInt160[] IVerifiable.GetScriptHashesForVerifying(DataCache snapshot)
void ISerializable.Serialize(BinaryWriter writer)
{
((IVerifiable)this).SerializeUnsigned(writer);
writer.Write((byte)1); writer.Write(Witness);
writer.Write((byte)1);
writer.Write(Witness);
}

void IVerifiable.SerializeUnsigned(BinaryWriter writer)
Expand Down
9 changes: 6 additions & 3 deletions src/Neo/Network/P2P/Payloads/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ public UInt256 Hash
UInt256.Length + // PrevHash
UInt256.Length + // MerkleRoot
sizeof(ulong) + // Timestamp
sizeof(ulong) + // Nonce
sizeof(ulong) + // Nonce
sizeof(uint) + // Index
sizeof(byte) + // PrimaryIndex
UInt160.Length + // NextConsensus
1 + Witness.Size; // Witness
(Witness is null ? 1 : 1 + Witness.Size); // Witness, cannot be null for invalid header

Witness[] IVerifiable.Witnesses
{
Expand All @@ -145,7 +145,10 @@ Witness[] IVerifiable.Witnesses
}
set
{
if (value.Length != 1) throw new ArgumentException(null, nameof(value));
if (value is null)
throw new ArgumentNullException(nameof(IVerifiable.Witnesses));
if (value.Length != 1)
throw new ArgumentException($"Expected 1 witness, got {value.Length}.", nameof(IVerifiable.Witnesses));
Witness = value[0];
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/Plugins/StateService/Network/StateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,19 @@ Witness[] IVerifiable.Witnesses
}
set
{
if (value.Length != 1) throw new ArgumentException(null, nameof(value));
if (value is null)
throw new ArgumentNullException(nameof(IVerifiable.Witnesses));
if (value.Length != 1)
throw new ArgumentException($"Expected 1 witness, got {value.Length}.", nameof(IVerifiable.Witnesses));
Witness = value[0];
}
}

int ISerializable.Size =>
sizeof(byte) + //Version
sizeof(uint) + //Index
UInt256.Length + //RootHash
(Witness is null ? 1 : 1 + Witness.Size); //Witness
sizeof(byte) + // Version
sizeof(uint) + // Index
UInt256.Length + // RootHash
(Witness is null ? 1 : 1 + Witness.Size); // Witness

void ISerializable.Deserialize(ref MemoryReader reader)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/StateService/StatePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public JToken FindStates(JArray _params)
jarr.Add(j);
}
i++;
};
}
if (0 < jarr.Count)
{
json["firstProof"] = GetProof(trie, contract.Id, Convert.FromBase64String(jarr.First()["key"].AsString()));
Expand Down
12 changes: 11 additions & 1 deletion tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;

namespace Neo.UnitTests.Network.P2P.Payloads
{
Expand Down Expand Up @@ -218,5 +218,15 @@ public void ToJson()
Assert.AreEqual(0, ((JArray)txObj["attributes"]).Count);
Assert.AreEqual("0", txObj["netfee"].AsString());
}

[TestMethod]
public void Witness()
{
IVerifiable item = new Block() { Header = new(), };
Assert.AreEqual(1, item.Witnesses.Length);

Action actual = () => item.Witnesses = null;
Assert.ThrowsException<NotSupportedException>(actual);
}
}
}
23 changes: 21 additions & 2 deletions tests/Neo.UnitTests/Network/P2P/Payloads/UT_ExtensiblePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public void Size_Get()
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>() }
Witness = new Witness()
{
InvocationScript = new byte[] { 3, 5, 6 },
VerificationScript = Array.Empty<byte>()
}
};
Assert.AreEqual(42, test.Size);
}
Expand All @@ -45,7 +49,11 @@ public void DeserializeAndSerialize()
ValidBlockEnd = 789,
Sender = Array.Empty<byte>().ToScriptHash(),
Data = new byte[] { 1, 2, 3 },
Witness = new Witness() { InvocationScript = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH2, (byte)OpCode.PUSH3 }, VerificationScript = Array.Empty<byte>() }
Witness = new Witness()
{
InvocationScript = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH2, (byte)OpCode.PUSH3 },
VerificationScript = Array.Empty<byte>()
}
};
var clone = test.ToArray().AsSerializable<ExtensiblePayload>();

Expand All @@ -55,5 +63,16 @@ public void DeserializeAndSerialize()
Assert.AreEqual(test.ValidBlockEnd, clone.ValidBlockEnd);
Assert.AreEqual(test.Category, clone.Category);
}

[TestMethod]
public void Witness()
{
IVerifiable item = new ExtensiblePayload();
Action actual = () => item.Witnesses = null;
Assert.ThrowsException<ArgumentNullException>(actual);

item.Witnesses = [new()];
Assert.AreEqual(1, item.Witnesses.Length);
}
}
}
11 changes: 11 additions & 0 deletions tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,16 @@ public void Serialize()
var hex = "0000000000000000000000000000000000000000000000000000000000000000000000007227ba7b747f1a98f68679d4a98b68927646ab195a6f56b542ca5a0e6a412662493ed0e58f01000000000000000000000000000000000000000000000000000000000000000000000001000111";
Assert.AreEqual(hex, uut.ToArray().ToHexString());
}

[TestMethod]
public void Witness()
{
IVerifiable item = new Header();
Action actual = () => item.Witnesses = null;
Assert.ThrowsException<ArgumentNullException>(actual);

item.Witnesses = [new()];
Assert.AreEqual(1, item.Witnesses.Length);
}
}
}
1 change: 0 additions & 1 deletion tests/Neo.UnitTests/TestUtils.Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Akka.Util.Internal;
using Neo.Cryptography;
using Neo.Extensions;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
Expand Down
1 change: 0 additions & 1 deletion tests/Neo.UnitTests/TestUtils.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System;
using System.Linq;

namespace Neo.UnitTests
Expand Down
1 change: 0 additions & 1 deletion tests/Neo.UnitTests/TestUtils.Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Network.P2P.Payloads;
Expand Down
6 changes: 0 additions & 6 deletions tests/Neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Neo.UnitTests
{
Expand Down

0 comments on commit 3c567fd

Please sign in to comment.