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

feat: implement proof of work mining functionality #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

miladsoft
Copy link
Member

No description provided.

@miladsoft miladsoft requested review from Copilot and DavidGershony and removed request for Copilot March 24, 2025 08:04
/// Indicates if the response is successful.
/// </summary>
[ArrayProperty(2)]
public bool IsSuccess { get; init; }

Choose a reason for hiding this comment

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

What is the difference between this and the Accepted property?

Copy link
Member Author

Choose a reason for hiding this comment

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

This IsSuccess property can be removed as it was an additional property added for testing purposes. It was redundant with the existing Accepted property which already provides the same functionality. The IsSuccess property has now been removed to simplify the codebase and avoid duplication.

using System.Threading.Tasks;
using Nostr.Client.Messages;

namespace Nostr.Client.NostrPow.Mining

Choose a reason for hiding this comment

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

I thing PoW is enough the whole namespace is nostr anyway

var tags = originalEvent.Tags ?? new NostrEventTags();

// Find the existing nonce tag if any
NostrEventTag? nonceTag = tags.FindFirstTag("nonce");

Choose a reason for hiding this comment

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

I noticed you use the "nonce" everywhere, can you add it to the const strings on the tag instead? or a local const...

// Set a current timestamp
miningEvent = new NostrEvent
{
Id = miningEvent.Id,

Choose a reason for hiding this comment

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

This is wrong, The id of a different event can't fit a new event

Kind = miningEvent.Kind,
Tags = miningEvent.Tags,
Content = miningEvent.Content,
Sig = miningEvent.Sig

Choose a reason for hiding this comment

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

The sig can't fit an event with changed parameters

if (achievedDifficulty >= difficulty)
{
// We found a valid nonce, return the mined event
return candidateEvent.DeepClone(id, candidateEvent.Sig);

Choose a reason for hiding this comment

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

The sig you are setting here is not valid, better to sign it again

/// <summary>
/// Interface for Nostr event miners
/// </summary>
public interface IMiner

Choose a reason for hiding this comment

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

I guess we don't need this

if (targetDifficulty.HasValue && targetDifficulty.Value < minDifficulty)
return false;

return true;

Choose a reason for hiding this comment

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

You also need to check that the difficulty is greater than or equal to the target difficulty

/// </summary>
public static int CountLeadingZeroBitsInNibble(int nibble)
{
if (nibble >= 8) return 0;

Choose a reason for hiding this comment

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

return return count + BitOperations.LeadingZeroCount((uint)nibble) - 24;

/// <summary>
/// Try to parse a hex character to its integer value
/// </summary>
private static bool TryParseHexDigit(char c, out int value)

Choose a reason for hiding this comment

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

Why use int if we only look at the char value?

using System;
using System.Security.Cryptography;

namespace NostrDebug.Web.Pow

Choose a reason for hiding this comment

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

I'm not really sure, but why do we have a second implementation of the PoW instead of calling the implementation on the NostrEvent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants