2021-05-19 00:01:30 +00:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
2021-06-10 02:40:00 +00:00
|
|
|
blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
|
2021-05-19 00:01:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// toArray converts the given set of CIDs to an AMT. This is usually used to pack messages into blocks.
|
2021-05-19 00:01:30 +00:00
|
|
|
func toArray(store blockadt.Store, cids []cid.Cid) (cid.Cid, error) {
|
|
|
|
arr := blockadt.MakeEmptyArray(store)
|
|
|
|
for i, c := range cids {
|
|
|
|
oc := cbg.CborCid(c)
|
|
|
|
if err := arr.Set(uint64(i), &oc); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr.Root()
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// storeMessages packs a set of messages into a types.MsgMeta and returns the resulting CID. The
|
|
|
|
// resulting CID is valid for the BlocKHeader's Messages field.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (nd *Node) storeMessages(ctx context.Context, messages []*types.Message) (cid.Cid, error) {
|
2021-06-08 04:14:57 +00:00
|
|
|
// We store all messages as "bls" messages so they're executed in-order. This ensures
|
|
|
|
// accurate gas accounting. It also ensures we don't, e.g., try to fund a miner after we
|
|
|
|
// fail a pre-commit...
|
|
|
|
var msgCids []cid.Cid
|
2021-05-19 00:01:30 +00:00
|
|
|
for _, msg := range messages {
|
2021-06-08 04:14:57 +00:00
|
|
|
c, err := nd.Chainstore.PutMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
2021-06-08 04:14:57 +00:00
|
|
|
msgCids = append(msgCids, c)
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
adtStore := nd.Chainstore.ActorStore(ctx)
|
2021-06-08 04:14:57 +00:00
|
|
|
blsMsgArr, err := toArray(adtStore, msgCids)
|
2021-05-19 00:01:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2021-06-08 04:14:57 +00:00
|
|
|
sekpMsgArr, err := toArray(adtStore, nil)
|
2021-05-19 00:01:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgsCid, err := adtStore.Put(adtStore.Context(), &types.MsgMeta{
|
|
|
|
BlsMessages: blsMsgArr,
|
|
|
|
SecpkMessages: sekpMsgArr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
return msgsCid, nil
|
|
|
|
}
|