github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/prefix.go (about)

     1  package operation
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  
     7  	"github.com/onflow/flow-go/model/flow"
     8  )
     9  
    10  const (
    11  
    12  	// codes for special database markers
    13  	codeMax    = 1 // keeps track of the maximum key size
    14  	codeDBType = 2 // specifies a database type
    15  
    16  	// codes for views with special meaning
    17  	codeSafetyData   = 10 // safety data for hotstuff state
    18  	codeLivenessData = 11 // liveness data for hotstuff state
    19  
    20  	// codes for fields associated with the root state
    21  	codeSporkID                    = 13
    22  	codeProtocolVersion            = 14
    23  	codeEpochCommitSafetyThreshold = 15
    24  	codeSporkRootBlockHeight       = 16
    25  
    26  	// code for heights with special meaning
    27  	codeFinalizedHeight         = 20 // latest finalized block height
    28  	codeSealedHeight            = 21 // latest sealed block height
    29  	codeClusterHeight           = 22 // latest finalized height on cluster
    30  	codeExecutedBlock           = 23 // latest executed block with max height
    31  	codeFinalizedRootHeight     = 24 // the height of the highest finalized block contained in the root snapshot
    32  	codeLastCompleteBlockHeight = 25 // the height of the last block for which all collections were received
    33  	codeEpochFirstHeight        = 26 // the height of the first block in a given epoch
    34  	codeSealedRootHeight        = 27 // the height of the highest sealed block contained in the root snapshot
    35  
    36  	// codes for single entity storage
    37  	codeHeader               = 30
    38  	_                        = 31 // DEPRECATED: 31 was used for identities before epochs
    39  	codeGuarantee            = 32
    40  	codeSeal                 = 33
    41  	codeTransaction          = 34
    42  	codeCollection           = 35
    43  	codeExecutionResult      = 36
    44  	codeResultApproval       = 37
    45  	codeChunk                = 38
    46  	codeExecutionReceiptMeta = 39 // NOTE: prior to Mainnet25, this erroneously had the same value as codeExecutionResult (36)
    47  
    48  	// codes for indexing single identifier by identifier/integer
    49  	codeHeightToBlock              = 40 // index mapping height to block ID
    50  	codeBlockIDToLatestSealID      = 41 // index mapping a block its last payload seal
    51  	codeClusterBlockToRefBlock     = 42 // index cluster block ID to reference block ID
    52  	codeRefHeightToClusterBlock    = 43 // index reference block height to cluster block IDs
    53  	codeBlockIDToFinalizedSeal     = 44 // index _finalized_ seal by sealed block ID
    54  	codeBlockIDToQuorumCertificate = 45 // index of quorum certificates by block ID
    55  	codeProtocolStateByBlockID     = 46 // index of protocol state entry by block ID
    56  	codeProtocolKVStoreByBlockID   = 47 // index of protocol KV store entry by block ID
    57  
    58  	// codes for indexing multiple identifiers by identifier
    59  	codeBlockChildren          = 50 // index mapping block ID to children blocks
    60  	_                          = 51 // DEPRECATED: 51 was used for identity indexes before epochs
    61  	codePayloadGuarantees      = 52 // index mapping block ID to payload guarantees
    62  	codePayloadSeals           = 53 // index mapping block ID to payload seals
    63  	codeCollectionBlock        = 54 // index mapping collection ID to block ID
    64  	codeOwnBlockReceipt        = 55 // index mapping block ID to execution receipt ID for execution nodes
    65  	_                          = 56 // DEPRECATED: 56 was used for block->epoch status prior to Dynamic Protocol State in Mainnet25
    66  	codePayloadReceipts        = 57 // index mapping block ID to payload receipts
    67  	codePayloadResults         = 58 // index mapping block ID to payload results
    68  	codeAllBlockReceipts       = 59 // index mapping of blockID to multiple receipts
    69  	codePayloadProtocolStateID = 60 // index mapping block ID to payload protocol state ID
    70  
    71  	// codes related to protocol level information
    72  	codeEpochSetup       = 61 // EpochSetup service event, keyed by ID
    73  	codeEpochCommit      = 62 // EpochCommit service event, keyed by ID
    74  	codeBeaconPrivateKey = 63 // BeaconPrivateKey, keyed by epoch counter
    75  	codeDKGStarted       = 64 // flag that the DKG for an epoch has been started
    76  	codeDKGEnded         = 65 // flag that the DKG for an epoch has ended (stores end state)
    77  	codeVersionBeacon    = 67 // flag for storing version beacons
    78  	codeProtocolState    = 68
    79  	codeProtocolKVStore  = 69
    80  
    81  	// code for ComputationResult upload status storage
    82  	// NOTE: for now only GCP uploader is supported. When other uploader (AWS e.g.) needs to
    83  	//		 be supported, we will need to define new code.
    84  	codeComputationResults = 66
    85  
    86  	// job queue consumers and producers
    87  	codeJobConsumerProcessed = 70
    88  	codeJobQueue             = 71
    89  	codeJobQueuePointer      = 72
    90  
    91  	// legacy codes (should be cleaned up)
    92  	codeChunkDataPack                = 100
    93  	codeCommit                       = 101
    94  	codeEvent                        = 102
    95  	codeExecutionStateInteractions   = 103
    96  	codeTransactionResult            = 104
    97  	codeFinalizedCluster             = 105
    98  	codeServiceEvent                 = 106
    99  	codeTransactionResultIndex       = 107
   100  	codeLightTransactionResult       = 108
   101  	codeLightTransactionResultIndex  = 109
   102  	codeIndexCollection              = 200
   103  	codeIndexExecutionResultByBlock  = 202
   104  	codeIndexCollectionByTransaction = 203
   105  	codeIndexResultApprovalByChunk   = 204
   106  
   107  	// TEMPORARY codes
   108  	blockedNodeIDs = 205 // manual override for adding node IDs to list of ejected nodes, applies to networking layer only
   109  
   110  	// internal failure information that should be preserved across restarts
   111  	codeExecutionFork                   = 254
   112  	codeEpochEmergencyFallbackTriggered = 255
   113  )
   114  
   115  func makePrefix(code byte, keys ...interface{}) []byte {
   116  	prefix := make([]byte, 1)
   117  	prefix[0] = code
   118  	for _, key := range keys {
   119  		prefix = append(prefix, b(key)...)
   120  	}
   121  	return prefix
   122  }
   123  
   124  func b(v interface{}) []byte {
   125  	switch i := v.(type) {
   126  	case uint8:
   127  		return []byte{i}
   128  	case uint32:
   129  		b := make([]byte, 4)
   130  		binary.BigEndian.PutUint32(b, i)
   131  		return b
   132  	case uint64:
   133  		b := make([]byte, 8)
   134  		binary.BigEndian.PutUint64(b, i)
   135  		return b
   136  	case string:
   137  		return []byte(i)
   138  	case flow.Role:
   139  		return []byte{byte(i)}
   140  	case flow.Identifier:
   141  		return i[:]
   142  	case flow.ChainID:
   143  		return []byte(i)
   144  	default:
   145  		panic(fmt.Sprintf("unsupported type to convert (%T)", v))
   146  	}
   147  }