From 918b759d03146228e44ac660d97e2a150b1bee9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 16 Apr 2020 23:06:31 +0200 Subject: [PATCH] mpool: Fix PushWithNonce for ID addresses --- chain/messagepool/messagepool.go | 21 ++++++++++++++++----- markets/storageadapter/client.go | 12 +++++++++++- markets/storageadapter/provider.go | 7 ++++++- node/impl/full/mpool.go | 4 ++-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index 016e471d1..fe727f2a3 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -130,6 +130,7 @@ type Provider interface { PutMessage(m types.ChainMsg) (cid.Cid, error) PubSubPublish(string, []byte) error StateGetActor(address.Address, *types.TipSet) (*types.Actor, error) + StateAccountKey(context.Context, address.Address, *types.TipSet) (address.Address, error) MessagesForBlock(*types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) MessagesForTipset(*types.TipSet) ([]types.ChainMsg, error) LoadTipSet(tsk types.TipSetKey) (*types.TipSet, error) @@ -161,6 +162,10 @@ func (mpp *mpoolProvider) StateGetActor(addr address.Address, ts *types.TipSet) return mpp.sm.GetActor(addr, ts) } +func (mpp *mpoolProvider) StateAccountKey(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) { + return mpp.sm.ResolveToKeyAddress(ctx, addr, ts) +} + func (mpp *mpoolProvider) MessagesForBlock(h *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) { return mpp.sm.ChainStore().MessagesForBlock(h) } @@ -470,22 +475,28 @@ func (mp *MessagePool) getStateBalance(addr address.Address, ts *types.TipSet) ( return act.Balance, nil } -func (mp *MessagePool) PushWithNonce(addr address.Address, cb func(uint64) (*types.SignedMessage, error)) (*types.SignedMessage, error) { +func (mp *MessagePool) PushWithNonce(ctx context.Context, addr address.Address, cb func(address.Address, uint64) (*types.SignedMessage, error)) (*types.SignedMessage, error) { mp.curTsLk.Lock() defer mp.curTsLk.Unlock() mp.lk.Lock() defer mp.lk.Unlock() - if addr.Protocol() == address.ID { - log.Warnf("Called pushWithNonce with ID address (%s) this might not be handled properly yet", addr) - } + nonce, err := mp.getNonceLocked(addr, mp.curTs) if err != nil { return nil, xerrors.Errorf("get nonce locked failed: %w", err) } - msg, err := cb(nonce) + fromKey := addr + if fromKey.Protocol() == address.ID { + fromKey, err = mp.api.StateAccountKey(ctx, fromKey, mp.curTs) + if err != nil { + return nil, xerrors.Errorf("resolving sender key: %w", err) + } + } + + msg, err := cb(fromKey, nonce) if err != nil { return nil, err } diff --git a/markets/storageadapter/client.go b/markets/storageadapter/client.go index 33b624364..62f950755 100644 --- a/markets/storageadapter/client.go +++ b/markets/storageadapter/client.go @@ -85,7 +85,12 @@ func (n *ClientNodeAdapter) ListStorageProviders(ctx context.Context, encodedTs } func (n *ClientNodeAdapter) VerifySignature(ctx context.Context, sig crypto.Signature, addr address.Address, input []byte, encodedTs shared.TipSetToken) (bool, error) { - err := sigs.Verify(&sig, addr, input) + addr, err := n.StateAccountKey(ctx, addr, types.EmptyTSK) + if err != nil { + return false, err + } + + err = sigs.Verify(&sig, addr, input) return err == nil, err } @@ -332,6 +337,11 @@ func (n *ClientNodeAdapter) SignProposal(ctx context.Context, signer address.Add return nil, err } + signer, err = n.StateAccountKey(ctx, signer, types.EmptyTSK) + if err != nil { + return nil, err + } + sig, err := n.Wallet.Sign(ctx, signer, buf) if err != nil { return nil, err diff --git a/markets/storageadapter/provider.go b/markets/storageadapter/provider.go index 85a1b3360..025f788b9 100644 --- a/markets/storageadapter/provider.go +++ b/markets/storageadapter/provider.go @@ -118,7 +118,12 @@ func (n *ProviderNodeAdapter) OnDealComplete(ctx context.Context, deal storagema } func (n *ProviderNodeAdapter) VerifySignature(ctx context.Context, sig crypto.Signature, addr address.Address, input []byte, encodedTs shared.TipSetToken) (bool, error) { - err := sigs.Verify(&sig, addr, input) + addr, err := n.StateAccountKey(ctx, addr, types.EmptyTSK) + if err != nil { + return false, err + } + + err = sigs.Verify(&sig, addr, input) return err == nil, err } diff --git a/node/impl/full/mpool.go b/node/impl/full/mpool.go index dadfa1704..99e06994c 100644 --- a/node/impl/full/mpool.go +++ b/node/impl/full/mpool.go @@ -91,7 +91,7 @@ func (a *MpoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message) (*t return nil, xerrors.Errorf("MpoolPushMessage expects message nonce to be 0, was %d", msg.Nonce) } - return a.Mpool.PushWithNonce(msg.From, func(nonce uint64) (*types.SignedMessage, error) { + return a.Mpool.PushWithNonce(ctx, msg.From, func(from address.Address, nonce uint64) (*types.SignedMessage, error) { msg.Nonce = nonce b, err := a.WalletBalance(ctx, msg.From) @@ -103,7 +103,7 @@ func (a *MpoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message) (*t return nil, xerrors.Errorf("mpool push: not enough funds: %s < %s", b, msg.Value) } - return a.WalletSignMessage(ctx, msg.From, msg) + return a.WalletSignMessage(ctx, from, msg) }) }