github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/types/keys.go (about)

     1  package types
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  )
     6  
     7  const (
     8  	// ModuleName is the name of the contract module
     9  	ModuleName = "wasm"
    10  
    11  	// StoreKey is the string store representation
    12  	StoreKey = ModuleName
    13  
    14  	// TStoreKey is the string transient store representation
    15  	TStoreKey = "transient_" + ModuleName
    16  
    17  	// QuerierRoute is the querier route for the wasm module
    18  	QuerierRoute = ModuleName
    19  
    20  	// RouterKey is the msg router key for the wasm module
    21  	RouterKey = ModuleName
    22  )
    23  
    24  // nolint
    25  var (
    26  	CodeKeyPrefix                                  = []byte{0x01}
    27  	ContractKeyPrefix                              = []byte{0x02}
    28  	ContractStorePrefix                            = []byte{0x03}
    29  	SequenceKeyPrefix                              = []byte{0x04}
    30  	ContractCodeHistoryElementPrefix               = []byte{0x05}
    31  	ContractByCodeIDAndCreatedSecondaryIndexPrefix = []byte{0x06}
    32  	PinnedCodeIndexPrefix                          = []byte{0x07}
    33  	TXCounterPrefix                                = []byte{0x08}
    34  	ContractMethodBlockedListPrefix                = []byte{0x10}
    35  
    36  	KeyLastCodeID     = append(SequenceKeyPrefix, []byte("lastCodeId")...)
    37  	KeyLastInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...)
    38  )
    39  
    40  // GetCodeKey constructs the key for retreiving the ID for the WASM code
    41  func GetCodeKey(codeID uint64) []byte {
    42  	contractIDBz := sdk.Uint64ToBigEndian(codeID)
    43  	return append(CodeKeyPrefix, contractIDBz...)
    44  }
    45  
    46  // GetContractAddressKey returns the key for the WASM contract instance
    47  func GetContractAddressKey(addr sdk.AccAddress) []byte {
    48  	return append(ContractKeyPrefix, addr...)
    49  }
    50  
    51  // GetContractStorePrefix returns the store prefix for the WASM contract instance
    52  func GetContractStorePrefix(addr sdk.AccAddress) []byte {
    53  	return append(ContractStorePrefix, addr...)
    54  }
    55  
    56  // GetContractByCreatedSecondaryIndexKey returns the key for the secondary index:
    57  // `<prefix><codeID><created/last-migrated><contractAddr>`
    58  func GetContractByCreatedSecondaryIndexKey(contractAddr sdk.AccAddress, c ContractCodeHistoryEntry) []byte {
    59  	prefix := GetContractByCodeIDSecondaryIndexPrefix(c.CodeID)
    60  	prefixLen := len(prefix)
    61  	contractAddrLen := len(contractAddr)
    62  	r := make([]byte, prefixLen+AbsoluteTxPositionLen+contractAddrLen)
    63  	copy(r[0:], prefix)
    64  	copy(r[prefixLen:], c.Updated.Bytes())
    65  	copy(r[prefixLen+AbsoluteTxPositionLen:], contractAddr)
    66  	return r
    67  }
    68  
    69  // GetContractByCodeIDSecondaryIndexPrefix returns the prefix for the second index: `<prefix><codeID>`
    70  func GetContractByCodeIDSecondaryIndexPrefix(codeID uint64) []byte {
    71  	prefixLen := len(ContractByCodeIDAndCreatedSecondaryIndexPrefix)
    72  	const codeIDLen = 8
    73  	r := make([]byte, prefixLen+codeIDLen)
    74  	copy(r[0:], ContractByCodeIDAndCreatedSecondaryIndexPrefix)
    75  	copy(r[prefixLen:], sdk.Uint64ToBigEndian(codeID))
    76  	return r
    77  }
    78  
    79  // GetContractCodeHistoryElementKey returns the key a contract code history entry: `<prefix><contractAddr><position>`
    80  func GetContractCodeHistoryElementKey(contractAddr sdk.AccAddress, pos uint64) []byte {
    81  	prefix := GetContractCodeHistoryElementPrefix(contractAddr)
    82  	prefixLen := len(prefix)
    83  	r := make([]byte, prefixLen+8)
    84  	copy(r[0:], prefix)
    85  	copy(r[prefixLen:], sdk.Uint64ToBigEndian(pos))
    86  	return r
    87  }
    88  
    89  // GetContractCodeHistoryElementPrefix returns the key prefix for a contract code history entry: `<prefix><contractAddr>`
    90  func GetContractCodeHistoryElementPrefix(contractAddr sdk.AccAddress) []byte {
    91  	prefixLen := len(ContractCodeHistoryElementPrefix)
    92  	contractAddrLen := len(contractAddr)
    93  	r := make([]byte, prefixLen+contractAddrLen)
    94  	copy(r[0:], ContractCodeHistoryElementPrefix)
    95  	copy(r[prefixLen:], contractAddr)
    96  	return r
    97  }
    98  
    99  // GetPinnedCodeIndexPrefix returns the key prefix for a code id pinned into the wasmvm cache
   100  func GetPinnedCodeIndexPrefix(codeID uint64) []byte {
   101  	prefixLen := len(PinnedCodeIndexPrefix)
   102  	r := make([]byte, prefixLen+8)
   103  	copy(r[0:], PinnedCodeIndexPrefix)
   104  	copy(r[prefixLen:], sdk.Uint64ToBigEndian(codeID))
   105  	return r
   106  }
   107  
   108  func GetContractMethodBlockedListPrefix(contractAddr string) []byte {
   109  	prefixLen := len(ContractMethodBlockedListPrefix)
   110  	contractAddrLen := len(contractAddr)
   111  	r := make([]byte, prefixLen+contractAddrLen)
   112  	copy(r, ContractMethodBlockedListPrefix)
   113  	copy(r[prefixLen:], contractAddr)
   114  	return r
   115  }
   116  
   117  // ParsePinnedCodeIndex converts the serialized code ID back.
   118  func ParsePinnedCodeIndex(s []byte) uint64 {
   119  	return sdk.BigEndianToUint64(s)
   120  }