Skip to content

Commit e9adf78

Browse files
authored
681 disable smithing non registered node (#694)
* #681 add zero participation score blocker * #681 inject blockchainStatusService to noderegistration service * #681 add flag to say if a node is blocksmith or not * #681 update main.go with new struct constructor * #681 only update the blocksmith status when detect zero participation score * #681 compare with bytes.equal()
1 parent 8902947 commit e9adf78

File tree

8 files changed

+58
-19
lines changed

8 files changed

+58
-19
lines changed

cmd/block/blockGenerator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func initialize(
152152
query.NewParticipationScoreQuery(),
153153
query.NewBlockQuery(chainType),
154154
log.New(),
155+
&mockBlockchainStatusService{},
155156
)
156157
blocksmithStrategy = strategy.NewBlocksmithStrategyMain(
157158
queryExecutor, query.NewNodeRegistrationQuery(), query.NewSkippedBlocksmithQuery(), log.New(),

common/blocker/blocker.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ type (
1616
)
1717

1818
var (
19-
DBErr TypeBlocker = "DBErr"
20-
DBRowNotFound TypeBlocker = "DBRowNotFound"
21-
BlockErr TypeBlocker = "BlockErr"
22-
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
23-
RequestParameterErr TypeBlocker = "RequestParameterErr"
24-
AppErr TypeBlocker = "AppErr"
25-
AuthErr TypeBlocker = "AuthErr"
26-
ValidationErr TypeBlocker = "ValidationErr"
27-
DuplicateMempoolErr TypeBlocker = "DuplicateMempoolErr"
28-
DuplicateTransactionErr TypeBlocker = "DuplicateTransactionErr"
29-
ParserErr TypeBlocker = "ParserErr"
30-
ServerError TypeBlocker = "ServerError"
31-
SmithingErr TypeBlocker = "SmithingErr"
32-
ChainValidationErr TypeBlocker = "ChainValidationErr"
33-
P2PNetworkConnectionErr TypeBlocker = "P2PNetworkConnectionErr"
19+
DBErr TypeBlocker = "DBErr"
20+
DBRowNotFound TypeBlocker = "DBRowNotFound"
21+
BlockErr TypeBlocker = "BlockErr"
22+
BlockNotFoundErr TypeBlocker = "BlockNotFoundErr"
23+
RequestParameterErr TypeBlocker = "RequestParameterErr"
24+
AppErr TypeBlocker = "AppErr"
25+
AuthErr TypeBlocker = "AuthErr"
26+
ValidationErr TypeBlocker = "ValidationErr"
27+
DuplicateMempoolErr TypeBlocker = "DuplicateMempoolErr"
28+
DuplicateTransactionErr TypeBlocker = "DuplicateTransactionErr"
29+
ParserErr TypeBlocker = "ParserErr"
30+
ServerError TypeBlocker = "ServerError"
31+
SmithingErr TypeBlocker = "SmithingErr"
32+
ZeroParticipationScoreErr TypeBlocker = "ZeroParticipationScoreErr"
33+
ChainValidationErr TypeBlocker = "ChainValidationErr"
34+
P2PNetworkConnectionErr TypeBlocker = "P2PNetworkConnectionErr"
3435
)
3536

3637
func NewBlocker(typeBlocker TypeBlocker, message string) error {

core/service/blockMainService.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,7 @@ func (bs *BlockService) WillSmith(
14761476
// no negative scores allowed
14771477
blocksmithScore = 0
14781478
bs.Logger.Errorf("Participation score calculation: %s", err)
1479+
return 0, blocker.NewBlocker(blocker.ZeroParticipationScoreErr, "participation score = 0")
14791480
}
14801481
err = bs.BlocksmithStrategy.CalculateSmith(
14811482
lastBlock,

core/service/blockchainStatusService.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type (
1818
IsSmithing(ct chaintype.ChainType) bool
1919
SetIsDownloadingSnapshot(ct chaintype.ChainType, isDownloadingSnapshot bool)
2020
IsDownloadingSnapshot(ct chaintype.ChainType) bool
21+
SetIsBlocksmith(isBlocksmith bool)
22+
IsBlocksmith() bool
2123
}
2224
)
2325

@@ -33,6 +35,7 @@ var (
3335
isDownloadingSnapshot = model.NewMapIntBool()
3436
isSmithing = model.NewMapIntBool()
3537
isSmithingLocked bool
38+
isBlocksmith bool
3639
)
3740

3841
func NewBlockchainStatusService(
@@ -123,3 +126,11 @@ func (btss *BlockchainStatusService) IsDownloadingSnapshot(ct chaintype.ChainTyp
123126
}
124127
return false
125128
}
129+
130+
func (btss *BlockchainStatusService) SetIsBlocksmith(blocksmith bool) {
131+
isBlocksmith = blocksmith
132+
}
133+
134+
func (btss *BlockchainStatusService) IsBlocksmith() bool {
135+
return isBlocksmith
136+
}

core/service/nodeAdminCoreService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (*NodeAdminService) GetLastNodeKey(nodeKeys []*model.NodeKey) *model.NodeKe
120120
return max
121121
}
122122

123-
// GenerateNodeKey generates a new node ket from its seed and store it, together with relative public key into node_keys file
123+
// GenerateNodeKey generates a new node key from its seed and store it, together with relative public key into node_keys file
124124
func (nas *NodeAdminService) GenerateNodeKey(seed string) ([]byte, error) {
125125
publicKey := crypto.NewEd25519Signature().GetPublicKeyFromSeed(seed)
126126
nodeKey := &model.NodeKey{

core/service/nodeRegistrationCoreService.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"bytes"
45
"math/big"
56
"sort"
67
"sync"
@@ -31,6 +32,7 @@ type (
3132
blockHeight uint32,
3233
) (*model.ScrambledNodes, error)
3334
AddParticipationScore(nodeID, scoreDelta int64, height uint32, dbTx bool) (newScore int64, err error)
35+
SetCurrentNodePublicKey(publicKey []byte)
3436
}
3537

3638
// NodeRegistrationService mockable service methods
@@ -45,6 +47,8 @@ type (
4547
ScrambledNodes map[uint32]*model.ScrambledNodes
4648
ScrambledNodesLock sync.RWMutex
4749
MemoizedLatestScrambledNodes *model.ScrambledNodes
50+
BlockchainStatusService BlockchainStatusServiceInterface
51+
CurrentNodePublicKey []byte
4852
}
4953
)
5054

@@ -55,6 +59,7 @@ func NewNodeRegistrationService(
5559
participationScoreQuery query.ParticipationScoreQueryInterface,
5660
blockQuery query.BlockQueryInterface,
5761
logger *log.Logger,
62+
blockchainStatusService BlockchainStatusServiceInterface,
5863
) *NodeRegistrationService {
5964
return &NodeRegistrationService{
6065
QueryExecutor: queryExecutor,
@@ -65,6 +70,7 @@ func NewNodeRegistrationService(
6570
NodeAdmittanceCycle: constant.NodeAdmittanceCycle,
6671
Logger: logger,
6772
ScrambledNodes: map[uint32]*model.ScrambledNodes{},
73+
BlockchainStatusService: blockchainStatusService,
6874
}
6975
}
7076

@@ -149,6 +155,9 @@ func (nrs *NodeRegistrationService) AdmitNodes(nodeRegistrations []*model.NodeRe
149155
if err := nrs.QueryExecutor.ExecuteTransactions(queries); err != nil {
150156
return err
151157
}
158+
if bytes.Equal(nrs.CurrentNodePublicKey, nodeRegistration.NodePublicKey) {
159+
nrs.BlockchainStatusService.SetIsBlocksmith(true)
160+
}
152161
}
153162
return nil
154163
}
@@ -411,3 +420,9 @@ func (nrs *NodeRegistrationService) AddParticipationScore(nodeID, scoreDelta int
411420
err = nrs.QueryExecutor.ExecuteTransactions(updateParticipationScoreQuery)
412421
return newScore, err
413422
}
423+
424+
// SetCurrentNodePublicKey set the public key of running node, this information will be used to check if current node is
425+
// being admitted and can start unlock smithing process
426+
func (nrs *NodeRegistrationService) SetCurrentNodePublicKey(publicKey []byte) {
427+
nrs.CurrentNodePublicKey = publicKey
428+
}

core/smith/blockchainProcessor.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package smith
22

33
import (
4-
"github.com/zoobc/zoobc-core/common/chaintype"
54
"time"
65

6+
"github.com/zoobc/zoobc-core/common/chaintype"
7+
78
log "github.com/sirupsen/logrus"
89
"github.com/zoobc/zoobc-core/common/blocker"
910
"github.com/zoobc/zoobc-core/common/constant"
@@ -180,12 +181,15 @@ func (bp *BlockchainProcessor) Start(sleepPeriod time.Duration) {
180181
return
181182
case <-ticker.C:
182183
// when starting a node, do not start smithing until the main blocks have been fully downloaded
183-
if !bp.BlockchainStatusService.IsSmithingLocked() {
184+
if !bp.BlockchainStatusService.IsSmithingLocked() && bp.BlockchainStatusService.IsBlocksmith() {
184185
err := bp.StartSmithing()
185186
if err != nil {
186187
bp.Logger.Debugf("Smith Error for %s. %s", bp.BlockService.GetChainType().GetName(), err.Error())
187188
bp.BlockchainStatusService.SetIsSmithing(bp.ChainType, false)
188189
bp.smithError = err
190+
if blockErr, ok := err.(blocker.Blocker); ok && blockErr.Type == blocker.ZeroParticipationScoreErr {
191+
bp.BlockchainStatusService.SetIsBlocksmith(false)
192+
}
189193
} else {
190194
bp.BlockchainStatusService.SetIsSmithing(bp.ChainType, true)
191195
bp.smithError = nil

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ func init() {
140140
kvExecutor = kvdb.NewKVExecutor(badgerDb)
141141

142142
// initialize services
143+
blockchainStatusService = service.NewBlockchainStatusService(true, loggerCoreService)
144+
143145
nodeRegistrationService = service.NewNodeRegistrationService(
144146
queryExecutor,
145147
query.NewAccountBalanceQuery(),
146148
query.NewNodeRegistrationQuery(),
147149
query.NewParticipationScoreQuery(),
148150
query.NewBlockQuery(mainchain),
149151
loggerCoreService,
152+
blockchainStatusService,
150153
)
151154
receiptService = service.NewReceiptService(
152155
query.NewNodeReceiptQuery(),
@@ -161,7 +164,6 @@ func init() {
161164
query.NewPublishedReceiptQuery(),
162165
receiptUtil,
163166
)
164-
blockchainStatusService = service.NewBlockchainStatusService(true, loggerCoreService)
165167
spineBlockManifestService = service.NewSpineBlockManifestService(
166168
queryExecutor,
167169
query.NewSpineBlockManifestQuery(),
@@ -557,6 +559,10 @@ func startMainchain() {
557559
)
558560
}
559561
if node != nil {
562+
// register node config public key, so node registration service can detect if node has been admitted
563+
nodeRegistrationService.SetCurrentNodePublicKey(nodePublicKey)
564+
// default to isBlocksmith=true
565+
blockchainStatusService.SetIsBlocksmith(true)
560566
mainchainProcessor = smith.NewBlockchainProcessor(
561567
mainchainBlockService.GetChainType(),
562568
model.NewBlocksmith(nodeSecretPhrase, nodePublicKey, node.NodeID),

0 commit comments

Comments
 (0)