From 633658d48bc8dda138a218cc28ce56067cbb4500 Mon Sep 17 00:00:00 2001 From: Sonic Date: Wed, 24 Apr 2019 16:24:20 +0800 Subject: core, rawdb, dex: improve gov state for syncing (#393) * core, rawdb, dex: improve gov state for syncing --- core/rawdb/accessors_chain.go | 47 +++++++++++++++++++++++++++++++++++++++++++ core/rawdb/schema.go | 6 ++++++ 2 files changed, 53 insertions(+) (limited to 'core/rawdb') diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 801ad9362..41a79b21d 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/binary" "math/big" + "time" "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core/types" @@ -382,3 +383,49 @@ func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header { } return a } + +// ReadGovStateRLP retrieves +func ReadGovStateRLP(db DatabaseReader, hash common.Hash) rlp.RawValue { + data, _ := db.Get(govStateKey(hash)) + return data +} + +// WriteGovStateRLP +func WriteGovStateRLP(db DatabaseWriter, hash common.Hash, rlp rlp.RawValue) { + if err := db.Put(govStateKey(hash), rlp); err != nil { + log.Crit("Failed to store gov state", "err", err) + } +} + +// ReadGovState +func ReadGovState(db DatabaseReader, hash common.Hash) *types.GovState { + data := ReadGovStateRLP(db, hash) + if len(data) == 0 { + return nil + } + govState := new(types.GovState) + if err := rlp.Decode(bytes.NewReader(data), govState); err != nil { + log.Error("Invalid gov state RLP", "hash", hash, "err", err) + return nil + } + return govState +} + +// WriteGovState +func WriteGovState(db DatabaseWriter, hash common.Hash, govState *types.GovState) { + t := time.Now() + log.Debug("Rawdb WriteGovState", "t", t) + data, err := rlp.EncodeToBytes(govState) + if err != nil { + log.Crit("Failed to RLP encode gov state", "err", err) + } + log.Debug("Rawdb WriteGovState", "len", len(data), "elapsed", time.Since(t)) + WriteGovStateRLP(db, hash, data) +} + +// DeleteGovState +func DeleteGovState(db DatabaseDeleter, hash common.Hash) { + if err := db.Delete(govStateKey(hash)); err != nil { + log.Crit("Failed to delete gov satate", "err", err) + } +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 8ca47676d..5b92891df 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -50,6 +50,8 @@ var ( blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts + govStatePrefix = []byte("g") + txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits @@ -118,6 +120,10 @@ func txLookupKey(hash common.Hash) []byte { return append(txLookupPrefix, hash.Bytes()...) } +func govStateKey(hash common.Hash) []byte { + return append(govStatePrefix, hash.Bytes()...) +} + // coreBlockKey = coreBlockPrefix + hash func coreBlockKey(hash common.Hash) []byte { return append(coreBlockPrefix, hash.Bytes()...) -- cgit