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

     1  package types
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  	"github.com/Finschia/finschia-sdk/types/address"
     8  	"github.com/Finschia/finschia-sdk/types/kv"
     9  )
    10  
    11  const (
    12  	// ModuleName is the module name constant used in many places
    13  	ModuleName = "fbridge"
    14  
    15  	// StoreKey is the store key string for fbridge
    16  	StoreKey = ModuleName
    17  
    18  	// RouterKey is the message route for fbridge
    19  	RouterKey = ModuleName
    20  
    21  	// QuerierRoute defines the module's query routing key
    22  	QuerierRoute = ModuleName
    23  
    24  	// MemStoreKey is the in-memory store key string for fbridge
    25  	MemStoreKey = "mem_" + StoreKey
    26  )
    27  
    28  // - 0x01: params
    29  // - 0x02: next sequence number for bridge sending
    30  // - 0x03<sequence (8-byte)>: block number of sequence
    31  //
    32  // - 0x10: next proposal ID
    33  // 	 0x11<proposalID (8-byte)>: proposal
    34  //   0x12<proposalID (8-byte)><voterAddrLen (1-byte)><voterAddr>: vote
    35  // - 0x13<addrLen (1-byte)><targetAddr>: role
    36  // - 0x14<addrLen (1-byte)><guardianAddr>: bridge switch
    37  //
    38  // - 0xF0: memstore initialized
    39  // - 0xF1: role metadata
    40  // - 0xF2: bridge status
    41  
    42  var (
    43  	KeyParams              = []byte{0x01} // key for fbridge module params
    44  	KeyNextSeqSend         = []byte{0x02} // key for the next bridge send sequence
    45  	KeySeqToBlocknumPrefix = []byte{0x03} // key prefix for the sequence to block number mapping
    46  
    47  	KeyNextProposalID     = []byte{0x10} // key for the next role proposal ID
    48  	KeyProposalPrefix     = []byte{0x11} // key prefix for the role proposal
    49  	KeyProposalVotePrefix = []byte{0x12} // key prefix for the role proposal vote
    50  	KeyRolePrefix         = []byte{0x13} // key prefix for the role of an address
    51  	KeyBridgeSwitchPrefix = []byte{0x14} // key for the switch to halt
    52  
    53  	KeyMemInitialized           = []byte{0xF0}
    54  	KeyMemRoleMetadata          = []byte{0xF1} // key for the role metadata
    55  	KeyMemBridgeInactiveCounter = []byte{0xF2} // key for the bridge inactive status
    56  )
    57  
    58  func SeqToBlocknumKey(seq uint64) []byte {
    59  	bz := make([]byte, 8)
    60  	binary.BigEndian.PutUint64(bz, seq)
    61  	return append(KeySeqToBlocknumPrefix, bz...)
    62  }
    63  
    64  // GetProposalIDBytes returns the byte representation of the proposalID
    65  func GetProposalIDBytes(proposalID uint64) []byte {
    66  	bz := make([]byte, 8)
    67  	binary.BigEndian.PutUint64(bz, proposalID)
    68  	return bz
    69  }
    70  
    71  // ProposalKey key of a specific role proposal
    72  func ProposalKey(proposalID uint64) []byte {
    73  	return append(KeyProposalPrefix, GetProposalIDBytes(proposalID)...)
    74  }
    75  
    76  // VotesKey gets the first part of the votes key based on the proposalID
    77  func VotesKey(proposalID uint64) []byte {
    78  	return append(KeyProposalVotePrefix, GetProposalIDBytes(proposalID)...)
    79  }
    80  
    81  // VoterVoteKey key of a specific vote from the store
    82  func VoterVoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
    83  	return append(VotesKey(proposalID), address.MustLengthPrefix(voterAddr.Bytes())...)
    84  }
    85  
    86  // SplitVoterVoteKey split the voter key and returns the proposal id and voter address
    87  func SplitVoterVoteKey(key []byte) (uint64, sdk.AccAddress) {
    88  	kv.AssertKeyAtLeastLength(key, 11)
    89  	proposalID := binary.BigEndian.Uint64(key[1:9])
    90  	voter := sdk.AccAddress(key[10:])
    91  	return proposalID, voter
    92  }
    93  
    94  // RoleKey key of a specific role of the address from the store
    95  func RoleKey(target sdk.AccAddress) []byte {
    96  	return append(KeyRolePrefix, address.MustLengthPrefix(target.Bytes())...)
    97  }
    98  
    99  // SplitRoleKey split the role key and returns the address
   100  func SplitRoleKey(key []byte) sdk.AccAddress {
   101  	kv.AssertKeyAtLeastLength(key, 3)
   102  	return key[2:]
   103  }
   104  
   105  func BridgeSwitchKey(guardian sdk.AccAddress) []byte {
   106  	return append(KeyBridgeSwitchPrefix, address.MustLengthPrefix(guardian.Bytes())...)
   107  }
   108  
   109  // SplitBridgeSwitchKey split the bridge switch key and returns the guardian address
   110  func SplitBridgeSwitchKey(key []byte) sdk.AccAddress {
   111  	kv.AssertKeyAtLeastLength(key, 3)
   112  	return key[2:]
   113  }