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: add user side hashrate #289

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions hardhat/contracts/LilypadPow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract LilypadPow is Initializable, OwnableUpgradeable {
uint256 complete_timestamp; //used to estimate hashrate of this submission
bytes32 challenge; //record this to provent user never change challenge
uint256 difficulty;
uint256 report_hashrates; //report by users, but this value maybe not truth
}

struct Challenge {
Expand Down Expand Up @@ -95,7 +96,7 @@ contract LilypadPow is Initializable, OwnableUpgradeable {
}

// 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 {
function submitWork(uint256 nonce, string calldata nodeId, uint256 report_hashrates) external {
Copy link
Contributor

@alvin-reyes alvin-reyes Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it report_hashrates? Should it be report_hashrate (without 's')?

Also, I think we should name it to computed_hashrate for clarity.

checkTimeWindow();

Challenge memory lastChallenge = lastChallenges[msg.sender];
Expand Down Expand Up @@ -135,7 +136,8 @@ contract LilypadPow is Initializable, OwnableUpgradeable {
lastChallenge.timestamp,
block.timestamp,
lastChallenge.challenge,
lastChallenge.difficulty
lastChallenge.difficulty,
report_hashrates
)
);

Expand All @@ -148,7 +150,8 @@ contract LilypadPow is Initializable, OwnableUpgradeable {
lastChallenge.timestamp,
block.timestamp,
lastChallenge.challenge,
lastChallenge.difficulty
lastChallenge.difficulty,
report_hashrates
);
}

Expand All @@ -169,7 +172,8 @@ contract LilypadPow is Initializable, OwnableUpgradeable {
uint256 start_timestamp,
uint256 complete_timestamp,
bytes32 challenge,
uint256 difficulty
uint256 difficulty,
uint256 report_hashrates
);
event GenerateChallenge(bytes32 challenge, uint256 difficulty);
event NewPowRound();
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourceprovider/minerctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ out:
stopWorkers()
cache.Add(result.Id, new(uint256.Int))
time.Sleep(time.Second * hpsUpdateSecs) //to ensure data was reported
hashrate := float64(m.totalHash) / 1000 / 1000 / dur
hashrate := float64(m.totalHash) / 1000 / dur
m.submit(result.Nonce.ToBig(), hashrate)
case allTask := <-m.task:
stopWorkers()
Expand Down
5 changes: 3 additions & 2 deletions pkg/resourceprovider/resourceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ func (resourceProvider *ResourceProvider) StartMineLoop(ctx context.Context) cha
ID: id,
Address: walletAddress.String(),
Date: finishTime,
Hashrate: hashrate,
Hashrate: hashrate / 1000, //mhash
})
txId, err := resourceProvider.web3SDK.SubmitWork(ctx, nonce, nodeId)

txId, err := resourceProvider.web3SDK.SubmitWork(ctx, nonce, nodeId, big.NewInt(int64(hashrate))) //store khash/s
if err != nil {
log.Err(err).Msgf("Submit work fail")
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/web3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ func (sdk *Web3SDK) SubmitWork(
ctx context.Context,
nonce *big.Int,
nodeId string,
hashrate *big.Int,
) (common.Hash, error) {
tx, err := sdk.Contracts.Pow.SubmitWork(sdk.TransactOpts, nonce, nodeId)
tx, err := sdk.Contracts.Pow.SubmitWork(sdk.TransactOpts, nonce, nodeId, hashrate)
if err != nil {
return common.Hash{}, err
}
Expand Down
59 changes: 33 additions & 26 deletions pkg/web3/bindings/pow/pow.go

Large diffs are not rendered by default.