2020-09-16 04:06:04 +00:00
|
|
|
package paych
|
|
|
|
|
|
|
|
import (
|
2022-09-06 15:49:29 +00:00
|
|
|
actorstypes "github.com/filecoin-project/go-state-types/actors"
|
2020-09-28 21:25:58 +00:00
|
|
|
"encoding/base64"
|
2021-04-27 18:48:32 +00:00
|
|
|
"fmt"
|
2020-09-28 21:25:58 +00:00
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-15 10:31:02 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/cbor"
|
2020-09-28 21:25:58 +00:00
|
|
|
ipldcbor "github.com/ipfs/go-ipld-cbor"
|
2020-09-19 03:46:03 +00:00
|
|
|
|
2020-09-23 04:48:35 +00:00
|
|
|
paych0 "github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
2022-04-20 21:34:28 +00:00
|
|
|
paychtypes "github.com/filecoin-project/go-state-types/builtin/v8/paych"
|
2021-04-27 18:48:32 +00:00
|
|
|
{{range .versions}}
|
2022-04-20 21:34:28 +00:00
|
|
|
{{if (le . 7)}}
|
|
|
|
builtin{{.}} "github.com/filecoin-project/specs-actors{{import .}}actors/builtin"
|
|
|
|
{{end}}
|
2021-04-30 15:51:24 +00:00
|
|
|
{{end}}
|
2020-09-16 04:06:04 +00:00
|
|
|
|
2021-04-27 18:48:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Load returns an abstract copy of payment channel state, irregardless of actor version
|
|
|
|
func Load(store adt.Store, act *types.Actor) (State, error) {
|
2022-04-04 11:23:13 +00:00
|
|
|
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
|
2022-04-20 21:34:28 +00:00
|
|
|
if name != actors.PaychKey {
|
|
|
|
return nil, xerrors.Errorf("actor code is not paych: %s", name)
|
2022-04-04 11:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch av {
|
|
|
|
{{range .versions}}
|
2022-04-20 21:34:28 +00:00
|
|
|
{{if (ge . 8)}}
|
2022-09-06 15:49:29 +00:00
|
|
|
case actorstypes.Version{{.}}:
|
2022-04-20 21:34:28 +00:00
|
|
|
return load{{.}}(store, act.Head)
|
|
|
|
{{end}}
|
2022-04-04 11:23:13 +00:00
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
switch act.Code {
|
2021-04-30 15:51:24 +00:00
|
|
|
{{range .versions}}
|
2022-04-20 21:34:28 +00:00
|
|
|
{{if (le . 7)}}
|
|
|
|
case builtin{{.}}.PaymentChannelActorCodeID:
|
|
|
|
return load{{.}}(store, act.Head)
|
|
|
|
{{end}}
|
2021-04-30 15:51:24 +00:00
|
|
|
{{end}}
|
|
|
|
}
|
2022-04-04 11:23:13 +00:00
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
|
2021-05-15 01:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
// State is an abstract version of payment channel state that works across
|
|
|
|
// versions
|
|
|
|
type State interface {
|
|
|
|
cbor.Marshaler
|
|
|
|
// Channel owner, who has funded the actor
|
2020-09-23 05:19:43 +00:00
|
|
|
From() (address.Address, error)
|
2020-09-16 04:06:04 +00:00
|
|
|
// Recipient of payouts from channel
|
2020-09-23 05:19:43 +00:00
|
|
|
To() (address.Address, error)
|
2020-09-16 04:06:04 +00:00
|
|
|
|
|
|
|
// Height at which the channel can be `Collected`
|
2020-09-23 05:19:43 +00:00
|
|
|
SettlingAt() (abi.ChainEpoch, error)
|
2020-09-16 04:06:04 +00:00
|
|
|
|
|
|
|
// Amount successfully redeemed through the payment channel, paid out on `Collect()`
|
2020-09-23 05:19:43 +00:00
|
|
|
ToSend() (abi.TokenAmount, error)
|
2020-09-16 04:06:04 +00:00
|
|
|
|
|
|
|
// Get total number of lanes
|
|
|
|
LaneCount() (uint64, error)
|
|
|
|
|
|
|
|
// Iterate lane states
|
|
|
|
ForEachLaneState(cb func(idx uint64, dl LaneState) error) error
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
GetState() interface{}
|
2020-09-16 04:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LaneState is an abstract copy of the state of a single lane
|
|
|
|
type LaneState interface {
|
2020-09-23 05:19:43 +00:00
|
|
|
Redeemed() (big.Int, error)
|
|
|
|
Nonce() (uint64, error)
|
2020-09-16 04:06:04 +00:00
|
|
|
}
|
2020-09-23 04:48:35 +00:00
|
|
|
|
2020-09-28 21:25:58 +00:00
|
|
|
// DecodeSignedVoucher decodes base64 encoded signed voucher.
|
2022-04-20 21:34:28 +00:00
|
|
|
func DecodeSignedVoucher(s string) (*paychtypes.SignedVoucher, error) {
|
2020-09-28 21:25:58 +00:00
|
|
|
data, err := base64.RawURLEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
var sv paychtypes.SignedVoucher
|
2020-09-28 21:25:58 +00:00
|
|
|
if err := ipldcbor.DecodeInto(data, &sv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sv, nil
|
|
|
|
}
|
2021-04-27 18:48:32 +00:00
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
func Message(version actorstypes.Version, from address.Address) MessageBuilder {
|
2021-04-27 18:48:32 +00:00
|
|
|
switch version {
|
2021-04-30 15:51:24 +00:00
|
|
|
{{range .versions}}
|
2022-09-06 15:49:29 +00:00
|
|
|
case actorstypes.Version{{.}}:
|
2021-04-27 18:48:32 +00:00
|
|
|
return message{{.}}{from}
|
2021-04-30 15:51:24 +00:00
|
|
|
{{end}}
|
|
|
|
default:
|
2021-04-27 18:48:32 +00:00
|
|
|
panic(fmt.Sprintf("unsupported actors version: %d", version))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageBuilder interface {
|
|
|
|
Create(to address.Address, initialAmount abi.TokenAmount) (*types.Message, error)
|
2022-04-20 21:34:28 +00:00
|
|
|
Update(paych address.Address, voucher *paychtypes.SignedVoucher, secret []byte) (*types.Message, error)
|
2021-04-27 18:48:32 +00:00
|
|
|
Settle(paych address.Address) (*types.Message, error)
|
|
|
|
Collect(paych address.Address) (*types.Message, error)
|
|
|
|
}
|
2022-04-20 21:34:28 +00:00
|
|
|
|
|
|
|
func toV0SignedVoucher(sv paychtypes.SignedVoucher) paych0.SignedVoucher {
|
|
|
|
return paych0.SignedVoucher{
|
|
|
|
ChannelAddr: sv.ChannelAddr,
|
|
|
|
TimeLockMin: sv.TimeLockMin,
|
|
|
|
TimeLockMax: sv.TimeLockMax,
|
|
|
|
SecretPreimage: sv.SecretHash,
|
|
|
|
Extra: (*paych0.ModVerifyParams)(sv.Extra),
|
|
|
|
Lane: sv.Lane,
|
|
|
|
Nonce: sv.Nonce,
|
|
|
|
Amount: sv.Amount,
|
|
|
|
MinSettleHeight: sv.MinSettleHeight,
|
|
|
|
Merges: nil,
|
|
|
|
Signature: sv.Signature,
|
|
|
|
}
|
|
|
|
}
|