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

     1  package types
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"time"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  )
    10  
    11  const (
    12  	// ModuleName is the name of the module
    13  	ModuleName = "gov"
    14  
    15  	// StoreKey is the store key string for gov
    16  	StoreKey = ModuleName
    17  
    18  	// RouterKey is the message route for gov
    19  	RouterKey = ModuleName
    20  
    21  	// QuerierRoute is the querier route for gov
    22  	QuerierRoute = ModuleName
    23  
    24  	// DefaultParamspace default name for parameter store
    25  	DefaultParamspace = ModuleName
    26  )
    27  
    28  // Keys for governance store
    29  // Items are stored with the following key: values
    30  //
    31  // - 0x00<proposalID_Bytes>: Proposal
    32  //
    33  // - 0x01<endTime_Bytes><proposalID_Bytes>: activeProposalID
    34  //
    35  // - 0x02<endTime_Bytes><proposalID_Bytes>: inactiveProposalID
    36  //
    37  // - 0x03: nextProposalID
    38  //
    39  // - 0x10<proposalID_Bytes><depositorAddr_Bytes>: Deposit
    40  //
    41  // - 0x20<proposalID_Bytes><voterAddr_Bytes>: Voter
    42  var (
    43  	ProposalsKeyPrefix          = []byte{0x00}
    44  	ActiveProposalQueuePrefix   = []byte{0x01}
    45  	InactiveProposalQueuePrefix = []byte{0x02}
    46  	ProposalIDKey               = []byte{0x03}
    47  
    48  	DepositsKeyPrefix = []byte{0x10}
    49  
    50  	VotesKeyPrefix = []byte{0x20}
    51  
    52  	// PrefixWaitingProposalQueue defines the prefix of waiting proposal queue
    53  	PrefixWaitingProposalQueue = []byte{0x30}
    54  )
    55  
    56  // WaitingProposalByBlockHeightKey gets the waiting proposal queue key by block height
    57  func WaitingProposalByBlockHeightKey(blockHeight uint64) []byte {
    58  	return append(PrefixWaitingProposalQueue, sdk.Uint64ToBigEndian(blockHeight)...)
    59  }
    60  
    61  // WaitingProposalQueueKey returns the key for a proposalID in the WaitingProposalQueue
    62  func WaitingProposalQueueKey(proposalID uint64, blockHeight uint64) []byte {
    63  	bz := make([]byte, 8)
    64  	binary.LittleEndian.PutUint64(bz, proposalID)
    65  
    66  	return append(WaitingProposalByBlockHeightKey(blockHeight), bz...)
    67  }
    68  
    69  // SplitWaitingProposalQueueKey split the active proposal key and returns the proposal id and endTime
    70  func SplitWaitingProposalQueueKey(key []byte) (proposalID uint64, height uint64) {
    71  	return splitKeyWithHeight(key)
    72  }
    73  
    74  func splitKeyWithHeight(key []byte) (proposalID uint64, height uint64) {
    75  	// 16 is sum of proposalID length and height length
    76  	if len(key[1:]) != 16 {
    77  		panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key[1:]), 16))
    78  	}
    79  
    80  	height = binary.BigEndian.Uint64(key[1 : 1+8])
    81  	proposalID = binary.LittleEndian.Uint64(key[1+8:])
    82  	return
    83  }
    84  
    85  var lenTime = len(sdk.FormatTimeBytes(time.Now()))
    86  
    87  // ProposalKey gets a specific proposal from the store
    88  func ProposalKey(proposalID uint64) []byte {
    89  	bz := make([]byte, 8)
    90  	binary.LittleEndian.PutUint64(bz, proposalID)
    91  	return append(ProposalsKeyPrefix, bz...)
    92  }
    93  
    94  // ActiveProposalByTimeKey gets the active proposal queue key by endTime
    95  func ActiveProposalByTimeKey(endTime time.Time) []byte {
    96  	return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
    97  }
    98  
    99  // ActiveProposalQueueKey returns the key for a proposalID in the activeProposalQueue
   100  func ActiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
   101  	bz := make([]byte, 8)
   102  	binary.LittleEndian.PutUint64(bz, proposalID)
   103  
   104  	return append(ActiveProposalByTimeKey(endTime), bz...)
   105  }
   106  
   107  // InactiveProposalByTimeKey gets the inactive proposal queue key by endTime
   108  func InactiveProposalByTimeKey(endTime time.Time) []byte {
   109  	return append(InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
   110  }
   111  
   112  // InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue
   113  func InactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
   114  	bz := make([]byte, 8)
   115  	binary.LittleEndian.PutUint64(bz, proposalID)
   116  
   117  	return append(InactiveProposalByTimeKey(endTime), bz...)
   118  }
   119  
   120  // DepositsKey gets the first part of the deposits key based on the proposalID
   121  func DepositsKey(proposalID uint64) []byte {
   122  	bz := make([]byte, 8)
   123  	binary.LittleEndian.PutUint64(bz, proposalID)
   124  	return append(DepositsKeyPrefix, bz...)
   125  }
   126  
   127  // DepositKey key of a specific deposit from the store
   128  func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
   129  	return append(DepositsKey(proposalID), depositorAddr.Bytes()...)
   130  }
   131  
   132  // VotesKey gets the first part of the votes key based on the proposalID
   133  func VotesKey(proposalID uint64) []byte {
   134  	bz := make([]byte, 8)
   135  	binary.LittleEndian.PutUint64(bz, proposalID)
   136  	return append(VotesKeyPrefix, bz...)
   137  }
   138  
   139  // VoteKey key of a specific vote from the store
   140  func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
   141  	return append(VotesKey(proposalID), voterAddr.Bytes()...)
   142  }
   143  
   144  // Split keys function; used for iterators
   145  
   146  // SplitProposalKey split the proposal key and returns the proposal id
   147  func SplitProposalKey(key []byte) (proposalID uint64) {
   148  	if len(key[1:]) != 8 {
   149  		panic(fmt.Sprintf("unexpected key length (%d ≠ 8)", len(key[1:])))
   150  	}
   151  
   152  	return binary.LittleEndian.Uint64(key[1:])
   153  }
   154  
   155  // SplitActiveProposalQueueKey split the active proposal key and returns the proposal id and endTime
   156  func SplitActiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   157  	return splitKeyWithTime(key)
   158  }
   159  
   160  // SplitInactiveProposalQueueKey split the inactive proposal key and returns the proposal id and endTime
   161  func SplitInactiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   162  	return splitKeyWithTime(key)
   163  }
   164  
   165  // SplitKeyDeposit split the deposits key and returns the proposal id and depositor address
   166  func SplitKeyDeposit(key []byte) (proposalID uint64, depositorAddr sdk.AccAddress) {
   167  	return splitKeyWithAddress(key)
   168  }
   169  
   170  // SplitKeyVote split the votes key and returns the proposal id and voter address
   171  func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) {
   172  	return splitKeyWithAddress(key)
   173  }
   174  
   175  // private functions
   176  
   177  func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) {
   178  	if len(key[1:]) != 8+lenTime {
   179  		panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key[1:]), lenTime+8))
   180  	}
   181  
   182  	endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime])
   183  	if err != nil {
   184  		panic(err)
   185  	}
   186  	proposalID = binary.LittleEndian.Uint64(key[1+lenTime:])
   187  	return
   188  }
   189  
   190  func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
   191  	if len(key[1:]) != 8+sdk.AddrLen {
   192  		panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key), 8+sdk.AddrLen))
   193  	}
   194  
   195  	proposalID = binary.LittleEndian.Uint64(key[1:9])
   196  	addr = sdk.AccAddress(key[9:])
   197  	return
   198  }