Skip to content

Commit d21cc8b

Browse files
authored
Add JSON labels to Genesis structs (#960)
* fix(ledger): json labels on genesis objects - Add missing json struct tags for musttag linter - Remove nolint:musttag comments - Add unit tests for decoding genesis files Signed-off-by: Akhil Repala <[email protected]>
1 parent 9425192 commit d21cc8b

File tree

7 files changed

+223
-33
lines changed

7 files changed

+223
-33
lines changed

ledger/alonzo/genesis.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ import (
2222
)
2323

2424
type AlonzoGenesis struct {
25-
LovelacePerUtxoWord uint64 `json:"lovelacePerUTxOWord"`
26-
MaxValueSize uint
27-
CollateralPercentage uint
28-
MaxCollateralInputs uint
29-
ExecutionPrices AlonzoGenesisExecutionPrices
30-
MaxTxExUnits AlonzoGenesisExUnits
31-
MaxBlockExUnits AlonzoGenesisExUnits
32-
CostModels map[string]map[string]int
25+
LovelacePerUtxoWord uint64 `json:"lovelacePerUTxOWord"`
26+
MaxValueSize uint `json:"maxValueSize"`
27+
CollateralPercentage uint `json:"collateralPercentage"`
28+
MaxCollateralInputs uint `json:"maxCollateralInputs"`
29+
ExecutionPrices AlonzoGenesisExecutionPrices `json:"executionPrices"`
30+
MaxTxExUnits AlonzoGenesisExUnits `json:"maxTxExUnits"`
31+
MaxBlockExUnits AlonzoGenesisExUnits `json:"maxBlockExUnits"`
32+
CostModels map[string]map[string]int `json:"costModels"`
3333
}
3434

3535
func NewAlonzoGenesisFromReader(r io.Reader) (AlonzoGenesis, error) {
3636
var ret AlonzoGenesis
3737
dec := json.NewDecoder(r)
3838
dec.DisallowUnknownFields()
39-
//nolint:musttag
4039
if err := dec.Decode(&ret); err != nil {
4140
return ret, err
4241
}

ledger/alonzo/genesis_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,62 @@ func TestGenesisFromJson(t *testing.T) {
430430
)
431431
}
432432
}
433+
434+
func TestNewAlonzoGenesisFromReader(t *testing.T) {
435+
jsonData := `{
436+
"lovelacePerUTxOWord": 34482,
437+
"maxValueSize": 5000,
438+
"collateralPercentage": 150,
439+
"maxCollateralInputs": 3,
440+
"executionPrices": {
441+
"prSteps": { "numerator": 721, "denominator": 10000 },
442+
"prMem": { "numerator": 577, "denominator": 10000 }
443+
},
444+
"maxTxExUnits": { "exUnitsMem": 1000000, "exUnitsSteps": 10000000 },
445+
"maxBlockExUnits": { "exUnitsMem": 50000000, "exUnitsSteps": 40000000000 },
446+
"costModels": {
447+
"PlutusV1": {
448+
"addInteger-cpu-arguments-intercept": 205665,
449+
"addInteger-cpu-arguments-slope": 812
450+
}
451+
}
452+
}`
453+
454+
reader := strings.NewReader(jsonData)
455+
result, err := alonzo.NewAlonzoGenesisFromReader(reader)
456+
if err != nil {
457+
t.Errorf("Failed to decode JSON: %v", err)
458+
}
459+
460+
if result.LovelacePerUtxoWord != 34482 {
461+
t.Errorf("Expected LovelacePerUtxoWord 34482, got %d", result.LovelacePerUtxoWord)
462+
} else {
463+
t.Logf("LovelacePerUtxoWord is correct: %d", result.LovelacePerUtxoWord)
464+
}
465+
466+
if result.ExecutionPrices.Steps.Rat.Cmp(big.NewRat(721, 10000)) != 0 {
467+
t.Errorf("Unexpected prSteps: got %v, expected 721/10000", result.ExecutionPrices.Steps.Rat)
468+
} else {
469+
t.Logf("prSteps is correct: %v", result.ExecutionPrices.Steps.Rat)
470+
}
471+
472+
if result.ExecutionPrices.Mem.Rat.Cmp(big.NewRat(577, 10000)) != 0 {
473+
t.Errorf("Unexpected prMem: got %v, expected 577/10000", result.ExecutionPrices.Mem.Rat)
474+
} else {
475+
t.Logf("prMem is correct: %v", result.ExecutionPrices.Mem.Rat)
476+
}
477+
478+
expectedCostModels := map[string]map[string]int{
479+
"PlutusV1": {
480+
"addInteger-cpu-arguments-intercept": 205665,
481+
"addInteger-cpu-arguments-slope": 812,
482+
},
483+
}
484+
if !reflect.DeepEqual(result.CostModels, expectedCostModels) {
485+
t.Errorf("Unexpected CostModels:\nGot: %v\nExpected: %v", result.CostModels, expectedCostModels)
486+
} else {
487+
t.Logf("CostModels are correct")
488+
}
489+
490+
t.Logf("AlonzoGenesis JSON decoding test completed successfully.")
491+
}

