Skip to content
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: P2P Response Signature Message #1553

Merged
merged 6 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/neo/Network/P2P/MessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public enum MessageCommand : byte
Block = 0x2c,
[ReflectionCache(typeof(ConsensusPayload))]
Consensus = 0x2d,
[ReflectionCache(typeof(OraclePayload))]
Oracle = 0x2e,
Reject = 0x2f,

//SPV protocol
Expand All @@ -52,6 +54,6 @@ public enum MessageCommand : byte
MerkleBlock = 0x38,

//others
Alert = 0x40,
Alert = 0x40
}
}
145 changes: 145 additions & 0 deletions src/neo/Network/P2P/Payloads/OraclePayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Neo.IO;
using System.IO;
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.Persistence;
using System;
using System.Linq;
using Neo.SmartContract;
using Neo.SmartContract.Native;

namespace Neo.Network.P2P.Payloads
{
public class OraclePayload : IVerifiable
{
private byte[] data;
//public uint OracleValidatorIndex;
private ECPoint oraclePub;
private Witness witness;

public byte[] Data
{
get => data;
set { data = value; _hash = null; _size = 0; }
}

public ECPoint OraclePub
{
get => oraclePub;
set { oraclePub = value; _hash = null; _size = 0; }
}

public Witness Witness
{
get => witness;
set { witness = value; _hash = null; _size = 0; }
}

private int _size;
public int Size
{
get
{
if (_size == 0)
{
_size = sizeof(byte) + //Type
shargon marked this conversation as resolved.
Show resolved Hide resolved
Data.GetVarSize() + //Data
OraclePub.Size + //Oracle Public key
Witness.Size; //Witness
}
return _size;
}
}

private UInt256 _hash = null;
public UInt256 Hash
{
get
{
if (_hash == null)
{
_hash = new UInt256(Crypto.Hash256(this.GetHashData()));
}
return _hash;
}
}

Witness[] IVerifiable.Witnesses
{
get
{
return new[] { Witness };
}
set
{
if (value.Length != 1) throw new ArgumentException();
Witness = value[0];
}
}

private OracleResponseSignature _deserializedOracleSignature = null;
public OracleResponseSignature OracleSignature
{
get
{
if (_deserializedOracleSignature is null)
_deserializedOracleSignature = OracleResponseSignature.DeserializeFrom(Data);
return _deserializedOracleSignature;
}
internal set
{
if (!ReferenceEquals(_deserializedOracleSignature, value))
{
_deserializedOracleSignature = value;
Data = value?.ToArray();
}
}
}

public OracleResponseSignature GetDeserializedOracleSignature()
//public T GetDeserializedOracleSignature<T>() where T : OracleResponseSignature
{
return OracleSignature;
}

void ISerializable.Deserialize(BinaryReader reader)
{
((IVerifiable)this).DeserializeUnsigned(reader);
Witness = reader.ReadSerializable<Witness>();
}
void IVerifiable.DeserializeUnsigned(BinaryReader reader)
{
Data = reader.ReadVarBytes(Transaction.MaxTransactionSize);
OraclePub = reader.ReadSerializable<ECPoint>();
}

public virtual void Serialize(BinaryWriter writer)
{
((IVerifiable)this).SerializeUnsigned(writer);
writer.Write(new Witness[] { Witness });
}

void IVerifiable.SerializeUnsigned(BinaryWriter writer)
{
writer.Write(Data);
writer.Write(OraclePub);
}

UInt160[] IVerifiable.GetScriptHashesForVerifying(StoreView snapshot)
{
//ECPoint[] validators = NativeContract.Oracle.GetOracleValidators(snapshot);
shargon marked this conversation as resolved.
Show resolved Hide resolved
//if (validators.Length <= OracleValidatorIndex)
// throw new InvalidOperationException();
//return new[] { Contract.CreateSignatureRedeemScript(validators[OracleValidatorIndex]).ToScriptHash() };
return new[] { Contract.CreateSignatureRedeemScript(OraclePub).ToScriptHash() };
}

public bool Verify(StoreView snapshot)
{
ECPoint[] validators = NativeContract.Oracle.GetOracleValidators(snapshot);
if (!validators.Any(u => u.Equals(OraclePub)))
return false;
return this.VerifyWitnesses(snapshot, 0_02000000);
}
}
}
121 changes: 121 additions & 0 deletions src/neo/Network/P2P/Payloads/OracleResponseSignature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.IO;
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace Neo.Network.P2P.Payloads
{
public class OracleResponseSignature : ISerializable
{
private const byte ResponseSignatureType = 0x01;
private UInt256 transactionRequestHash;
shargon marked this conversation as resolved.
Show resolved Hide resolved
private UInt256 oracleExecutionCacheHash;
private ECPoint oraclePub;
shargon marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Signature for the oracle response tx for this public key
/// </summary>
private byte[] signature;

public UInt256 TransactionRequestHash
{
get => transactionRequestHash;
set { transactionRequestHash = value; _hash = null; _size = 0; }
}

public UInt256 OracleExecutionCacheHash
{
get => oracleExecutionCacheHash;
set { oracleExecutionCacheHash = value; _hash = null; _size = 0; }
}

public ECPoint OraclePub
{
get => oraclePub;
set { oraclePub = value; _hash = null; _size = 0; }
}

public byte[] Signature
{
get => signature;
set
{
if (value.Length != 64) throw new ArgumentException();
signature = value;
_hash = null;
_size = 0;
}
}

private int _size;
public int Size
{
get
{
if (_size == 0)
{
_size = sizeof(byte) + //Type
UInt256.Length + //Transaction Hash
UInt256.Length + //OracleExecutionCache Hash
OraclePub.Size + //Oracle Public key
Signature.GetVarSize(); //Oracle Validator Signature
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
return _size;
}
}

private UInt256 _hash = null;
public UInt256 Hash
{
get
{
if (_hash == null)
{
_hash = new UInt256(Crypto.Hash256(this.ToArray()));
}
return _hash;
}
}

public virtual void Deserialize(BinaryReader reader)
{
if (reader.ReadByte() != ResponseSignatureType) throw new FormatException();
DeserializeWithoutType(reader);
}

private void DeserializeWithoutType(BinaryReader reader)
{
TransactionRequestHash = reader.ReadSerializable<UInt256>();
OracleExecutionCacheHash = reader.ReadSerializable<UInt256>();
OraclePub = reader.ReadSerializable<ECPoint>();
Signature = reader.ReadFixedBytes(64);
}

public static OracleResponseSignature DeserializeFrom(byte[] data)
{
switch (data[0])
{
case ResponseSignatureType:
{
using BinaryReader reader = new BinaryReader(new MemoryStream(data, 1, data.Length - 1), Encoding.UTF8, false);

var ret = new OracleResponseSignature();
ret.DeserializeWithoutType(reader);
return ret;
}
default: throw new FormatException();
}
}

public virtual void Serialize(BinaryWriter writer)
{
writer.Write(ResponseSignatureType);
writer.Write(TransactionRequestHash);
writer.Write(OracleExecutionCacheHash);
writer.Write(OraclePub);
writer.Write(Signature);
}
}
}