Skip to content

Allow to blacklist block hashes #3803

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.fs.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"morph5.fs.neo.org:40333",
"morph6.fs.neo.org:40333",
"morph7.fs.neo.org:40333"
]
],
"BlockBlackList": []
}
}
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.fs.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"morph05.testnet.fs.neo.org:50333",
"morph06.testnet.fs.neo.org:50333",
"morph07.testnet.fs.neo.org:50333"
]
],
"BlockBlackList": []
}
}
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"seed3.neo.org:10333",
"seed4.neo.org:10333",
"seed5.neo.org:10333"
]
],
"BlockBlackList": []
}
}
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"seed3.neo.org:10333",
"seed4.neo.org:10333",
"seed5.neo.org:10333"
]
],
"BlockBlackList": []
}
}
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"seed3t5.neo.org:20333",
"seed4t5.neo.org:20333",
"seed5t5.neo.org:20333"
]
],
"BlockBlackList": []
}
}
4 changes: 4 additions & 0 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ private VerifyResult OnNewBlock(Block block)
{
if (!block.TryGetHash(out var blockHash)) return VerifyResult.Invalid;

if (ProtocolSettings.Default.IsBlacklisted(block.Header))
return VerifyResult.Invalid;

var snapshot = system.StoreView;
uint currentHeight = NativeContract.Ledger.CurrentIndex(snapshot);
uint headerHeight = system.HeaderCache.Last?.Index ?? currentHeight;
Expand Down Expand Up @@ -323,6 +326,7 @@ private void OnNewHeaders(Header[] headers)
foreach (var header in headers)
{
if (!header.TryGetHash(out _)) continue;
if (ProtocolSettings.Default.IsBlacklisted(header)) continue;
if (header.Index > headerHeight + 1) break;
if (header.Index < headerHeight + 1) continue;
if (!header.Verify(system.Settings, snapshot, system.HeaderCache)) break;
Expand Down
23 changes: 21 additions & 2 deletions src/Neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public record ProtocolSettings
/// </summary>
public string[] SeedList { get; init; }

/// <summary>
/// Block black list
/// </summary>
public HashSet<UInt256> BlockBlackList { get; init; }

/// <summary>
/// Indicates the time in milliseconds between two blocks.
/// </summary>
Expand Down Expand Up @@ -111,9 +116,10 @@ public record ProtocolSettings
{
Network = 0u,
AddressVersion = 0x35,
StandbyCommittee = Array.Empty<ECPoint>(),
StandbyCommittee = [],
ValidatorsCount = 0,
SeedList = Array.Empty<string>(),
SeedList = [],
BlockBlackList = [],
MillisecondsPerBlock = 15000,
MaxTransactionsPerBlock = 512,
MemoryPoolMaxTransactions = 50_000,
Expand Down Expand Up @@ -207,6 +213,9 @@ public static ProtocolSettings Load(IConfigurationSection section)
SeedList = section.GetSection("SeedList").Exists()
? section.GetSection("SeedList").GetChildren().Select(p => p.Get<string>()).ToArray()
: Default.SeedList,
BlockBlackList = section.GetSection("BlockBlackList").Exists()
? new(section.GetSection("BlockBlackList").GetChildren().Select(p => UInt256.Parse(p.Get<string>())))
: Default.BlockBlackList,
MillisecondsPerBlock = section.GetValue("MillisecondsPerBlock", Default.MillisecondsPerBlock),
MaxTransactionsPerBlock = section.GetValue("MaxTransactionsPerBlock", Default.MaxTransactionsPerBlock),
MemoryPoolMaxTransactions = section.GetValue("MemoryPoolMaxTransactions", Default.MemoryPoolMaxTransactions),
Expand Down Expand Up @@ -287,5 +296,15 @@ public bool IsHardforkEnabled(Hardfork hardfork, uint index)
// If the hardfork isn't specified in the configuration, return false.
return false;
}

/// <summary>
/// Check if the header is blacklisted
/// </summary>
/// <param name="header">Headeer</param>
/// <returns>True if blacklisted</returns>
public bool IsBlacklisted(Header header)
{
return BlockBlackList.Contains(header.Hash);
}
}
}
18 changes: 13 additions & 5 deletions src/Plugins/DBFTPlugin/Consensus/ConsensusService.OnMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ private void OnPrepareRequestReceived(ExtensiblePayload payload, PrepareRequest
return;
}

// Timeout extension: prepare request has been received with success
// around 2*15/M=30.0/5 ~ 40% block time (for M=5)
ExtendTimerByFactor(2);

context.Block.Header.Timestamp = message.Timestamp;
context.Block.Header.Nonce = message.Nonce;
context.TransactionHashes = message.TransactionHashes;
Expand All @@ -109,7 +105,19 @@ private void OnPrepareRequestReceived(ExtensiblePayload payload, PrepareRequest
if (!context.GetMessage<PrepareResponse>(context.PreparationPayloads[i]).PreparationHash.Equals(payload.Hash))
context.PreparationPayloads[i] = null;
context.PreparationPayloads[message.ValidatorIndex] = payload;
byte[] hashData = context.EnsureHeader().GetSignData(neoSystem.Settings.Network);

var header = context.EnsureHeader();
if (ProtocolSettings.Default.IsBlacklisted(header.Header))
{
Log($"Block is blacklisted", LogLevel.Warning);
return;
}

// Timeout extension: prepare request has been received with success
// around 2*15/M=30.0/5 ~ 40% block time (for M=5)
ExtendTimerByFactor(2);

byte[] hashData = header.GetSignData(neoSystem.Settings.Network);
for (int i = 0; i < context.CommitPayloads.Length; i++)
if (context.GetMessage(context.CommitPayloads[i])?.ViewNumber == context.ViewNumber)
if (!Crypto.VerifySignature(hashData, context.GetMessage<Commit>(context.CommitPayloads[i]).Signature.Span, context.Validators[i]))
Expand Down