Skip to content

Commit

Permalink
Merge pull request #829 from lochjin/dev2.0
Browse files Browse the repository at this point in the history
snap synchronization for P2P
  • Loading branch information
dindinw authored Nov 18, 2024
2 parents 3ecff42 + 0db2ff2 commit 96e48cc
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 340 deletions.
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Config struct {
Miner bool `long:"miner" description:"Enable miner module"`
Generate bool `long:"generate" description:"Generate (mine) coins using the CPU"`
GenerateOnTx bool `long:"generateontx" description:"Generate (mine) coins using the CPU when there is a new transaction"`
GenerateNoDevGap bool `long:"generatenodevgap" description:"Generate (mine) coins using the CPU on develop mode whithout gap"`
MiningAddrs []string `long:"miningaddr" description:"Add the specified payment address to the list of addresses to use for generated blocks -- At least one address is required if the generate option is set"`
MiningTimeOffset int `long:"miningtimeoffset" description:"Offset the mining timestamp of a block by this many seconds (positive values are in the past)"`
BlockMinSize uint32 `long:"blockminsize" description:"Mininum block size in bytes to be used when creating a block"`
Expand Down Expand Up @@ -140,6 +141,9 @@ type Config struct {
// wallet
WalletPass string
AutoCollectEvm bool `long:"autocollectevm" description:"auto collect utxo to evm"`

// TODO: It will soon be discarded in the near future
DevSnapSync bool `long:"devsnapsync" description:"Enable snap sync for P2P that only exist in development mode"`
}

func (c *Config) GetMinningAddrs() []types.Address {
Expand Down
8 changes: 5 additions & 3 deletions core/blockchain/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ func (b *BlockChain) ProcessBlockBySnap(sds []*SnapData) (meerdag.IBlock, error)
if err != nil {
return returnFun(err)
}
err = b.addTokenState(dblock.GetID(), sd.tokenState, sd.prevTSHash)
if err != nil {
return returnFun(err)
if sd.prevTSHash != nil {
err = b.addTokenState(dblock.GetID(), sd.tokenState, sd.prevTSHash)
if err != nil {
return returnFun(err)
}
}
} else {
err := b.indexManager.ConnectBlock(sd.block, dblock, nil)
Expand Down
1 change: 0 additions & 1 deletion core/blockchain/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func (b *BlockChain) addTokenState(id uint, state *token.TokenState, PrevStateHa
func (b *BlockChain) GetTokenState(bid uint32) *token.TokenState {
state, err := token.DBFetchTokenState(b.DB(), uint(bid))
if err != nil {
log.Error(err.Error())
return nil
}
return state
Expand Down
3 changes: 3 additions & 0 deletions core/blockchain/token/tokenstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ func DBRemoveTokenState(db model.DataBase, id uint) error {
}

func NewTokenStateFromBytes(data []byte) (*TokenState, error) {
if len(data) <= 0 {
return nil, fmt.Errorf("No token state")
}
ts := &TokenState{}
_, err := ts.Deserialize(data)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions meerdag/blocktransf.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ func (bd *MeerDAG) getBlockByOrder(order uint) IBlock {
return bd.getBlockById(id)
}

func (bd *MeerDAG) getBlockIDByOrder(order uint) uint {
if order >= MaxBlockOrder {
return MaxId
}
id, ok := bd.commitOrder[order]
if ok {
return id
}
id, err := DBGetBlockIdByOrder(bd.db, order)
if err != nil {
log.Error(err.Error())
return MaxId
}
return id
}

// Return the last added block
func (bd *MeerDAG) GetLastBlock() IBlock {
bd.stateLock.Lock()
Expand Down
83 changes: 64 additions & 19 deletions meerdag/dagsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,44 +238,82 @@ func (ds *DAGSync) getBlockChainFromMain(point IBlock, maxHashes uint) []*hash.H
return result
}

func (ds *DAGSync) CalcSnapSyncBlocks(locator []*SnapLocator, maxHashes uint, target *SnapLocator) ([]IBlock, error) {
func (ds *DAGSync) CalcSnapSyncBlocks(locator []*SnapLocator, maxHashes uint, target *SnapLocator) ([]IBlock, IBlock, error) {
ds.bd.stateLock.Lock()
defer ds.bd.stateLock.Unlock()

if target != nil {
result := []IBlock{}
if len(locator) != 1 {
return nil, fmt.Errorf("Locator len error")
return nil, nil, fmt.Errorf("Locator len error")
}
point := ds.bd.getBlock(locator[0].block)
if point == nil {
return nil, fmt.Errorf("No block:%s", locator[0].block.String())
return nil, nil, fmt.Errorf("No block:%s", locator[0].block.String())
}
if !point.GetState().Root().IsEqual(locator[0].root) {
return nil, fmt.Errorf("Target root inconsistent:%s != %s", point.GetState().Root().String(), locator[0].root.String())
return nil, nil, fmt.Errorf("Target root inconsistent:%s != %s", point.GetState().Root().String(), locator[0].root.String())
}
targetBlock := ds.bd.getBlock(target.block)
if point == nil {
return nil, fmt.Errorf("No target:%s", target.block.String())
if targetBlock == nil {
return nil, nil, fmt.Errorf("No target:%s", target.block.String())
}
for i := point.GetOrder() + 1; i <= targetBlock.GetOrder(); i++ {
block := ds.bd.getBlockByOrder(i)
if block == nil {
return result, fmt.Errorf("No block in order:%d", i)
}
result = append(result, block)
if uint(len(result)) >= maxHashes {
break
}
if point.GetOrder() >= targetBlock.GetOrder() {
return nil, nil, fmt.Errorf("Start point is invalid:%d >= %d(target)", point.GetOrder(), targetBlock.GetOrder())
}
return result, nil
return ds.getBlockChainForSnapSync(point, targetBlock, maxHashes), nil, nil
}
// get target
mp := ds.bd.getMainChainTip()
if mp.GetOrder() <= MaxSnapSyncTargetDepth {
return nil, fmt.Errorf("No blocks for snap-sync:%d", mp.GetOrder())
return nil, nil, fmt.Errorf("No blocks for snap-sync:%d", mp.GetOrder())
}
var targetBlock IBlock
for targetOrder := mp.GetOrder() - MaxSnapSyncTargetDepth; targetOrder > 0; targetOrder-- {
targetID := ds.bd.getBlockIDByOrder(targetOrder)
if ds.bd.isOnMainChain(targetID) {
targetBlock = ds.bd.getBlockById(targetID)
if targetBlock != nil {
break
}
}
}

var point IBlock
for i := len(locator) - 1; i >= 0; i-- {
mainBlock := ds.bd.getBlock(locator[i].block)
if mainBlock == nil {
continue
}
if !mainBlock.GetState().Root().IsEqual(locator[i].root) {
continue
}
point = mainBlock
break
}
return nil, nil

if point == nil {
point = ds.bd.getGenesis()
}
if point.GetOrder() >= targetBlock.GetOrder() {
return nil, nil, fmt.Errorf("Start point is invalid:%d >= %d(target)", point.GetOrder(), targetBlock.GetOrder())
}
return ds.getBlockChainForSnapSync(point, targetBlock, maxHashes), targetBlock, nil
}

func (ds *DAGSync) getBlockChainForSnapSync(point IBlock, target IBlock, maxHashes uint) []IBlock {
result := []IBlock{}
for i := point.GetOrder() + 1; i <= target.GetOrder(); i++ {
block := ds.bd.getBlockByOrder(i)
if block == nil {
log.Warn("No block", "order", i)
return result
}
result = append(result, block)
if uint(len(result)) >= maxHashes {
break
}
}
return result
}

// NewDAGSync
Expand All @@ -287,3 +325,10 @@ type SnapLocator struct {
block *hash.Hash
root *hash.Hash
}

func NewSnapLocator(block *hash.Hash, root *hash.Hash) *SnapLocator {
return &SnapLocator{
block: block,
root: root,
}
}
Loading

0 comments on commit 96e48cc

Please sign in to comment.