-
Notifications
You must be signed in to change notification settings - Fork 4
/
event.go
149 lines (131 loc) · 3.54 KB
/
event.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/meshplus/bitxhub-model/pb"
"github.com/meshplus/pier-client-ethereum/relay"
)
func (c *Client) Convert2IBTP(ev *relay.BrokerThrowInterchainEvent, timeoutHeight int64) (*pb.IBTP, error) {
fullEv, encrypt, err := c.fillInterchainEvent(ev)
if err != nil {
return nil, err
}
pd, err := encodePayload(fullEv, encrypt)
if err != nil {
return nil, err
}
group, err := c.fillGroup(fullEv.Group)
if err != nil {
return nil, err
}
return &pb.IBTP{
From: ev.SrcFullID,
To: ev.DstFullID,
Index: ev.Index,
Type: pb.IBTP_INTERCHAIN,
TimeoutHeight: timeoutHeight,
Proof: []byte("1"),
Payload: pd,
Group: group,
}, nil
}
func (c *Client) fillGroup(evGroup []string) (*pb.StringUint64Map, error) {
if len(evGroup) == 0 {
return nil, nil
}
keys := make([]string, len(evGroup))
values := make([]uint64, len(evGroup))
for i, str := range evGroup {
group := strings.Split(str, "-")
if len(group) != 2 {
return nil, fmt.Errorf("input group err: %s", str)
}
keys[i] = group[0]
v, err := strconv.Atoi(group[1])
if err != nil {
return nil, fmt.Errorf("convert group value %s err:%s", group[i], err)
}
values[i] = uint64(v)
}
return &pb.StringUint64Map{
Keys: keys,
Vals: values,
}, nil
}
func (c *Client) Convert2Receipt(ev *relay.BrokerThrowReceiptEvent) (*pb.IBTP, error) {
fullEv, encrypt, err := c.fillReceiptEvent(ev)
if err != nil {
return nil, err
}
return generateReceipt(fullEv.SrcFullID, fullEv.DstFullID, fullEv.Index, fullEv.Results, fullEv.Typ, encrypt, fullEv.MultiStatus)
}
func encodePayload(ev *relay.BrokerThrowInterchainEvent, encrypt bool) ([]byte, error) {
var args [][]byte
args = append(args, ev.Args...)
//for _, arg := range ev.Args {
// args = append(args, arg)
//}
content := &pb.Content{
Func: ev.Func,
Args: args,
}
data, err := content.Marshal()
if err != nil {
return nil, err
}
ibtppd := &pb.Payload{
Encrypted: encrypt,
Content: data,
Hash: ev.Hash[:],
}
return ibtppd.Marshal()
}
func (c *Client) fillInterchainEvent(ev *relay.BrokerThrowInterchainEvent) (*relay.BrokerThrowInterchainEvent, bool, error) {
if ev.Func == "" {
fun, args, encrypt, group, err := c.session.GetOutMessage(pb.GenServicePair(ev.SrcFullID, ev.DstFullID), ev.Index)
if err != nil {
return nil, false, err
}
ev.Func = fun
ev.Args = args
ev.Group = group
emptyHash := common.Hash{}
if bytes.Equal(ev.Hash[:], emptyHash[:]) {
var data []byte
data = append(data, []byte(fun)...)
for _, arg := range args {
data = append(data, arg...)
}
ev.Hash = common.BytesToHash(crypto.Keccak256(data))
}
return ev, encrypt, nil
}
return ev, false, nil
}
func (c *Client) fillReceiptEvent(ev *relay.BrokerThrowReceiptEvent) (*relay.BrokerThrowReceiptEvent, bool, error) {
if ev.Results == nil {
results, typ, encrypt, multiStatus, err := c.session.GetReceiptMessage(pb.GenServicePair(ev.SrcFullID, ev.DstFullID), ev.Index)
if err != nil {
return nil, false, err
}
ev.Results = results
ev.Typ = typ
ev.MultiStatus = multiStatus
emptyHash := common.Hash{}
if bytes.Equal(ev.Hash[:], emptyHash[:]) {
var packed []byte
for _, ele := range results {
for _, result := range ele {
packed = append(packed, result...)
}
}
ev.Hash = common.BytesToHash(crypto.Keccak256(packed))
}
return ev, encrypt, nil
}
return ev, false, nil
}