Commit 63c5a046 authored by Luke Champine's avatar Luke Champine

separate EAPaymentRevision method

parent 1bbab5d4
Pipeline #153006317 failed with stages
in 7 minutes and 9 seconds
......@@ -122,7 +122,8 @@ func (fcr FileContractRevision) HostPublicKey() SiaPublicKey {
}
// PaymentRevision returns a copy of the revision with incremented revision
// number where the given amount has moved from renter to the host.
// number where the given amount has moved from the renter to the host (for
// valid outputs) and from renter to the void (for missed outputs).
func (fcr FileContractRevision) PaymentRevision(amount Currency) (FileContractRevision, error) {
rev := fcr
......@@ -142,6 +143,40 @@ func (fcr FileContractRevision) PaymentRevision(amount Currency) (FileContractRe
rev.SetValidRenterPayout(fcr.ValidRenterPayout().Sub(amount))
rev.SetValidHostPayout(fcr.ValidHostPayout().Add(amount))
// move missed payout from renter to void
rev.SetMissedRenterPayout(fcr.MissedRenterOutput().Value.Sub(amount))
void, err := fcr.MissedVoidOutput()
if err != nil {
return FileContractRevision{}, err
}
rev.SetMissedVoidPayout(void.Value.Add(amount))
// increment revision number
rev.NewRevisionNumber++
return rev, nil
}
// EAPaymentRevision returns a copy of the revision with incremented revision
// number where the given amount has moved from renter to the host.
func (fcr FileContractRevision) EAPaymentRevision(amount Currency) (FileContractRevision, error) {
rev := fcr
// need to manually copy slice memory
rev.NewValidProofOutputs = append([]SiacoinOutput{}, fcr.NewValidProofOutputs...)
rev.NewMissedProofOutputs = append([]SiacoinOutput{}, fcr.NewMissedProofOutputs...)
// Check that there are enough funds to pay this cost.
if fcr.ValidRenterPayout().Cmp(amount) < 0 {
return FileContractRevision{}, errors.AddContext(ErrRevisionCostTooHigh, "valid proof output smaller than cost")
}
if fcr.MissedRenterOutput().Value.Cmp(amount) < 0 {
return FileContractRevision{}, errors.AddContext(ErrRevisionCostTooHigh, "missed proof output smaller than cost")
}
// move valid payout from renter to host
rev.SetValidRenterPayout(fcr.ValidRenterPayout().Sub(amount))
rev.SetValidHostPayout(fcr.ValidHostPayout().Add(amount))
// move missed payout from renter to host
rev.SetMissedRenterPayout(fcr.MissedRenterOutput().Value.Sub(amount))
rev.SetMissedHostPayout(rev.MissedHostPayout().Add(amount))
......
......@@ -40,8 +40,8 @@ func TestTax(t *testing.T) {
}
}
// TestPaymentRevision probes the PaymentRevision function
func TestPaymentRevision(t *testing.T) {
// TestEAPaymentRevision probes the EAPaymentRevision function
func TestEAPaymentRevision(t *testing.T) {
mock := func(renterFunds, hostCollateral uint64) FileContractRevision {
return FileContractRevision{
NewValidProofOutputs: []SiacoinOutput{
......@@ -58,18 +58,18 @@ func TestPaymentRevision(t *testing.T) {
// expect no error if amount is less than or equal to the renter funds
rev := mock(100, 0)
_, err := rev.PaymentRevision(NewCurrency64(99))
_, err := rev.EAPaymentRevision(NewCurrency64(99))
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
_, err = rev.PaymentRevision(NewCurrency64(100))
_, err = rev.EAPaymentRevision(NewCurrency64(100))
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
// expect ErrRevisionCostTooHigh
rev = mock(100, 0)
_, err = rev.PaymentRevision(NewCurrency64(100 + 1))
_, err = rev.EAPaymentRevision(NewCurrency64(100 + 1))
if !errors.Contains(err, ErrRevisionCostTooHigh) {
t.Fatalf("Expected error '%v' but received '%v' ", ErrRevisionCostTooHigh, err)
}
......@@ -77,18 +77,18 @@ func TestPaymentRevision(t *testing.T) {
// expect ErrRevisionCostTooHigh
rev = mock(100, 0)
rev.SetMissedRenterPayout(NewCurrency64(99))
_, err = rev.PaymentRevision(NewCurrency64(100))
_, err = rev.EAPaymentRevision(NewCurrency64(100))
if !errors.Contains(err, ErrRevisionCostTooHigh) {
t.Fatalf("Expected error '%v' but received '%v' ", ErrRevisionCostTooHigh, err)
}
// expect no error if amount is less than or equal to the host collateral
rev = mock(100, 100)
_, err = rev.PaymentRevision(NewCurrency64(99))
_, err = rev.EAPaymentRevision(NewCurrency64(99))
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
_, err = rev.PaymentRevision(NewCurrency64(100))
_, err = rev.EAPaymentRevision(NewCurrency64(100))
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
......@@ -96,7 +96,7 @@ func TestPaymentRevision(t *testing.T) {
// verify funds moved to the appropriate outputs
existing := mock(100, 0)
amount := NewCurrency64(99)
payment, err := existing.PaymentRevision(amount)
payment, err := existing.EAPaymentRevision(amount)
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment