Skip to content
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

Tests: Add test defending against regression in testnet genesis generation #4741

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 47 additions & 9 deletions gen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gen
import (
"encoding/json"
"fmt"
"github.com/algorand/go-algorand/data/basics"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -131,27 +132,37 @@ func TestGenesisJsonCreation(t *testing.T) {
t.Parallel()

type testCase struct {
name string
gd GenesisData
name string
gd GenesisData
expectedOverride string
}

// `base` is a canonical test confirming `devnet.json` generates the intended `genesis.json`.
base := func() testCase {
jsonBytes, err := os.ReadFile("devnet.json")
defaultGenesisFromFile := func(filename string) GenesisData {
jsonBytes, err := os.ReadFile(filename)
require.NoError(t, err)

gd := DefaultGenesis
err = json.Unmarshal(jsonBytes, &gd)
require.NoError(t, err)

return testCase{"base", gd}
return gd
}

// `base` is a canonical test confirming `devnet.json` generates the intended `genesis.json`.
base := func() testCase {
return testCase{"base", defaultGenesisFromFile("devnet.json"), ""}
}

// `balance` extends `base` to confirm overriding the rewards pool balance works.
balance := func() testCase {
gd := base().gd
gd.RewardsPoolBalance = 0 // Expect generated balance == MinBalance
return testCase{"balance", gd}
return testCase{"balance", gd, ""}
}

// `testnet` confirms the generated genesis.json conforms to a previously generated _installer_ artifact.
testnet := func() testCase {
return testCase{"testnet", defaultGenesisFromFile("testnet.json"), "../installer/genesis/testnet/genesis.json"}
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
}

// `blotOutRandomValues` replaces random values with static values to support equality checks.
Expand Down Expand Up @@ -180,6 +191,21 @@ func TestGenesisJsonCreation(t *testing.T) {
}
}

const quickLastPartKeyRound = basics.Round(10) // Ensure quick test execution by reducing rounds.

// `blotOutFixedValues` replaces values from actual genesis values in order to be compatible with artifacts generated by tests.
blotOutFixedValues := func(g *bookkeeping.Genesis) {
for i := range g.Allocation {
if g.Allocation[i].State.Status == basics.Online {
require.Greater(t, g.Allocation[i].State.VoteLastValid, quickLastPartKeyRound)
g.Allocation[i].State.VoteLastValid = quickLastPartKeyRound
}
}

require.NotZero(t, g.Timestamp)
g.Timestamp = 0
}

saveGeneratedGenesisJSON := func(filename, artifactName string) {
src, err := os.Open(filename)
require.NoError(t, err)
Expand All @@ -206,10 +232,11 @@ func TestGenesisJsonCreation(t *testing.T) {
for _, tc := range []testCase{
base(),
balance(),
testnet(),
} {
t.Run(fmt.Sprintf("name=%v", tc.name), func(t *testing.T) {
gd := tc.gd
gd.LastPartKeyRound = 10 // Ensure quick test execution by reducing rounds.
gd.LastPartKeyRound = uint64(quickLastPartKeyRound)

outDir := t.TempDir()
err := GenerateGenesisFiles(gd, config.Consensus, outDir, nil)
Expand All @@ -224,11 +251,22 @@ func TestGenesisJsonCreation(t *testing.T) {
roundtrip, err := bookkeeping.LoadGenesisFromFile(generatedFilename)
require.NoError(t, err)

expected, err := bookkeeping.LoadGenesisFromFile("resources/" + artifactName)
expectedFilepath := func() string {
if len(tc.expectedOverride) == 0 {
return "resources/" + artifactName
}
return tc.expectedOverride
}
expected, err := bookkeeping.LoadGenesisFromFile(expectedFilepath())
saveOnFailure(assert.NoError(t, err))

blotOutRandomValues(expected.Allocation)
blotOutRandomValues(roundtrip.Allocation)

if len(tc.expectedOverride) > 0 {
blotOutFixedValues(&expected)
}

saveOnFailure(assert.Equal(t, expected, roundtrip))
})
}
Expand Down
1 change: 1 addition & 0 deletions gen/testnet.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"VersionModifier": ".0",
"NetworkName": "testnet",
michaeldiamant marked this conversation as resolved.
Show resolved Hide resolved
"FirstPartKeyRound": 0,
"LastPartKeyRound": 3000000,
"ConsensusProtocol": "https://github.com/algorand/spec/tree/a26ed78ed8f834e2b9ccb6eb7d3ee9f629a6e622",
Expand Down