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: simply state loop in sc & fix typo #193

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions hardhat/contracts/LilypadPow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ contract LilypadPow is Ownable, Initializable {
address walletAddress;
string nodeId;
uint256 nonce;
uint256 start_timestap;
uint256 complete_timestap; //used to estimate hashrate of this submission
uint256 start_timestamp;
uint256 complete_timestamp; //used to estimate hashrate of this submission
bytes32 challenge; //record this to provent user never change challenge
uint256 difficulty;
}
Expand All @@ -29,7 +29,6 @@ contract LilypadPow is Ownable, Initializable {
uint256 public targetDifficulty =
555460709263765739036470010701196062214039696708679004195670928130048;
mapping(address => POWSubmission[]) public powSubmissions;
mapping(address => uint256) public minerSubmissionCount; //used for loop powsubmission
address[] public miners;

mapping(address => Challenge) public lastChallenges;
Expand All @@ -45,10 +44,14 @@ contract LilypadPow is Ownable, Initializable {
// https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
function initialize() public initializer {}

function getMiners() external view returns (address[] memory) {
function getMiners() public view returns (address[] memory) {
return miners;
}

function getMinerPosSubmissions(address addr) public view returns (POWSubmission[] memory) {
hunjixin marked this conversation as resolved.
Show resolved Hide resolved
return powSubmissions[addr];
}

// generateChallenge gen a byte32 value as challenge value, Sc store this one for verify
function generateChallenge(string calldata nodeId) external {
checkTimeWindow();
Expand Down Expand Up @@ -103,13 +106,10 @@ contract LilypadPow is Ownable, Initializable {

validProofs++;

if (minerSubmissionCount[msg.sender] == 0) {
//first submit, append to miners
POWSubmission[] storage posSubmissions = powSubmissions[msg.sender];
if (posSubmissions.length == 0) {
miners.push(msg.sender);
}

minerSubmissionCount[msg.sender]++; //increase miner's valid proofs
POWSubmission[] storage posSubmissions = powSubmissions[msg.sender];
posSubmissions.push(
POWSubmission(
msg.sender,
Expand All @@ -128,6 +128,7 @@ contract LilypadPow is Ownable, Initializable {
msg.sender,
nodeId,
nonce,
lastChallenge.timestamp,
block.timestamp,
lastChallenge.challenge,
lastChallenge.difficulty
Expand All @@ -145,13 +146,14 @@ contract LilypadPow is Ownable, Initializable {
}

event ValidPOWSubmitted(
address indexed walletAddress,
address walletAddress,
string nodeId,
uint256 nonce,
uint256 timestamp,
uint256 start_timestamp,
uint256 complete_timestamp,
bytes32 challenge,
uint256 difficulty
);
event GenerateChallenge(bytes32 challenge, uint256 difficulty);
event NewPowRound();
}
}
34 changes: 0 additions & 34 deletions pkg/web3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,40 +308,6 @@ type PowValidPOWSubmission struct {
Difficulty *big.Int
}

func (sdk *Web3SDK) GetPowSubmission(ctx context.Context) (map[common.Address][]PowValidPOWSubmission, error) {
miners, err := sdk.Contracts.Pow.GetMiners(sdk.CallOpts)
if err != nil {
return nil, err
}

results := make(map[common.Address][]PowValidPOWSubmission)
for _, minerAddr := range miners {
validProofCount, err := sdk.Contracts.Pow.MinerSubmissionCount(sdk.CallOpts, minerAddr)
if err != nil {
return nil, err
}

var powSubmissions []PowValidPOWSubmission
for i := uint64(0); i < validProofCount.Uint64(); i++ { //enough large
submission, err := sdk.Contracts.Pow.PowSubmissions(sdk.CallOpts, minerAddr, new(big.Int).SetUint64(i))
if err != nil {
return nil, err
}
powSubmissions = append(powSubmissions, PowValidPOWSubmission{
WalletAddress: submission.WalletAddress,
NodeId: submission.NodeId,
Nonce: submission.Nonce,
StartTimestap: submission.StartTimestap,
CompleteTimestap: submission.CompleteTimestap,
Challenge: submission.Challenge,
Difficulty: submission.Difficulty,
})
}
results[minerAddr] = powSubmissions
}
return results, nil
}

func (sdk *Web3SDK) SendPowSignal(ctx context.Context) (*pow.PowNewPowRound, error) {
tx, err := sdk.Contracts.Pow.TriggerNewPowRound(sdk.TransactOpts)
if err != nil {
Expand Down
Loading
Loading