Skip to content

Commit

Permalink
Merge pull request #575 from lochjin/dev1.2
Browse files Browse the repository at this point in the history
Upgrade v1.13.5 for meerevm and chain database bugs
  • Loading branch information
dindinw authored Nov 18, 2023
2 parents ed88fd1 + 832580a commit 9cb34e0
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 47 deletions.
8 changes: 5 additions & 3 deletions core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,7 @@ func New(consensus model.Consensus) (*BlockChain, error) {
}
b.subsidyCache = NewSubsidyCache(0, b.params)

b.bd = meerdag.New(config.DAGType,
1.0/float64(par.TargetTimePerBlock/time.Second),
b.DB(), b.getBlockData, state.CreateBlockState, state.CreateBlockStateFromBytes)
b.bd = meerdag.New(config.DAGType, 1.0/float64(par.TargetTimePerBlock/time.Second), b.DB(), b.getBlockData)
b.bd.SetTipsDisLimit(int64(par.CoinbaseMaturity))
b.bd.SetCacheSize(config.DAGCacheSize, config.BlockDataCacheSize)

Expand All @@ -1073,3 +1071,7 @@ func New(consensus model.Consensus) (*BlockChain, error) {
b.Services().RegisterService(b.meerChain)
return &b, nil
}

func init() {
meerdag.SetBlockStateFactory(state.CreateBlockState, state.CreateBlockStateFromBytes)
}
35 changes: 10 additions & 25 deletions database/rawdb/accessors_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ func ReadDAGBlock(db ethdb.Reader, id uint64) meerdag.IBlock {
if len(data) == 0 {
return nil
}
var block meerdag.IBlock
err := block.Decode(bytes.NewReader(data))
block := &meerdag.Block{}
block.SetID(uint(id))
ib := &meerdag.PhantomBlock{Block: block}
err := ib.Decode(bytes.NewReader(data))
if err != nil {
log.Error(err.Error())
return nil
}
return block
return ib
}

