2019-08-09 21:41:50 +00:00
|
|
|
package actors_test
|
|
|
|
|
|
|
|
import (
|
2019-08-16 04:40:59 +00:00
|
|
|
"context"
|
2019-08-09 21:41:50 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPaychCreate(t *testing.T) {
|
|
|
|
var creatorAddr, targetAddr address.Address
|
|
|
|
opts := []HarnessOpt{
|
2019-08-29 18:20:59 +00:00
|
|
|
HarnessAddr(&creatorAddr, 100000),
|
|
|
|
HarnessAddr(&targetAddr, 100000),
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
h := NewHarness(t, opts...)
|
2019-08-09 21:41:50 +00:00
|
|
|
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
2019-09-10 23:03:17 +00:00
|
|
|
&actors.PCAConstructorParams{
|
2019-08-09 21:41:50 +00:00
|
|
|
To: targetAddr,
|
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func signVoucher(t *testing.T, w *wallet.Wallet, addr address.Address, sv *types.SignedVoucher) {
|
|
|
|
vb, err := sv.SigningBytes()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-16 04:40:59 +00:00
|
|
|
sig, err := w.Sign(context.TODO(), addr, vb)
|
2019-08-09 21:41:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sv.Signature = sig
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaychUpdate(t *testing.T) {
|
|
|
|
var creatorAddr, targetAddr address.Address
|
|
|
|
opts := []HarnessOpt{
|
2019-08-29 18:20:59 +00:00
|
|
|
HarnessAddr(&creatorAddr, 100000),
|
|
|
|
HarnessAddr(&targetAddr, 100000),
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
h := NewHarness(t, opts...)
|
2019-08-09 21:41:50 +00:00
|
|
|
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
2019-09-10 23:03:17 +00:00
|
|
|
&actors.PCAConstructorParams{
|
2019-08-09 21:41:50 +00:00
|
|
|
To: targetAddr,
|
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
pch, err := address.NewFromBytes(ret.Return)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _ = h.SendFunds(t, creatorAddr, pch, types.NewInt(5000))
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
sv := &types.SignedVoucher{
|
|
|
|
Amount: types.NewInt(100),
|
|
|
|
Nonce: 1,
|
|
|
|
}
|
|
|
|
signVoucher(t, h.w, creatorAddr, sv)
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
ret, _ = h.Invoke(t, targetAddr, pch, actors.PCAMethods.UpdateChannelState, &actors.PCAUpdateChannelStateParams{
|
2019-08-09 21:41:50 +00:00
|
|
|
Sv: *sv,
|
|
|
|
})
|
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
ret, _ = h.Invoke(t, targetAddr, pch, actors.PCAMethods.GetToSend, nil)
|
2019-08-09 21:41:50 +00:00
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
bi := types.BigFromBytes(ret.Return)
|
|
|
|
if bi.String() != "100" {
|
|
|
|
t.Fatal("toSend amount was wrong: ", bi.String())
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
ret, _ = h.Invoke(t, targetAddr, pch, actors.PCAMethods.Close, nil)
|
2019-08-09 21:41:50 +00:00
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
|
|
|
// now we have to 'wait' for the chain to advance.
|
|
|
|
h.vm.SetBlockHeight(1000)
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
ret, _ = h.Invoke(t, targetAddr, pch, actors.PCAMethods.Collect, nil)
|
2019-08-09 21:41:50 +00:00
|
|
|
ApplyOK(t, ret)
|
|
|
|
|
2019-08-21 20:47:13 +00:00
|
|
|
h.AssertBalanceChange(t, targetAddr, 100)
|
|
|
|
h.AssertBalanceChange(t, creatorAddr, -100)
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|