Skip to content

Commit

Permalink
feat: add support for generating tipset CIDs
Browse files Browse the repository at this point in the history
(cherry-picked from feat/nv18-fevm)
  • Loading branch information
ychiaoli18 authored and Stebalien committed Nov 19, 2022
1 parent fb90ba1 commit 16e0799
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions chain/types/tipset_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"strings"

block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
typegen "github.com/whyrusleeping/cbor-gen"

Expand Down Expand Up @@ -98,6 +99,28 @@ func (k *TipSetKey) UnmarshalJSON(b []byte) error {
return nil
}

func (k TipSetKey) Cid() (cid.Cid, error) {
blk, err := k.ToStorageBlock()
if err != nil {
return cid.Cid{}, err
}
return blk.Cid(), nil
}

func (k TipSetKey) ToStorageBlock() (block.Block, error) {
buf := new(bytes.Buffer)
if err := k.MarshalCBOR(buf); err != nil {
log.Errorf("failed to marshal ts key as CBOR: %s", k)
}

cid, err := abi.CidBuilder.Sum(buf.Bytes())
if err != nil {
return nil, err
}

return block.NewBlockWithCid(buf.Bytes(), cid)
}

func (k TipSetKey) MarshalCBOR(writer io.Writer) error {
if err := typegen.WriteMajorTypeHeader(writer, typegen.MajByteString, uint64(len(k.Bytes()))); err != nil {
return err
Expand Down

0 comments on commit 16e0799

Please sign in to comment.