func WriteDAGBlockRaw(db ethdb.KeyValueWriter, id uint, data []byte) error {
Expand Down Expand Up @@ -95,31 +97,14 @@ func ReadBlockHashByID(db ethdb.Reader, id uint64) (*hash.Hash, error) {
// main chain

func ReadMainChainTip(db ethdb.Reader) *uint64 {
data, err := db.Get(mainchainTipKey)
if err != nil {
log.Debug("main chain tip", "err", err.Error())
tips := ReadDAGTips(db)
if len(tips) <= 0 {
return nil
}
number := binary.BigEndian.Uint64(data)
return &number
}

func WriteMainChainTip(db ethdb.KeyValueWriter, mainchaintip uint64) error {
err := db.Put(mainchainTipKey, encodeBlockID(mainchaintip))
if err != nil {
log.Error(err.Error())
return err
}
return nil
}

func DeleteMainChainTip(db ethdb.KeyValueWriter) error {
err := db.Delete(mainchainTipKey)
if err != nil {
log.Error(err.Error())
return err
if tips[0] == uint64(meerdag.MaxId) {
return nil
}
return nil
return &tips[0]
}

func ReadMainChain(db ethdb.Reader, id uint64) bool {
Expand Down
8 changes: 4 additions & 4 deletions database/rawdb/chain_freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) {
frozen := f.frozen.Load()
switch {
case *mt < threshold:
log.Debug("Current full block not old enough", "DAG_ID", *mt, "hash", mb.GetHash(), "delay", threshold)
log.Debug("Current full block not old enough", "tip", *mt, "hash", mb.GetHash(), "delay", threshold)
backoff = true
continue

case *mt-threshold <= frozen:
log.Debug("Ancient blocks frozen already", "DAG_ID", *mt, "hash", mb.GetHash(), "frozen", frozen)
log.Debug("Ancient blocks frozen already", "tip", *mt, "hash", mb.GetHash(), "frozen", frozen)
backoff = true
continue
}
Expand Down Expand Up @@ -159,13 +159,13 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) {
log.Crit("Failed to delete frozen canonical blocks", "err", err)
}
batch.Reset()

frozen = f.frozen.Load()
// Log something friendly for the user
context := []interface{}{
"blocks", frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "DAG_ID", frozen - 1,
}
if n := len(ancients); n > 0 {
context = append(context, []interface{}{"hash", ancients[n-1].GetHash()}...)
context = append(context, []interface{}{"hash", ancients[n-1].GetHash(), "order", ancients[n-1].GetOrder()}...)
}
log.Debug("Deep froze chain segment", context...)

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/dchest/blake256 v1.1.0
github.com/deckarep/golang-set v1.8.0
github.com/dgraph-io/ristretto v0.0.2
github.com/ethereum/go-ethereum v1.13.4
github.com/ethereum/go-ethereum v1.13.5
github.com/ferranbt/fastssz v0.0.0-20200514094935-99fccaf93472
github.com/go-stack/stack v1.8.1
github.com/gofrs/flock v0.8.1
Expand Down Expand Up @@ -76,7 +76,7 @@ require (
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand All @@ -85,7 +85,7 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dop251/goja v0.0.0-20230806174421-c933cf95e127 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/flynn/noise v1.0.0 // indirect
Expand Down Expand Up @@ -216,6 +216,6 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum v1.13.4 => github.com/Qitmeer/go-ethereum v1.13.4-q.0
replace github.com/ethereum/go-ethereum v1.13.5 => github.com/Qitmeer/go-ethereum v1.13.5-q.0

replace github.com/karalabe/usb v0.0.2 => github.com/dindinw/karalabe-usb v0.0.0-20230613095851-cd69a62c9c30
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a h1:LUGOJG/lF0TlnlYT
github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:gbGKdXSJn71Mc2xcKJHqC/waPiX0byZae67zarj83m4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a h1:O2Erw/YvYAkIqkc2uvP/WwuWf0V8S0+pjU/FKHmjFU4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:KiA7g46zc6dkgf/3NbEpJirY75v656WYlmSQNR1wTVk=
github.com/Qitmeer/go-ethereum v1.13.4-q.0 h1:drb+YSq57Mz8L4gsEoShzj2N6odev+jBrR4fG4EzCZo=
github.com/Qitmeer/go-ethereum v1.13.4-q.0/go.mod h1:I0U5VewuuTzvBtVzKo7b3hJzDhXOUtn9mJW7SsIPB0Q=
github.com/Qitmeer/go-ethereum v1.13.5-q.0 h1:1T1bPZvfhz2Lv2IDKisb/W5bCxHZHdRkWUKGw8vvmro=
github.com/Qitmeer/go-ethereum v1.13.5-q.0/go.mod h1:yMTu38GSuyxaYzQMViqNmQ1s3cE84abZexQmTgenWk0=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
Expand Down Expand Up @@ -111,6 +111,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crate-crypto/go-kzg-4844 v0.3.0 h1:UBlWE0CgyFqqzTI+IFyCzA7A3Zw4iip6uzRv5NIXG0A=
github.com/crate-crypto/go-kzg-4844 v0.3.0/go.mod h1:SBP7ikXEgDnUPONgm33HtuDZEDtWa3L4QtN1ocJSEQ4=
github.com/crate-crypto/go-kzg-4844 v0.7.0 h1:C0vgZRk4q4EZ/JgPfzuSoxdCq3C3mOZMBShovmncxvA=
github.com/crate-crypto/go-kzg-4844 v0.7.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -165,6 +167,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/ethereum/c-kzg-4844 v0.3.1 h1:sR65+68+WdnMKxseNWxSJuAv2tsUrihTpVBTfM/U5Zg=
github.com/ethereum/c-kzg-4844 v0.3.1/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down
1 change: 0 additions & 1 deletion meerdag/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ type IBlock interface {
// GetState
GetState() model.BlockState

// TODO:
Bytes() []byte

// Setting the order of block
Expand Down
13 changes: 8 additions & 5 deletions meerdag/meerdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ type GetBlockData func(*hash.Hash) IBlockData

type CreateBlockState func(id uint64) model.BlockState

var createBlockState CreateBlockState
var createBlockState CreateBlockState = CreateMockBlockState

type CreateBlockStateFromBytes func(data []byte) (model.BlockState, error)

var createBlockStateFromBytes CreateBlockStateFromBytes
var createBlockStateFromBytes CreateBlockStateFromBytes = CreateMockBlockStateFromBytes

// The general foundation framework of Block DAG implement
type MeerDAG struct {
Expand Down Expand Up @@ -1377,9 +1377,7 @@ out:
log.Trace("MeerDAG handler done")
}

func New(dagType string, blockRate float64, db model.DataBase, getBlockData GetBlockData, createBS CreateBlockState, createBSB CreateBlockStateFromBytes) *MeerDAG {
createBlockState = createBS
createBlockStateFromBytes = createBSB
func New(dagType string, blockRate float64, db model.DataBase, getBlockData GetBlockData) *MeerDAG {
md := &MeerDAG{
quit: make(chan struct{}),
blockDataCache: map[uint]time.Time{},
Expand All @@ -1389,3 +1387,8 @@ func New(dagType string, blockRate float64, db model.DataBase, getBlockData GetB
md.init(dagType, blockRate, db, getBlockData)
return md
}

func SetBlockStateFactory(createBS CreateBlockState, createBSB CreateBlockStateFromBytes) {
createBlockState = createBS
createBlockStateFromBytes = createBSB
}
11 changes: 11 additions & 0 deletions meerdag/pantomblock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meerdag

import (
"bytes"
s "github.com/Qitmeer/qng/core/serialization"
"io"
)
Expand Down Expand Up @@ -229,3 +230,13 @@ func (pb *PhantomBlock) CleanDiffAnticone() {
pb.redDiffAnticone.Clean()
}
}

func (pb *PhantomBlock) Bytes() []byte {
var buff bytes.Buffer
err := pb.Encode(&buff)
if err != nil {
log.Error(err.Error())
return nil
}
return buff.Bytes()
}
2 changes: 1 addition & 1 deletion meerdag/spectre.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (sp *Spectre) votedPast(virtualBlock IBlock) *MeerDAG {
vh = *virtualBlock.GetHash()
}
sb := &SpectreBlockData{hash: vh}
vp := New(SPECTRE, -1, nil, nil, CreateMockBlockState, CreateMockBlockStateFromBytes)
vp := New(SPECTRE, -1, nil, nil)
vp.AddBlock(sb)
visited = NewHashSet()

Expand Down
2 changes: 1 addition & 1 deletion meerdag/test/meerdag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func InitBlockDAG(dagType string, graph string) meerdag.ConsensusAlgorithm {
fmt.Println(err)
return nil
}
bd = meerdag.New(dagType, -1, db, nil, meerdag.CreateMockBlockState, meerdag.CreateMockBlockStateFromBytes)
bd = meerdag.New(dagType, -1, db, nil)
instance := bd.GetInstance()
tbMap = map[string]meerdag.IBlock{}
for i := 0; i < blen; i++ {
Expand Down
2 changes: 1 addition & 1 deletion meerdag/test/phantom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func checkLoad(t *testing.T) {
}
return tb
}
bd = meerdag.New(meerdag.PHANTOM, -1, db, getBlockData, meerdag.CreateMockBlockState, meerdag.CreateMockBlockStateFromBytes)
bd = meerdag.New(meerdag.PHANTOM, -1, db, getBlockData)
total, err := dbGetTotal()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9cb34e0

Please sign in to comment.