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

Commit

Permalink
Add api client method for cancel contract, siatest, and moved LoadStr…
Browse files Browse the repository at this point in the history
…ing method to encoding.go
  • Loading branch information
MSevey committed Jul 13, 2018
1 parent 470626c commit cac126e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
10 changes: 10 additions & 0 deletions node/api/client/renter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import (

"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/node/api"
"github.com/NebulousLabs/Sia/types"
)

// RenterCancelContractPost uses the /renter/cancel/contract endpoint to cancel
// a contract
func (c *Client) RenterCancelContractPost(id types.FileContractID) error {
values := url.Values{}
values.Set("id", id.String())
err := c.post("/renter/cancel/contract", values.Encode(), nil)
return err
}

// RenterContractsGet requests the /renter/contracts resource and returns
// Contracts and ActiveContracts
func (c *Client) RenterContractsGet() (rc api.RenterContracts, err error) {
Expand Down
49 changes: 49 additions & 0 deletions siatest/renter/renter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestRenter(t *testing.T) {
name string
test func(*testing.T, *siatest.TestGroup)
}{
{"TestCancelContract", testCancelContract},
{"TestClearDownloadHistory", testClearDownloadHistory},
{"TestDownloadAfterRenew", testDownloadAfterRenew},
{"TestDownloadMultipleLargeSectors", testDownloadMultipleLargeSectors},
Expand All @@ -74,6 +75,54 @@ func TestRenter(t *testing.T) {
}
}

// testCancelContract tests the RenterCancelContract Endpoint
func testCancelContract(t *testing.T, tg *siatest.TestGroup) {
// Grab the first of the group's renters
r := tg.Renters()[0]

// Grab contracts
rc, err := r.RenterContractsGet()
if err != nil {
t.Fatal(err)
}

// Grab contract to cancel
contract := rc.ActiveContracts[0]

// Cancel Contract
if err := r.RenterCancelContractPost(contract.ID); err != nil {
t.Fatal(err)
}

// Mine block to trigger threadedContractMaintenance
m := tg.Miners()[0]
if err := m.MineBlock(); err != nil {
t.Fatal(err)
}
tg.Sync()

// Check that Contract is now in inactive contracts and no longer in Active contracts
rc, err = r.RenterInactiveContractsGet()
if err != nil {
t.Fatal(err)
}
for _, c := range rc.ActiveContracts {
if c.ID == contract.ID {
t.Fatal("Contract not cancelled, contract found in Active Contracts")
}
}
i := 1
for _, c := range rc.InactiveContracts {
if c.ID == contract.ID {
break
}
if i == len(rc.InactiveContracts) {
t.Fatal("Contract not found in Inactive Contracts")
}
i++
}
}

// testClearDownloadHistory makes sure that the download history is
// properly cleared when called through the API
func testClearDownloadHistory(t *testing.T, tg *siatest.TestGroup) {
Expand Down
5 changes: 5 additions & 0 deletions types/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ func (fcr *FileContractRevision) UnmarshalSia(r io.Reader) error {
return d.Err()
}

// LoadString loads a FileContractID from a string
func (fcid *FileContractID) LoadString(str string) error {
return (*crypto.Hash)(fcid).LoadString(str)
}

// MarshalJSON marshals an id as a hex string.
func (fcid FileContractID) MarshalJSON() ([]byte, error) {
return json.Marshal(fcid.String())
Expand Down
5 changes: 0 additions & 5 deletions types/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ type (
UnlockHash crypto.Hash
)

// LoadString loads a FileContractID from a string
func (fcid *FileContractID) LoadString(str string) error {
return (*crypto.Hash)(fcid).LoadString(str)
}

// ID returns the id of a transaction, which is taken by marshalling all of the
// fields except for the signatures and taking the hash of the result.
func (t Transaction) ID() TransactionID {
Expand Down

0 comments on commit cac126e

Please sign in to comment.