mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 06:36:46 +00:00
New definition for "out of sync" (#1996)
* Changed the definition of should mine and redefined "out of sync" * Check `IsNearlySynced` only if IBD running since IBD check is cheap
This commit is contained in:
parent
cb65dae63d
commit
95fa045297
@ -53,6 +53,7 @@ type FlowContext struct {
|
|||||||
onPruningPointUTXOSetOverrideHandler OnPruningPointUTXOSetOverrideHandler
|
onPruningPointUTXOSetOverrideHandler OnPruningPointUTXOSetOverrideHandler
|
||||||
onTransactionAddedToMempoolHandler OnTransactionAddedToMempoolHandler
|
onTransactionAddedToMempoolHandler OnTransactionAddedToMempoolHandler
|
||||||
|
|
||||||
|
expectedDAAWindowDurationInMilliseconds int64
|
||||||
lastRebroadcastTime time.Time
|
lastRebroadcastTime time.Time
|
||||||
sharedRequestedTransactions *SharedRequestedTransactions
|
sharedRequestedTransactions *SharedRequestedTransactions
|
||||||
|
|
||||||
@ -92,6 +93,8 @@ func New(cfg *config.Config, domain domain.Domain, addressManager *addressmanage
|
|||||||
transactionIDsToPropagate: []*externalapi.DomainTransactionID{},
|
transactionIDsToPropagate: []*externalapi.DomainTransactionID{},
|
||||||
lastTransactionIDPropagationTime: time.Now(),
|
lastTransactionIDPropagationTime: time.Now(),
|
||||||
shutdownChan: make(chan struct{}),
|
shutdownChan: make(chan struct{}),
|
||||||
|
expectedDAAWindowDurationInMilliseconds: cfg.NetParams().TargetTimePerBlock.Milliseconds() *
|
||||||
|
int64(cfg.NetParams().DifficultyAdjustmentWindowSize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,21 +2,12 @@ package flowcontext
|
|||||||
|
|
||||||
import "github.com/kaspanet/kaspad/util/mstime"
|
import "github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
|
||||||
const (
|
// IsNearlySynced returns whether this node is considered synced or close to being synced. This info
|
||||||
maxSelectedParentTimeDiffToAllowMiningInMilliSeconds = 60 * 60 * 1000 // 1 Hour
|
// is used to determine if it's ok to use a block template from this node for mining purposes.
|
||||||
)
|
func (f *FlowContext) IsNearlySynced() (bool, error) {
|
||||||
|
|
||||||
// ShouldMine returns whether it's ok to use block template from this node
|
|
||||||
// for mining purposes.
|
|
||||||
func (f *FlowContext) ShouldMine() (bool, error) {
|
|
||||||
peers := f.Peers()
|
peers := f.Peers()
|
||||||
if len(peers) == 0 {
|
if len(peers) == 0 {
|
||||||
log.Debugf("The node is not connected, so ShouldMine returns false")
|
log.Debugf("The node is not connected to peers, so IsNearlySynced returns false")
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.IsIBDRunning() {
|
|
||||||
log.Debugf("IBD is running, so ShouldMine returns false")
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,13 +26,15 @@ func (f *FlowContext) ShouldMine() (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
now := mstime.Now().UnixMilliseconds()
|
now := mstime.Now().UnixMilliseconds()
|
||||||
if now-virtualSelectedParentHeader.TimeInMilliseconds() < maxSelectedParentTimeDiffToAllowMiningInMilliSeconds {
|
// As a heuristic, we allow the node to mine if he is likely to be within the current DAA window of fully synced nodes.
|
||||||
log.Debugf("The selected tip timestamp is recent (%d), so ShouldMine returns true",
|
// Such blocks contribute to security by maintaining the current difficulty despite possibly being slightly out of sync.
|
||||||
|
if now-virtualSelectedParentHeader.TimeInMilliseconds() < f.expectedDAAWindowDurationInMilliseconds {
|
||||||
|
log.Debugf("The selected tip timestamp is recent (%d), so IsNearlySynced returns true",
|
||||||
virtualSelectedParentHeader.TimeInMilliseconds())
|
virtualSelectedParentHeader.TimeInMilliseconds())
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("The selected tip timestamp is old (%d), so ShouldMine returns false",
|
log.Debugf("The selected tip timestamp is old (%d), so IsNearlySynced returns false",
|
||||||
virtualSelectedParentHeader.TimeInMilliseconds())
|
virtualSelectedParentHeader.TimeInMilliseconds())
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ type RelayInvsContext interface {
|
|||||||
IsOrphan(blockHash *externalapi.DomainHash) bool
|
IsOrphan(blockHash *externalapi.DomainHash) bool
|
||||||
IsIBDRunning() bool
|
IsIBDRunning() bool
|
||||||
IsRecoverableError(err error) bool
|
IsRecoverableError(err error) bool
|
||||||
|
IsNearlySynced() (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type handleRelayInvsFlow struct {
|
type handleRelayInvsFlow struct {
|
||||||
@ -106,11 +107,17 @@ func (flow *handleRelayInvsFlow) start() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block relay is disabled during IBD
|
// Block relay is disabled if the node is already during IBD AND considered out of sync
|
||||||
if flow.IsIBDRunning() {
|
if flow.IsIBDRunning() {
|
||||||
log.Debugf("Got block %s while in IBD. continuing...", inv.Hash)
|
isNearlySynced, err := flow.IsNearlySynced()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !isNearlySynced {
|
||||||
|
log.Debugf("Got block %s while in IBD and the node is out of sync. Continuing...", inv.Hash)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("Requesting block %s", inv.Hash)
|
log.Debugf("Requesting block %s", inv.Hash)
|
||||||
block, exists, err := flow.requestBlock(inv.Hash)
|
block, exists, err := flow.requestBlock(inv.Hash)
|
||||||
|
@ -22,7 +22,7 @@ type TransactionsRelayContext interface {
|
|||||||
SharedRequestedTransactions() *flowcontext.SharedRequestedTransactions
|
SharedRequestedTransactions() *flowcontext.SharedRequestedTransactions
|
||||||
OnTransactionAddedToMempool()
|
OnTransactionAddedToMempool()
|
||||||
EnqueueTransactionIDsForPropagation(transactionIDs []*externalapi.DomainTransactionID) error
|
EnqueueTransactionIDsForPropagation(transactionIDs []*externalapi.DomainTransactionID) error
|
||||||
IsIBDRunning() bool
|
IsNearlySynced() (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type handleRelayedTransactionsFlow struct {
|
type handleRelayedTransactionsFlow struct {
|
||||||
@ -50,7 +50,12 @@ func (flow *handleRelayedTransactionsFlow) start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if flow.IsIBDRunning() {
|
isNearlySynced, err := flow.IsNearlySynced()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Transaction relay is disabled if the node is out of sync and thus not mining
|
||||||
|
if !isNearlySynced {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +47,8 @@ func (m *mocTransactionsRelayContext) EnqueueTransactionIDsForPropagation(transa
|
|||||||
func (m *mocTransactionsRelayContext) OnTransactionAddedToMempool() {
|
func (m *mocTransactionsRelayContext) OnTransactionAddedToMempool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mocTransactionsRelayContext) IsIBDRunning() bool {
|
func (m *mocTransactionsRelayContext) IsNearlySynced() (bool, error) {
|
||||||
return false
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHandleRelayedTransactionsNotFound tests the flow of HandleRelayedTransactions when the peer doesn't
|
// TestHandleRelayedTransactionsNotFound tests the flow of HandleRelayedTransactions when the peer doesn't
|
||||||
|
@ -36,6 +36,7 @@ type RelayInvsContext interface {
|
|||||||
IsOrphan(blockHash *externalapi.DomainHash) bool
|
IsOrphan(blockHash *externalapi.DomainHash) bool
|
||||||
IsIBDRunning() bool
|
IsIBDRunning() bool
|
||||||
IsRecoverableError(err error) bool
|
IsRecoverableError(err error) bool
|
||||||
|
IsNearlySynced() (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type handleRelayInvsFlow struct {
|
type handleRelayInvsFlow struct {
|
||||||
@ -106,11 +107,17 @@ func (flow *handleRelayInvsFlow) start() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block relay is disabled during IBD
|
// Block relay is disabled if the node is already during IBD AND considered out of sync
|
||||||
if flow.IsIBDRunning() {
|
if flow.IsIBDRunning() {
|
||||||
log.Debugf("Got block %s while in IBD. continuing...", inv.Hash)
|
isNearlySynced, err := flow.IsNearlySynced()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !isNearlySynced {
|
||||||
|
log.Debugf("Got block %s while in IBD and the node is out of sync. Continuing...", inv.Hash)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("Requesting block %s", inv.Hash)
|
log.Debugf("Requesting block %s", inv.Hash)
|
||||||
block, exists, err := flow.requestBlock(inv.Hash)
|
block, exists, err := flow.requestBlock(inv.Hash)
|
||||||
|
@ -22,7 +22,7 @@ type TransactionsRelayContext interface {
|
|||||||
SharedRequestedTransactions() *flowcontext.SharedRequestedTransactions
|
SharedRequestedTransactions() *flowcontext.SharedRequestedTransactions
|
||||||
OnTransactionAddedToMempool()
|
OnTransactionAddedToMempool()
|
||||||
EnqueueTransactionIDsForPropagation(transactionIDs []*externalapi.DomainTransactionID) error
|
EnqueueTransactionIDsForPropagation(transactionIDs []*externalapi.DomainTransactionID) error
|
||||||
IsIBDRunning() bool
|
IsNearlySynced() (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type handleRelayedTransactionsFlow struct {
|
type handleRelayedTransactionsFlow struct {
|
||||||
@ -50,7 +50,12 @@ func (flow *handleRelayedTransactionsFlow) start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if flow.IsIBDRunning() {
|
isNearlySynced, err := flow.IsNearlySynced()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Transaction relay is disabled if the node is out of sync and thus not mining
|
||||||
|
if !isNearlySynced {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +47,8 @@ func (m *mocTransactionsRelayContext) EnqueueTransactionIDsForPropagation(transa
|
|||||||
func (m *mocTransactionsRelayContext) OnTransactionAddedToMempool() {
|
func (m *mocTransactionsRelayContext) OnTransactionAddedToMempool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mocTransactionsRelayContext) IsIBDRunning() bool {
|
func (m *mocTransactionsRelayContext) IsNearlySynced() (bool, error) {
|
||||||
return false
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestHandleRelayedTransactionsNotFound tests the flow of HandleRelayedTransactions when the peer doesn't
|
// TestHandleRelayedTransactionsNotFound tests the flow of HandleRelayedTransactions when the peer doesn't
|
||||||
|
@ -118,7 +118,7 @@ func (m *Manager) SetOnTransactionAddedToMempoolHandler(onTransactionAddedToMemp
|
|||||||
// ShouldMine returns whether it's ok to use block template from this node
|
// ShouldMine returns whether it's ok to use block template from this node
|
||||||
// for mining purposes.
|
// for mining purposes.
|
||||||
func (m *Manager) ShouldMine() (bool, error) {
|
func (m *Manager) ShouldMine() (bool, error) {
|
||||||
return m.context.ShouldMine()
|
return m.context.IsNearlySynced()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIBDRunning returns true if IBD is currently marked as running
|
// IsIBDRunning returns true if IBD is currently marked as running
|
||||||
|
Loading…
x
Reference in New Issue
Block a user