Skip to content

fix GetNodeRegistryAtHeight() query and the unit tests #765

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 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/query/nodeRegistrationQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ func (nrq *NodeRegistrationQuery) GetNodeRegistrationsWithZeroScore(registration

// GetNodeRegistryAtHeight returns unique latest node registry record at specific height
func (nrq *NodeRegistrationQuery) GetNodeRegistryAtHeight(height uint32) string {
return fmt.Sprintf("SELECT %s, max(height) AS max_height FROM %s where height <= %d AND registration_status = 0 "+
"GROUP BY id ORDER BY height DESC",
strings.Join(nrq.Fields, ", "), nrq.getTableName(), height)
return fmt.Sprintf("SELECT %s FROM %s where registration_status = 0 AND (id,height) in (SELECT id,MAX(height) "+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"FROM %s WHERE height <= %d GROUP BY id) ORDER BY height DESC",
strings.Join(nrq.Fields, ", "), nrq.getTableName(), nrq.getTableName(), height)
}

// ExtractModel extract the model struct fields to the order of NodeRegistrationQuery.Fields
Expand Down
31 changes: 31 additions & 0 deletions common/query/nodeRegistrationQuery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,34 @@ func TestNodeRegistrationQuery_SelectDataForSnapshot(t *testing.T) {
})
}
}

func TestNodeRegistrationQuery_GetNodeRegistryAtHeight(t *testing.T) {
type args struct {
height uint32
}
tests := []struct {
name string
args args
want string
}{
{
name: "GetNodeRegistryAtHeightQuery",
args: args{
height: 11120,
},
want: "SELECT id, node_public_key, account_address, registration_height, node_address, " +
"locked_balance, registration_status, latest, height FROM node_registry " +
"where registration_status = 0 AND (id,height) in " +
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 11120 GROUP BY id) " +
"ORDER BY height DESC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nrq := NewNodeRegistrationQuery()
if got := nrq.GetNodeRegistryAtHeight(tt.args.height); got != tt.want {
t.Errorf("NodeRegistrationQuery.GetNodeRegistryAtHeight() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 10 additions & 6 deletions core/service/blockMainService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,20 @@ func (*mockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...inter
mockPublishedReceipt[0].ReceiptIndex,
mockPublishedReceipt[0].PublishedIndex,
))
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, " +
"registration_status, latest, height, max(height) AS max_height FROM node_registry where height <= 0 AND " +
"registration_status = 0 GROUP BY id ORDER BY height DESC":
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
"locked_balance, registration_status, latest, height " +
"FROM node_registry where registration_status = 0 AND (id,height) in " +
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 0 GROUP BY id) " +
"ORDER BY height DESC":
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
"id", "node_public_key", "account_address", "registration_height", "node_address", "locked_balance",
"registration_status", "latest", "height",
}))
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, " +
"registration_status, latest, height, max(height) AS max_height FROM node_registry where height <= 1 " +
"AND registration_status = 0 GROUP BY id ORDER BY height DESC":
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
"locked_balance, registration_status, latest, height " +
"FROM node_registry where registration_status = 0 AND (id,height) in " +
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 1 GROUP BY id) " +
"ORDER BY height DESC":
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
"id", "node_public_key", "account_address", "registration_height", "node_address", "locked_balance",
"registration_status", "latest", "height",
Expand Down
11 changes: 6 additions & 5 deletions core/service/nodeRegistrationCoreService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ func (*nrsMockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...in
"participation_score",
},
).AddRow(1, nrsNodePubKey1, 8000))
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, registration_status, " +
"latest, height, max(height) AS max_height FROM node_registry where height <= 1 AND registration_status = 0 " +
"GROUP BY id ORDER BY height DESC":
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
"locked_balance, registration_status, latest, height " +
"FROM node_registry where registration_status = 0 AND (id,height) in " +
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 1 GROUP BY id) " +
"ORDER BY height DESC":
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
"id",
"node_public_key",
Expand All @@ -194,10 +196,9 @@ func (*nrsMockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...in
"registration_status",
"latest",
"height",
"max_height",
},
).AddRow(1, nrsNodePubKey1, nrsAddress1, 10, "10.10.10.10", 100000000,
uint32(model.NodeRegistrationState_NodeRegistered), true, 200, 200))
uint32(model.NodeRegistrationState_NodeRegistered), true, 200))
default:
return nil, errors.New("InvalidQuery")
}
Expand Down