mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
contracts, core/rawdb: refactor read and write randomizeKey (#1806)
This commit is contained in:
parent
bf4c48c7c6
commit
70755237e7
3 changed files with 36 additions and 5 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -82,6 +82,37 @@ func DeleteectionHead(db ethdb.KeyValueWriter, section uint64) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadValidSections retrieves the number of valid sections from database.
|
||||
func ReadValidSections(db ethdb.KeyValueReader) *uint64 {
|
||||
data, err := db.Get(validSectionsKey)
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ var (
|
|||
xdposV1Prefix = []byte("XDPoS-")
|
||||
xdposV2Prefix = []byte("XDPoS-V2-")
|
||||
|
||||
randomizeKey = []byte("randomizeKey")
|
||||
sectionHeadKeyPrefix = []byte("shead")
|
||||
validSectionsKey = []byte("count")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue