-
Notifications
You must be signed in to change notification settings - Fork 1k
[N3] Proof of node #4304
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
Open
shargon
wants to merge
17
commits into
neo-project:master-n3
Choose a base branch
from
shargon:proof-of-node
base: master-n3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−21
Open
[N3] Proof of node #4304
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cbc8ce8
Proof of node
shargon 7b73285
Fix pow
shargon 58778c3
Ensure Primary it's alive
shargon de06e7b
Max Proof Of Node Height in policy
shargon 75b2bc7
Set difficulty by policy
shargon 1140f19
Update src/Neo/SmartContract/Native/PolicyContract.cs
shargon 0d2a56f
Remove extra blank line
shargon 841ba47
Add comment
shargon 6738aa7
Pow in separate class
shargon fe20a2e
Remove null
shargon c34ab6c
Reduce difficulty
shargon 829f58e
Update src/Neo/Cryptography/ProofOfWork.cs
shargon 35ca4e2
fix
shargon a938a2b
Merge branch 'proof-of-node' of https://github.com/shargon/neo into p…
shargon 9cdbeec
Merge branch 'master-n3' into proof-of-node
ajara87 b3a5248
Fix conflicts
ajara87 1f96009
Merge pull request #18 from ajara87/pr-4304
shargon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // ProofOfWork.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.Extensions; | ||
| using Neo.Extensions.Factories; | ||
| using System; | ||
| using System.Buffers.Binary; | ||
|
|
||
| namespace Neo.Cryptography | ||
| { | ||
| public class ProofOfWork | ||
| { | ||
| /// <summary> | ||
| /// Verify if the proof of work match with the difficulty. | ||
| /// </summary> | ||
| /// <param name="proofOfWork">Proof of Work</param> | ||
| /// <param name="difficulty">Difficulty</param> | ||
| /// <returns></returns> | ||
| public static bool VerifyDifficulty(UInt256 proofOfWork, uint difficulty) | ||
| { | ||
| // Take the first 8 bytes in order to check the proof of work difficulty | ||
|
|
||
| var bytes = proofOfWork.ToArray(); | ||
| var value = BinaryPrimitives.ReadUInt32BigEndian(bytes.AsSpan(0, 4)); | ||
| return value < difficulty; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compute proof of work | ||
| /// </summary> | ||
| /// <param name="blockHash">BlockHash</param> | ||
| /// <param name="nonce">Nonce</param> | ||
| /// <returns>Proof of Work</returns> | ||
| public static UInt256 Compute(UInt256 blockHash, long nonce) | ||
| { | ||
| var salt = new byte[16]; | ||
| BinaryPrimitives.WriteInt64BigEndian(salt, nonce); | ||
|
|
||
| return (UInt256)Helper.Blake2b_256(blockHash.ToArray(), salt); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compute proof of work with difficulty | ||
| /// </summary> | ||
| /// <param name="blockHash">Block hash</param> | ||
| /// <param name="difficulty">Difficulty</param> | ||
| /// <returns>Nonce</returns> | ||
| public static long ComputeNonce(UInt256 blockHash, uint difficulty) | ||
| { | ||
| while (true) | ||
| { | ||
| var nonce = RandomNumberFactory.NextInt64(); | ||
| var pow = Compute(blockHash, nonce); | ||
|
|
||
| if (VerifyDifficulty(pow, difficulty)) | ||
| return nonce; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.