diff --git a/gen/generate_test.go b/gen/generate_test.go index fe618cfecc..64c393b5bc 100644 --- a/gen/generate_test.go +++ b/gen/generate_test.go @@ -19,6 +19,7 @@ package gen import ( "encoding/json" "fmt" + "github.com/algorand/go-algorand/data/basics" "io" "os" "path/filepath" @@ -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"} } // `blotOutRandomValues` replaces random values with static values to support equality checks. @@ -180,6 +191,24 @@ 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 + + require.NotEmpty(t, g.Network) + g.Network = "" + } + saveGeneratedGenesisJSON := func(filename, artifactName string) { src, err := os.Open(filename) require.NoError(t, err) @@ -206,10 +235,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) @@ -224,11 +254,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)) }) }