From 817358f1bb8786da4204ee7afcc8ed403ae59a55 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 21 Aug 2020 23:24:53 +0300 Subject: [PATCH] better semantics for mpool clear local argument local messages should be kept unless the parameter is true --- api/api_full.go | 2 +- api/apistruct/struct.go | 4 +-- chain/messagepool/messagepool.go | 44 +++++++++++++++++++------------- cli/mpool.go | 2 +- documentation/en/mpool.md | 10 +++----- node/impl/full/mpool.go | 4 +-- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/api/api_full.go b/api/api_full.go index 9a8643146..2d8a4e515 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -184,7 +184,7 @@ type FullNode interface { MpoolGetNonce(context.Context, address.Address) (uint64, error) MpoolSub(context.Context) (<-chan MpoolUpdate, error) - // MpoolClear clears all pending messages from the mpool + // MpoolClear clears pending messages from the mpool MpoolClear(context.Context, bool) error // MpoolGetConfig returns (a copy of) the current mpool config diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index 05f22139a..0b8ba00e4 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -496,8 +496,8 @@ func (c *FullNodeStruct) MpoolPending(ctx context.Context, tsk types.TipSetKey) return c.Internal.MpoolPending(ctx, tsk) } -func (c *FullNodeStruct) MpoolClear(ctx context.Context, localonly bool) error { - return c.Internal.MpoolClear(ctx, localonly) +func (c *FullNodeStruct) MpoolClear(ctx context.Context, local bool) error { + return c.Internal.MpoolClear(ctx, local) } func (c *FullNodeStruct) MpoolPush(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) { diff --git a/chain/messagepool/messagepool.go b/chain/messagepool/messagepool.go index 684aa80aa..644a9104f 100644 --- a/chain/messagepool/messagepool.go +++ b/chain/messagepool/messagepool.go @@ -974,31 +974,39 @@ func (mp *MessagePool) loadLocal() error { return nil } -func (mp *MessagePool) Clear(localonly bool) { +func (mp *MessagePool) Clear(local bool) { mp.lk.Lock() defer mp.lk.Unlock() - // remove local messages from the datastore - for a := range mp.localAddrs { - mset, ok := mp.pending[a] - if !ok { - continue - } + // remove everything if local is true, including removing local messages from + // the datastore + if local { + for a := range mp.localAddrs { + mset, ok := mp.pending[a] + if !ok { + continue + } - for _, m := range mset.msgs { - err := mp.localMsgs.Delete(datastore.NewKey(string(m.Cid().Bytes()))) - if err != nil { - log.Warnf("error deleting local message: %s", err) + for _, m := range mset.msgs { + err := mp.localMsgs.Delete(datastore.NewKey(string(m.Cid().Bytes()))) + if err != nil { + log.Warnf("error deleting local message: %s", err) + } } } + mp.pending = make(map[address.Address]*msgSet) + mp.republished = nil + + return + } + + // remove everything except the local messages + for a := range mp.pending { + _, isLocal := mp.localAddrs[a] + if isLocal { + continue + } delete(mp.pending, a) } - - // clear the maps - mp.republished = nil - if !localonly { - mp.pending = make(map[address.Address]*msgSet) - } - } diff --git a/cli/mpool.go b/cli/mpool.go index 50a4485d2..4562ef398 100644 --- a/cli/mpool.go +++ b/cli/mpool.go @@ -90,7 +90,7 @@ var mpoolClear = &cli.Command{ Flags: []cli.Flag{ &cli.BoolFlag{ Name: "local", - Usage: "clear local messages only", + Usage: "also clear local messages", }, &cli.BoolFlag{ Name: "really-do-it", diff --git a/documentation/en/mpool.md b/documentation/en/mpool.md index d26120d8e..bbb7f2440 100644 --- a/documentation/en/mpool.md +++ b/documentation/en/mpool.md @@ -20,7 +20,7 @@ The full node API defines the following methods for interacting with the mpool: MpoolSub(context.Context) (<-chan MpoolUpdate, error) MpoolGetConfig(context.Context) (*types.MpoolConfig, error) MpoolSetConfig(context.Context, *types.MpoolConfig) error - MpoolClear(context.Context, localonly bool) error + MpoolClear(context.Context, local bool) error ``` ### MpoolPending @@ -64,7 +64,7 @@ Sets the mpool configuration to (a copy of) the supplied configuration object. ### MpoolClear -Clears pending messages from the mpool; if `localonly` is `true` then only local messages are cleared. +Clears pending messages from the mpool; if `local` is `true` then local messages are also cleared and removed from the datastore. This should be used with extreme care and only in the case of errors during head changes that would leave the mpool in an inconsistent state. @@ -109,11 +109,9 @@ Gets or sets the current mpool configuration. ### lotus mpool clear Unconditionally clears pending messages from the mpool. -If the `--local` flag is passed, then only local messages are cleared; otherwise all messages -are cleared. +If the `--local` flag is passed, then local messages are also cleared; otherwise local messages are retained. -*Warning*: this command should only be used in the case of head change errors leaving the mpool in an -inconsistent state. +*Warning*: this command should only be used in the case of head change errors leaving the mpool in an state. ## Configuration diff --git a/node/impl/full/mpool.go b/node/impl/full/mpool.go index 388eaa9d0..fe2b054dd 100644 --- a/node/impl/full/mpool.go +++ b/node/impl/full/mpool.go @@ -105,8 +105,8 @@ func (a *MpoolAPI) MpoolPending(ctx context.Context, tsk types.TipSetKey) ([]*ty } } -func (a *MpoolAPI) MpoolClear(ctx context.Context, localonly bool) error { - a.Mpool.Clear(localonly) +func (a *MpoolAPI) MpoolClear(ctx context.Context, local bool) error { + a.Mpool.Clear(local) return nil }