Skip to content

Commit

Permalink
Merge branch 'master' of github.com:copernet/copernicus
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwei9743 committed Sep 26, 2018
2 parents 9445b7b + ef9e160 commit 0a967c4
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 81 deletions.
40 changes: 17 additions & 23 deletions logic/lchain/lactivechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package lchain

import (
"bytes"
//"github.com/copernet/copernicus/logic/lundo"

"github.com/copernet/copernicus/logic/lmempool"
"github.com/copernet/copernicus/model/block"
"github.com/copernet/copernicus/model/blockindex"
Expand All @@ -21,10 +19,8 @@ func ActivateBestChain(pblock *block.Block) error {
// far from a guarantee. Things in the P2P/RPC will often end up calling
// us in the middle of ProcessNewBlock - do not assume pblock is set
// sanely for performance or correctness!
var (
pindexMostWork *blockindex.BlockIndex
pindexNewTip *blockindex.BlockIndex
)
var pindexMostWork *blockindex.BlockIndex

// global.CsMain.Lock()
// defer global.CsMain.Unlock()
for {
Expand Down Expand Up @@ -73,27 +69,13 @@ func ActivateBestChain(pblock *block.Block) error {
pindexMostWork = nil
}

pindexNewTip = gChain.Tip()
pindexFork := gChain.FindFork(pindexOldTip)

// throw all transactions though the signal-interface

// MemPoolConflictRemovalTracker destroyed and conflict evictions
// are notified

gChain.SendNotification(chain.NTBlockConnected, pblock)

// When we reach this point, we switched to a new tip (stored in
// pindexNewTip).
// Notifications/callbacks that can run without cs_main
// Notify external listeners about the new tip.
//event := chain.TipUpdatedEvent{pindexNewTip, pindexFork, lundo.IsInitialBlockDownload()}
event := chain.TipUpdatedEvent{pindexNewTip, pindexFork, false}
gChain.SendNotification(chain.NTChainTipUpdated, &event)

// Always notify the UI if a new block tip was connected
gChain.UpdateSyncingState()
sendNotifications(pindexOldTip, pblock)

if pindexNewTip == pindexMostWork {
if gChain.Tip() == pindexMostWork {
break
}
}
Expand All @@ -102,6 +84,18 @@ func ActivateBestChain(pblock *block.Block) error {
return err
}

// sendNotifications When we reach this point, we switched to a new tip.
// Notify external listeners about the new tip.
func sendNotifications(pindexOldTip *blockindex.BlockIndex, pblock *block.Block) {
gChain := chain.GetInstance()

gChain.SendNotification(chain.NTBlockConnected, pblock)

forkIndex := gChain.FindFork(pindexOldTip)
event := chain.TipUpdatedEvent{gChain.Tip(), forkIndex, IsInitialBlockDownload()}
gChain.SendNotification(chain.NTChainTipUpdated, &event)
}

// ActivateBestChainStep Try to make some progress towards making pindexMostWork
// the active block. pblock is either nullptr or a pointer to a CBlock corresponding to
// pindexMostWork.
Expand Down
6 changes: 6 additions & 0 deletions logic/lchain/lchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import (
"github.com/copernet/copernicus/model/pow"
)

// IsInitialBlockDownload Check whether we are doing an initial block download
// (synchronizing from disk or network)
func IsInitialBlockDownload() bool {
return persist.Reindex || !chain.GetInstance().IsAlmostSynced()
}

func ConnectBlock(pblock *block.Block, pindex *blockindex.BlockIndex, view *utxo.CoinsMap, fJustCheck bool) error {
gChain := chain.GetInstance()
tip := gChain.Tip()
Expand Down
44 changes: 0 additions & 44 deletions logic/lundo/lundo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,13 @@ package lundo

import (
"fmt"
"sync/atomic"

"github.com/copernet/copernicus/log"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/block"
"github.com/copernet/copernicus/model/chain"
"github.com/copernet/copernicus/model/outpoint"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/model/undo"
"github.com/copernet/copernicus/model/utxo"
"github.com/copernet/copernicus/persist"
"github.com/copernet/copernicus/util"
)

var latchToFalse int32

// IsInitialBlockDownload Check whether we are doing an initial block download
// (synchronizing from disk or network)
func IsInitialBlockDownload() bool {
gChainActive := chain.GetInstance()
// latchToFalse: pre-test latch before taking the lock.
if atomic.LoadInt32(&latchToFalse) != 0 {
return false
}

persist.CsMain.Lock()
defer persist.CsMain.Unlock()

if atomic.LoadInt32(&latchToFalse) != 0 {
return false
}
if persist.Reindex {
return true
}

if gChainActive.Tip() == nil {
return true
}
minWorkSum := pow.HashToBig(&model.ActiveNetParams.MinimumChainWork)
if gChainActive.Tip().ChainWork.Cmp(minWorkSum) < 0 {
return true
}

if int64(gChainActive.Tip().GetBlockTime()) < util.GetTime()-persist.DefaultMaxTipAge {
return true
}
atomic.AddInt32(&latchToFalse, 1)

return false
}

func ApplyBlockUndo(blockUndo *undo.BlockUndo, blk *block.Block,
cm *utxo.CoinsMap) undo.DisconnectResult {
clean := true
Expand Down
33 changes: 33 additions & 0 deletions model/chain/SyncingState.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package chain

import (
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/util"
)

const defaultMaxTipAge = 24 * 60 * 60

type SyncingState struct {
isAlmostSynced bool
}

func (ds *SyncingState) UpdateSyncingState() {
if !ds.isAlmostSynced {

gChain := GetInstance()

if gChain.Tip() != nil {
minWorkSum := pow.HashToBig(&model.ActiveNetParams.MinimumChainWork)
hasEnoughWork := gChain.Tip().ChainWork.Cmp(minWorkSum) > 0

hasRecentBlocks := int64(gChain.Tip().GetBlockTime()) > util.GetTime()-defaultMaxTipAge

ds.isAlmostSynced = hasEnoughWork && hasRecentBlocks
}
}
}

func (ds *SyncingState) IsAlmostSynced() bool {
return ds.isAlmostSynced
}
8 changes: 6 additions & 2 deletions model/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Chain struct {
// certain blockchain events.
notificationsLock sync.RWMutex
notifications []NotificationCallback

*SyncingState
}

var globalChain *Chain
Expand All @@ -50,7 +52,6 @@ func GetInstance() *Chain {
func InitGlobalChain() {
if globalChain == nil {
globalChain = NewChain()
globalChain.params = model.ActiveNetParams
}
if len(conf.Cfg.Chain.AssumeValid) > 0 {
hash, err := util.GetHashFromStr(conf.Cfg.Chain.AssumeValid)
Expand All @@ -64,7 +65,10 @@ func InitGlobalChain() {
}

func NewChain() *Chain {
return &Chain{}
c := &Chain{}
c.params = model.ActiveNetParams
c.SyncingState = &SyncingState{}
return c
}

func (c *Chain) GetParams() *model.BitcoinParams {
Expand Down
1 change: 0 additions & 1 deletion persist/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const (
// UndoFileChunkSize is the pre-allocation chunk size for rev?????.dat files (since 0.8) */
UndoFileChunkSize = 0x100000
DefaultMaxMemPoolSize = 300
DefaultMaxTipAge = 24 * 60 * 60
)

var (
Expand Down
5 changes: 5 additions & 0 deletions rpc/btcjson/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,8 @@ type VersionResult struct {
Prerelease string `json:"prerelease"`
BuildMetadata string `json:"buildmetadata"`
}

type WaitForBlockHeight struct {
Hash string `json:"hash"`
Height int32 `json:"height"`
}
2 changes: 1 addition & 1 deletion rpc/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewTLSCertPair(organization string, validUntil time.Time, extraHosts []stri

KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature |
x509.KeyUsageCertSign,
IsCA: true, // so can sign self.
IsCA: true, // so can sign self.
BasicConstraintsValid: true,

DNSNames: dnsNames,
Expand Down
4 changes: 2 additions & 2 deletions rpc/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"encoding/hex"
"fmt"
"github.com/copernet/copernicus/logic/lchain"
"math/big"

"errors"
"github.com/copernet/copernicus/errcode"
"github.com/copernet/copernicus/log"
"github.com/copernet/copernicus/logic/lblock"
"github.com/copernet/copernicus/logic/lundo"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/block"
"github.com/copernet/copernicus/model/blockindex"
Expand Down Expand Up @@ -160,7 +160,7 @@ func handleGetBlockTemplateRequest(request *btcjson.TemplateRequest, closeChan <
//}
log.Debug("getblocktemplate %#v", request)

if lundo.IsInitialBlockDownload() {
if lchain.IsInitialBlockDownload() {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCClientInInitialDownload,
Message: "Bitcoin is downloading blocks...",
Expand Down
21 changes: 14 additions & 7 deletions rpc/rpcblockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func getPrunMode() (bool, error) {
/* pruneArg := util.GetArg("-prune", 0)
if pruneArg < 0 {
return false, errors.New("Prune cannot be configured with a negative value")
}*/ // todo open
}*/// todo open
return true, nil
}

Expand Down Expand Up @@ -771,7 +771,7 @@ func handlePruneBlockChain(s *Server, cmd interface{}, closeChan <-chan struct{}
}
chain.PruneBlockFilesManual(*height)
return uint64(*height), nil*/ // todo realise
return uint64(*height), nil*/// todo realise

return nil, nil
}
Expand All @@ -788,7 +788,7 @@ func handleVerifyChain(s *Server, cmd interface{}, closeChan <-chan struct{}) (i
checkDepth = *c.CheckDepth
}
return VerifyDB(consensus.ActiveNetParams, utxo.GetUtxoCacheInstance(), checkLevel, checkDepth), nil*/ // todo open
return VerifyDB(consensus.ActiveNetParams, utxo.GetUtxoCacheInstance(), checkLevel, checkDepth), nil*/// todo open
return nil, nil
}

Expand All @@ -809,7 +809,7 @@ func handlePreciousblock(s *Server, cmd interface{}, closeChan <-chan struct{})
chain.PreciousBlock(consensus.ActiveNetParams, &state, blockIndex)
if !state.IsValid() {
}*/ // todo open
}*/// todo open
return nil, nil
}

Expand All @@ -836,7 +836,7 @@ func handlInvalidateBlock(s *Server, cmd interface{}, closeChan <-chan struct{})
Code: btcjson.ErrRPCDatabase,
Message: state.GetRejectReason(),
}
}*/ // todo open
}*/// todo open

return nil, nil
}
Expand All @@ -862,7 +862,7 @@ func handleReconsiderBlock(s *Server, cmd interface{}, closeChan <-chan struct{}
Code: btcjson.ErrRPCDatabase,
Message: state.FormatStateMessage(),
}
}*/ // todo open
}*/// todo open
return nil, nil
}

Expand All @@ -875,7 +875,14 @@ func handleWaitForBlock(s *Server, cmd interface{}, closeChan <-chan struct{}) (
}

func handleWaitForBlockHeight(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return nil, nil
//todo handle args

gchain := chain.GetInstance()
ret := btcjson.WaitForBlockHeight{
Hash: gchain.Tip().GetBlockHash().String(),
Height: gchain.TipHeight(),
}
return ret, nil
}

func registerBlockchainRPCCommands() {
Expand Down
2 changes: 1 addition & 1 deletion rpc/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func NewServer(config *ServerConfig) (*Server, error) {
//gbtWorkState: newGbtWorkState(config.TimeSource), // todo open
helpCacher: newHelpCacher(),
requestProcessShutdown: make(chan struct{}),
quit: make(chan int),
quit: make(chan int),
}
if conf.Cfg.RPC.RPCUser != "" && conf.Cfg.RPC.RPCPass != "" {
login := conf.Cfg.RPC.RPCUser + ":" + conf.Cfg.RPC.RPCPass
Expand Down

0 comments on commit 0a967c4

Please sign in to comment.