ledger/byron/genesis.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ import (
2727
)
2828

2929
type ByronGenesis struct {
30-
AvvmDistr map[string]string
31-
BlockVersionData ByronGenesisBlockVersionData
32-
FtsSeed string
33-
ProtocolConsts ByronGenesisProtocolConsts
34-
StartTime int
35-
BootStakeholders map[string]int
36-
HeavyDelegation map[string]ByronGenesisHeavyDelegation
37-
NonAvvmBalances map[string]string
38-
VssCerts map[string]ByronGenesisVssCert
30+
AvvmDistr map[string]string `json:"avvmDistr"`
31+
BlockVersionData ByronGenesisBlockVersionData `json:"blockVersionData"`
32+
FtsSeed string `json:"ftsSeed"`
33+
ProtocolConsts ByronGenesisProtocolConsts `json:"protocolConsts"`
34+
StartTime int `json:"startTime"`
35+
BootStakeholders map[string]int `json:"bootStakeholders"`
36+
HeavyDelegation map[string]ByronGenesisHeavyDelegation `json:"heavyDelegation"`
37+
NonAvvmBalances map[string]string `json:"nonAvvmBalances"`
38+
VssCerts map[string]ByronGenesisVssCert `json:"vssCerts"`
3939
}
4040

4141
type ByronGenesisBlockVersionData struct {
@@ -180,7 +180,6 @@ func NewByronGenesisFromReader(r io.Reader) (ByronGenesis, error) {
180180
var ret ByronGenesis
181181
dec := json.NewDecoder(r)
182182
dec.DisallowUnknownFields()
183-
//nolint:musttag
184183
if err := dec.Decode(&ret); err != nil {
185184
return ret, err
186185
}

ledger/byron/genesis_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,76 @@ func TestGenesisAvvmUtxos(t *testing.T) {
356356
)
357357
}
358358
}
359+
360+
func TestNewByronGenesisFromReader(t *testing.T) {
361+
jsonData := `{
362+
"avvmDistr": { "addr1": "1000" },
363+
"blockVersionData": {
364+
"heavyDelThd": "1",
365+
"maxBlockSize": "2",
366+
"maxHeaderSize": "3",
367+
"maxProposalSize": "4",
368+
"maxTxSize": "5",
369+
"mpcThd": "6",
370+
"scriptVersion": 1,
371+
"slotDuration": "7",
372+
"softforkRule": {
373+
"initThd": "8",
374+
"minThd": "9",
375+
"thdDecrement": "10"
376+
},
377+
"txFeePolicy": {
378+
"multiplier": "11",
379+
"summand": "12"
380+
},
381+
"unlockStakeEpoch": "13",
382+
"updateImplicit": "14",
383+
"updateProposalThd": "15",
384+
"updateVoteThd": "16"
385+
},
386+
"ftsSeed": "seed",
387+
"protocolConsts": {
388+
"k": 1,
389+
"protocolMagic": 42,
390+
"vssMinTtl": 2,
391+
"vssMaxTtl": 10
392+
},
393+
"startTime": 100000,
394+
"bootStakeholders": { "stakeholder1": 1 },
395+
"heavyDelegation": {
396+
"key1": {
397+
"cert": "cert-val",
398+
"delegatePk": "delegate-pk",
399+
"issuerPk": "issuer-pk",
400+
"omega": 5
401+
}
402+
},
403+
"nonAvvmBalances": { "addr2": "2000" },
404+
"vssCerts": {
405+
"cert1": {
406+
"expiryEpoch": 5,
407+
"signature": "sig",
408+
"signingKey": "sign-key",
409+
"vssKey": "vss-key"
410+
}
411+
}
412+
}`
413+
414+
expected := byron.ByronGenesis{}
415+
err := json.Unmarshal([]byte(jsonData), &expected)
416+
if err != nil {
417+
t.Errorf("Failed to unmarshal expected: %v", err)
418+
}
419+
420+
reader := strings.NewReader(jsonData)
421+
result, err := byron.NewByronGenesisFromReader(reader)
422+
if err != nil {
423+
t.Errorf("Failed to decode: %v", err)
424+
}
425+
426+
if !reflect.DeepEqual(result, expected) {
427+
t.Errorf("ByronGenesis struct does not match expected.\nGot: %#v\nExpected: %#v", result, expected)
428+
} else {
429+
t.Logf("ByronGenesis decoded correctly")
430+
}
431+
}

ledger/conway/genesis.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ import (
2323
)
2424

