-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathConsensusContext.Get.cs
138 lines (125 loc) · 5.11 KB
/
ConsensusContext.Get.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
138
// Copyright (C) 2015-2024 The Neo Project.
//
// ConsensusContext.Get.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository 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.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.Wallets;
using System.Linq;
using System.Runtime.CompilerServices;
using static Neo.Consensus.RecoveryMessage;
namespace Neo.Consensus
{
partial class ConsensusContext
{
public ConsensusMessage GetMessage(ExtensiblePayload payload)
{
if (payload is null) return null;
if (!cachedMessages.TryGetValue(payload.Hash, out ConsensusMessage message))
cachedMessages.Add(payload.Hash, message = ConsensusMessage.DeserializeFrom(payload.Data));
return message;
}
public T GetMessage<T>(ExtensiblePayload payload) where T : ConsensusMessage
{
return (T)GetMessage(payload);
}
private ChangeViewPayloadCompact GetChangeViewPayloadCompact(ExtensiblePayload payload)
{
ChangeView message = GetMessage<ChangeView>(payload);
return new ChangeViewPayloadCompact
{
ValidatorIndex = message.ValidatorIndex,
OriginalViewNumber = message.ViewNumber,
Timestamp = message.Timestamp,
InvocationScript = payload.Witness.InvocationScript
};
}
private PreCommitPayloadCompact GetPreCommitPayloadCompact(ExtensiblePayload payload)
{
PreCommit preCommit = GetMessage<PreCommit>(payload);
return new PreCommitPayloadCompact
{
ViewNumber = preCommit.ViewNumber,
ValidatorIndex = preCommit.ValidatorIndex,
PreparationHash = preCommit.PreparationHash,
InvocationScript = payload.Witness.InvocationScript,
};
}
private CommitPayloadCompact GetCommitPayloadCompact(ExtensiblePayload payload)
{
Commit message = GetMessage<Commit>(payload);
return new CommitPayloadCompact
{
ViewNumber = message.ViewNumber,
ValidatorIndex = message.ValidatorIndex,
Signature = message.Signature,
InvocationScript = payload.Witness.InvocationScript
};
}
private PreparationPayloadCompact GetPreparationPayloadCompact(ExtensiblePayload payload)
{
return new PreparationPayloadCompact
{
ValidatorIndex = GetMessage(payload).ValidatorIndex,
InvocationScript = payload.Witness.InvocationScript
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte GetPriorityPrimaryIndex(byte viewNumber)
{
int p = ((int)Block[0].Index - viewNumber) % Validators.Length;
return p >= 0 ? (byte)p : (byte)(p + Validators.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte GetFallbackPrimaryIndex(byte priorityPrimaryIndex)
{
if (Validators.Length <= 1) return priorityPrimaryIndex;
int p = ((int)Block[0].Index + 1) % (Validators.Length - 1);
p = p >= 0 ? (byte)p : (byte)(p + Validators.Length);
return p < priorityPrimaryIndex ? (byte)p : (byte)(p + 1);
}
public UInt160 GetSender(int index)
{
return Contract.CreateSignatureRedeemScript(Validators[index]).ToScriptHash();
}
/// <summary>
/// Return the expected block size
/// </summary>
public int GetExpectedBlockSize(uint pId)
{
return GetExpectedBlockSizeWithoutTransactions(Transactions[pId].Count) + // Base size
Transactions[pId].Values.Sum(u => u.Size); // Sum Txs
}
/// <summary>
/// Return the expected block system fee
/// </summary>
public long GetExpectedBlockSystemFee(uint pId)
{
return Transactions[pId].Values.Sum(u => u.SystemFee); // Sum Txs
}
/// <summary>
/// Return the expected block size without txs
/// </summary>
/// <param name="expectedTransactions">Expected transactions</param>
internal int GetExpectedBlockSizeWithoutTransactions(int expectedTransactions)
{
return
sizeof(uint) + // Version
UInt256.Length + // PrevHash
UInt256.Length + // MerkleRoot
sizeof(ulong) + // Timestamp
sizeof(ulong) + // Nonce
sizeof(uint) + // Index
sizeof(byte) + // PrimaryIndex
UInt160.Length + // NextConsensus
1 + _witnessSize + // Witness
IO.Helper.GetVarSize(expectedTransactions);
}
}
}