Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

add balance maintainer job #47

Merged
merged 4 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 8 additions & 7 deletions ant/ant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
// AntConfig represents a configuration object passed to New(), used to
// configure a newly created Sia Ant.
type AntConfig struct {
APIAddr string `json:",omitempty"`
RPCAddr string `json:",omitempty"`
HostAddr string `json:",omitempty"`
SiaDirectory string `json:",omitempty"`
SiadPath string
Jobs []string
APIAddr string `json:",omitempty"`
RPCAddr string `json:",omitempty"`
HostAddr string `json:",omitempty"`
SiaDirectory string `json:",omitempty"`
SiadPath string
Jobs []string
DesiredCurrency uint64
}

// An Ant is a Sia Client programmed with network user stories. It executes
Expand Down Expand Up @@ -48,7 +49,7 @@ func New(config AntConfig) (*Ant, error) {
for _, job := range config.Jobs {
switch job {
case "miner":
go j.blockMining()
go j.blockMining(types.SiacoinPrecision.Mul64(config.DesiredCurrency))
case "host":
go j.jobHost()
case "renter":
Expand Down
14 changes: 1 addition & 13 deletions ant/job_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,12 @@ func (j *jobRunner) jobHost() {
j.tg.Add()
defer j.tg.Done()

err := j.client.Post("/wallet/unlock", fmt.Sprintf("encryptionpassword=%s&dictionary=%s", j.walletPassword, "english"), nil)
if err != nil {
log.Printf("[%v jobHost ERROR: %v\n", j.siaDirectory, err)
return
}

err = j.client.Get("/miner/start", nil)
if err != nil {
log.Printf("[%v jobHost ERROR: %v\n", j.siaDirectory, err)
return
}

// Mine at least 50,000 SC
desiredbalance := types.NewCurrency64(50000).Mul(types.SiacoinPrecision)
success := false
for start := time.Now(); time.Since(start) < 5*time.Minute; time.Sleep(time.Second) {
var walletInfo api.WalletGET
err = j.client.Get("/wallet", &walletInfo)
err := j.client.Get("/wallet", &walletInfo)
if err != nil {
log.Printf("[%v jobHost ERROR]: %v\n", j.siaDirectory, err)
return
Expand Down
56 changes: 32 additions & 24 deletions ant/job_miner.go
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
Copy link
Member

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?

Copy link
Contributor Author

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 of DesiredCurrency.

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
}
}
}
}
14 changes: 1 addition & 13 deletions ant/job_renter.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,18 +383,6 @@ func (j *jobRunner) storageRenter() {
j.tg.Add()
defer j.tg.Done()

// Unlock the wallet and begin mining to earn enough coins for uploading.
err := j.client.Post("/wallet/unlock", fmt.Sprintf("encryptionpassword=%s&dictionary=%s", j.walletPassword, "english"), nil)
if err != nil {
log.Printf("[ERROR] [renter] [%v] Trouble when unlocking wallet: %v\n", j.siaDirectory, err)
return
}
err = j.client.Get("/miner/start", nil)
if err != nil {
log.Printf("[ERROR] [renter] [%v] Trouble when starting the miner: %v\n", j.siaDirectory, err)
return
}

// Block until a minimum threshold of coins have been mined.
start := time.Now()
var walletInfo api.WalletGET
Expand All @@ -413,7 +401,7 @@ func (j *jobRunner) storageRenter() {
}

// Update the wallet balance.
err = j.client.Get("/wallet", &walletInfo)
err := j.client.Get("/wallet", &walletInfo)
if err != nil {
log.Printf("[ERROR] [renter] [%v] Trouble when calling /wallet: %v\n", j.siaDirectory, err)
}
Expand Down
8 changes: 8 additions & 0 deletions ant/jobrunner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ant

import (
"fmt"

"github.com/NebulousLabs/Sia/api"
"github.com/NebulousLabs/Sia/sync"
)
Expand Down Expand Up @@ -28,6 +30,12 @@ func newJobRunner(apiaddr string, authpassword string, siadirectory string) (*jo
return nil, err
}
jr.walletPassword = walletParams.PrimarySeed

err = jr.client.Post("/wallet/unlock", fmt.Sprintf("encryptionpassword=%s&dictionary=%s", jr.walletPassword, "english"), nil)
if err != nil {
return nil, err
}

return jr, nil
}

Expand Down