2525
type ConwayGenesis struct {
26-
PoolVotingThresholds ConwayGenesisPoolVotingThresholds
27-
DRepVotingThresholds ConwayGenesisDRepVotingThresholds
28-
MinCommitteeSize uint `json:"committeeMinSize"`
29-
CommitteeTermLimit uint64 `json:"committeeMaxTermLength"`
30-
GovActionValidityPeriod uint64 `json:"govActionLifetime"`
31-
GovActionDeposit uint64
32-
DRepDeposit uint64 `json:"dRepDeposit"`
33-
DRepInactivityPeriod uint64 `json:"dRepActivity"`
34-
MinFeeRefScriptCostPerByte *common.GenesisRat
35-
PlutusV3CostModel []int64 `json:"plutusV3CostModel"`
36-
Constitution ConwayGenesisConstitution
37-
Committee ConwayGenesisCommittee
26+
PoolVotingThresholds ConwayGenesisPoolVotingThresholds `json:"poolVotingThresholds"`
27+
DRepVotingThresholds ConwayGenesisDRepVotingThresholds `json:"dRepVotingThresholds"`
28+
MinCommitteeSize uint `json:"committeeMinSize"`
29+
CommitteeTermLimit uint64 `json:"committeeMaxTermLength"`
30+
GovActionValidityPeriod uint64 `json:"govActionLifetime"`
31+
GovActionDeposit uint64 `json:"govActionDeposit"`
32+
DRepDeposit uint64 `json:"dRepDeposit"`
33+
DRepInactivityPeriod uint64 `json:"dRepActivity"`
34+
MinFeeRefScriptCostPerByte *common.GenesisRat `json:"minFeeRefScriptCostPerByte"`
35+
PlutusV3CostModel []int64 `json:"plutusV3CostModel"`
36+
Constitution ConwayGenesisConstitution `json:"constitution"`
37+
Committee ConwayGenesisCommittee `json:"committee"`
3838
}
3939

4040
type ConwayGenesisPoolVotingThresholds struct {
@@ -77,7 +77,6 @@ func NewConwayGenesisFromReader(r io.Reader) (ConwayGenesis, error) {
7777
var ret ConwayGenesis
7878
dec := json.NewDecoder(r)
7979
dec.DisallowUnknownFields()
80-
//nolint:musttag
8180
if err := dec.Decode(&ret); err != nil {
8281
return ret, err
8382
}

ledger/conway/genesis_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package conway_test
1616

1717
import (
18+
"encoding/json"
1819
"math/big"
1920
"reflect"
2021
"strings"
@@ -401,3 +402,64 @@ func TestGenesisFromJson(t *testing.T) {
401402
)
402403
}
403404
}
405+
406+
func TestNewConwayGenesisFromReader(t *testing.T) {
407+
jsonData := `{
408+
"poolVotingThresholds": {
409+
"committeeNormal": null,
410+
"committeeNoConfidence": null,
411+
"hardForkInitiation": null,
412+
"motionNoConfidence": null,
413+
"ppSecurityGroup": null
414+
},
415+
"dRepVotingThresholds": {
416+
"motionNoConfidence": null,
417+
"committeeNormal": null,
418+
"committeeNoConfidence": null,
419+
"updateToConstitution": null,
420+
"hardForkInitiation": null,
421+
"ppNetworkGroup": null,
422+
"ppEconomicGroup": null,
423+
"ppTechnicalGroup": null,
424+
"ppGovGroup": null,
425+
"treasuryWithdrawal": null
426+
},
427+
"committeeMinSize": 5,
428+
"committeeMaxTermLength": 365,
429+
"govActionLifetime": 90,
430+
"govActionDeposit": 1000,
431+
"dRepDeposit": 200,
432+
"dRepActivity": 60,
433+
"minFeeRefScriptCostPerByte": null,
434+
"plutusV3CostModel": [1, 2, 3],
435+
"constitution": {
436+
"anchor": {
437+
"dataHash": "abc123",
438+
"url": "https://example.com"
439+
},
440+
"script": "example-script"
441+
},
442+
"committee": {
443+
"members": { "key1": 1 },
444+
"threshold": { "key1": 2 }
445+
}
446+
}`
447+
448+
expected := conway.ConwayGenesis{}
449+
err := json.Unmarshal([]byte(jsonData), &expected)
450+
if err != nil {
451+
t.Errorf("Failed to unmarshal expected JSON: %v", err)
452+
}
453+
454+
reader := strings.NewReader(jsonData)
455+
actual, err := conway.NewConwayGenesisFromReader(reader)
456+
if err != nil {
457+
t.Errorf("Failed to decode JSON via NewConwayGenesisFromReader: %v", err)
458+
}
459+
460+
if !reflect.DeepEqual(expected, actual) {
461+
t.Errorf("Mismatch between expected and actual structs\nExpected: %#v\nActual: %#v", expected, actual)
462+
} else {
463+
t.Logf("ConwayGenesis decoded correctly and matches expected structure")
464+
}
465+
}

ledger/shelley/genesis.go

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ func NewShelleyGenesisFromReader(r io.Reader) (ShelleyGenesis, error) {
158158
var ret ShelleyGenesis
159159
dec := json.NewDecoder(r)
160160
dec.DisallowUnknownFields()
161-
//nolint:musttag
162161
if err := dec.Decode(&ret); err != nil {
163162
return ret, err
164163
}

0 commit comments

Comments
 (0)