Skip to content

681 disable smithing non registered node #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 27, 2020
1 change: 1 addition & 0 deletions cmd/block/blockGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func initialize(
query.NewParticipationScoreQuery(),
query.NewBlockQuery(chainType),
log.New(),
&mockBlockchainStatusService{},
)
blocksmithStrategy = strategy.NewBlocksmithStrategyMain(
queryExecutor, query.NewNodeRegistrationQuery(), query.NewSkippedBlocksmithQuery(), log.New(),
Expand Down
31 changes: 16 additions & 15 deletions common/blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ type (
)

var (
DBErr TypeBlocker = "DBErr"
DBRowNotFound TypeBlocker = "DBRowNotFound"
BlockErr TypeBlocker = "BlockErr"
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
RequestParameterErr TypeBlocker = "RequestParameterErr"
AppErr TypeBlocker = "AppErr"
AuthErr TypeBlocker = "AuthErr"
ValidationErr TypeBlocker = "ValidationErr"
DuplicateMempoolErr TypeBlocker = "DuplicateMempoolErr"
DuplicateTransactionErr TypeBlocker = "DuplicateTransactionErr"
ParserErr TypeBlocker = "ParserErr"
ServerError TypeBlocker = "ServerError"
SmithingErr TypeBlocker = "SmithingErr"
ChainValidationErr TypeBlocker = "ChainValidationErr"
P2PNetworkConnectionErr TypeBlocker = "P2PNetworkConnectionErr"
DBErr TypeBlocker = "DBErr"
DBRowNotFound TypeBlocker = "DBRowNotFound"
BlockErr TypeBlocker = "BlockErr"
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
RequestParameterErr TypeBlocker = "RequestParameterErr"
AppErr TypeBlocker = "AppErr"
AuthErr TypeBlocker = "AuthErr"
ValidationErr TypeBlocker = "ValidationErr"
DuplicateMempoolErr TypeBlocker = "DuplicateMempoolErr"
DuplicateTransactionErr TypeBlocker = "DuplicateTransactionErr"
ParserErr TypeBlocker = "ParserErr"
ServerError TypeBlocker = "ServerError"
SmithingErr TypeBlocker = "SmithingErr"
ZeroParticipationScoreErr TypeBlocker = "ZeroParticipationScoreErr"
ChainValidationErr TypeBlocker = "ChainValidationErr"
P2PNetworkConnectionErr TypeBlocker = "P2PNetworkConnectionErr"
)

func NewBlocker(typeBlocker TypeBlocker, message string) error {
Expand Down
1 change: 1 addition & 0 deletions core/service/blockMainService.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,7 @@ func (bs *BlockService) WillSmith(
// no negative scores allowed
blocksmithScore = 0
bs.Logger.Errorf("Participation score calculation: %s", err)
return 0, blocker.NewBlocker(blocker.ZeroParticipationScoreErr, "participation score = 0")
}
err = bs.BlocksmithStrategy.CalculateSmith(
lastBlock,
Expand Down
11 changes: 11 additions & 0 deletions core/service/blockchainStatusService.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type (
IsSmithing(ct chaintype.ChainType) bool
SetIsDownloadingSnapshot(ct chaintype.ChainType, isDownloadingSnapshot bool)
IsDownloadingSnapshot(ct chaintype.ChainType) bool
SetIsBlocksmith(isBlocksmith bool)
IsBlocksmith() bool
}
)

Expand All @@ -33,6 +35,7 @@ var (
isDownloadingSnapshot = model.NewMapIntBool()
isSmithing = model.NewMapIntBool()
isSmithingLocked bool
isBlocksmith bool
)

func NewBlockchainStatusService(
Expand Down Expand Up @@ -123,3 +126,11 @@ func (btss *BlockchainStatusService) IsDownloadingSnapshot(ct chaintype.ChainTyp
}
return false
}

func (btss *BlockchainStatusService) SetIsBlocksmith(blocksmith bool) {
isBlocksmith = blocksmith
}

func (btss *BlockchainStatusService) IsBlocksmith() bool {
return isBlocksmith
}
2 changes: 1 addition & 1 deletion core/service/nodeAdminCoreService.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (*NodeAdminService) GetLastNodeKey(nodeKeys []*model.NodeKey) *model.NodeKe
return max
}

// GenerateNodeKey generates a new node ket from its seed and store it, together with relative public key into node_keys file
// GenerateNodeKey generates a new node key from its seed and store it, together with relative public key into node_keys file
func (nas *NodeAdminService) GenerateNodeKey(seed string) ([]byte, error) {
publicKey := crypto.NewEd25519Signature().GetPublicKeyFromSeed(seed)
nodeKey := &model.NodeKey{
Expand Down
15 changes: 15 additions & 0 deletions core/service/nodeRegistrationCoreService.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"bytes"
"math/big"
"sort"
"sync"
Expand Down Expand Up @@ -31,6 +32,7 @@ type (
blockHeight uint32,
) (*model.ScrambledNodes, error)
AddParticipationScore(nodeID, scoreDelta int64, height uint32, dbTx bool) (newScore int64, err error)
SetCurrentNodePublicKey(publicKey []byte)
}

// NodeRegistrationService mockable service methods
Expand All @@ -45,6 +47,8 @@ type (
ScrambledNodes map[uint32]*model.ScrambledNodes
ScrambledNodesLock sync.RWMutex
MemoizedLatestScrambledNodes *model.ScrambledNodes
BlockchainStatusService BlockchainStatusServiceInterface
CurrentNodePublicKey []byte
}
)

Expand All @@ -55,6 +59,7 @@ func NewNodeRegistrationService(
participationScoreQuery query.ParticipationScoreQueryInterface,
blockQuery query.BlockQueryInterface,
logger *log.Logger,
blockchainStatusService BlockchainStatusServiceInterface,
) *NodeRegistrationService {
return &NodeRegistrationService{
QueryExecutor: queryExecutor,
Expand All @@ -65,6 +70,7 @@ func NewNodeRegistrationService(
NodeAdmittanceCycle: constant.NodeAdmittanceCycle,
Logger: logger,
ScrambledNodes: map[uint32]*model.ScrambledNodes{},
BlockchainStatusService: blockchainStatusService,
}
}

Expand Down Expand Up @@ -149,6 +155,9 @@ func (nrs *NodeRegistrationService) AdmitNodes(nodeRegistrations []*model.NodeRe
if err := nrs.QueryExecutor.ExecuteTransactions(queries); err != nil {
return err
}
if bytes.Equal(nrs.CurrentNodePublicKey, nodeRegistration.NodePublicKey) {
nrs.BlockchainStatusService.SetIsBlocksmith(true)
}
}
return nil
}
Expand Down Expand Up @@ -411,3 +420,9 @@ func (nrs *NodeRegistrationService) AddParticipationScore(nodeID, scoreDelta int
err = nrs.QueryExecutor.ExecuteTransactions(updateParticipationScoreQuery)
return newScore, err
}

// SetCurrentNodePublicKey set the public key of running node, this information will be used to check if current node is
// being admitted and can start unlock smithing process
func (nrs *NodeRegistrationService) SetCurrentNodePublicKey(publicKey []byte) {
nrs.CurrentNodePublicKey = publicKey
}
8 changes: 6 additions & 2 deletions core/smith/blockchainProcessor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package smith

import (
"github.com/zoobc/zoobc-core/common/chaintype"
"time"

"github.com/zoobc/zoobc-core/common/chaintype"

log "github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/constant"
Expand Down Expand Up @@ -180,12 +181,15 @@ func (bp *BlockchainProcessor) Start(sleepPeriod time.Duration) {
return
case <-ticker.C:
// when starting a node, do not start smithing until the main blocks have been fully downloaded
if !bp.BlockchainStatusService.IsSmithingLocked() {
if !bp.BlockchainStatusService.IsSmithingLocked() && bp.BlockchainStatusService.IsBlocksmith() {
err := bp.StartSmithing()
if err != nil {
bp.Logger.Debugf("Smith Error for %s. %s", bp.BlockService.GetChainType().GetName(), err.Error())
bp.BlockchainStatusService.SetIsSmithing(bp.ChainType, false)
bp.smithError = err
if blockErr, ok := err.(blocker.Blocker); ok && blockErr.Type == blocker.ZeroParticipationScoreErr {
bp.BlockchainStatusService.SetIsBlocksmith(false)
}
} else {
bp.BlockchainStatusService.SetIsSmithing(bp.ChainType, true)
bp.smithError = nil
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ func init() {
kvExecutor = kvdb.NewKVExecutor(badgerDb)

// initialize services
blockchainStatusService = service.NewBlockchainStatusService(true, loggerCoreService)

nodeRegistrationService = service.NewNodeRegistrationService(
queryExecutor,
query.NewAccountBalanceQuery(),
query.NewNodeRegistrationQuery(),
query.NewParticipationScoreQuery(),
query.NewBlockQuery(mainchain),
loggerCoreService,
blockchainStatusService,
)
receiptService = service.NewReceiptService(
query.NewNodeReceiptQuery(),
Expand All @@ -161,7 +164,6 @@ func init() {
query.NewPublishedReceiptQuery(),
receiptUtil,
)
blockchainStatusService = service.NewBlockchainStatusService(true, loggerCoreService)
spineBlockManifestService = service.NewSpineBlockManifestService(
queryExecutor,
query.NewSpineBlockManifestQuery(),
Expand Down Expand Up @@ -557,6 +559,10 @@ func startMainchain() {
)
}
if node != nil {
// register node config public key, so node registration service can detect if node has been admitted
nodeRegistrationService.SetCurrentNodePublicKey(nodePublicKey)
// default to isBlocksmith=true
blockchainStatusService.SetIsBlocksmith(true)
mainchainProcessor = smith.NewBlockchainProcessor(
mainchainBlockService.GetChainType(),
model.NewBlocksmith(nodeSecretPhrase, nodePublicKey, node.NodeID),
Expand Down