Skip to content

Commit

Permalink
Charge for size and witnesses (#791)
Browse files Browse the repository at this point in the history
* Fee= VerificationCost+ApplicationCost

* NetworkFee for verification

* Clean

* Clean

* Update

* Remove priority from Policy and MemoryPool

* Fix CalculateFee

* Allow to cancel transactions with more Gas

* Revert change

We need to test this first

* Add Cosigners

* some fixes

* Update Transaction.cs

* format

* fix overflow

* fix `Wallet.MakeTransaction()`

* fix `Transaction.CalculateFees()`

* remove contract get from verification

* Revert "remove contract get from verification"

This reverts commit 6d0dad2.

* Fix fee calculation

* fix

* fix tests

* Update MemoryPool.cs

* fix tests

* UT - Good calculation!

* Clean

* Revert Nep5Token.cs

* Revert conditional

* Improve readability

* Revert "Improve readability"

This reverts commit cd61b98.

* Possible fix for unit test

* Error verbosity

* Add using for ApplicationEngine

* Fix unit test

* more readable merkleroot test

* Sample for multisig contract

* Sign ut

* Signed!

* Same format for single signature unit test

* Prevent create unwanted objects

* format

* Remove `snapshot` from `MakeTransaction()`

* Rename

* format

* using json based NEP6Wallet import

* at least using read serializable array

* revert last commit
  • Loading branch information
shargon authored Jun 23, 2019
1 parent 87ea7c6 commit 90fa9af
Show file tree
Hide file tree
Showing 29 changed files with 821 additions and 745 deletions.
13 changes: 5 additions & 8 deletions neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,26 @@ public static class Nep5NativeContractExtensions
{
internal class ManualWitness : IVerifiable
{
private readonly UInt160 _hashForVerify;
private readonly UInt160[] _hashForVerify;

public Witness Witness
public Witness[] Witnesses
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

public int Size => 0;

public ManualWitness(UInt160 hashForVerify)
public ManualWitness(params UInt160[] hashForVerify)
{
_hashForVerify = hashForVerify;
_hashForVerify = hashForVerify ?? new UInt160[0];
}

public void Deserialize(BinaryReader reader) { }

public void DeserializeUnsigned(BinaryReader reader) { }

public UInt160 GetScriptHashForVerification(Persistence.Snapshot snapshot)
{
return _hashForVerify;
}
public UInt160[] GetScriptHashesForVerifying(Persistence.Snapshot snapshot) => _hashForVerify;

public void Serialize(BinaryWriter writer) { }

Expand Down
16 changes: 10 additions & 6 deletions neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public static Transaction GetTransaction()
Script = new byte[1],
Sender = UInt160.Zero,
Attributes = new TransactionAttribute[0],
Witness = new Witness
Witnesses = new Witness[]{ new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
}
} }
};
}

Expand Down Expand Up @@ -61,7 +61,8 @@ public static void SetupBlockWithValues(Block block, UInt256 val256, out UInt256
private static void setupBlockBaseWithValues(BlockBase bb, UInt256 val256, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal)
{
bb.PrevHash = val256;
merkRootVal = new UInt256(new byte[] { 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197 });
merkRootVal = UInt256.Parse("0xd841af3d6bd7adb4bca24306725f9aec363edb10de3cafc5f8cca948d7b0290f");

bb.MerkleRoot = merkRootVal;
timestampVal = new DateTime(1968, 06, 01, 0, 0, 0, DateTimeKind.Utc).ToTimestamp();
bb.Timestamp = timestampVal;
Expand All @@ -86,10 +87,13 @@ public static Transaction CreateRandomHashTransaction()
Script = randomBytes,
Sender = UInt160.Zero,
Attributes = new TransactionAttribute[0],
Witness = new Witness
Witnesses = new[]
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
}
}
};
}
Expand Down
12 changes: 8 additions & 4 deletions neo.UnitTests/TestVerifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ namespace Neo.UnitTests
{
public class TestVerifiable : IVerifiable
{
private string testStr = "testStr";
private readonly string testStr = "testStr";

public Witness Witness { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public Witness[] Witnesses
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

public int Size => throw new NotImplementedException();

Expand All @@ -23,7 +27,7 @@ public void DeserializeUnsigned(BinaryReader reader)
throw new NotImplementedException();
}

public UInt160 GetScriptHashForVerification(Snapshot snapshot)
public UInt160[] GetScriptHashesForVerifying(Snapshot snapshot)
{
throw new NotImplementedException();
}
Expand All @@ -35,7 +39,7 @@ public void Serialize(BinaryWriter writer)

public void SerializeUnsigned(BinaryWriter writer)
{
writer.Write((string) testStr);
writer.Write((string)testStr);
}
}
}
66 changes: 18 additions & 48 deletions neo.UnitTests/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public void Transactions_Get()
public void Header_Get()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var merkRootVal, out var val160, out var timestampVal, out var indexVal, out var scriptVal, out var transactionsVal, 0);

