-
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
Oracle: Set the right Tx version #1542
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ public class Transaction : IEquatable<Transaction>, IInventory, IInteroperable | |
private Cosigner[] cosigners; | ||
private byte[] script; | ||
private Witness[] witnesses; | ||
private UInt256 oracleRequestTx; | ||
|
||
public const int HeaderSize = | ||
sizeof(TransactionVersion) + //Version | ||
|
@@ -61,6 +62,12 @@ public Cosigner[] Cosigners | |
set { cosigners = value; _hash = null; _size = 0; } | ||
} | ||
|
||
public UInt256 OracleRequestTx | ||
{ | ||
get => oracleRequestTx; | ||
set { oracleRequestTx = value; _hash = null; _size = 0; } | ||
} | ||
|
||
/// <summary> | ||
/// The <c>NetworkFee</c> for the transaction divided by its <c>Size</c>. | ||
/// <para>Note that this property must be used with care. Getting the value of this property multiple times will return the same result. The value of this property can only be obtained after the transaction has been completely built (no longer modified).</para> | ||
|
@@ -121,6 +128,11 @@ public int Size | |
Cosigners.GetVarSize() + //Cosigners | ||
Script.GetVarSize() + //Script | ||
Witnesses.GetVarSize(); //Witnesses | ||
|
||
if (Version == TransactionVersion.OracleResponse) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, types are types and versions are versions, both have their own valid meaning and purpose and it's better not to mix them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original idea was this #1539 (comment) |
||
_size += UInt256.Length; | ||
} | ||
} | ||
return _size; | ||
} | ||
|
@@ -182,6 +194,7 @@ public void DeserializeUnsigned(BinaryReader reader) | |
if (Cosigners.Select(u => u.Account).Distinct().Count() != Cosigners.Length) throw new FormatException(); | ||
Script = reader.ReadVarBytes(ushort.MaxValue); | ||
if (Script.Length == 0) throw new FormatException(); | ||
OracleRequestTx = Version == TransactionVersion.OracleResponse ? reader.ReadSerializable<UInt256>() : null; | ||
} | ||
|
||
public bool Equals(Transaction other) | ||
|
@@ -225,6 +238,10 @@ void IVerifiable.SerializeUnsigned(BinaryWriter writer) | |
writer.Write(Attributes); | ||
writer.Write(Cosigners); | ||
writer.WriteVarBytes(Script); | ||
if (Version == TransactionVersion.OracleResponse) | ||
{ | ||
writer.Write(OracleRequestTx); | ||
} | ||
} | ||
|
||
public JObject ToJson() | ||
|
@@ -242,6 +259,10 @@ public JObject ToJson() | |
json["cosigners"] = Cosigners.Select(p => p.ToJson()).ToArray(); | ||
json["script"] = Convert.ToBase64String(Script); | ||
json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); | ||
if (Version == TransactionVersion.OracleResponse) | ||
{ | ||
json["oracle_response_tx"] = OracleRequestTx.ToString(); | ||
} | ||
return json; | ||
} | ||
|
||
|
@@ -259,6 +280,14 @@ public static Transaction FromJson(JObject json) | |
tx.Cosigners = ((JArray)json["cosigners"]).Select(p => Cosigner.FromJson(p)).ToArray(); | ||
tx.Script = Convert.FromBase64String(json["script"].AsString()); | ||
tx.Witnesses = ((JArray)json["witnesses"]).Select(p => Witness.FromJson(p)).ToArray(); | ||
if (tx.Version == TransactionVersion.OracleResponse) | ||
{ | ||
tx.OracleRequestTx = UInt256.Parse(json["oracle_response_tx"].AsString()); | ||
} | ||
else | ||
{ | ||
tx.OracleRequestTx = null; | ||
} | ||
return tx; | ||
} | ||
|
||
|
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.
We create a new field! Why not use attribute?
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.
#1539 (comment)
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 interesting to me to compare this implementation to attribute-based one, for some reason I think that reusing attributes would make things easier and require less changes. And adding this field to each and every transaction is a bit inefficient memory-wise.