github.com/theQRL/go-zond@v0.1.1/core/rawdb/accessors_sync.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"github.com/theQRL/go-zond/core/types"
    21  	"github.com/theQRL/go-zond/log"
    22  	"github.com/theQRL/go-zond/rlp"
    23  	"github.com/theQRL/go-zond/zonddb"
    24  )
    25  
    26  // ReadSkeletonSyncStatus retrieves the serialized sync status saved at shutdown.
    27  func ReadSkeletonSyncStatus(db zonddb.KeyValueReader) []byte {
    28  	data, _ := db.Get(skeletonSyncStatusKey)
    29  	return data
    30  }
    31  
    32  // WriteSkeletonSyncStatus stores the serialized sync status to save at shutdown.
    33  func WriteSkeletonSyncStatus(db zonddb.KeyValueWriter, status []byte) {
    34  	if err := db.Put(skeletonSyncStatusKey, status); err != nil {
    35  		log.Crit("Failed to store skeleton sync status", "err", err)
    36  	}
    37  }
    38  
    39  // DeleteSkeletonSyncStatus deletes the serialized sync status saved at the last
    40  // shutdown
    41  func DeleteSkeletonSyncStatus(db zonddb.KeyValueWriter) {
    42  	if err := db.Delete(skeletonSyncStatusKey); err != nil {
    43  		log.Crit("Failed to remove skeleton sync status", "err", err)
    44  	}
    45  }
    46  
    47  // ReadSkeletonHeader retrieves a block header from the skeleton sync store,
    48  func ReadSkeletonHeader(db zonddb.KeyValueReader, number uint64) *types.Header {
    49  	data, _ := db.Get(skeletonHeaderKey(number))
    50  	if len(data) == 0 {
    51  		return nil
    52  	}
    53  	header := new(types.Header)
    54  	if err := rlp.DecodeBytes(data, header); err != nil {
    55  		log.Error("Invalid skeleton header RLP", "number", number, "err", err)
    56  		return nil
    57  	}
    58  	return header
    59  }
    60  
    61  // WriteSkeletonHeader stores a block header into the skeleton sync store.
    62  func WriteSkeletonHeader(db zonddb.KeyValueWriter, header *types.Header) {
    63  	data, err := rlp.EncodeToBytes(header)
    64  	if err != nil {
    65  		log.Crit("Failed to RLP encode header", "err", err)
    66  	}
    67  	key := skeletonHeaderKey(header.Number.Uint64())
    68  	if err := db.Put(key, data); err != nil {
    69  		log.Crit("Failed to store skeleton header", "err", err)
    70  	}
    71  }
    72  
    73  // DeleteSkeletonHeader removes all block header data associated with a hash.
    74  func DeleteSkeletonHeader(db zonddb.KeyValueWriter, number uint64) {
    75  	if err := db.Delete(skeletonHeaderKey(number)); err != nil {
    76  		log.Crit("Failed to delete skeleton header", "err", err)
    77  	}
    78  }