Skip to content

Commit

Permalink
chore: add SigningBytes to the message
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Jan 31, 2023
1 parent 3c77fad commit 866d788
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
5 changes: 2 additions & 3 deletions app/submodule/mpool/mpool_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/messagepool"
"github.com/filecoin-project/venus/pkg/messagepool/journal"
"github.com/filecoin-project/venus/pkg/messagesigner"
"github.com/filecoin-project/venus/pkg/repo"
v0api "github.com/filecoin-project/venus/venus-shared/api/chain/v0"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
Expand Down Expand Up @@ -56,7 +55,7 @@ type MessagePoolSubmodule struct { //nolint
MessageSub *pubsub.Subscription

MPool *messagepool.MessagePool
msgSigner *messagesigner.MessageSigner
msgSigner *messagepool.MessageSigner
chain *chain.ChainSubmodule
network *network.NetworkSubmodule
walletAPI v1api.IWallet
Expand Down Expand Up @@ -95,7 +94,7 @@ func NewMpoolSubmodule(ctx context.Context, cfg messagepoolConfig,
walletAPI: wallet.API(),
network: network,
networkCfg: cfg.Repo().Config().NetworkParams,
msgSigner: messagesigner.NewMessageSigner(wallet.WalletIntersection(), mp, cfg.Repo().MetaDatastore()),
msgSigner: messagepool.NewMessageSigner(wallet.WalletIntersection(), mp, cfg.Repo().MetaDatastore()),
}, nil
}

Expand Down
3 changes: 1 addition & 2 deletions app/submodule/wallet/wallet_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/filecoin-project/venus/app/submodule/wallet/remotewallet"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/messagesigner"
"github.com/filecoin-project/venus/pkg/wallet"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
Expand Down Expand Up @@ -124,7 +123,7 @@ func (walletAPI *WalletAPI) WalletSign(ctx context.Context, k address.Address, m

// WalletSignMessage signs the given message using the given address.
func (walletAPI *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
sb, err := messagesigner.SigningBytes(msg, types.AddressProtocol2SignType(k.Protocol()))
sb, err := msg.SigningBytes(types.AddressProtocol2SignType(k.Protocol()))
if err != nil {
return nil, err
}
Expand Down
14 changes: 9 additions & 5 deletions pkg/messagepool/messagepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,12 @@ func TestCheckMessageBig(t *testing.T) {
GasPremium: types.NewInt(1),
Params: make([]byte, 41<<10), // 41KiB payload
}
sb, err := msg.SigningBytes(types.AddressProtocol2SignType(from.Protocol()))
assert.NoError(t, err)

sig, err := w.WalletSign(context.Background(), from, sb, types.MsgMeta{})
assert.NoError(t, err)

sig, err := w.WalletSign(context.Background(), from, msg.Cid().Bytes(), types.MsgMeta{})
if err != nil {
panic(err)
}
sm := &types.SignedMessage{
Message: *msg,
Signature: *sig,
Expand All @@ -515,7 +516,10 @@ func TestCheckMessageBig(t *testing.T) {
Params: make([]byte, 64<<10), // 64KiB payload
}

sig, err := w.WalletSign(context.Background(), from, msg.Cid().Bytes(), types.MsgMeta{})
sb, err := msg.SigningBytes(types.AddressProtocol2SignType(from.Protocol()))
assert.NoError(t, err)

sig, err := w.WalletSign(context.Background(), from, sb, types.MsgMeta{})
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package messagesigner
package messagepool

import (
"bytes"
Expand All @@ -8,17 +8,13 @@ import (
"sync"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/venus/pkg/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
logging "github.com/ipfs/go-log/v2"
cbg "github.com/whyrusleeping/cbor-gen"
)

var log = logging.Logger("messagepool")

const dsKeyActorNonce = "ActorNextNonce"

type MpoolNonceAPI interface {
Expand Down Expand Up @@ -59,7 +55,7 @@ func (ms *MessageSigner) SignMessage(ctx context.Context, msg *types.Message, cb
// Sign the message with the nonce
msg.Nonce = nonce

sb, err := SigningBytes(msg, types.AddressProtocol2SignType(msg.From.Protocol()))
sb, err := msg.SigningBytes(types.AddressProtocol2SignType(msg.From.Protocol()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,19 +160,3 @@ func (ms *MessageSigner) saveNonce(ctx context.Context, addr address.Address, no
func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key {
return datastore.KeyWithNamespaces([]string{dsKeyActorNonce, addr.String()})
}

func SigningBytes(msg *types.Message, sigType crypto.SigType) ([]byte, error) {
if sigType == crypto.SigTypeDelegated {
txArgs, err := types.EthTxArgsFromMessage(msg)
if err != nil {
return nil, fmt.Errorf("failed to reconstruct eth transaction: %w", err)
}
rlpEncodedMsg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return nil, fmt.Errorf("failed to repack eth rlp message: %w", err)
}
return rlpEncodedMsg, nil
}

return msg.Cid().Bytes(), nil
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// stm: #unit
package messagesigner
package messagepool

import (
"context"
Expand Down
5 changes: 2 additions & 3 deletions pkg/messagepool/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/messagesigner"
"github.com/filecoin-project/venus/pkg/statemanger"
"github.com/filecoin-project/venus/venus-shared/actors/policy"
"github.com/filecoin-project/venus/venus-shared/types"
Expand Down Expand Up @@ -50,7 +49,7 @@ type mpoolProvider struct {
config *config.NetworkParamsConfig
ps *pubsub.PubSub

lite messagesigner.MpoolNonceAPI
lite MpoolNonceAPI
}

var _ Provider = (*mpoolProvider)(nil)
Expand All @@ -65,7 +64,7 @@ func NewProvider(sm *statemanger.Stmgr, cs *chain.Store, cms *chain.MessageStore
}
}

func NewProviderLite(sm *chain.Store, ps *pubsub.PubSub, noncer messagesigner.MpoolNonceAPI) Provider {
func NewProviderLite(sm *chain.Store, ps *pubsub.PubSub, noncer MpoolNonceAPI) Provider {
return &mpoolProvider{sm: sm, ps: ps, lite: noncer}
}

Expand Down
17 changes: 17 additions & 0 deletions venus-shared/actors/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/venus/venus-shared/types/params"
blocks "github.com/ipfs/go-block-format"
Expand Down Expand Up @@ -208,4 +209,20 @@ func (m *Message) RequiredFunds() abi.TokenAmount {
return abi.TokenAmount{Int: BigMul(BigInt{Int: m.GasFeeCap.Int}, NewInt(uint64(m.GasLimit))).Int}
}

func (m *Message) SigningBytes(sigType crypto.SigType) ([]byte, error) {
if sigType == crypto.SigTypeDelegated {
txArgs, err := EthTxArgsFromMessage(m)
if err != nil {
return nil, fmt.Errorf("failed to reconstruct eth transaction: %w", err)
}
rlpEncodedMsg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
return nil, fmt.Errorf("failed to repack eth rlp message: %w", err)
}
return rlpEncodedMsg, nil
}

return m.Cid().Bytes(), nil
}

var _ ChainMsg = (*Message)(nil)

0 comments on commit 866d788

Please sign in to comment.