-
Notifications
You must be signed in to change notification settings - Fork 0
/
deliver.go
43 lines (37 loc) · 909 Bytes
/
deliver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package chainx
import (
"encoding/json"
"github.com/hootuu/domain/chain"
"github.com/hootuu/domain/letter"
"github.com/hootuu/utils/errors"
"github.com/hootuu/utils/logger"
"go.uber.org/zap"
)
const (
DeliverTopic = "deliver"
)
type DeliverPayload struct {
Chn chain.Chain `bson:"c" json:"c"`
Data chain.Cid `bson:"d" json:"d"`
}
func DeliverPayloadOf(str string) (*DeliverPayload, *errors.Error) {
var p DeliverPayload
nErr := json.Unmarshal([]byte(str), &p)
if nErr != nil {
logger.Logger.Error("DeliverPayloadOf.json.Unmarshal error", zap.String("payload", str))
return nil, errors.Sys("invalid payload")
}
return &p, nil
}
func NewDeliverLetter(p DeliverPayload) *letter.Letter {
return letter.NewLetter(
DeliverTopic,
p,
)
}
func Deliver(chn chain.Chain, data chain.Cid) {
letter.PostOffice().Broadcast(NewDeliverLetter(DeliverPayload{
Chn: chn,
Data: data,
}))
}