github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/builtin/codes.go (about)

     1  package builtin
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/ipfs/go-cid"
     7  	mh "github.com/multiformats/go-multihash"
     8  )
     9  
    10  // The built-in actor code IDs
    11  var (
    12  	SystemActorCodeID           cid.Cid
    13  	InitActorCodeID             cid.Cid
    14  	CronActorCodeID             cid.Cid
    15  	AccountActorCodeID          cid.Cid
    16  	StoragePowerActorCodeID     cid.Cid
    17  	StorageMinerActorCodeID     cid.Cid
    18  	StorageMarketActorCodeID    cid.Cid
    19  	PaymentChannelActorCodeID   cid.Cid
    20  	MultisigActorCodeID         cid.Cid
    21  	RewardActorCodeID           cid.Cid
    22  	VerifiedRegistryActorCodeID cid.Cid
    23  	CallerTypesSignable         []cid.Cid
    24  )
    25  
    26  var builtinActors map[cid.Cid]*actorInfo
    27  
    28  type actorInfo struct {
    29  	name   string
    30  	signer bool
    31  }
    32  
    33  func init() {
    34  	builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
    35  	builtinActors = make(map[cid.Cid]*actorInfo)
    36  
    37  	for id, info := range map[*cid.Cid]*actorInfo{ //nolint:nomaprange
    38  		&SystemActorCodeID:           {name: "fil/4/system"},
    39  		&InitActorCodeID:             {name: "fil/4/init"},
    40  		&CronActorCodeID:             {name: "fil/4/cron"},
    41  		&StoragePowerActorCodeID:     {name: "fil/4/storagepower"},
    42  		&StorageMinerActorCodeID:     {name: "fil/4/storageminer"},
    43  		&StorageMarketActorCodeID:    {name: "fil/4/storagemarket"},
    44  		&PaymentChannelActorCodeID:   {name: "fil/4/paymentchannel"},
    45  		&RewardActorCodeID:           {name: "fil/4/reward"},
    46  		&VerifiedRegistryActorCodeID: {name: "fil/4/verifiedregistry"},
    47  		&AccountActorCodeID:          {name: "fil/4/account", signer: true},
    48  		&MultisigActorCodeID:         {name: "fil/4/multisig", signer: true},
    49  	} {
    50  		c, err := builder.Sum([]byte(info.name))
    51  		if err != nil {
    52  			panic(err)
    53  		}
    54  		*id = c
    55  		builtinActors[c] = info
    56  	}
    57  
    58  	// Set of actor code types that can represent external signing parties.
    59  	for id, info := range builtinActors { //nolint:nomaprange
    60  		if info.signer {
    61  			CallerTypesSignable = append(CallerTypesSignable, id)
    62  		}
    63  	}
    64  	sort.Slice(CallerTypesSignable, func(i, j int) bool {
    65  		return CallerTypesSignable[i].KeyString() < CallerTypesSignable[j].KeyString()
    66  	})
    67  
    68  }
    69  
    70  // IsBuiltinActor returns true if the code belongs to an actor defined in this repo.
    71  func IsBuiltinActor(code cid.Cid) bool {
    72  	_, isBuiltin := builtinActors[code]
    73  	return isBuiltin
    74  }
    75  
    76  // ActorNameByCode returns the (string) name of the actor given a cid code.
    77  func ActorNameByCode(code cid.Cid) string {
    78  	if !code.Defined() {
    79  		return "<undefined>"
    80  	}
    81  
    82  	info, ok := builtinActors[code]
    83  	if !ok {
    84  		return "<unknown>"
    85  	}
    86  	return info.name
    87  }
    88  
    89  // Tests whether a code CID represents an actor that can be an external principal: i.e. an account or multisig.
    90  // We could do something more sophisticated here: https://github.com/filecoin-project/specs-actors/issues/178
    91  func IsPrincipal(code cid.Cid) bool {
    92  	info, ok := builtinActors[code]
    93  	if !ok {
    94  		return false
    95  	}
    96  	return info.signer
    97  }