github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/keeper/internal/keys.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/binary"
     5  	"time"
     6  
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  )
     9  
    10  // Keys for foundation store
    11  // Items are stored with the following key: values
    12  var (
    13  	// reserved for the future usage (cosmos-sdk v0.47)
    14  	// paramsKey         = []byte{0x00}
    15  	foundationInfoKey = []byte{0x01}
    16  
    17  	memberKeyPrefix          = []byte{0x10}
    18  	previousProposalIDKey    = []byte{0x11}
    19  	proposalKeyPrefix        = []byte{0x12}
    20  	proposalByVPEndKeyPrefix = []byte{0x13}
    21  	voteKeyPrefix            = []byte{0x14}
    22  
    23  	censorshipKeyPrefix = []byte{0x20}
    24  	grantKeyPrefix      = []byte{0x21}
    25  
    26  	poolKey = []byte{0x30}
    27  
    28  	// deprecatedGovMintKey Deprecated. Don't use it again.
    29  	deprecatedGovMintKey = []byte{0x40}
    30  	_                    = deprecatedGovMintKey
    31  )
    32  
    33  // must be constant
    34  var lenTime = len(sdk.FormatTimeBytes(time.Now()))
    35  
    36  // Uint64FromBytes converts a byte array to uint64
    37  func Uint64FromBytes(bz []byte) uint64 {
    38  	return binary.BigEndian.Uint64(bz)
    39  }
    40  
    41  // Uint64ToBytes converts a number in uint64 to a byte array
    42  func Uint64ToBytes(number uint64) []byte {
    43  	bz := make([]byte, 8)
    44  	binary.BigEndian.PutUint64(bz, number)
    45  	return bz
    46  }
    47  
    48  // memberKey key for a specific member from the store
    49  func memberKey(address sdk.AccAddress) []byte {
    50  	prefix := memberKeyPrefix
    51  	key := make([]byte, len(prefix)+len(address))
    52  
    53  	copy(key, prefix)
    54  	copy(key[len(prefix):], address)
    55  
    56  	return key
    57  }
    58  
    59  // proposalKey key for a specific proposal from the store
    60  func proposalKey(id uint64) []byte {
    61  	prefix := proposalKeyPrefix
    62  	idBz := Uint64ToBytes(id)
    63  	key := make([]byte, len(prefix)+len(idBz))
    64  
    65  	copy(key, prefix)
    66  	copy(key[len(prefix):], idBz)
    67  
    68  	return key
    69  }
    70  
    71  func voteKey(proposalID uint64, voter sdk.AccAddress) []byte {
    72  	prefix := voteKeyPrefix
    73  	idBz := Uint64ToBytes(proposalID)
    74  	key := make([]byte, len(prefix)+len(idBz)+len(voter))
    75  
    76  	begin := 0
    77  	copy(key[begin:], prefix)
    78  
    79  	begin += len(prefix)
    80  	copy(key[begin:], idBz)
    81  
    82  	begin += len(idBz)
    83  	copy(key[begin:], voter)
    84  
    85  	return key
    86  }
    87  
    88  func proposalByVPEndKey(vpEnd time.Time, id uint64) []byte {
    89  	prefix := proposalByVPEndKeyPrefix
    90  	vpEndBz := sdk.FormatTimeBytes(vpEnd)
    91  	idBz := Uint64ToBytes(id)
    92  	key := make([]byte, len(prefix)+lenTime+len(idBz))
    93  
    94  	begin := 0
    95  	copy(key[begin:], prefix)
    96  
    97  	begin += len(prefix)
    98  	copy(key[begin:], vpEndBz)
    99  
   100  	begin += len(vpEndBz)
   101  	copy(key[begin:], idBz)
   102  
   103  	return key
   104  }
   105  
   106  func splitProposalByVPEndKey(key []byte) (vpEnd time.Time, id uint64) {
   107  	prefix := proposalByVPEndKeyPrefix
   108  	begin := len(prefix)
   109  	end := begin + lenTime
   110  	vpEnd, err := sdk.ParseTimeBytes(key[begin:end])
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  
   115  	begin = end
   116  	id = Uint64FromBytes(key[begin:])
   117  
   118  	return
   119  }
   120  
   121  // memberKey key for a specific member from the store
   122  func censorshipKey(url string) []byte {
   123  	prefix := censorshipKeyPrefix
   124  	key := make([]byte, len(prefix)+len(url))
   125  
   126  	copy(key, prefix)
   127  	copy(key[len(prefix):], url)
   128  
   129  	return key
   130  }
   131  
   132  func grantKey(grantee sdk.AccAddress, url string) []byte {
   133  	prefix := grantKeyPrefixByGrantee(grantee)
   134  	key := make([]byte, len(prefix)+len(url))
   135  
   136  	copy(key, prefix)
   137  	copy(key[len(prefix):], url)
   138  
   139  	return key
   140  }
   141  
   142  func grantKeyPrefixByGrantee(grantee sdk.AccAddress) []byte {
   143  	prefix := grantKeyPrefix
   144  	key := make([]byte, len(prefix)+1+len(grantee))
   145  
   146  	begin := 0
   147  	copy(key[begin:], prefix)
   148  
   149  	begin += len(prefix)
   150  	key[begin] = byte(len(grantee))
   151  
   152  	begin++
   153  	copy(key[begin:], grantee)
   154  
   155  	return key
   156  }
   157  
   158  func splitGrantKey(key []byte) (grantee sdk.AccAddress, url string) {
   159  	prefix := grantKeyPrefix
   160  
   161  	begin := len(prefix) + 1
   162  	end := begin + int(key[begin-1])
   163  	grantee = key[begin:end]
   164  
   165  	begin = end
   166  	url = string(key[begin:])
   167  
   168  	return
   169  }