Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MpoolCheckReplaceMessages API #5863

Merged
merged 4 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ type FullNode interface {
MpoolCheckMessages(context.Context, []*types.Message) ([][]MessageCheckStatus, error)
// MpoolCheckPendingMessages performs logical checks for all pending messages from a given address
MpoolCheckPendingMessages(context.Context, address.Address) ([][]MessageCheckStatus, error)
// MpoolCheckMessages performs logical checks on pending messages with replacement
MpoolCheckReplaceMessages(context.Context, []*types.Message) ([][]MessageCheckStatus, error)

// MpoolGetNonce gets next nonce for the specified sender.
// Note that this method may not be atomic. Use MpoolPushMessage instead.
Expand Down
5 changes: 5 additions & 0 deletions api/apistruct/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type FullNodeStruct struct {

MpoolCheckMessages func(context.Context, []*types.Message) ([][]api.MessageCheckStatus, error) `perm:"read"`
MpoolCheckPendingMessages func(context.Context, address.Address) ([][]api.MessageCheckStatus, error) `perm:"read"`
MpoolCheckReplaceMessages func(context.Context, []*types.Message) ([][]api.MessageCheckStatus, error) `perm:"read"`

MinerGetBaseInfo func(context.Context, address.Address, abi.ChainEpoch, types.TipSetKey) (*api.MiningBaseInfo, error) `perm:"read"`
MinerCreateBlock func(context.Context, *api.BlockTemplate) (*types.BlockMsg, error) `perm:"write"`
Expand Down Expand Up @@ -740,6 +741,10 @@ func (c *FullNodeStruct) MpoolCheckPendingMessages(ctx context.Context, from add
return c.Internal.MpoolCheckPendingMessages(ctx, from)
}

func (c *FullNodeStruct) MpoolCheckReplaceMessages(ctx context.Context, msgs []*types.Message) ([][]api.MessageCheckStatus, error) {
return c.Internal.MpoolCheckReplaceMessages(ctx, msgs)
}

func (c *FullNodeStruct) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, error) {
return c.Internal.MpoolSub(ctx)
}
Expand Down
45 changes: 45 additions & 0 deletions chain/messagepool/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@ func (mp *MessagePool) CheckPendingMessages(from address.Address) ([][]api.Messa
return mp.checkMessages(msgs, true)
}

// CheckReplaceMessages performs a set of logical checks for related messages while performing a
// replacement.
func (mp *MessagePool) CheckReplaceMessages(replace []*types.Message) ([][]api.MessageCheckStatus, error) {
msgMap := make(map[address.Address]map[uint64]*types.Message)
count := 0

mp.lk.Lock()
for _, m := range replace {
mmap, ok := msgMap[m.From]
if !ok {
mmap = make(map[uint64]*types.Message)
msgMap[m.From] = mmap
mset, ok := mp.pending[m.From]
if ok {
count += len(mset.msgs)
for _, sm := range mset.msgs {
mmap[sm.Message.Nonce] = &sm.Message
}
} else {
count += 1
}
}
mmap[m.Nonce] = m
}
mp.lk.Unlock()

msgs := make([]*types.Message, 0, count)
start := 0
for _, mmap := range msgMap {
end := start + len(mmap)

for _, m := range mmap {
msgs = append(msgs, m)
}

sort.Slice(msgs[start:end], func(i, j int) bool {
return msgs[start+i].Nonce < msgs[start+j].Nonce
})

start = end
}

return mp.checkMessages(msgs, true)
}

func (mp *MessagePool) checkMessages(msgs []*types.Message, interned bool) (result [][]api.MessageCheckStatus, err error) {
mp.curTsLk.Lock()
curTs := mp.curTs
Expand Down
4 changes: 4 additions & 0 deletions node/impl/full/mpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (a *MpoolAPI) MpoolCheckPendingMessages(ctx context.Context, from address.A
return a.Mpool.CheckPendingMessages(from)
}

func (a *MpoolAPI) MpoolCheckReplaceMessages(ctx context.Context, msgs []*types.Message) ([][]api.MessageCheckStatus, error) {
return a.Mpool.CheckReplaceMessages(msgs)
}

func (a *MpoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
return a.Mpool.GetNonce(addr)
}
Expand Down