-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ExtensiblePayload.cs
137 lines (120 loc) · 4.31 KB
/
ExtensiblePayload.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2015-2022 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
using System.IO;
namespace Neo.Network.P2P.Payloads
{
/// <summary>
/// Represents an extensible message that can be relayed.
/// </summary>
public class ExtensiblePayload : IInventory
{
/// <summary>
/// The category of the extension.
/// </summary>
public string Category;
/// <summary>
/// Indicates that the payload is only valid when the block height is greater than or equal to this value.
/// </summary>
public uint ValidBlockStart;
/// <summary>
/// Indicates that the payload is only valid when the block height is less than this value.
/// </summary>
public uint ValidBlockEnd;
/// <summary>
/// The sender of the payload.
/// </summary>
public UInt160 Sender;
/// <summary>
/// The data of the payload.
/// </summary>
public ReadOnlyMemory<byte> Data;
/// <summary>
/// The witness of the payload. It must match the <see cref="Sender"/>.
/// </summary>
public Witness Witness;
private UInt256 _hash = null;
public UInt256 Hash
{
get
{
if (_hash == null)
{
_hash = this.CalculateHash();
}
return _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
Witness[] IVerifiable.Witnesses
{
get
{
return new[] { Witness };
}
set
{
if (value.Length != 1) throw new ArgumentException();
Witness = value[0];
}
}
void ISerializable.Deserialize(ref MemoryReader reader)
{
((IVerifiable)this).DeserializeUnsigned(ref reader);
if (reader.ReadByte() != 1) throw new FormatException();
Witness = reader.ReadSerializable<Witness>();
}
void IVerifiable.DeserializeUnsigned(ref MemoryReader reader)
{
Category = reader.ReadVarString(32);
ValidBlockStart = reader.ReadUInt32();
ValidBlockEnd = reader.ReadUInt32();
if (ValidBlockStart >= ValidBlockEnd) throw new FormatException();
Sender = reader.ReadSerializable<UInt160>();
Data = reader.ReadVarMemory();
}
UInt160[] IVerifiable.GetScriptHashesForVerifying(DataCache snapshot)
{
return new[] { Sender }; // This address should be checked by consumer
}
void ISerializable.Serialize(BinaryWriter writer)
{
((IVerifiable)this).SerializeUnsigned(writer);
writer.Write((byte)1); writer.Write(Witness);
}
void IVerifiable.SerializeUnsigned(BinaryWriter writer)
{
writer.WriteVarString(Category);
writer.Write(ValidBlockStart);
writer.Write(ValidBlockEnd);
writer.Write(Sender);
writer.WriteVarBytes(Data.Span);
}
internal bool Verify(ProtocolSettings settings, DataCache snapshot, ISet<UInt160> extensibleWitnessWhiteList)
{
uint height = NativeContract.Ledger.CurrentIndex(snapshot);
if (height < ValidBlockStart || height >= ValidBlockEnd) return false;
if (!extensibleWitnessWhiteList.Contains(Sender)) return false;
return this.VerifyWitnesses(settings, snapshot, 0_06000000L);
}
}
}