-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: power implementation * feat: simplly code * chore: add a explain comment for target difficulty * fix typo * fix: blocked error channel * chore: update dev toml sc address * feat: change wrong log and add toml tag * fix: upgrade cowsay to v0.0.4 * fix: update devent controller address * fix: Add POW config processor * feat: add pow address for testnet * chore: update to latest controller address * chore: update contract addresses * build: add hardhat testnet config --------- Co-authored-by: root <[email protected]> Co-authored-by: hunjixin <[email protected]> Co-authored-by: Brian Ginsburg <[email protected]> Co-authored-by: Gorka <[email protected]> Co-authored-by: Arsen Yeremin <[email protected]>
- Loading branch information
1 parent
6ca4289
commit a08786c
Showing
35 changed files
with
2,647 additions
and
40 deletions.
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.