github.com/onflow/flow-go@v0.33.17/storage/badger/operation/prefix.go (about)

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