github.com/jimmyx0x/go-ethereum@v1.10.28/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  	"bytes"
    21  
    22  	"github.com/ethereum/go-ethereum/core/types"
    23  	"github.com/ethereum/go-ethereum/ethdb"
    24  	"github.com/ethereum/go-ethereum/log"
    25  	"github.com/ethereum/go-ethereum/rlp"
    26  )
    27  
    28  // ReadSkeletonSyncStatus retrieves the serialized sync status saved at shutdown.
    29  func ReadSkeletonSyncStatus(db ethdb.KeyValueReader) []byte {
    30  	data, _ := db.Get(skeletonSyncStatusKey)
    31  	return data
    32  }
    33  
    34  // WriteSkeletonSyncStatus stores the serialized sync status to save at shutdown.
    35  func WriteSkeletonSyncStatus(db ethdb.KeyValueWriter, status []byte) {
    36  	if err := db.Put(skeletonSyncStatusKey, status); err != nil {
    37  		log.Crit("Failed to store skeleton sync status", "err", err)
    38  	}
    39  }
    40  
    41  // DeleteSkeletonSyncStatus deletes the serialized sync status saved at the last
    42  // shutdown
    43  func DeleteSkeletonSyncStatus(db ethdb.KeyValueWriter) {
    44  	if err := db.Delete(skeletonSyncStatusKey); err != nil {
    45  		log.Crit("Failed to remove skeleton sync status", "err", err)
    46  	}
    47  }
    48  
    49  // ReadSkeletonHeader retrieves a block header from the skeleton sync store,
    50  func ReadSkeletonHeader(db ethdb.KeyValueReader, number uint64) *types.Header {
    51  	data, _ := db.Get(skeletonHeaderKey(number))
    52  	if len(data) == 0 {
    53  		return nil
    54  	}
    55  	header := new(types.Header)
    56  	if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
    57  		log.Error("Invalid skeleton header RLP", "number", number, "err", err)
    58  		return nil
    59  	}
    60  	return header
    61  }
    62  
    63  // WriteSkeletonHeader stores a block header into the skeleton sync store.
    64  func WriteSkeletonHeader(db ethdb.KeyValueWriter, header *types.Header) {
    65  	data, err := rlp.EncodeToBytes(header)
    66  	if err != nil {
    67  		log.Crit("Failed to RLP encode header", "err", err)
    68  	}
    69  	key := skeletonHeaderKey(header.Number.Uint64())
    70  	if err := db.Put(key, data); err != nil {
    71  		log.Crit("Failed to store skeleton header", "err", err)
    72  	}
    73  }
    74  
    75  // DeleteSkeletonHeader removes all block header data associated with a hash.
    76  func DeleteSkeletonHeader(db ethdb.KeyValueWriter, number uint64) {
    77  	if err := db.Delete(skeletonHeaderKey(number)); err != nil {
    78  		log.Crit("Failed to delete skeleton header", "err", err)
    79  	}
    80  }