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

     1  package types
     2  
     3  import (
     4  	"encoding/binary"
     5  	"time"
     6  
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  	"github.com/Finschia/finschia-sdk/types/address"
     9  	"github.com/Finschia/finschia-sdk/types/kv"
    10  )
    11  
    12  const (
    13  	// ModuleName is the name of the module
    14  	ModuleName = "gov"
    15  
    16  	// StoreKey is the store key string for gov
    17  	StoreKey = ModuleName
    18  
    19  	// RouterKey is the message route for gov
    20  	RouterKey = ModuleName
    21  
    22  	// QuerierRoute is the querier route for gov
    23  	QuerierRoute = ModuleName
    24  )
    25  
    26  // Keys for governance store
    27  // Items are stored with the following key: values
    28  //
    29  // - 0x00<proposalID_Bytes>: Proposal
    30  //
    31  // - 0x01<endTime_Bytes><proposalID_Bytes>: activeProposalID
    32  //
    33  // - 0x02<endTime_Bytes><proposalID_Bytes>: inactiveProposalID
    34  //
    35  // - 0x03: nextProposalID
    36  //
    37  // - 0x10<proposalID_Bytes><depositorAddrLen (1 Byte)><depositorAddr_Bytes>: Deposit
    38  //
    39  // - 0x20<proposalID_Bytes><voterAddrLen (1 Byte)><voterAddr_Bytes>: Voter
    40  var (
    41  	ProposalsKeyPrefix          = []byte{0x00}
    42  	ActiveProposalQueuePrefix   = []byte{0x01}
    43  	InactiveProposalQueuePrefix = []byte{0x02}
    44  	ProposalIDKey               = []byte{0x03}
    45  
    46  	DepositsKeyPrefix = []byte{0x10}
    47  
    48  	VotesKeyPrefix = []byte{0x20}
    49  )
    50  
    51  var lenTime = len(sdk.FormatTimeBytes(time.Now()))
    52  
    53  // GetProposalIDBytes returns the byte representation of the proposalID
    54  func GetProposalIDBytes(proposalID uint64) (proposalIDBz []byte) {
    55  	proposalIDBz = make([]byte, 8)
    56  	binary.BigEndian.PutUint64(proposalIDBz, proposalID)
    57  	return
    58  }
    59  
    60  // GetProposalIDFromBytes returns proposalID in uint64 format from a byte array
    61  func GetProposalIDFromBytes(bz []byte) (proposalID uint64) {
    62  	return binary.BigEndian.Uint64(bz)
    63  }
    64  
    65  // ProposalKey gets a specific proposal from the store
    66  func ProposalKey(proposalID uint64) []byte {
    67  	return append(ProposalsKeyPrefix, GetProposalIDBytes(proposalID)...)
    68  }
    69  
    70  // ActiveProposalByTimeKey gets the active proposal queue key by endTime
    71  func ActiveProposalByTimeKey(endTime time.Time) []byte {
    72  	return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
    73  }
    74  
    75  // ActiveProposalQueueKey returns the key for a proposalID in the activeProposalQueue
    76  func ActiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
    77  	return append(ActiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
    78  }
    79  
    80  // InactiveProposalByTimeKey gets the inactive proposal queue key by endTime
    81  func InactiveProposalByTimeKey(endTime time.Time) []byte {
    82  	return append(InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
    83  }
    84  
    85  // InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue
    86  func InactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
    87  	return append(InactiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
    88  }
    89  
    90  // DepositsKey gets the first part of the deposits key based on the proposalID
    91  func DepositsKey(proposalID uint64) []byte {
    92  	return append(DepositsKeyPrefix, GetProposalIDBytes(proposalID)...)
    93  }
    94  
    95  // DepositKey key of a specific deposit from the store
    96  func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
    97  	return append(DepositsKey(proposalID), address.MustLengthPrefix(depositorAddr.Bytes())...)
    98  }
    99  
   100  // VotesKey gets the first part of the votes key based on the proposalID
   101  func VotesKey(proposalID uint64) []byte {
   102  	return append(VotesKeyPrefix, GetProposalIDBytes(proposalID)...)
   103  }
   104  
   105  // VoteKey key of a specific vote from the store
   106  func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
   107  	return append(VotesKey(proposalID), address.MustLengthPrefix(voterAddr.Bytes())...)
   108  }
   109  
   110  // Split keys function; used for iterators
   111  
   112  // SplitProposalKey split the proposal key and returns the proposal id
   113  func SplitProposalKey(key []byte) (proposalID uint64) {
   114  	kv.AssertKeyLength(key[1:], 8)
   115  
   116  	return GetProposalIDFromBytes(key[1:])
   117  }
   118  
   119  // SplitActiveProposalQueueKey split the active proposal key and returns the proposal id and endTime
   120  func SplitActiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   121  	return splitKeyWithTime(key)
   122  }
   123  
   124  // SplitInactiveProposalQueueKey split the inactive proposal key and returns the proposal id and endTime
   125  func SplitInactiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   126  	return splitKeyWithTime(key)
   127  }
   128  
   129  // SplitKeyDeposit split the deposits key and returns the proposal id and depositor address
   130  func SplitKeyDeposit(key []byte) (proposalID uint64, depositorAddr sdk.AccAddress) {
   131  	return splitKeyWithAddress(key)
   132  }
   133  
   134  // SplitKeyVote split the votes key and returns the proposal id and voter address
   135  func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) {
   136  	return splitKeyWithAddress(key)
   137  }
   138  
   139  // private functions
   140  
   141  func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) {
   142  	kv.AssertKeyLength(key[1:], 8+lenTime)
   143  
   144  	endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime])
   145  	if err != nil {
   146  		panic(err)
   147  	}
   148  
   149  	proposalID = GetProposalIDFromBytes(key[1+lenTime:])
   150  	return
   151  }
   152  
   153  func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
   154  	// Both Vote and Deposit store keys are of format:
   155  	// <prefix (1 Byte)><proposalID (8 bytes)><addrLen (1 Byte)><addr_Bytes>
   156  	kv.AssertKeyAtLeastLength(key, 10)
   157  	proposalID = GetProposalIDFromBytes(key[1:9])
   158  	kv.AssertKeyAtLeastLength(key, 11)
   159  	addr = sdk.AccAddress(key[10:])
   160  	return
   161  }