update process M1 synchronie only for downloader , not for fetcher

This commit is contained in:
Nguyen Ba Tam 2018-08-24 17:30:50 +07:00
parent 8818e652d2
commit e6ae9f1360
3 changed files with 44 additions and 42 deletions

View file

@ -1010,41 +1010,9 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
// //
// After insertion is done, all accumulated events will be fired. // After insertion is done, all accumulated events will be fired.
func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
if bc.chainConfig != nil && bc.chainConfig.Posv != nil {
epoch := bc.chainConfig.Posv.Epoch
gap := bc.chainConfig.Posv.Gap
length := len(chain)
start := int(chain[0].NumberU64() % epoch)
end := int(epoch - gap - uint64(start))
if (end < 0) {
end = end + int(epoch)
}
start = 0
for {
if end >= length {
end = length - 1
}
inserts := make([]*types.Block, end-start+1)
copy(inserts, chain[start:end+1])
if len(inserts) > 0 {
n, events, logs, err := bc.insertChain(inserts)
bc.PostChainEvents(events, logs)
if err != nil {
return n, err
}
}
start = end + 1
end = end + int(epoch)
if (start >= length) {
break
}
}
return 0, nil
} else {
n, events, logs, err := bc.insertChain(chain) n, events, logs, err := bc.insertChain(chain)
bc.PostChainEvents(events, logs) bc.PostChainEvents(events, logs)
return n, err return n, err
}
} }
// insertChain will execute the actual chain insertion and event aggregation. The // insertChain will execute the actual chain insertion and event aggregation. The
@ -1224,14 +1192,14 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
stats.processed++ stats.processed++
stats.usedGas += usedGas stats.usedGas += usedGas
stats.report(chain, i, bc.stateCache.TrieDB().Size()) stats.report(chain, i, bc.stateCache.TrieDB().Size())
if bc.chainConfig.Posv != nil { if i == len(chain)-1 && bc.chainConfig.Posv != nil {
// epoch block // epoch block
if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == 0 { if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == 0 {
CheckpointCh <- 1 CheckpointCh <- 1
} }
// prepare set of masternodes for the next epoch // prepare set of masternodes for the next epoch
if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == (bc.chainConfig.Posv.Epoch - bc.chainConfig.Posv.Gap) { if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == (bc.chainConfig.Posv.Epoch - bc.chainConfig.Posv.Gap) {
bc.UpdateM1() M1Ch <- 1
} }
} }
} }

View file

@ -25,7 +25,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
ethereum "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -172,6 +172,8 @@ type LightChain interface {
// BlockChain encapsulates functions required to sync a (full or fast) blockchain. // BlockChain encapsulates functions required to sync a (full or fast) blockchain.
type BlockChain interface { type BlockChain interface {
Config() *params.ChainConfig
UpdateM1()
LightChain LightChain
// HasBlock verifies a block's presence in the local chain. // HasBlock verifies a block's presence in the local chain.
@ -1322,12 +1324,39 @@ func (d *Downloader) processFullSyncContent() error {
if len(results) == 0 { if len(results) == 0 {
return nil return nil
} }
if d.chainInsertHook != nil { epoch := d.blockchain.Config().Posv.Epoch
d.chainInsertHook(results) gap := d.blockchain.Config().Posv.Gap
length := len(results)
start := int(results[0].Header.Number.Uint64() % epoch)
end := int(epoch - gap - uint64(start))
if (end < 0) {
end = end + int(epoch)
} }
if err := d.importBlockResults(results); err != nil { start = 0
for {
if end >= length {
end = length - 1
}
inserts := make([]*fetchResult, end-start+1)
copy(inserts, results[start:end+1])
if len(inserts) > 0 {
if d.chainInsertHook != nil {
d.chainInsertHook(inserts)
}
if err := d.importBlockResults(inserts); err != nil {
return err return err
} }
// prepare set of masternodes for the next epoch
if (inserts[len(inserts)-1].Header.Number.Uint64() % epoch) == (epoch - gap) {
d.blockchain.UpdateM1()
}
}
start = end + 1
end = end + int(epoch)
if (start >= length) {
break
}
}
} }
} }
@ -1355,6 +1384,7 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err) log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
return errInvalidChain return errInvalidChain
} }
return nil return nil
} }

View file

@ -456,6 +456,10 @@ func (dl *downloadTester) dropPeer(id string) {
dl.downloader.UnregisterPeer(id) dl.downloader.UnregisterPeer(id)
} }
// Config retrieves the blockchain's chain configuration.
func (dl *downloadTester) Config() *params.ChainConfig { return dl.downloader.blockchain.Config() }
func (dl *downloadTester) UpdateM1() { dl.downloader.blockchain.UpdateM1() }
type downloadTesterPeer struct { type downloadTesterPeer struct {
dl *downloadTester dl *downloadTester
id string id string