This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
add balance maintainer job #47
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -1,57 +1,65 @@ | ||
package ant | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/NebulousLabs/Sia/api" | ||
"github.com/NebulousLabs/Sia/types" | ||
) | ||
|
||
// blockMining unlocks the wallet and mines some currency. If more than 100 | ||
// seconds passes before the wallet has received some amount of currency, this | ||
// job will print an error. | ||
func (j *jobRunner) blockMining() { | ||
// blockMining mines blocks until desiredBalance is reached. If desiredBalance | ||
// is zero, blockMining will start the miner and return, mining indefinitely. | ||
func (j *jobRunner) blockMining(desiredBalance types.Currency) { | ||
j.tg.Add() | ||
defer j.tg.Done() | ||
|
||
err := j.client.Post("/wallet/unlock", fmt.Sprintf("encryptionpassword=%s&dictionary=%s", j.walletPassword, "english"), nil) | ||
minerRunning := true | ||
err := j.client.Get("/miner/start", nil) | ||
if err != nil { | ||
log.Printf("[%v blockMining ERROR]: %v\n", j.siaDirectory, err) | ||
return | ||
} | ||
|
||
err = j.client.Get("/miner/start", nil) | ||
if err != nil { | ||
log.Printf("[%v blockMining ERROR]: %v\n", j.siaDirectory, err) | ||
// If desiredBalance is zero, just return, leaving the miner running. | ||
runForever := desiredBalance.Cmp(types.ZeroCurrency) == 0 | ||
if runForever { | ||
return | ||
} | ||
|
||
var walletInfo api.WalletGET | ||
err = j.client.Get("/wallet", &walletInfo) | ||
if err != nil { | ||
log.Printf("[%v blockMining ERROR]: %v\n", j.siaDirectory, err) | ||
return | ||
} | ||
lastBalance := walletInfo.ConfirmedSiacoinBalance | ||
|
||
// Every 100 seconds, verify that the balance has increased. | ||
// Every 20 seconds, check if the balance has exceeded the desiredBalance. If | ||
// it has and the miner is running, the miner is throttled. If the desired | ||
// balance has not been reached and the miner is not running, the miner is | ||
// started. | ||
for { | ||
select { | ||
case <-j.tg.StopChan(): | ||
return | ||
case <-time.After(time.Second * 100): | ||
case <-time.After(time.Second * 20): | ||
} | ||
|
||
var walletInfo api.WalletGET | ||
err = j.client.Get("/wallet", &walletInfo) | ||
if err != nil { | ||
log.Printf("[%v blockMining ERROR]: %v\n", j.siaDirectory, err) | ||
return | ||
} | ||
if walletInfo.ConfirmedSiacoinBalance.Cmp(lastBalance) > 0 { | ||
log.Printf("[%v SUCCESS] blockMining job succeeded", j.siaDirectory) | ||
lastBalance = walletInfo.ConfirmedSiacoinBalance | ||
} else { | ||
log.Printf("[%v blockMining ERROR]: it took too long to receive new funds in miner job\n", j.siaDirectory) | ||
|
||
haveDesiredBalance := walletInfo.ConfirmedSiacoinBalance.Cmp(desiredBalance) > 0 | ||
if !minerRunning && !haveDesiredBalance { | ||
log.Printf("[%v miner INFO]: not enough currency, starting the miner\n", j.siaDirectory) | ||
minerRunning = true | ||
if err = j.client.Get("/miner/start", nil); err != nil { | ||
log.Printf("[%v miner ERROR]: %v\n", j.siaDirectory, err) | ||
return | ||
} | ||
} else if minerRunning && haveDesiredBalance { | ||
log.Printf("[%v miner INFO]: mined enough currency, stopping the miner\n", j.siaDirectory) | ||
minerRunning = false | ||
if err = j.client.Get("/miner/stop", nil); err != nil { | ||
log.Printf("[%v miner ERROR]: %v\n", j.siaDirectory, err) | ||
return | ||
} | ||
} | ||
} | ||
} |
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
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.
if I understand correctly, it's still possible to avoid mining entirely by not passing
-miner
, correct?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.
correct. by not including miner in the
Jobs
, the miner never gets run, regardless ofDesiredCurrency
.