github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/util/adt/store.go (about)

     1  package adt
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/go-state-types/cbor"
     7  	"github.com/filecoin-project/go-state-types/exitcode"
     8  	adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt"
     9  	cid "github.com/ipfs/go-cid"
    10  	ipldcbor "github.com/ipfs/go-ipld-cbor"
    11  
    12  	vmr "github.com/filecoin-project/specs-actors/v4/actors/runtime"
    13  )
    14  
    15  type Store = adt2.Store
    16  
    17  // Adapts a vanilla IPLD store as an ADT store.
    18  func WrapStore(ctx context.Context, store ipldcbor.IpldStore) Store {
    19  	return &wstore{
    20  		ctx:       ctx,
    21  		IpldStore: store,
    22  	}
    23  }
    24  
    25  // Adapts a block store as an ADT store.
    26  func WrapBlockStore(ctx context.Context, bs ipldcbor.IpldBlockstore) Store {
    27  	return WrapStore(ctx, ipldcbor.NewCborStore(bs))
    28  }
    29  
    30  type wstore struct {
    31  	ctx context.Context
    32  	ipldcbor.IpldStore
    33  }
    34  
    35  var _ Store = &wstore{}
    36  
    37  func (s *wstore) Context() context.Context {
    38  	return s.ctx
    39  }
    40  
    41  // Adapter for a Runtime as an ADT Store.
    42  
    43  // Adapts a Runtime as an ADT store.
    44  func AsStore(rt vmr.Runtime) Store {
    45  	return rtStore{rt}
    46  }
    47  
    48  type rtStore struct {
    49  	vmr.Runtime
    50  }
    51  
    52  var _ Store = &rtStore{}
    53  
    54  func (r rtStore) Context() context.Context {
    55  	return r.Runtime.Context()
    56  }
    57  
    58  func (r rtStore) Get(_ context.Context, c cid.Cid, out interface{}) error {
    59  	// The Go context is (un/fortunately?) dropped here.
    60  	// See https://github.com/filecoin-project/specs-actors/issues/140
    61  	if !r.StoreGet(c, out.(cbor.Unmarshaler)) {
    62  		r.Abortf(exitcode.ErrNotFound, "not found")
    63  	}
    64  	return nil
    65  }
    66  
    67  func (r rtStore) Put(_ context.Context, v interface{}) (cid.Cid, error) {
    68  	// The Go context is (un/fortunately?) dropped here.
    69  	// See https://github.com/filecoin-project/specs-actors/issues/140
    70  	return r.StorePut(v.(cbor.Marshaler)), nil
    71  }