github.com/Finschia/finschia-sdk@v0.48.1/x/capability/types/keys.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  )
     8  
     9  const (
    10  	// ModuleName defines the module name
    11  	ModuleName = "capability"
    12  
    13  	// StoreKey defines the primary module store key
    14  	StoreKey = ModuleName
    15  
    16  	// MemStoreKey defines the in-memory store key
    17  	MemStoreKey = "mem_capability"
    18  )
    19  
    20  var (
    21  	// KeyIndex defines the key that stores the current globally unique capability
    22  	// index.
    23  	KeyIndex = []byte("index")
    24  
    25  	// KeyPrefixIndexCapability defines a key prefix that stores index to capability
    26  	// name mappings.
    27  	KeyPrefixIndexCapability = []byte("capability_index")
    28  
    29  	// KeyMemInitialized defines the key that stores the initialized flag in the memory store
    30  	KeyMemInitialized = []byte("mem_initialized")
    31  )
    32  
    33  // RevCapabilityKey returns a reverse lookup key for a given module and capability
    34  // name.
    35  func RevCapabilityKey(module, name string) []byte {
    36  	return []byte(fmt.Sprintf("%s/rev/%s", module, name))
    37  }
    38  
    39  // FwdCapabilityKey returns a forward lookup key for a given module and capability
    40  // reference.
    41  func FwdCapabilityKey(module string, cap *Capability) []byte {
    42  	// encode the key to a fixed length to avoid breaking consensus state machine
    43  	// it's a hacky backport of https://github.com/cosmos/cosmos-sdk/pull/11737
    44  	// the length 10 is picked so it's backward compatible on common architectures.
    45  	key := fmt.Sprintf("%#010p", cap)
    46  	if len(key) > 10 {
    47  		key = key[len(key)-10:]
    48  	}
    49  	return []byte(fmt.Sprintf("%s/fwd/0x%s", module, key))
    50  }
    51  
    52  // IndexToKey returns bytes to be used as a key for a given capability index.
    53  func IndexToKey(index uint64) []byte {
    54  	return sdk.Uint64ToBigEndian(index)
    55  }
    56  
    57  // IndexFromKey returns an index from a call to IndexToKey for a given capability
    58  // index.
    59  func IndexFromKey(key []byte) uint64 {
    60  	return sdk.BigEndianToUint64(key)
    61  }