lotus/chain/messagepool.go

179 lines
3.7 KiB
Go
Raw Normal View History

2019-07-05 14:46:21 +00:00
package chain
import (
2019-08-09 15:59:12 +00:00
"encoding/base64"
2019-07-05 14:46:21 +00:00
"sync"
2019-07-08 12:51:45 +00:00
"github.com/filecoin-project/go-lotus/chain/address"
2019-07-26 04:54:22 +00:00
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
2019-07-31 07:13:49 +00:00
"github.com/pkg/errors"
2019-07-05 14:46:21 +00:00
)
type MessagePool struct {
lk sync.Mutex
pending map[address.Address]*msgSet
2019-07-26 04:54:22 +00:00
cs *store.ChainStore
2019-07-05 14:46:21 +00:00
}
type msgSet struct {
msgs map[uint64]*types.SignedMessage
2019-07-05 14:46:21 +00:00
startNonce uint64
}
func newMsgSet() *msgSet {
return &msgSet{
msgs: make(map[uint64]*types.SignedMessage),
2019-07-05 14:46:21 +00:00
}
}
func (ms *msgSet) add(m *types.SignedMessage) {
2019-07-05 14:46:21 +00:00
if len(ms.msgs) == 0 || m.Message.Nonce < ms.startNonce {
ms.startNonce = m.Message.Nonce
}
ms.msgs[m.Message.Nonce] = m
}
2019-07-26 04:54:22 +00:00
func NewMessagePool(cs *store.ChainStore) *MessagePool {
2019-07-05 14:46:21 +00:00
mp := &MessagePool{
pending: make(map[address.Address]*msgSet),
cs: cs,
}
cs.SubscribeHeadChanges(mp.HeadChange)
2019-07-05 14:46:21 +00:00
return mp
}
func (mp *MessagePool) Add(m *types.SignedMessage) error {
2019-07-05 14:46:21 +00:00
mp.lk.Lock()
defer mp.lk.Unlock()
data, err := m.Message.Serialize()
if err != nil {
return err
}
2019-08-09 15:59:12 +00:00
log.Info("mpooladd: %s", base64.StdEncoding.EncodeToString(data))
2019-07-05 14:46:21 +00:00
if err := m.Signature.Verify(m.Message.From, data); err != nil {
return err
}
2019-07-26 04:54:22 +00:00
if _, err := mp.cs.PutMessage(m); err != nil {
2019-07-05 14:46:21 +00:00
return err
}
mset, ok := mp.pending[m.Message.From]
if !ok {
mset = newMsgSet()
mp.pending[m.Message.From] = mset
}
mset.add(m)
return nil
}
2019-07-17 06:05:11 +00:00
func (mp *MessagePool) GetNonce(addr address.Address) (uint64, error) {
mp.lk.Lock()
defer mp.lk.Unlock()
mset, ok := mp.pending[addr]
if ok {
return mset.startNonce + uint64(len(mset.msgs)), nil
}
2019-07-26 04:54:22 +00:00
act, err := mp.cs.GetActor(addr)
2019-07-17 06:05:11 +00:00
if err != nil {
return 0, err
}
return act.Nonce, nil
}
2019-08-14 04:43:29 +00:00
func (mp *MessagePool) Remove(from address.Address, nonce uint64) {
2019-07-05 14:46:21 +00:00
mp.lk.Lock()
defer mp.lk.Unlock()
2019-08-14 04:43:29 +00:00
mset, ok := mp.pending[from]
2019-07-05 14:46:21 +00:00
if !ok {
return
}
// NB: This deletes any message with the given nonce. This makes sense
// as two messages with the same sender cannot have the same nonce
2019-08-14 04:43:29 +00:00
delete(mset.msgs, nonce)
2019-07-05 14:46:21 +00:00
if len(mset.msgs) == 0 {
2019-08-14 04:43:29 +00:00
delete(mp.pending, from)
2019-07-05 14:46:21 +00:00
}
}
func (mp *MessagePool) Pending() []*types.SignedMessage {
2019-07-05 14:46:21 +00:00
mp.lk.Lock()
defer mp.lk.Unlock()
var out []*types.SignedMessage
2019-07-05 14:46:21 +00:00
for _, mset := range mp.pending {
for i := mset.startNonce; true; i++ {
m, ok := mset.msgs[i]
if !ok {
break
}
out = append(out, m)
}
}
return out
}
2019-07-26 04:54:22 +00:00
func (mp *MessagePool) HeadChange(revert []*types.TipSet, apply []*types.TipSet) error {
2019-07-05 14:46:21 +00:00
for _, ts := range revert {
for _, b := range ts.Blocks() {
bmsgs, smsgs, err := mp.cs.MessagesForBlock(b)
2019-07-05 14:46:21 +00:00
if err != nil {
2019-07-31 07:13:49 +00:00
return errors.Wrapf(err, "failed to get messages for revert block %s(height %d)", b.Cid(), b.Height)
2019-07-05 14:46:21 +00:00
}
for _, msg := range smsgs {
2019-07-05 14:46:21 +00:00
if err := mp.Add(msg); err != nil {
return err
}
}
for _, msg := range bmsgs {
smsg := mp.RecoverSig(msg)
if smsg != nil {
if err := mp.Add(smsg); err != nil {
return err
}
} else {
log.Warnf("could not recover signature for bls message %s during a reorg revert", msg.Cid())
}
}
2019-07-05 14:46:21 +00:00
}
}
for _, ts := range apply {
for _, b := range ts.Blocks() {
bmsgs, smsgs, err := mp.cs.MessagesForBlock(b)
2019-07-05 14:46:21 +00:00
if err != nil {
2019-07-31 07:13:49 +00:00
return errors.Wrapf(err, "failed to get messages for apply block %s(height %d) (msgroot = %s)", b.Cid(), b.Height, b.Messages)
2019-07-05 14:46:21 +00:00
}
for _, msg := range smsgs {
2019-08-14 04:43:29 +00:00
mp.Remove(msg.Message.From, msg.Message.Nonce)
2019-07-05 14:46:21 +00:00
}
for _, msg := range bmsgs {
2019-08-14 04:43:29 +00:00
mp.Remove(msg.From, msg.Nonce)
}
2019-07-05 14:46:21 +00:00
}
}
return nil
}
func (mp *MessagePool) RecoverSig(msg *types.Message) *types.SignedMessage {
// TODO: persist signatures for BLS messages for a little while in case of reorgs
return nil
}