uut.Header.Should().NotBeNull();
uut.Header.PrevHash.Should().Be(val256);
Expand All @@ -47,46 +42,31 @@ public void Header_Get()
public void Size_Get()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 3
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 4
// block 9 + 1
uut.Size.Should().Be(109);
uut.Size.Should().Be(110);
}

[TestMethod]
public void Size_Get_1_Transaction()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);

uut.Transactions = new[]
{
TestUtils.GetTransaction()
};

uut.Size.Should().Be(159);
uut.Size.Should().Be(161);
}

[TestMethod]
public void Size_Get_3_Transaction()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);

uut.Transactions = new[]
{
Expand All @@ -95,19 +75,14 @@ public void Size_Get_3_Transaction()
TestUtils.GetTransaction()
};

uut.Size.Should().Be(259);
uut.Size.Should().Be(263);
}

[TestMethod]
public void Serialize()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 1);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 1);

byte[] data;
using (MemoryStream stream = new MemoryStream())
Expand All @@ -119,7 +94,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < data.Length; i++)
Expand All @@ -132,16 +107,11 @@ public void Serialize()
public void Deserialize()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRoot;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(new Block(), val256, out merkRoot, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 1);
TestUtils.SetupBlockWithValues(new Block(), val256, out var merkRoot, out var val160, out var timestampVal, out var indexVal, out var scriptVal, out var transactionsVal, 1);

uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid

byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
Expand Down Expand Up @@ -244,23 +214,23 @@ public void ToJson()

JObject jObj = uut.ToJson();
jObj.Should().NotBeNull();
jObj["hash"].AsString().Should().Be("0xe0b482b6e4c176c9af520dd7caa6d80a9aeaeb80d016e788c702b05f5ac5ba4b");
jObj["size"].AsNumber().Should().Be(159);
jObj["hash"].AsString().Should().Be("0x1d8642796276c8ce3c5c03b8984a1b593d99b49a63d830bb06f800b8c953be77");
jObj["size"].AsNumber().Should().Be(161);
jObj["version"].AsNumber().Should().Be(0);
jObj["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
jObj["merkleroot"].AsString().Should().Be("0xc59eab3a287f74405b80325bc26c66ac7ef18d94c434a18d60950d3f098280f2");
jObj["merkleroot"].AsString().Should().Be("0xd841af3d6bd7adb4bca24306725f9aec363edb10de3cafc5f8cca948d7b0290f");
jObj["time"].AsNumber().Should().Be(4244941696);
jObj["index"].AsNumber().Should().Be(0);
jObj["nextconsensus"].AsString().Should().Be("AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM");

JObject scObj = jObj["witness"];
JObject scObj = ((JArray)jObj["witnesses"])[0];
scObj["invocation"].AsString().Should().Be("");
scObj["verification"].AsString().Should().Be("51");

jObj["tx"].Should().NotBeNull();
JArray txObj = (JArray)jObj["tx"];
txObj[0]["hash"].AsString().Should().Be("0x7647acf1ef8a841b87f2369398a0e9f0ccde0ed9e835d980657103da6da59580");
txObj[0]["size"].AsNumber().Should().Be(50);
txObj[0]["hash"].AsString().Should().Be("0x64ed4e0d79407c60bde534feb44fbbd19bd065282d27ecd3a1a7a647f66affa6");
txObj[0]["size"].AsNumber().Should().Be(51);
txObj[0]["version"].AsNumber().Should().Be(0);
((JArray)txObj[0]["attributes"]).Count.Should().Be(0);
txObj[0]["net_fee"].AsString().Should().Be("0");
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart()
// Creating proposed block
Header header = new Header();
TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal);
header.Size.Should().Be(100);
header.Size.Should().Be(101);

Console.WriteLine($"header {header} hash {header.Hash} timstamp {timestampVal}");

Expand Down
8 changes: 4 additions & 4 deletions neo.UnitTests/UT_Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void Size_Get()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupHeaderWithValues(uut, val256, out _, out _, out _, out _, out _);
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 3
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 4
// header 1
uut.Size.Should().Be(100);
uut.Size.Should().Be(101);
}

[TestMethod]
Expand All @@ -35,7 +35,7 @@ public void Deserialize()

uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid

byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 0 };
byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 0 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 128, 130, 9, 63, 13, 149, 96, 141, 161, 52, 196, 148, 141, 241, 126, 172, 102, 108, 194, 91, 50, 128, 91, 64, 116, 127, 40, 58, 171, 158, 197, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 81, 0 };
byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 0 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < data.Length; i++)
Expand Down
Loading

0 comments on commit 90fa9af

Please sign in to comment.