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

Feature: added extra data length checks #4805

Merged
Merged
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
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Config/ExitCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public static class ExitCodes
public const int NoEngineModule = 100;

public const int NoDownloadOldReceiptsOrBlocks = 101;
public const int TooLongExtraData = 102;
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Consensus/IMiningConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface IMiningConfig : IConfig
DefaultValue = "false")]
bool RandomizedBlocks { get; set; }

[ConfigItem(Description = "Block header extra data.", DefaultValue = "Nethermind")]
[ConfigItem(Description = "Block header extra data. 32-bytes shall be extra data max length.", DefaultValue = "Nethermind")]
string ExtraData { get; set; }

byte[] GetExtraDataBytes();
Expand Down
13 changes: 12 additions & 1 deletion src/Nethermind/Nethermind.Consensus/MiningConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Text;
using Nethermind.Config;
using Nethermind.Core.Exceptions;
using Nethermind.Core.Extensions;
using Nethermind.Int256;

Expand All @@ -42,8 +45,16 @@ public string ExtraData
}
set
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
if (bytes != null && bytes.Length > 32)
{
throw new InvalidConfigurationException($"Extra Data length was more than 32 bytes. You provided: {_extraDataString}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if this exception will stop the node?

Copy link
Contributor Author

@OlegJakushkin OlegJakushkin Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes:
With --Mining.ExtraData=Nethermind-Nethermind-Nethermind-Nethermind
in Debug
image
in Release
image

Copy link
Member

@LukaszRozmej LukaszRozmej Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer a nicer message, not an exception trace.
It is now thrown very early in the pipeline and not handled correctly or it is because it is being packed by other exceptions. I also think exit code might not be used. Can we make this simple error log in next release?

Exception on this line: https://github.com/NethermindEth/nethermind/blob/master/src/Nethermind/Nethermind.Runner/Program.cs#L158

Handling:
https://github.com/NethermindEth/nethermind/blob/master/src/Nethermind/Nethermind.Runner/Program.cs#L211-L223

ExitCodes.TooLongExtraData);

}

_extraDataString = value;
_extraDataBytes = Encoding.UTF8.GetBytes(_extraDataString);
_extraDataBytes = bytes;
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/Nethermind/Nethermind.Mining.Test/MiningConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using Nethermind.Consensus;
using Nethermind.Core.Exceptions;
using NUnit.Framework;

namespace Nethermind.Mining.Test;
Expand All @@ -8,8 +9,10 @@ namespace Nethermind.Mining.Test;
public class MiningConfigTest
{
[TestCase]
[TestCase("")]
[TestCase("1, 2, 3, 4, 5")]
[TestCase("Other Extra data")]

public void Test(string data = "Nethermind")
{
IMiningConfig config = new MiningConfig();
Expand All @@ -19,4 +22,24 @@ public void Test(string data = "Nethermind")
Assert.AreEqual(config.ExtraData, data);
Assert.AreEqual(config.GetExtraDataBytes(), dataBytes);
}

[Test]
public void TestTooLongExtraData()
{
string data = "1234567890" +
"1234567890" +
"1234567890" +
"1234567890";

IMiningConfig config = new MiningConfig();
string defaultData = config.ExtraData;
byte[] defaultDataBytes = Encoding.UTF8.GetBytes(defaultData);

byte[] dataBytes = Encoding.UTF8.GetBytes(data);
Assert.Greater(dataBytes.Length, 32);

Assert.Throws<InvalidConfigurationException>(() => config.ExtraData = data); //throw on update
Assert.AreEqual(config.ExtraData, defaultData); // Keep previous one
Assert.AreEqual(config.GetExtraDataBytes(), defaultDataBytes);
}
}