Skip to content

Commit

Permalink
feat: simply state loop in sc & fix typo (#193)
Browse files Browse the repository at this point in the history
* feat: simply state loop in sc & fix typo

---------

Co-authored-by: hunjixin <16705420332lee@gmai.com>
  • Loading branch information
hunjixin and hunjixin committed Jun 27, 2024
1 parent a499e55 commit 666cbb6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 140 deletions.
28 changes: 15 additions & 13 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 getMinerPowSubmissions(address addr) public view returns (POWSubmission[] memory) {
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,14 +106,11 @@ contract LilypadPow is Ownable, Initializable {

validProofs++;

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

minerSubmissionCount[msg.sender]++; //increase miner's valid proofs
POWSubmission[] storage posSubmissions = powSubmissions[msg.sender];
posSubmissions.push(
onwMinerPowSubmissions.push(
POWSubmission(
msg.sender,
nodeId,
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

0 comments on commit 666cbb6

Please sign in to comment.