-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Payloads: add NotValidBefore transaction attribute #2812
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Neo.IO; | ||
using Neo.Json; | ||
using Neo.Persistence; | ||
using Neo.SmartContract.Native; | ||
using System.IO; | ||
|
||
namespace Neo.Network.P2P.Payloads | ||
{ | ||
public class NotValidBefore : TransactionAttribute | ||
{ | ||
/// <summary> | ||
/// Indicates that the transaction is not valid before this height. | ||
/// </summary> | ||
public uint Height; | ||
|
||
public override TransactionAttributeType Type => TransactionAttributeType.NotValidBefore; | ||
|
||
public override bool AllowMultiple => false; | ||
|
||
public override int Size => base.Size + | ||
sizeof(uint); // Height. | ||
|
||
protected override void DeserializeWithoutType(ref MemoryReader reader) | ||
{ | ||
Height = reader.ReadUInt32(); | ||
} | ||
|
||
protected override void SerializeWithoutType(BinaryWriter writer) | ||
{ | ||
writer.Write(Height); | ||
} | ||
|
||
public override JObject ToJson() | ||
{ | ||
JObject json = base.ToJson(); | ||
json["height"] = Height; | ||
return json; | ||
} | ||
|
||
public override bool Verify(DataCache snapshot, Transaction tx) | ||
{ | ||
var block_height = NativeContract.Ledger.CurrentIndex(snapshot); | ||
return block_height >= Height; | ||
} | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.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,75 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.IO; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
|
||
namespace Neo.UnitTests.Network.P2P.Payloads | ||
{ | ||
[TestClass] | ||
public class UT_NotValidBefore | ||
{ | ||
[TestMethod] | ||
public void Size_Get() | ||
{ | ||
var test = new NotValidBefore(); | ||
test.Size.Should().Be(5); | ||
} | ||
|
||
[TestMethod] | ||
public void ToJson() | ||
{ | ||
var test = new NotValidBefore(); | ||
test.Height = 42; | ||
var json = test.ToJson().ToString(); | ||
Assert.AreEqual(@"{""type"":""NotValidBefore"",""height"":42}", json); | ||
} | ||
|
||
[TestMethod] | ||
public void DeserializeAndSerialize() | ||
{ | ||
var test = new NotValidBefore(); | ||
|
||
var clone = test.ToArray().AsSerializable<NotValidBefore>(); | ||
Assert.AreEqual(clone.Type, test.Type); | ||
|
||
// As transactionAttribute | ||
|
||
byte[] buffer = test.ToArray(); | ||
var reader = new MemoryReader(buffer); | ||
clone = TransactionAttribute.DeserializeFrom(ref reader) as NotValidBefore; | ||
Assert.AreEqual(clone.Type, test.Type); | ||
|
||
// Wrong type | ||
|
||
buffer[0] = 0xff; | ||
reader = new MemoryReader(buffer); | ||
try | ||
{ | ||
TransactionAttribute.DeserializeFrom(ref reader); | ||
Assert.Fail(); | ||
} | ||
catch (FormatException) { } | ||
reader = new MemoryReader(buffer); | ||
try | ||
{ | ||
new NotValidBefore().Deserialize(ref reader); | ||
Assert.Fail(); | ||
} | ||
catch (FormatException) { } | ||
} | ||
|
||
[TestMethod] | ||
public void Verify() | ||
{ | ||
var test = new NotValidBefore(); | ||
var snapshot = TestBlockchain.GetTestSnapshot(); | ||
test.Height = NativeContract.Ledger.CurrentIndex(snapshot) + 1; | ||
|
||
Assert.IsFalse(test.Verify(snapshot, new Transaction())); | ||
test.Height--; | ||
Assert.IsTrue(test.Verify(snapshot, new Transaction())); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wont wait in the mempool until it's valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the desired behaviour, the transaction is not supposed to wait. It's the sender's responsibility to send it at the right time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then... why we need it? the sender can sign it after the desired Height.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1992:
In #1573 context this allows to have some "hostage" transaction that can be used if the main one is not completed in time (which works especially nice with #1991). But in general, any scenario where you can/need to create a transaction now, but you don't want it to be used until some block. Some offline signing scenarios might benefit from it as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the transaction is not valid, it will be included in the know hashes list and it won't be valid until an unknown time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not supposed to ever appear on the P2P network (unless it's wrapped into P2PNotaryPayload) until it becomes valid. It can't really appear there before NVB height, any node will reject it right away, so I doubt anyone will even try sending it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's a different problem, but what will happend if it's in known hashes and now it's valid?
How we will know that it was rejected because previously was rejected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.