-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Init commen test * Add vote module tests
- Loading branch information
Showing
12 changed files
with
568 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package vote_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/forbole/soljuno/db" | ||
dbtypes "github.com/forbole/soljuno/db/types" | ||
"github.com/forbole/soljuno/modules/vote" | ||
clienttypes "github.com/forbole/soljuno/solana/client/types" | ||
) | ||
|
||
var _ db.VoteDb = &MockDb{} | ||
|
||
type MockDb struct { | ||
isLatest bool | ||
err error | ||
} | ||
|
||
func NewDefaultMockDb() *MockDb { | ||
return &MockDb{isLatest: true} | ||
} | ||
|
||
func (db *MockDb) SaveValidator(account dbtypes.VoteAccountRow) error { return db.err } | ||
func (db *MockDb) SaveValidatorStatuses(statuses []dbtypes.ValidatorStatusRow) error { return db.err } | ||
func (db *MockDb) GetEpochProducedBlocks(epoch uint64) ([]uint64, error) { return []uint64{0}, db.err } | ||
func (db *MockDb) SaveValidatorSkipRates(skipRates []dbtypes.ValidatorSkipRateRow) error { | ||
return db.err | ||
} | ||
func (db *MockDb) SaveHistoryValidatorSkipRates(skipRates []dbtypes.ValidatorSkipRateRow) error { | ||
return db.err | ||
} | ||
|
||
func (db *MockDb) CheckValidatorLatest(address string, currentSlot uint64) bool { | ||
return db.isLatest | ||
} | ||
|
||
func (m MockDb) GetCached() MockDb { | ||
return m | ||
} | ||
|
||
func (m *MockDb) WithError(err error) { | ||
m.err = err | ||
} | ||
|
||
func (m *MockDb) WithLatest(isLatest bool) { | ||
m.isLatest = isLatest | ||
} | ||
|
||
// ---------------------------------------------------------------- | ||
|
||
var _ vote.ClientProxy = &MockClient{} | ||
|
||
type MockClient struct { | ||
account clienttypes.AccountInfo | ||
err error | ||
} | ||
|
||
func NewDefaultMockClient() *MockClient { | ||
return &MockClient{} | ||
} | ||
|
||
func (m MockClient) GetCached() MockClient { | ||
return m | ||
} | ||
|
||
func (m *MockClient) WithError(err error) { | ||
m.err = err | ||
} | ||
|
||
func (m *MockClient) WithAccount(account clienttypes.AccountInfo) { | ||
m.account = account | ||
} | ||
|
||
func (m *MockClient) GetAccountInfo(address string) (clienttypes.AccountInfo, error) { | ||
return m.account, m.err | ||
} | ||
|
||
func (m *MockClient) GetVoteAccountsWithSlot() (uint64, clienttypes.VoteAccounts, error) { | ||
return 0, clienttypes.VoteAccounts{ | ||
Current: []clienttypes.VoteAccount{{VotePubkey: "current"}}, | ||
Delinquent: []clienttypes.VoteAccount{{VotePubkey: "delinquent"}}, | ||
}, m.err | ||
} | ||
|
||
func (m *MockClient) GetLeaderSchedule(uint64) (clienttypes.LeaderSchedule, error) { | ||
return clienttypes.LeaderSchedule{"address": []int{0, 1}}, m.err | ||
} | ||
|
||
// ---------------------------------------------------------------- | ||
|
||
type ModuleTestSuite struct { | ||
suite.Suite | ||
module *vote.Module | ||
db *MockDb | ||
client *MockClient | ||
} | ||
|
||
func TestModuleTestSuite(t *testing.T) { | ||
suite.Run(t, new(ModuleTestSuite)) | ||
} | ||
|
||
func (suite *ModuleTestSuite) SetupTest() { | ||
suite.module = vote.NewModule(NewDefaultMockDb(), NewDefaultMockClient()) | ||
suite.db = NewDefaultMockDb() | ||
suite.client = NewDefaultMockClient() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,5 @@ | ||
package vote | ||
|
||
import ( | ||
"fmt" | ||
|
||
dbtypes "github.com/forbole/soljuno/db/types" | ||
solanatypes "github.com/forbole/soljuno/solana/types" | ||
) | ||
|
||
func (m *Module) ExecEpoch(epoch uint64) error { | ||
return m.updateValidatorSkipRates(epoch - 1) | ||
} | ||
|
||
// updateValidatorSkipRates properly stores the skip rates of all validators inside the database | ||
func (m *Module) updateValidatorSkipRates(epoch uint64) error { | ||
slots, err := m.db.GetEpochProducedBlocks(epoch) | ||
if err != nil { | ||
return err | ||
} | ||
if len(slots) == 0 { | ||
return fmt.Errorf("%d epoch blocks does not exist", epoch) | ||
} | ||
|
||
endSlot := slots[len(slots)-1] | ||
schedules, err := m.client.GetLeaderSchedule(epoch * solanatypes.SlotsInEpoch) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
produced := make(map[int]bool) | ||
for _, slot := range slots { | ||
produced[int(slot%solanatypes.SlotsInEpoch)] = true | ||
} | ||
|
||
skipRateRows := make([]dbtypes.ValidatorSkipRateRow, len(schedules)) | ||
count := 0 | ||
end := int(endSlot % solanatypes.SlotsInEpoch) | ||
for validator, schedule := range schedules { | ||
total, skip := getSkipRateReference(end, produced, schedule) | ||
skipRate := float64(skip) / float64(total) | ||
skipRateRows[count] = dbtypes.NewValidatorSkipRateRow(validator, epoch, skipRate, total, skip) | ||
count++ | ||
} | ||
|
||
err = m.db.SaveValidatorSkipRates(skipRateRows) | ||
if err != nil { | ||
return err | ||
} | ||
return m.db.SaveHistoryValidatorSkipRates(skipRateRows) | ||
} | ||
|
||
// getSkipRateReference returns the total and skip amount in a epoch of the validator from the given produced map and the validator schedule | ||
func getSkipRateReference(end int, produced map[int]bool, schedule []int) (int, int) { | ||
var skip int = 0 | ||
var total int = 0 | ||
for _, slotInEpoch := range schedule { | ||
total++ | ||
if slotInEpoch > end { | ||
break | ||
} | ||
if ok := produced[slotInEpoch]; !ok { | ||
skip++ | ||
} | ||
} | ||
return total, skip | ||
return UpdateValidatorSkipRates(epoch-1, m.db, m.client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.