move common from message param into message builder constructor

This commit is contained in:
Steven Allen 2020-09-30 10:58:34 -07:00
parent 5253c0d959
commit 2e9915ac28
5 changed files with 39 additions and 39 deletions

View File

@ -9,20 +9,20 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)
func Message(version actors.Version) MessageBuilder {
func Message(version actors.Version, from address.Address) MessageBuilder {
switch version {
case actors.Version0:
return message0{}
return message0{from}
case actors.Version2:
return message2{}
return message2{from}
default:
panic(fmt.Sprintf("unsupported actors version: %d", version))
}
}
type MessageBuilder interface {
Create(from, to address.Address, initialAmount abi.TokenAmount) (*types.Message, error)
Update(from, paych address.Address, voucher *SignedVoucher, secret []byte) (*types.Message, error)
Settle(from, paych address.Address) (*types.Message, error)
Collect(from, paych address.Address) (*types.Message, error)
Create(to address.Address, initialAmount abi.TokenAmount) (*types.Message, error)
Update(paych address.Address, voucher *SignedVoucher, secret []byte) (*types.Message, error)
Settle(paych address.Address) (*types.Message, error)
Collect(paych address.Address) (*types.Message, error)
}

View File

@ -13,10 +13,10 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)
type message0 struct{}
type message0 struct{ from address.Address }
func (message0) Create(from, to address.Address, initialAmount abi.TokenAmount) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych0.ConstructorParams{From: from, To: to})
func (m message0) Create(to address.Address, initialAmount abi.TokenAmount) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych0.ConstructorParams{From: m.from, To: to})
if aerr != nil {
return nil, aerr
}
@ -30,14 +30,14 @@ func (message0) Create(from, to address.Address, initialAmount abi.TokenAmount)
return &types.Message{
To: init_.Address,
From: from,
From: m.from,
Value: initialAmount,
Method: builtin0.MethodsInit.Exec,
Params: enc,
}, nil
}
func (message0) Update(from, paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
func (m message0) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych0.UpdateChannelStateParams{
Sv: *sv,
Secret: secret,
@ -48,26 +48,26 @@ func (message0) Update(from, paych address.Address, sv *SignedVoucher, secret []
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin0.MethodsPaych.UpdateChannelState,
Params: params,
}, nil
}
func (message0) Settle(from, paych address.Address) (*types.Message, error) {
func (m message0) Settle(paych address.Address) (*types.Message, error) {
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin0.MethodsPaych.Settle,
}, nil
}
func (message0) Collect(from, paych address.Address) (*types.Message, error) {
func (m message0) Collect(paych address.Address) (*types.Message, error) {
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin0.MethodsPaych.Collect,
}, nil

View File

@ -13,10 +13,10 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)
type message2 struct{}
type message2 struct{ from address.Address }
func (message2) Create(from, to address.Address, initialAmount abi.TokenAmount) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych2.ConstructorParams{From: from, To: to})
func (m message2) Create(to address.Address, initialAmount abi.TokenAmount) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych2.ConstructorParams{From: m.from, To: to})
if aerr != nil {
return nil, aerr
}
@ -30,14 +30,14 @@ func (message2) Create(from, to address.Address, initialAmount abi.TokenAmount)
return &types.Message{
To: init_.Address,
From: from,
From: m.from,
Value: initialAmount,
Method: builtin2.MethodsInit.Exec,
Params: enc,
}, nil
}
func (message2) Update(from, paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
func (m message2) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych2.UpdateChannelStateParams{
Sv: *sv,
Secret: secret,
@ -48,26 +48,26 @@ func (message2) Update(from, paych address.Address, sv *SignedVoucher, secret []
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin2.MethodsPaych.UpdateChannelState,
Params: params,
}, nil
}
func (message2) Settle(from, paych address.Address) (*types.Message, error) {
func (m message2) Settle(paych address.Address) (*types.Message, error) {
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin2.MethodsPaych.Settle,
}, nil
}
func (message2) Collect(from, paych address.Address) (*types.Message, error) {
func (m message2) Collect(paych address.Address) (*types.Message, error) {
return &types.Message{
To: paych,
From: from,
From: m.from,
Value: abi.NewTokenAmount(0),
Method: builtin2.MethodsPaych.Collect,
}, nil

View File

@ -82,13 +82,13 @@ func newChannelAccessor(pm *Manager, from address.Address, to address.Address) *
}
}
func (ca *channelAccessor) messageBuilder(ctx context.Context) (paych.MessageBuilder, error) {
func (ca *channelAccessor) messageBuilder(ctx context.Context, from address.Address) (paych.MessageBuilder, error) {
nwVersion, err := ca.api.StateNetworkVersion(ctx, types.EmptyTSK)
if err != nil {
return nil, err
}
return paych.Message(actors.VersionForNetwork(nwVersion)), nil
return paych.Message(actors.VersionForNetwork(nwVersion), from), nil
}
func (ca *channelAccessor) getChannelInfo(addr address.Address) (*ChannelInfo, error) {
@ -301,12 +301,12 @@ func (ca *channelAccessor) checkVoucherSpendable(ctx context.Context, ch address
return false, nil
}
mb, err := ca.messageBuilder(ctx)
mb, err := ca.messageBuilder(ctx, recipient)
if err != nil {
return false, err
}
mes, err := mb.Update(recipient, ch, sv, secret)
mes, err := mb.Update(ch, sv, secret)
if err != nil {
return false, err
}
@ -418,12 +418,12 @@ func (ca *channelAccessor) submitVoucher(ctx context.Context, ch address.Address
}
}
mb, err := ca.messageBuilder(ctx)
mb, err := ca.messageBuilder(ctx, ci.Control)
if err != nil {
return cid.Undef, err
}
msg, err := mb.Update(ci.Control, ch, sv, secret)
msg, err := mb.Update(ch, sv, secret)
if err != nil {
return cid.Undef, err
}
@ -574,11 +574,11 @@ func (ca *channelAccessor) settle(ctx context.Context, ch address.Address) (cid.
return cid.Undef, err
}
mb, err := ca.messageBuilder(ctx)
mb, err := ca.messageBuilder(ctx, ci.Control)
if err != nil {
return cid.Undef, err
}
msg, err := mb.Settle(ci.Control, ch)
msg, err := mb.Settle(ch)
if err != nil {
return cid.Undef, err
}
@ -605,12 +605,12 @@ func (ca *channelAccessor) collect(ctx context.Context, ch address.Address) (cid
return cid.Undef, err
}
mb, err := ca.messageBuilder(ctx)
mb, err := ca.messageBuilder(ctx, ci.Control)
if err != nil {
return cid.Undef, err
}
msg, err := mb.Collect(ci.Control, ch)
msg, err := mb.Collect(ch)
if err != nil {
return cid.Undef, err
}

View File

@ -384,11 +384,11 @@ func (ca *channelAccessor) processTask(ctx context.Context, amt types.BigInt) *p
// createPaych sends a message to create the channel and returns the message cid
func (ca *channelAccessor) createPaych(ctx context.Context, amt types.BigInt) (cid.Cid, error) {
mb, err := ca.messageBuilder(ctx)
mb, err := ca.messageBuilder(ctx, ca.from)
if err != nil {
return cid.Undef, err
}
msg, err := mb.Create(ca.from, ca.to, amt)
msg, err := mb.Create(ca.to, amt)
if err != nil {
return cid.Undef, err
}