Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions contracts/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -158,7 +157,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *txpool.TxPool,
}

// Clear randomize key in state db.
chainDb.Delete(randomizeKeyName)
rawdb.DeleteRandomize(chainDb)
}
}

Expand Down
53 changes: 53 additions & 0 deletions core/rawdb/accessors_xdc.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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)
}
}
2 changes: 2 additions & 0 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down