2019-07-12 10:43:15 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-21 17:15:28 +00:00
|
|
|
"bytes"
|
2019-07-12 10:43:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
To address.Address
|
|
|
|
From address.Address
|
|
|
|
|
|
|
|
Nonce uint64
|
|
|
|
|
|
|
|
Value BigInt
|
|
|
|
|
|
|
|
GasPrice BigInt
|
|
|
|
GasLimit BigInt
|
|
|
|
|
|
|
|
Method uint64
|
|
|
|
Params []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeMessage(b []byte) (*Message, error) {
|
|
|
|
var msg Message
|
2019-08-21 17:15:28 +00:00
|
|
|
if err := msg.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
|
2019-07-12 10:43:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Serialize() ([]byte, error) {
|
2019-08-21 17:15:28 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := m.MarshalCBOR(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2019-07-12 10:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) ToStorageBlock() (block.Block, error) {
|
|
|
|
data, err := m.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:15:28 +00:00
|
|
|
pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31)
|
2019-07-12 10:43:15 +00:00
|
|
|
c, err := pref.Sum(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(data, c)
|
|
|
|
}
|
2019-08-01 20:40:47 +00:00
|
|
|
|
|
|
|
func (m *Message) Cid() cid.Cid {
|
|
|
|
b, err := m.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to marshal message: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Cid()
|
|
|
|
}
|
2019-09-26 03:48:53 +00:00
|
|
|
|
|
|
|
func (m *Message) RequiredFunds() BigInt {
|
|
|
|
return BigAdd(
|
|
|
|
m.Value,
|
|
|
|
BigMul(m.GasPrice, m.GasLimit),
|
|
|
|
)
|
|
|
|
}
|