-
Notifications
You must be signed in to change notification settings - Fork 18
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/pow support #161
Merged
Merged
Feat/pow support #161
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c1ab643
feat: power implementation
hunjixin 6f04361
feat: simplly code
94984d2
chore: add a explain comment for target difficulty
3315281
fix typo
80af65a
fix: blocked error channel
b2d7287
chore: update dev toml sc address
44d8198
feat: change wrong log and add toml tag
f0ae8dc
fix: upgrade cowsay to v0.0.4
4211285
fix: update devent controller address
68ca34f
fix: Add POW config processor
bgins 072f997
feat: add pow address for testnet
AquiGorka 4f7caef
chore: update to latest controller address
AquiGorka a94fce7
chore: update contract addresses
arsen3d fcb82b4
build: add hardhat testnet config
arsen3d 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 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,51 @@ | ||
package lilypad | ||
|
||
import ( | ||
"github.com/lilypad-tech/lilypad/pkg/options" | ||
optionsfactory "github.com/lilypad-tech/lilypad/pkg/options" | ||
"github.com/lilypad-tech/lilypad/pkg/system" | ||
"github.com/lilypad-tech/lilypad/pkg/web3" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newPowSignalCmd() *cobra.Command { | ||
options := optionsfactory.NewPowSignalOptions() | ||
|
||
powSignalCmd := &cobra.Command{ | ||
Use: "pow-signal", | ||
Short: "Send a pow signal to smart contract.", | ||
Long: "Send a pow signal to smart contract.", | ||
Example: "", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
network, _ := cmd.Flags().GetString("network") | ||
|
||
options, err := optionsfactory.ProcessPowSignalOptions(options, network) | ||
if err != nil { | ||
return err | ||
} | ||
return runPowSignal(cmd, options) | ||
}, | ||
} | ||
|
||
optionsfactory.AddPowSignalCliFlags(powSignalCmd, &options) | ||
|
||
return powSignalCmd | ||
} | ||
|
||
func runPowSignal(cmd *cobra.Command, options options.PowSignalOptions) error { | ||
commandCtx := system.NewCommandContext(cmd) | ||
defer commandCtx.Cleanup() | ||
|
||
web3SDK, err := web3.NewContractSDK(options.Web3) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = web3SDK.SendPowSignal(commandCtx.Ctx) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info().Msgf("send pow signal successful.") | ||
return nil | ||
} |
This file contains 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 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 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 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 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,153 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.6; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
|
||
contract LilypadPow is Ownable, Initializable { | ||
struct POWSubmission { | ||
address walletAddress; | ||
string nodeId; | ||
uint256 nonce; | ||
uint256 start_timestap; | ||
uint256 complete_timestap; //used to estimate hashrate of this submission | ||
bytes32 challenge; //record this to provent user never change challenge | ||
uint256 difficulty; | ||
} | ||
|
||
struct Challenge { | ||
bytes32 challenge; | ||
uint256 difficulty; | ||
string nodeId; | ||
uint256 timestamp; | ||
} | ||
|
||
// todo difficulty may need to adjust in test | ||
// this difficulty was calculate with this tool https://github.com/hunjixin/pow-tool/tree/main/difficulty | ||
// Theoretically A machine with a hash rate of 2M has a probability of no more than 0.01% of not finding a nonce that meets the difficulty within 20 blocks. | ||
// However, this issue has not been well validated in practice. it can solve nonce within one minute most of the time. | ||
uint256 public immutable TARGET_DIFFICULTY = | ||
2221842798488549893930113429797694032668256326301844165995655665287168; | ||
mapping(address => POWSubmission[]) public powSubmissions; | ||
mapping(address => uint256) public minerSubmissionCount; //used for loop powsubmission | ||
address[] public miners; | ||
|
||
mapping(address => Challenge) public lastChallenges; | ||
uint256 public validProofs; | ||
uint256 public startTime; | ||
|
||
uint256 public window_start; | ||
uint256 public window_end; | ||
/** | ||
* Init | ||
*/ | ||
|
||
// https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable | ||
function initialize() public initializer {} | ||
|
||
function getMiners() external view returns (address[] memory) { | ||
return miners; | ||
} | ||
|
||
// generateChallenge gen a byte32 value as challenge value, Sc store this one for verify | ||
function generateChallenge(string calldata nodeId) external { | ||
checkTimeWindow(); | ||
|
||
bytes32 challenge = keccak256( | ||
abi.encodePacked(block.timestamp, window_start, msg.sender, nodeId) | ||
); | ||
|
||
uint256 difficulty = calculate_difficulty(); | ||
lastChallenges[msg.sender] = Challenge( | ||
challenge, | ||
difficulty, | ||
nodeId, | ||
block.timestamp | ||
); | ||
emit GenerateChallenge(challenge, difficulty); | ||
} | ||
|
||
function calculate_difficulty() public view returns (uint256) { | ||
uint256 percentChange = 90 + (block.prevrandao % 21); | ||
return (TARGET_DIFFICULTY * percentChange) / 100; | ||
} | ||
|
||
// submitWork miner submint a nonce value, sc check the difficulty and emit a valid pow event when success | ||
function submitWork(uint256 nonce, string calldata nodeId) external { | ||
checkTimeWindow(); | ||
|
||
Challenge memory lastChallenge = lastChallenges[msg.sender]; | ||
bytes32 challenge = keccak256( | ||
abi.encodePacked( | ||
lastChallenge.timestamp, | ||
window_start, | ||
msg.sender, | ||
nodeId | ||
) | ||
); | ||
|
||
require( | ||
lastChallenge.challenge == challenge, | ||
"Work submit not compatable with challenge" | ||
); | ||
|
||
bytes32 hash = keccak256(abi.encodePacked(challenge, nonce)); | ||
require( | ||
uint256(hash) < lastChallenge.difficulty, | ||
"Work does not meet difficulty target" | ||
); | ||
|
||
validProofs++; | ||
|
||
if (minerSubmissionCount[msg.sender] == 0) { | ||
//first submit, append to miners | ||
miners.push(msg.sender); | ||
} | ||
|
||
minerSubmissionCount[msg.sender]++; //increase miner's valid proofs | ||
POWSubmission[] storage posSubmissions = powSubmissions[msg.sender]; | ||
posSubmissions.push( | ||
POWSubmission( | ||
msg.sender, | ||
nodeId, | ||
nonce, | ||
lastChallenge.timestamp, | ||
block.timestamp, | ||
lastChallenge.challenge, | ||
lastChallenge.difficulty | ||
) | ||
); | ||
|
||
//clean last challenge to submit the same proof | ||
lastChallenges[msg.sender] = Challenge(0, 0, "", 0); | ||
emit ValidPOWSubmitted( | ||
msg.sender, | ||
nodeId, | ||
nonce, | ||
block.timestamp, | ||
lastChallenge.challenge, | ||
lastChallenge.difficulty | ||
); | ||
} | ||
|
||
function triggerNewPowRound() external onlyOwner { | ||
window_start = block.number; | ||
window_end = block.number + 30; //todo arbitary value , need to discuss | ||
emit NewPowRound(); | ||
} | ||
|
||
function checkTimeWindow() public view { | ||
require(block.number < window_end, "proof windows has closed"); | ||
} | ||
|
||
event ValidPOWSubmitted( | ||
address indexed walletAddress, | ||
string nodeId, | ||
uint256 nonce, | ||
uint256 timestamp, | ||
bytes32 challenge, | ||
uint256 difficulty | ||
); | ||
event GenerateChallenge(bytes32 challenge, uint256 difficulty); | ||
event NewPowRound(); | ||
} |
This file contains 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,28 @@ | ||
import { HardhatRuntimeEnvironment } from 'hardhat/types' | ||
import { DeployFunction } from 'hardhat-deploy/types' | ||
|
||
const deployUsers: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { | ||
const { deployments, getNamedAccounts } = hre | ||
const { deploy, execute } = deployments | ||
const { | ||
admin, | ||
} = await getNamedAccounts() | ||
await deploy("LilypadPow", { | ||
from: admin, | ||
args: [], | ||
log: true, | ||
}) | ||
await execute( | ||
'LilypadPow', | ||
{ | ||
from: admin, | ||
log: true, | ||
}, | ||
'initialize' | ||
) | ||
return true | ||
} | ||
|
||
deployUsers.id = 'deployPow' | ||
|
||
export default deployUsers |
This file contains 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 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename to
deployPow