Skip to content

Commit

Permalink
Feature: added extra data length checks (#4805)
Browse files Browse the repository at this point in the history
* Added 32 bytes check on extra data

Added checks and tests for the ExtraData not to be longer than 32 bytes
  • Loading branch information
OlegJakushkin authored and kamilchodola committed Oct 24, 2022
1 parent 0be02d8 commit 161d78e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
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}",
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);
}
}

0 comments on commit 161d78e

Please sign in to comment.