github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/db/iface/interface.go (about) 1 // Package iface defines the actual database interface used 2 // by a Prysm beacon node, also containing useful, scoped interfaces such as 3 // a ReadOnlyDatabase. 4 package iface 5 6 import ( 7 "context" 8 "io" 9 10 "github.com/ethereum/go-ethereum/common" 11 types "github.com/prysmaticlabs/eth2-types" 12 "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" 13 slashertypes "github.com/prysmaticlabs/prysm/beacon-chain/slasher/types" 14 iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" 15 "github.com/prysmaticlabs/prysm/proto/beacon/db" 16 ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 17 eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 18 "github.com/prysmaticlabs/prysm/proto/interfaces" 19 "github.com/prysmaticlabs/prysm/shared/backuputil" 20 ) 21 22 // ReadOnlyDatabase defines a struct which only has read access to database methods. 23 type ReadOnlyDatabase interface { 24 // Block related methods. 25 Block(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) 26 Blocks(ctx context.Context, f *filters.QueryFilter) ([]interfaces.SignedBeaconBlock, [][32]byte, error) 27 BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][32]byte, error) 28 BlocksBySlot(ctx context.Context, slot types.Slot) (bool, []interfaces.SignedBeaconBlock, error) 29 BlockRootsBySlot(ctx context.Context, slot types.Slot) (bool, [][32]byte, error) 30 HasBlock(ctx context.Context, blockRoot [32]byte) bool 31 GenesisBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) 32 IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool 33 FinalizedChildBlock(ctx context.Context, blockRoot [32]byte) (interfaces.SignedBeaconBlock, error) 34 HighestSlotBlocksBelow(ctx context.Context, slot types.Slot) ([]interfaces.SignedBeaconBlock, error) 35 // State related methods. 36 State(ctx context.Context, blockRoot [32]byte) (iface.BeaconState, error) 37 GenesisState(ctx context.Context) (iface.BeaconState, error) 38 HasState(ctx context.Context, blockRoot [32]byte) bool 39 StateSummary(ctx context.Context, blockRoot [32]byte) (*ethereum_beacon_p2p_v1.StateSummary, error) 40 HasStateSummary(ctx context.Context, blockRoot [32]byte) bool 41 HighestSlotStatesBelow(ctx context.Context, slot types.Slot) ([]iface.ReadOnlyBeaconState, error) 42 // Slashing operations. 43 ProposerSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.ProposerSlashing, error) 44 AttesterSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.AttesterSlashing, error) 45 HasProposerSlashing(ctx context.Context, slashingRoot [32]byte) bool 46 HasAttesterSlashing(ctx context.Context, slashingRoot [32]byte) bool 47 // Block operations. 48 VoluntaryExit(ctx context.Context, exitRoot [32]byte) (*eth.VoluntaryExit, error) 49 HasVoluntaryExit(ctx context.Context, exitRoot [32]byte) bool 50 // Checkpoint operations. 51 JustifiedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) 52 FinalizedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) 53 ArchivedPointRoot(ctx context.Context, slot types.Slot) [32]byte 54 HasArchivedPoint(ctx context.Context, slot types.Slot) bool 55 LastArchivedRoot(ctx context.Context) [32]byte 56 LastArchivedSlot(ctx context.Context) (types.Slot, error) 57 // Deposit contract related handlers. 58 DepositContractAddress(ctx context.Context) ([]byte, error) 59 // Powchain operations. 60 PowchainData(ctx context.Context) (*db.ETH1ChainData, error) 61 } 62 63 // NoHeadAccessDatabase defines a struct without access to chain head data. 64 type NoHeadAccessDatabase interface { 65 ReadOnlyDatabase 66 67 // Block related methods. 68 SaveBlock(ctx context.Context, block interfaces.SignedBeaconBlock) error 69 SaveBlocks(ctx context.Context, blocks []interfaces.SignedBeaconBlock) error 70 SaveGenesisBlockRoot(ctx context.Context, blockRoot [32]byte) error 71 // State related methods. 72 SaveState(ctx context.Context, state iface.ReadOnlyBeaconState, blockRoot [32]byte) error 73 SaveStates(ctx context.Context, states []iface.ReadOnlyBeaconState, blockRoots [][32]byte) error 74 DeleteState(ctx context.Context, blockRoot [32]byte) error 75 DeleteStates(ctx context.Context, blockRoots [][32]byte) error 76 SaveStateSummary(ctx context.Context, summary *ethereum_beacon_p2p_v1.StateSummary) error 77 SaveStateSummaries(ctx context.Context, summaries []*ethereum_beacon_p2p_v1.StateSummary) error 78 // Slashing operations. 79 SaveProposerSlashing(ctx context.Context, slashing *eth.ProposerSlashing) error 80 SaveAttesterSlashing(ctx context.Context, slashing *eth.AttesterSlashing) error 81 // Block operations. 82 SaveVoluntaryExit(ctx context.Context, exit *eth.VoluntaryExit) error 83 // Checkpoint operations. 84 SaveJustifiedCheckpoint(ctx context.Context, checkpoint *eth.Checkpoint) error 85 SaveFinalizedCheckpoint(ctx context.Context, checkpoint *eth.Checkpoint) error 86 // Deposit contract related handlers. 87 SaveDepositContractAddress(ctx context.Context, addr common.Address) error 88 // Powchain operations. 89 SavePowchainData(ctx context.Context, data *db.ETH1ChainData) error 90 // Run any required database migrations. 91 RunMigrations(ctx context.Context) error 92 93 CleanUpDirtyStates(ctx context.Context, slotsPerArchivedPoint types.Slot) error 94 } 95 96 // HeadAccessDatabase defines a struct with access to reading chain head data. 97 type HeadAccessDatabase interface { 98 NoHeadAccessDatabase 99 100 // Block related methods. 101 HeadBlock(ctx context.Context) (interfaces.SignedBeaconBlock, error) 102 SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error 103 104 // Genesis operations. 105 LoadGenesis(ctx context.Context, r io.Reader) error 106 SaveGenesisData(ctx context.Context, state iface.BeaconState) error 107 EnsureEmbeddedGenesis(ctx context.Context) error 108 } 109 110 // SlasherDatabase interface for persisting data related to detecting slashable offenses on Ethereum. 111 type SlasherDatabase interface { 112 io.Closer 113 SaveLastEpochWrittenForValidators( 114 ctx context.Context, validatorIndices []types.ValidatorIndex, epoch types.Epoch, 115 ) error 116 SaveAttestationRecordsForValidators( 117 ctx context.Context, 118 attestations []*slashertypes.IndexedAttestationWrapper, 119 ) error 120 SaveSlasherChunks( 121 ctx context.Context, kind slashertypes.ChunkKind, chunkKeys [][]byte, chunks [][]uint16, 122 ) error 123 SaveBlockProposals( 124 ctx context.Context, proposal []*slashertypes.SignedBlockHeaderWrapper, 125 ) error 126 LastEpochWrittenForValidators( 127 ctx context.Context, validatorIndices []types.ValidatorIndex, 128 ) ([]*slashertypes.AttestedEpochForValidator, error) 129 AttestationRecordForValidator( 130 ctx context.Context, validatorIdx types.ValidatorIndex, targetEpoch types.Epoch, 131 ) (*slashertypes.IndexedAttestationWrapper, error) 132 CheckAttesterDoubleVotes( 133 ctx context.Context, attestations []*slashertypes.IndexedAttestationWrapper, 134 ) ([]*slashertypes.AttesterDoubleVote, error) 135 LoadSlasherChunks( 136 ctx context.Context, kind slashertypes.ChunkKind, diskKeys [][]byte, 137 ) ([][]uint16, []bool, error) 138 CheckDoubleBlockProposals( 139 ctx context.Context, proposals []*slashertypes.SignedBlockHeaderWrapper, 140 ) ([]*eth.ProposerSlashing, error) 141 PruneAttestations( 142 ctx context.Context, currentEpoch, pruningEpochIncrements, historyLength types.Epoch, 143 ) error 144 PruneProposals( 145 ctx context.Context, currentEpoch, pruningEpochIncrements, historyLength types.Epoch, 146 ) error 147 DatabasePath() string 148 ClearDB() error 149 } 150 151 // Database interface with full access. 152 type Database interface { 153 io.Closer 154 backuputil.BackupExporter 155 HeadAccessDatabase 156 157 DatabasePath() string 158 ClearDB() error 159 }