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

     1  // Package v040 is copy-pasted from:
     2  // https://github.com/cosmos/cosmos-sdk/blob/v0.41.0/x/gov/types/keys.go
     3  package v040
     4  
     5  import (
     6  	"encoding/binary"
     7  	"time"
     8  
     9  	sdk "github.com/Finschia/finschia-sdk/types"
    10  	"github.com/Finschia/finschia-sdk/types/kv"
    11  	v040auth "github.com/Finschia/finschia-sdk/x/auth/legacy/v040"
    12  )
    13  
    14  const (
    15  	// ModuleName is the name of the module
    16  	ModuleName = "gov"
    17  
    18  	// StoreKey is the store key string for gov
    19  	StoreKey = ModuleName
    20  
    21  	// RouterKey is the message route for gov
    22  	RouterKey = ModuleName
    23  
    24  	// QuerierRoute is the querier route for gov
    25  	QuerierRoute = 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  
    53  var lenTime = len(sdk.FormatTimeBytes(time.Now()))
    54  
    55  // GetProposalIDBytes returns the byte representation of the proposalID
    56  func GetProposalIDBytes(proposalID uint64) (proposalIDBz []byte) {
    57  	proposalIDBz = make([]byte, 8)
    58  	binary.BigEndian.PutUint64(proposalIDBz, proposalID)
    59  	return
    60  }
    61  
    62  // GetProposalIDFromBytes returns proposalID in uint64 format from a byte array
    63  func GetProposalIDFromBytes(bz []byte) (proposalID uint64) {
    64  	return binary.BigEndian.Uint64(bz)
    65  }
    66  
    67  // ProposalKey gets a specific proposal from the store
    68  func ProposalKey(proposalID uint64) []byte {
    69  	return append(ProposalsKeyPrefix, GetProposalIDBytes(proposalID)...)
    70  }
    71  
    72  // ActiveProposalByTimeKey gets the active proposal queue key by endTime
    73  func ActiveProposalByTimeKey(endTime time.Time) []byte {
    74  	return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
    75  }
    76  
    77  // ActiveProposalQueueKey returns the key for a proposalID in the activeProposalQueue
    78  func ActiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
    79  	return append(ActiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
    80  }
    81  
    82  // InactiveProposalByTimeKey gets the inactive proposal queue key by endTime
    83  func InactiveProposalByTimeKey(endTime time.Time) []byte {
    84  	return append(InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
    85  }
    86  
    87  // InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue
    88  func InactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
    89  	return append(InactiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
    90  }
    91  
    92  // DepositsKey gets the first part of the deposits key based on the proposalID
    93  func DepositsKey(proposalID uint64) []byte {
    94  	return append(DepositsKeyPrefix, GetProposalIDBytes(proposalID)...)
    95  }
    96  
    97  // DepositKey key of a specific deposit from the store
    98  func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
    99  	return append(DepositsKey(proposalID), depositorAddr.Bytes()...)
   100  }
   101  
   102  // VotesKey gets the first part of the votes key based on the proposalID
   103  func VotesKey(proposalID uint64) []byte {
   104  	return append(VotesKeyPrefix, GetProposalIDBytes(proposalID)...)
   105  }
   106  
   107  // VoteKey key of a specific vote from the store
   108  func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
   109  	return append(VotesKey(proposalID), voterAddr.Bytes()...)
   110  }
   111  
   112  // Split keys function; used for iterators
   113  
   114  // SplitProposalKey split the proposal key and returns the proposal id
   115  func SplitProposalKey(key []byte) (proposalID uint64) {
   116  	kv.AssertKeyLength(key[1:], 8)
   117  
   118  	return GetProposalIDFromBytes(key[1:])
   119  }
   120  
   121  // SplitActiveProposalQueueKey split the active proposal key and returns the proposal id and endTime
   122  func SplitActiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   123  	return splitKeyWithTime(key)
   124  }
   125  
   126  // SplitInactiveProposalQueueKey split the inactive proposal key and returns the proposal id and endTime
   127  func SplitInactiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
   128  	return splitKeyWithTime(key)
   129  }
   130  
   131  // SplitKeyDeposit split the deposits key and returns the proposal id and depositor address
   132  func SplitKeyDeposit(key []byte) (proposalID uint64, depositorAddr sdk.AccAddress) {
   133  	return splitKeyWithAddress(key)
   134  }
   135  
   136  // SplitKeyVote split the votes key and returns the proposal id and voter address
   137  func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) {
   138  	return splitKeyWithAddress(key)
   139  }
   140  
   141  // private functions
   142  
   143  func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) {
   144  	kv.AssertKeyLength(key[1:], 8+lenTime)
   145  
   146  	endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime])
   147  	if err != nil {
   148  		panic(err)
   149  	}
   150  
   151  	proposalID = GetProposalIDFromBytes(key[1+lenTime:])
   152  	return
   153  }
   154  
   155  func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
   156  	kv.AssertKeyLength(key[1:], 8+v040auth.AddrLen)
   157  
   158  	kv.AssertKeyAtLeastLength(key, 10)
   159  	proposalID = GetProposalIDFromBytes(key[1:9])
   160  	addr = sdk.AccAddress(key[9:])
   161  	return
   162  }