Skip to content

Commit

Permalink
Merge pull request #2153 from cheng762/feature/bump-version-to-1.5.0
Browse files Browse the repository at this point in the history
prepare for move pivot
  • Loading branch information
benbaley authored Sep 7, 2023
2 parents 24c1cee + 0284539 commit efdaf4f
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 117 deletions.
27 changes: 13 additions & 14 deletions core/snapshotdb/blockdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package snapshotdb

import (
Expand All @@ -31,7 +30,7 @@ import (
"github.com/PlatONnetwork/PlatON-Go/common"
)

type blockData struct {
type BlockData struct {
BlockHash common.Hash
ParentHash common.Hash
Number *big.Int
Expand All @@ -50,7 +49,7 @@ type revision struct {
journalIndex int
}

func (b *blockData) DecodeRLP(s *rlp.Stream) error {
func (b *BlockData) DecodeRLP(s *rlp.Stream) error {
jk := new(blockWal)
if err := s.Decode(jk); err != nil {
return err
Expand All @@ -67,7 +66,7 @@ func (b *blockData) DecodeRLP(s *rlp.Stream) error {
return nil
}

func (b *blockData) EncodeRLP(w io.Writer) error {
func (b *BlockData) EncodeRLP(w io.Writer) error {
jk := new(blockWal)
jk.BlockHash = b.BlockHash
jk.ParentHash = b.ParentHash
Expand All @@ -89,25 +88,25 @@ func (b *blockData) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, jk)
}

func (b *blockData) BlockKey() []byte {
func (b *BlockData) BlockKey() []byte {
return EncodeWalKey(b.Number)
}

func (b *blockData) BlockVal() []byte {
func (b *BlockData) BlockVal() []byte {
val, err := rlp.EncodeToBytes(b)
if err != nil {
panic("Encode BlockVal to byte fail:" + err.Error())
}
return val
}

func (b *blockData) cleanJournal() {
func (b *BlockData) cleanJournal() {
b.journal = nil
b.validRevisions = nil
b.nextRevisionId = 0
}

func (b *blockData) revert(snapshot int) {
func (b *BlockData) revert(snapshot int) {
for i := len(b.journal) - 1; i >= snapshot; i-- {
// Undo the changes made by the operation
en := b.journal[i]
Expand All @@ -122,7 +121,7 @@ func (b *blockData) revert(snapshot int) {
}

// RevertToSnapshot reverts all state changes made since the given revision.
func (b *blockData) RevertToSnapshot(revid int) {
func (b *BlockData) RevertToSnapshot(revid int) {
// Find the snapshot in the stack of valid snapshots.
idx := sort.Search(len(b.validRevisions), func(i int) bool {
return b.validRevisions[i].id >= revid
Expand All @@ -138,14 +137,14 @@ func (b *blockData) RevertToSnapshot(revid int) {
}

// Snapshot returns an identifier for the current revision of the state.
func (b *blockData) Snapshot() int {
func (b *BlockData) Snapshot() int {
id := b.nextRevisionId
b.nextRevisionId++
b.validRevisions = append(b.validRevisions, revision{id, len(b.journal)})
return id
}

func (b *blockData) Write(key, val []byte) error {
func (b *BlockData) Write(key, val []byte) error {
var entry journalEntry = journalEntry{
key: key,
newVal: val,
Expand Down Expand Up @@ -177,11 +176,11 @@ type journalEntry struct {
}

type unCommitBlocks struct {
blocks map[common.Hash]*blockData
blocks map[common.Hash]*BlockData
sync.RWMutex
}

func (u *unCommitBlocks) Get(key common.Hash) *blockData {
func (u *unCommitBlocks) Get(key common.Hash) *BlockData {
u.RLock()
block, ok := u.blocks[key]
u.RUnlock()
Expand All @@ -191,7 +190,7 @@ func (u *unCommitBlocks) Get(key common.Hash) *blockData {
return block
}

func (u *unCommitBlocks) Set(key common.Hash, block *blockData) {
func (u *unCommitBlocks) Set(key common.Hash, block *BlockData) {
u.Lock()
u.blocks[key] = block
u.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion core/snapshotdb/blockdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestBlockData_RevertToSnapshot(t *testing.T) {
block := new(blockData)
block := new(BlockData)
block.data = memdb.New(DefaultComparer, 10)
keyA, keyB := []byte("a"), []byte("b")

Expand Down
4 changes: 2 additions & 2 deletions core/snapshotdb/chain_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newTestchain(path string) *testchain {
}
ch.db = db
SetDBBlockChain(ch)
ch.db.walCh = make(chan *blockData, 2)
ch.db.walCh = make(chan *BlockData, 2)
ch.db.walLoopCtx, ch.db.walLoopCancel = context.WithCancel(context.Background())
go ch.db.loopWriteWal()

Expand All @@ -55,7 +55,7 @@ func (c *testchain) reOpenSnapshotDB() {
panic(err)
}
c.db = db
c.db.walCh = make(chan *blockData, 2)
c.db.walCh = make(chan *BlockData, 2)
c.db.walLoopCtx, c.db.walLoopCancel = context.WithCancel(context.Background())
go c.db.loopWriteWal()

Expand Down
7 changes: 5 additions & 2 deletions core/snapshotdb/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package snapshotdb

import (
Expand Down Expand Up @@ -113,7 +112,7 @@ func (c *current) Valid() error {
return nil
}

//the current highest must not greater than block chain current
// the current highest must not greater than block chain current
func (c *current) resetHighestByChainCurrentHeader(currentHead *types.Header, baseDB *leveldb.DB) error {
if c.base.Num.Cmp(currentHead.Number) > 0 {
return fmt.Errorf("base num %v can't be greater than currentHead Number %v", c.base.Num, currentHead.Number)
Expand Down Expand Up @@ -206,6 +205,10 @@ func (c *current) EncodeBase() []byte {
return base
}

func (c *current) String() string {
return fmt.Sprintf("base:%d,hight:%d,%s,", c.base.Num, c.highest.Num, c.highest.Hash)
}

type CurrentHighest struct {
Num *big.Int `rlp:"nil"`
Hash common.Hash
Expand Down
5 changes: 2 additions & 3 deletions core/snapshotdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package snapshotdb

import (
Expand All @@ -36,8 +35,8 @@ func getBaseDBPath(dbpath string) string {
return path.Join(dbpath, DBBasePath)
}

func (s *snapshotDB) getBlockFromWal(block []byte) (*blockData, error) {
bk := new(blockData)
func (s *snapshotDB) getBlockFromWal(block []byte) (*BlockData, error) {
bk := new(BlockData)
if err := rlp.DecodeBytes(block, bk); err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions core/snapshotdb/db_cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package snapshotdb

import (
Expand Down Expand Up @@ -44,7 +43,7 @@ const (

func (s *snapshotDB) schedule() {
// Compaction condition , last Compaction execute time gt than 60s or commit block num gt than 100
if counter.get() >= 60 || s.current.GetHighest(false).Num.Uint64()-s.current.GetBase(false).Num.Uint64() >= 100 {
if counter.get() >= 60 || s.current.GetHighest(false).Num.Uint64()-s.current.GetBase(false).Num.Uint64() >= MaxCommitBlock {
//only one compaction can execute
if atomic.CompareAndSwapInt32(&s.snapshotLockC, snapshotUnLock, snapshotLock) {
if err := s.Compaction(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions core/snapshotdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestRecover(t *testing.T) {
high = ch.db.current.highest.Num.Int64()
commitLength := len(ch.db.committed)

oldarr := []blockData{
oldarr := []BlockData{
*ch.db.committed[0],
*ch.db.committed[1],
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestRecover(t *testing.T) {
return
}
for _, value := range baseDBArr {
v, err := ch.db.baseDB.Get(value.key, nil)
v, err := ch.db.Get(ch.db.current.highest.Hash, value.key)
if err != nil {
t.Error("should be nil", err)
return
Expand All @@ -122,7 +122,7 @@ func TestRecover(t *testing.T) {
t.Error("should recover commit ", len(ch.db.committed))
return
}
newarr := []blockData{
newarr := []BlockData{
*ch.db.committed[0],
*ch.db.committed[1],
}
Expand Down
4 changes: 2 additions & 2 deletions core/snapshotdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (s *snapshotDB) loopWriteWal() {
}
}

func (s *snapshotDB) writeBlockToWalAsynchronous(block *blockData) {
func (s *snapshotDB) writeBlockToWalAsynchronous(block *BlockData) {
s.walSync.Add(1)
s.walCh <- block
}

func (s *snapshotDB) writeWal(block *blockData) error {
func (s *snapshotDB) writeWal(block *BlockData) error {
return s.baseDB.Put(block.BlockKey(), block.BlockVal(), nil)
}
8 changes: 6 additions & 2 deletions core/snapshotdb/mem_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package snapshotdb

import (
"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/core/types"
"math/big"
"sync"
)
Expand Down Expand Up @@ -69,7 +69,11 @@ func (db *MemDatabase) WriteBaseDB(kvs [][2][]byte) error {
return nil
}

//SetCurrent use for fast sync
func (db *MemDatabase) WriteBaseDBWithBlock(current *types.Header, blocks []BlockData) error {
return nil
}

// SetCurrent use for fast sync
func (db *MemDatabase) SetCurrent(highestHash common.Hash, base, height big.Int) error {
db.lock.Lock()
defer db.lock.Unlock()
Expand Down
Loading

0 comments on commit efdaf4f

Please sign in to comment.