github.com/klaytn/klaytn@v1.12.1/storage/database/db_manager_stakinginfo.go (about) 1 // Copyright 2020 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package database 18 19 // HasStakingInfo returns existence of staking information of the given block number from database. 20 func (dbm *databaseManager) HasStakingInfo(blockNum uint64) (bool, error) { 21 db := dbm.getDatabase(MiscDB) 22 23 key := makeKey(stakingInfoPrefix, blockNum) 24 return db.Has(key) 25 } 26 27 // ReadStakingInfo reads staking information from database. It returns 28 // (StakingInfo, nil) if it succeeds to read and (nil, error) if it fails. 29 // StakingInfo is stored in MiscDB. 30 // Be sure to use the right block number before calling this function. 31 // (Refer to CalcStakingBlockNumber() in params/governance_params.go) 32 func (dbm *databaseManager) ReadStakingInfo(blockNum uint64) ([]byte, error) { 33 db := dbm.getDatabase(MiscDB) 34 35 key := makeKey(stakingInfoPrefix, blockNum) 36 stakingInfo, err := db.Get(key) 37 if err != nil { 38 return nil, err 39 } 40 41 return stakingInfo, nil 42 } 43 44 // WriteStakingInfo writes staking information to database. It returns 45 // nil if it succeeds to write and error if it fails. 46 // Key should be the blockNum of stakingIfo. Value is marshaled 47 // stakingInfo. StakingInfo is stored in MiscDB. 48 // stakingInfo should be type StakingInfo defined in reward/staking_info.go 49 // Be sure to use the right block number before calling this function. 50 // (Refer to CalcStakingBlockNumber() in params/governance_params.go) 51 func (dbm *databaseManager) WriteStakingInfo(blockNum uint64, stakingInfo []byte) error { 52 db := dbm.getDatabase(MiscDB) 53 54 key := makeKey(stakingInfoPrefix, blockNum) 55 return db.Put(key, stakingInfo) 56 } 57 58 func (dbm *databaseManager) DeleteStakingInfo(blockNum uint64) { 59 db := dbm.getDatabase(MiscDB) 60 61 key := makeKey(stakingInfoPrefix, blockNum) 62 if err := db.Delete(key); err != nil { 63 logger.Crit("Failed to delete staking info", "err", err) 64 } 65 }