solana_exporter/cmd/solana-exporter/utils_test.go

106 lines
2.8 KiB
Go
Raw Normal View History

2024-10-08 07:33:41 -07:00
package main
import (
"context"
2024-10-28 15:38:48 -07:00
_ "embed"
"encoding/json"
"github.com/asymmetric-research/solana-exporter/pkg/rpc"
2024-10-08 07:33:41 -07:00
"github.com/stretchr/testify/assert"
"testing"
)
func TestSelectFromSchedule(t *testing.T) {
2024-10-28 09:19:07 -07:00
selected := SelectFromSchedule(
map[string][]int64{
"aaa": {0, 3, 6, 9, 12},
"bbb": {1, 4, 7, 10, 13},
"ccc": {2, 5, 8, 11, 14},
},
5,
10,
)
2024-10-08 07:33:41 -07:00
assert.Equal(t,
map[string][]int64{"aaa": {6, 9}, "bbb": {7, 10}, "ccc": {5, 8}},
selected,
)
}
func TestGetTrimmedLeaderSchedule(t *testing.T) {
2024-10-28 09:19:07 -07:00
_, client := rpc.NewMockClient(t,
2024-10-28 15:38:48 -07:00
map[string]any{
"getLeaderSchedule": map[string]any{
"aaa": []int{0, 3, 6, 9, 12},
"bbb": []int{1, 4, 7, 10, 13},
"ccc": []int{2, 5, 8, 11, 14},
},
},
nil, nil, nil, nil,
2024-10-28 09:19:07 -07:00
)
2024-10-08 07:33:41 -07:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2024-10-28 09:19:07 -07:00
schedule, err := GetTrimmedLeaderSchedule(ctx, client, []string{"aaa", "bbb"}, 10, 10)
assert.NoError(t, err)
2024-10-08 07:33:41 -07:00
assert.Equal(t, map[string][]int64{"aaa": {10, 13, 16, 19, 22}, "bbb": {11, 14, 17, 20, 23}}, schedule)
}
2024-10-14 05:39:41 -07:00
func TestCombineUnique(t *testing.T) {
var (
v1 = []string{"1", "2", "3"}
v2 = []string{"2", "3", "4"}
v3 = []string{"3", "4", "5"}
)
assert.Equal(t, []string{"1", "2", "3", "4", "5"}, CombineUnique(v1, v2, v3))
assert.Equal(t, []string{"2", "3", "4", "5"}, CombineUnique(nil, v2, v3))
assert.Equal(t, []string{"1", "2", "3", "4", "5"}, CombineUnique(v1, nil, v3))
}
func TestFetchBalances(t *testing.T) {
2024-10-28 15:15:21 -07:00
simulator, client := NewSimulator(t, 0)
2024-10-28 09:19:07 -07:00
2024-10-14 05:39:41 -07:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2024-10-28 15:15:21 -07:00
fetchedBalances, err := FetchBalances(ctx, client, CombineUnique(simulator.Nodekeys, simulator.Votekeys))
2024-10-14 05:39:41 -07:00
assert.NoError(t, err)
2024-10-28 15:15:21 -07:00
assert.Equal(t,
map[string]float64{"aaa": 1, "bbb": 2, "ccc": 3, "AAA": 4, "BBB": 5, "CCC": 6},
fetchedBalances,
)
2024-10-14 05:39:41 -07:00
}
2024-10-14 09:07:34 -07:00
func TestGetAssociatedVoteAccounts(t *testing.T) {
2024-10-28 15:15:21 -07:00
simulator, client := NewSimulator(t, 1)
2024-10-28 09:19:07 -07:00
2024-10-14 09:07:34 -07:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2024-10-28 15:15:21 -07:00
voteAccounts, err := GetAssociatedVoteAccounts(ctx, client, rpc.CommitmentFinalized, simulator.Nodekeys)
2024-10-14 09:07:34 -07:00
assert.NoError(t, err)
2024-10-28 15:15:21 -07:00
assert.Equal(t, simulator.Votekeys, voteAccounts)
2024-10-14 09:07:34 -07:00
}
2024-10-18 08:10:42 -07:00
func TestGetEpochBounds(t *testing.T) {
epoch := rpc.EpochInfo{AbsoluteSlot: 25, SlotIndex: 5, SlotsInEpoch: 10}
first, last := GetEpochBounds(&epoch)
assert.Equal(t, int64(20), first)
assert.Equal(t, int64(29), last)
}
2024-10-28 15:38:48 -07:00
//go:embed testdata/block-297609329.json
var blockJson []byte
func TestCountVoteTransactions(t *testing.T) {
var block rpc.Block
err := json.Unmarshal(blockJson, &block)
assert.NoError(t, err)
voteCount, err := CountVoteTransactions(&block)
assert.NoError(t, err)
// https://explorer.solana.com/block/297609329
assert.Equal(t, 1048, voteCount)
2024-11-05 10:54:24 -08:00
assert.Equal(t, 446, len(block.Transactions)-voteCount)
2024-10-28 15:38:48 -07:00
}