diff --git a/contracts/utils.go b/contracts/utils.go index b41e5691b46b..32bd7b612b89 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -103,8 +103,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *txpool.TxPool, blockNumber := block.Number().Uint64() checkNumber := blockNumber % chainConfig.XDPoS.Epoch // Generate random private key and save into chaindb. - randomizeKeyName := []byte("randomizeKey") - exist, _ := chainDb.Has(randomizeKeyName) + exist := rawdb.HasRandomize(chainDb) // Set secret for randomize. if !exist && checkNumber > 0 && common.EpocBlockSecret <= checkNumber && common.EpocBlockOpening > checkNumber { @@ -129,12 +128,12 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *txpool.TxPool, } // Put randomize key into chainDb. - chainDb.Put(randomizeKeyName, randomizeKeyValue) + rawdb.WriteRandomize(chainDb, randomizeKeyValue) } // Set opening for randomize. if exist && checkNumber > 0 && common.EpocBlockOpening <= checkNumber && common.EpocBlockRandomize >= checkNumber { - randomizeKeyValue, err := chainDb.Get(randomizeKeyName) + randomizeKeyValue, err := rawdb.ReadRandomize(chainDb) if err != nil { log.Error("Fail to get randomize key from state db.", "error", err) return err @@ -158,7 +157,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *txpool.TxPool, } // Clear randomize key in state db. - chainDb.Delete(randomizeKeyName) + rawdb.DeleteRandomize(chainDb) } } diff --git a/core/rawdb/accessors_xdc.go b/core/rawdb/accessors_xdc.go new file mode 100644 index 000000000000..633a10f3de00 --- /dev/null +++ b/core/rawdb/accessors_xdc.go @@ -0,0 +1,53 @@ +// Copyright 2025 The XDPoSChain Authors +// This file is part of the XDPoSChain library. +// +// The XDPoSChain library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The XDPoSChain library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the XDPoSChain library. If not, see . + +package rawdb + +import ( + "github.com/XinFinOrg/XDPoSChain/ethdb" + "github.com/XinFinOrg/XDPoSChain/log" +) + +// HasRandomize verifies the existence of randomize. +func HasRandomize(db ethdb.KeyValueReader) bool { + if has, err := db.Has(randomizeKey); !has || err != nil { + return false + } + return true +} + +// ReadRandomize retrieves the randomiz from database. +func ReadRandomize(db ethdb.KeyValueReader) ([]byte, error) { + data, err := db.Get(randomizeKey) + if err != nil { + return nil, err + } + return data, nil +} + +// WriteRandomize writes the randomize into database. +func WriteRandomize(db ethdb.KeyValueWriter, data []byte) { + if err := db.Put(randomizeKey, data); err != nil { + log.Crit("Failed to store randomizeKey", "err", err) + } +} + +// DeleteRandomize deletes the randomize from database. +func DeleteRandomize(db ethdb.KeyValueWriter) { + if err := db.Delete(randomizeKey); err != nil { + log.Crit("Failed to delete randomizeKey", "err", err) + } +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 874b52c15ccc..5c07430164c4 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -78,6 +78,8 @@ var ( // used by old db, now only used for conversion oldTxMetaSuffix = []byte{0x01} + randomizeKey = []byte("randomizeKey") + preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) )