github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/migrations/v1/types.go (about)

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