Skip to content

Commit 6ffac49

Browse files
andy-shi88astaphobia
authored andcommitted
395 skipped blocksmith (#404)
* #395 add skipped blocksmith table migration * #395 move blocksmith sort to block service * #395 remove sort blocksmith listener * #395 use value instead of pointer * #395 add skipped blocksmith to block extended info * #395 fill real data for block extended info * #395 add method for getting receipt score * #395 get total node registry at height n when calculating total max receipt * #395 use constant max score change
1 parent 47d83e2 commit 6ffac49

20 files changed

+831
-514
lines changed

cmd/block/blockGenerator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func initialize(
136136
query.NewTransactionQuery(chainType),
137137
query.NewMerkleTreeQuery(),
138138
query.NewPublishedReceiptQuery(),
139+
query.NewSkippedBlocksmithQuery(),
139140
crypto.NewSignature(),
140141
mempoolService,
141142
receiptService,

cmd/genesisblock/genesisGenerator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ func getGenesisBlockID(genesisEntries []genesisEntry) int64 {
316316
nil,
317317
nil,
318318
nil,
319+
nil,
319320
&transaction.TypeSwitcher{},
320321
nil,
321322
nil,

common/constant/participationScore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const (
1010
UnlinkedReceiptScore float32 = 0.5
1111
// MaxScoreChange the maximum score that node wll get
1212
MaxScoreChange = 10 * int64(ScalarReceiptScore)
13+
// punishment amount
14+
ParticipationScorePunishAmount = -1 * MaxScoreChange / 2
1315
// MaxReceipt the maximum receipt will publish in every block
1416
MaxReceipt uint32 = 20
1517
// MaxParticipationScore maximum achievable score, this will be important to maintain smithing process so it doesn't

common/database/migration.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ func (m *Migration) Init() error {
191191
AlTER TABLE "main_block"
192192
ADD COLUMN "block_hash" BLOB AFTER "id"
193193
`,
194+
`
195+
CREATE TABLE IF NOT EXISTS "skipped_blocksmith" (
196+
"blocksmith_public_key" BLOB,
197+
"pop_change" INTEGER,
198+
"block_height" INTEGER,
199+
"blocksmith_index" INTEGER
200+
)
201+
`,
194202
}
195203
return nil
196204
}

common/model/block.pb.go

Lines changed: 18 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/model/skippedBlocksmith.pb.go

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/query/nodeRegistrationQuery.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type (
2626
GetNodeRegistryAtHeight(height uint32) string
2727
ExtractModel(nr *model.NodeRegistration) []interface{}
2828
BuildModel(nodeRegistrations []*model.NodeRegistration, rows *sql.Rows) ([]*model.NodeRegistration, error)
29-
BuildBlocksmith(blocksmiths []*model.Blocksmith, rows *sql.Rows) []*model.Blocksmith
29+
BuildBlocksmith(blocksmiths []*model.Blocksmith, rows *sql.Rows) ([]*model.Blocksmith, error)
3030
BuildNodeAddress(fullNodeAddress string) *model.NodeAddress
3131
ExtractNodeAddress(nodeAddress *model.NodeAddress) string
3232
Scan(nr *model.NodeRegistration, row *sql.Row) error
@@ -172,7 +172,6 @@ func (nrq *NodeRegistrationQuery) GetNodeRegistryAtHeight(height uint32) string
172172

173173
// ExtractModel extract the model struct fields to the order of NodeRegistrationQuery.Fields
174174
func (nrq *NodeRegistrationQuery) ExtractModel(tx *model.NodeRegistration) []interface{} {
175-
176175
return []interface{}{
177176
tx.NodeID,
178177
tx.NodePublicKey,
@@ -198,7 +197,10 @@ func (nrq *NodeRegistrationQuery) BuildModel(
198197
dumpString string
199198
)
200199

201-
columns, _ := rows.Columns()
200+
columns, err := rows.Columns()
201+
if err != nil {
202+
return nil, err
203+
}
202204
for i := 0; i < len(columns)-len(nrq.Fields); i++ {
203205
ignoredAggregateColumns = append(ignoredAggregateColumns, &dumpString)
204206
}
@@ -222,29 +224,36 @@ func (nrq *NodeRegistrationQuery) BuildModel(
222224
&nr.Height,
223225
)
224226
basicFieldsReceiver = append(basicFieldsReceiver, ignoredAggregateColumns...)
225-
_ = rows.Scan(basicFieldsReceiver...)
226-
227+
err := rows.Scan(basicFieldsReceiver...)
228+
if err != nil {
229+
return nil, err
230+
}
227231
nr.NodeAddress = nrq.BuildNodeAddress(fullNodeAddress)
228232
nodeRegistrations = append(nodeRegistrations, &nr)
229233
}
230234
return nodeRegistrations, nil
231235
}
232236

233-
func (*NodeRegistrationQuery) BuildBlocksmith(blocksmiths []*model.Blocksmith, rows *sql.Rows) []*model.Blocksmith {
237+
func (*NodeRegistrationQuery) BuildBlocksmith(
238+
blocksmiths []*model.Blocksmith, rows *sql.Rows,
239+
) ([]*model.Blocksmith, error) {
234240
for rows.Next() {
235241
var (
236242
blocksmith model.Blocksmith
237243
scoreString string
238244
)
239-
_ = rows.Scan(
245+
err := rows.Scan(
240246
&blocksmith.NodeID,
241247
&blocksmith.NodePublicKey,
242248
&scoreString,
243249
)
250+
if err != nil {
251+
return nil, err
252+
}
244253
blocksmith.Score, _ = new(big.Int).SetString(scoreString, 10)
245254
blocksmiths = append(blocksmiths, &blocksmith)
246255
}
247-
return blocksmiths
256+
return blocksmiths, nil
248257
}
249258

250259
// Rollback delete records `WHERE block_height > `height`

common/query/participationScoreQuery.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ func (ps *ParticipationScoreQuery) Rollback(height uint32) (multiQueries [][]int
175175
return [][]interface{}{
176176
{
177177
fmt.Sprintf("DELETE FROM %s WHERE height > ?", ps.TableName),
178-
[]interface{}{height},
178+
height,
179179
},
180180
{
181181
fmt.Sprintf(`
182182
UPDATE %s SET latest = ?
183-
WHERE height || '_' || id) IN (
184-
SELECT (MAX(height) || '_' || id) as con
183+
WHERE (height || '_' || node_id) IN (
184+
SELECT (MAX(height) || '_' || node_id) as con
185185
FROM %s
186-
GROUP BY id
186+
GROUP BY node_id
187187
)`,
188188
ps.TableName,
189189
ps.TableName,
190190
),
191-
[]interface{}{1},
191+
1,
192192
},
193193
}
194194
}

common/query/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ func GetDerivedQuery(chainType chaintype.ChainType) []DerivedQuery {
1818
NewNodeRegistrationQuery(),
1919
NewAccountBalanceQuery(),
2020
NewAccountDatasetsQuery(),
21+
NewSkippedBlocksmithQuery(),
22+
NewParticipationScoreQuery(),
2123
}
2224
}

common/query/query_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func TestGetDerivedQuery(t *testing.T) {
2525
NewNodeRegistrationQuery(),
2626
NewAccountBalanceQuery(),
2727
NewAccountDatasetsQuery(),
28+
NewSkippedBlocksmithQuery(),
29+
NewParticipationScoreQuery(),
2830
},
2931
},
3032
}

0 commit comments

Comments
 (0)