/
message.go
203 lines (173 loc) · 5.71 KB
/
message.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package types
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/big"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-amt-ipld/v2"
"github.com/filecoin-project/specs-actors/actors/abi"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
errPkg "github.com/pkg/errors"
typegen "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/go-filecoin/internal/pkg/cborutil"
"github.com/filecoin-project/go-filecoin/internal/pkg/constants"
e "github.com/filecoin-project/go-filecoin/internal/pkg/enccid"
"github.com/filecoin-project/go-filecoin/internal/pkg/encoding"
"github.com/filecoin-project/go-filecoin/internal/pkg/vm/gas"
)
const MessageVersion = 0
// BlockGasLimit is the maximum amount of gas that can be used to execute messages in a single block.
var BlockGasLimit = gas.NewGas(100e6)
// EmptyMessagesCID is the cid of an empty collection of messages.
var EmptyMessagesCID cid.Cid
// EmptyReceiptsCID is the cid of an empty collection of receipts.
var EmptyReceiptsCID cid.Cid
// EmptyTxMetaCID is the cid of a TxMeta wrapping empty cids
var EmptyTxMetaCID cid.Cid
func init() {
tmpCst := cborutil.NewIpldStore(blockstore.NewBlockstore(datastore.NewMapDatastore()))
emptyAMTCid, err := amt.FromArray(context.Background(), tmpCst, []typegen.CBORMarshaler{})
if err != nil {
panic("could not create CID for empty AMT")
}
EmptyMessagesCID = emptyAMTCid
EmptyReceiptsCID = emptyAMTCid
EmptyTxMetaCID, err = tmpCst.Put(context.Background(), TxMeta{SecpRoot: e.NewCid(EmptyMessagesCID), BLSRoot: e.NewCid(EmptyMessagesCID)})
if err != nil {
panic("could not create CID for empty TxMeta")
}
}
// UnsignedMessage is an exchange of information between two actors modeled
// as a function call.
type UnsignedMessage struct {
// control field for encoding struct as an array
_ struct{} `cbor:",toarray"`
Version int64 `json:"version"`
To address.Address `json:"to"`
From address.Address `json:"from"`
// When receiving a message from a user account the nonce in
// the message must match the expected nonce in the from actor.
// This prevents replay attacks.
CallSeqNum uint64 `json:"callSeqNum"`
Value AttoFIL `json:"value"`
GasPrice AttoFIL `json:"gasPrice"`
GasLimit gas.Unit `json:"gasLimit"`
Method abi.MethodNum `json:"method"`
Params []byte `json:"params"`
// Pay attention to Equals() if updating this struct.
}
// NewUnsignedMessage creates a new message.
func NewUnsignedMessage(from, to address.Address, nonce uint64, value AttoFIL, method abi.MethodNum, params []byte) *UnsignedMessage {
return &UnsignedMessage{
Version: MessageVersion,
To: to,
From: from,
CallSeqNum: nonce,
Value: value,
Method: method,
Params: params,
}
}
// NewMeteredMessage adds gas price and gas limit to the message
func NewMeteredMessage(from, to address.Address, nonce uint64, value AttoFIL, method abi.MethodNum, params []byte, price AttoFIL, limit gas.Unit) *UnsignedMessage {
return &UnsignedMessage{
Version: MessageVersion,
To: to,
From: from,
CallSeqNum: nonce,
Value: value,
GasPrice: price,
GasLimit: limit,
Method: method,
Params: params,
}
}
// Unmarshal a message from the given bytes.
func (msg *UnsignedMessage) Unmarshal(b []byte) error {
return encoding.Decode(b, msg)
}
// Marshal the message into bytes.
func (msg *UnsignedMessage) Marshal() ([]byte, error) {
return encoding.Encode(msg)
}
// ToNode converts the Message to an IPLD node.
func (msg *UnsignedMessage) ToNode() (ipld.Node, error) {
data, err := encoding.Encode(msg)
if err != nil {
return nil, err
}
c, err := constants.DefaultCidBuilder.Sum(data)
if err != nil {
return nil, err
}
blk, err := blocks.NewBlockWithCid(data, c)
if err != nil {
return nil, err
}
obj, err := cbor.DecodeBlock(blk)
if err != nil {
return nil, err
}
return obj, nil
}
// Cid returns the canonical CID for the message.
// TODO: can we avoid returning an error?
func (msg *UnsignedMessage) Cid() (cid.Cid, error) {
obj, err := msg.ToNode()
if err != nil {
return cid.Undef, errPkg.Wrap(err, "failed to marshal to cbor")
}
return obj.Cid(), nil
}
// OnChainLen returns the amount of bytes used to represent the message on chain.
func (msg *UnsignedMessage) OnChainLen() int {
bits, err := encoding.Encode(msg)
if err != nil {
panic(err)
}
return len(bits)
}
func (msg *UnsignedMessage) String() string {
errStr := "(error encoding Message)"
cid, err := msg.Cid()
if err != nil {
return errStr
}
js, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return errStr
}
return fmt.Sprintf("Message cid=[%v]: %s", cid, string(js))
}
// Equals tests whether two messages are equal
func (msg *UnsignedMessage) Equals(other *UnsignedMessage) bool {
return msg.To == other.To &&
msg.From == other.From &&
msg.CallSeqNum == other.CallSeqNum &&
msg.Value.Equals(other.Value) &&
msg.Method == other.Method &&
msg.GasPrice.Equals(other.GasPrice) &&
msg.GasLimit == other.GasLimit &&
bytes.Equal(msg.Params, other.Params)
}
// NewGasPrice constructs a gas price (in AttoFIL) from the given number.
func NewGasPrice(price int64) AttoFIL {
return NewAttoFIL(big.NewInt(price))
}
// TxMeta tracks the merkleroots of both secp and bls messages separately
type TxMeta struct {
_ struct{} `cbor:",toarray"`
BLSRoot e.Cid `json:"blsRoot"`
SecpRoot e.Cid `json:"secpRoot"`
}
// String returns a readable printing string of TxMeta
func (m TxMeta) String() string {
return fmt.Sprintf("secp: %s, bls: %s", m.SecpRoot.String(), m.BLSRoot.String())
}