-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from flare-foundation/staking-clients
Test for uptime voting cronjob
- Loading branch information
Showing
20 changed files
with
1,449 additions
and
890 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
([]string) (len=4) { | ||
(string) (len=40) "NodeID-GWPcbFJZFfZreETSoWjPimr846mXEKCtu", | ||
(string) (len=40) "NodeID-MFrZFVCXPv5iCn6M9K6XduxGTYp891xXZ", | ||
(string) (len=40) "NodeID-NFBbbJ4qCmNaCzeW7sxErhvWqvEQMnYcN", | ||
(string) (len=40) "NodeID-P7oB2McjBGgW2NXXWVYjV8JEDFoW9xDE5" | ||
} | ||
([]int64) (len=4) { | ||
(int64) 90, | ||
(int64) 90, | ||
(int64) 90, | ||
(int64) 40 | ||
} |
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,4 @@ | ||
([32]uint8) (len=32) { | ||
00000000 2b 2e 37 55 43 1e 55 53 ee 31 d3 cd 2d 5c 57 20 |+.7UC.US.1..-\W | | ||
00000010 31 1d aa f5 c7 d2 a2 fd 43 07 da bf c3 44 07 c4 |1.......C....D..| | ||
} |
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
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,129 @@ | ||
package cronjob | ||
|
||
import ( | ||
globalConfig "flare-indexer/config" | ||
"flare-indexer/database" | ||
"flare-indexer/indexer/config" | ||
"flare-indexer/indexer/context" | ||
"flare-indexer/indexer/pchain" | ||
"flare-indexer/indexer/shared" | ||
"flare-indexer/utils" | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bradleyjkemp/cupaloy" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func uptimeVotingCronjobTestConfig(epochStart time.Time) *config.Config { | ||
cfg := &config.Config{ | ||
Chain: globalConfig.ChainConfig{ | ||
ChainAddressHRP: "localflare", | ||
ChainID: 31337, | ||
EthRPCURL: "http://127.0.0.1:8545", | ||
PrivateKey: "0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb", | ||
}, | ||
UptimeCronjob: config.UptimeConfig{ | ||
CronjobConfig: config.CronjobConfig{ | ||
Enabled: true, | ||
TimeoutSeconds: 30, | ||
}, | ||
EpochConfig: config.EpochConfig{ | ||
Start: utils.Timestamp{Time: epochStart}, | ||
Period: 90 * time.Second, | ||
}, | ||
VotingInterval: 60 * time.Second, | ||
EnableVoting: true, | ||
UptimeThreshold: 0.8, | ||
}, | ||
VotingCronjob: config.VotingConfig{ | ||
ContractAddress: common.HexToAddress("0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F"), | ||
}, | ||
PChainIndexer: config.IndexerConfig{ | ||
Enabled: true, | ||
TimeoutMillis: 3000, | ||
BatchSize: 200, | ||
StartIndex: 0, | ||
}, | ||
DB: globalConfig.DBConfig{ | ||
Username: database.MysqlTestUser, | ||
Password: database.MysqlTestPassword, | ||
Host: database.MysqlTestHost, | ||
Port: database.MysqlTestPort, | ||
Database: "flare_indexer_indexer", | ||
LogQueries: false, | ||
}, | ||
} | ||
return cfg | ||
|
||
} | ||
|
||
func createTestUptimeVotingCronjob(epochStart time.Time) (*uptimeVotingCronjob, *shared.ChainIndexerBase, error) { | ||
ctx, err := context.BuildTestContext(uptimeVotingCronjobTestConfig(epochStart)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
cronjob, err := NewUptimeVotingCronjob(ctx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
indexer := &shared.ChainIndexerBase{ | ||
StateName: pchain.StateName, | ||
IndexerName: "P-chain Blocks Test", | ||
Client: testClient, | ||
DB: ctx.DB(), | ||
Config: ctx.Config().PChainIndexer, | ||
BatchIndexer: pchain.NewPChainBatchIndexer(ctx, testClient, testRPCClient), | ||
} | ||
return cronjob, indexer, nil | ||
} | ||
|
||
// Requires a running hardhat node | ||
// from the flare-smart-contracts project, branch origin/staking-tests | ||
// with yarn staking_test | ||
func TestUptimeVoting(t *testing.T) { | ||
now := time.Unix(1675348249, 0) | ||
|
||
// Epoch starts "now" | ||
votingCronjob, indexer, err := createTestUptimeVotingCronjob(now) | ||
require.NoError(t, err) | ||
|
||
uptimeCronjob, err := createTestUptimeCronjob() | ||
require.NoError(t, err) | ||
|
||
// Run indexer to allow uptime client test to fetch validator data | ||
err = indexer.IndexBatch() | ||
require.NoError(t, err) | ||
|
||
testUptimeClient.SetNow(now) | ||
votingCronjob.time.SetNow(now) | ||
for i := 0; i < 10; i++ { | ||
if err := uptimeCronjob.Call(); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := votingCronjob.Call(); err != nil { | ||
t.Fatal(err) | ||
} | ||
testUptimeClient.Time.AdvanceNow(10 * time.Second) | ||
votingCronjob.time.AdvanceNow(10 * time.Second) | ||
} | ||
aggr, err := database.FetchAggregations(votingCronjob.db) | ||
require.NoError(t, err) | ||
assert.Equal(t, 4, len(aggr)) | ||
|
||
// Sort by nodeID and compare to snapshots | ||
sort.Slice(aggr, func(i, j int) bool { | ||
return aggr[i].NodeID < aggr[j].NodeID | ||
}) | ||
aggrNodeIDs := utils.Map(aggr, func(a *database.UptimeAggregation) string { | ||
return a.NodeID | ||
}) | ||
aggrValue := utils.Map(aggr, func(a *database.UptimeAggregation) int64 { | ||
return a.Value | ||
}) | ||
cupaloy.SnapshotT(t, aggrNodeIDs, aggrValue) | ||
} |
Oops, something went wrong.