Skip to content

Commit f428ca1

Browse files
authored
refactor: remove embedding in pparam types (#985)
* make Allegra/Mary pparams type aliases of Shelley types * remove embedded types from Alonzo pparams * remove use of reflection for decoding pparams CBOR * remove now unused UnmarshalCbor function from DecodeStoreCbor Fixes #982 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 38fabf7 commit f428ca1

File tree

17 files changed

+332
-328
lines changed

17 files changed

+332
-328
lines changed

cbor/cbor.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,3 @@ func (d *DecodeStoreCbor) SetCbor(cborData []byte) {
6969
func (d DecodeStoreCbor) Cbor() []byte {
7070
return d.cborData
7171
}
72-
73-
// UnmarshalCbor decodes the specified CBOR into the destination object and saves the original CBOR
74-
func (d *DecodeStoreCbor) UnmarshalCbor(
75-
cborData []byte,
76-
dest DecodeStoreCborInterface,
77-
) error {
78-
if err := DecodeGeneric(cborData, dest); err != nil {
79-
return err
80-
}
81-
// Store a copy of the original CBOR data
82-
// This must be done after we copy from the temp object above, or it gets wiped out
83-
// when using struct embedding and the DecodeStoreCbor struct is embedded at a deeper level
84-
d.SetCbor(cborData)
85-
return nil
86-
}

ledger/allegra/pparams.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,14 @@ package allegra
1616

1717
import (
1818
"github.com/blinklabs-io/gouroboros/ledger/shelley"
19-
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2019
)
2120

22-
type AllegraProtocolParameters struct {
23-
shelley.ShelleyProtocolParameters
24-
}
25-
26-
func (p *AllegraProtocolParameters) Update(
27-
paramUpdate *AllegraProtocolParameterUpdate,
28-
) {
29-
p.ShelleyProtocolParameters.Update(
30-
&paramUpdate.ShelleyProtocolParameterUpdate,
31-
)
32-
}
33-
34-
type AllegraProtocolParameterUpdate struct {
35-
shelley.ShelleyProtocolParameterUpdate
36-
}
37-
38-
func (u *AllegraProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
39-
return u.UnmarshalCbor(data, u)
40-
}
21+
type AllegraProtocolParameters = shelley.ShelleyProtocolParameters
4122

42-
func (p *AllegraProtocolParameters) Utxorpc() *cardano.PParams {
43-
return p.ShelleyProtocolParameters.Utxorpc()
44-
}
23+
type AllegraProtocolParameterUpdate = shelley.ShelleyProtocolParameterUpdate
4524

4625
func UpgradePParams(
4726
prevPParams shelley.ShelleyProtocolParameters,
4827
) AllegraProtocolParameters {
49-
return AllegraProtocolParameters{
50-
ShelleyProtocolParameters: prevPParams,
51-
}
28+
return AllegraProtocolParameters(prevPParams)
5229
}

ledger/allegra/pparams_test.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
2424
"github.com/blinklabs-io/gouroboros/ledger/allegra"
25-
"github.com/blinklabs-io/gouroboros/ledger/shelley"
2625
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2726
)
2827

@@ -34,28 +33,20 @@ func TestAllegraProtocolParamsUpdate(t *testing.T) {
3433
}{
3534
{
3635
startParams: allegra.AllegraProtocolParameters{
37-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
38-
Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)},
39-
},
36+
Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)},
4037
},
4138
updateCbor: "a10cd81e82090a",
4239
expectedParams: allegra.AllegraProtocolParameters{
43-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
44-
Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)},
45-
},
40+
Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)},
4641
},
4742
},
4843
{
4944
startParams: allegra.AllegraProtocolParameters{
50-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
51-
ProtocolMajor: 3,
52-
},
45+
ProtocolMajor: 3,
5346
},
5447
updateCbor: "a10e820400",
5548
expectedParams: allegra.AllegraProtocolParameters{
56-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
57-
ProtocolMajor: 4,
58-
},
49+
ProtocolMajor: 4,
5950
},
6051
},
6152
}
@@ -82,23 +73,21 @@ func TestAllegraProtocolParamsUpdate(t *testing.T) {
8273

8374
func TestAllegraUtxorpc(t *testing.T) {
8475
inputParams := allegra.AllegraProtocolParameters{
85-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
86-
MinFeeA: 500,
87-
MinFeeB: 2,
88-
MaxBlockBodySize: 65536,
89-
MaxTxSize: 16384,
90-
MaxBlockHeaderSize: 1024,
91-
KeyDeposit: 2000,
92-
PoolDeposit: 500000,
93-
MaxEpoch: 2160,
94-
NOpt: 100,
95-
A0: &cbor.Rat{Rat: big.NewRat(1, 2)},
96-
Rho: &cbor.Rat{Rat: big.NewRat(3, 4)},
97-
Tau: &cbor.Rat{Rat: big.NewRat(5, 6)},
98-
ProtocolMajor: 8,
99-
ProtocolMinor: 0,
100-
MinUtxoValue: 1000000,
101-
},
76+
MinFeeA: 500,
77+
MinFeeB: 2,
78+
MaxBlockBodySize: 65536,
79+
MaxTxSize: 16384,
80+
MaxBlockHeaderSize: 1024,
81+
KeyDeposit: 2000,
82+
PoolDeposit: 500000,
83+
MaxEpoch: 2160,
84+
NOpt: 100,
85+
A0: &cbor.Rat{Rat: big.NewRat(1, 2)},
86+
Rho: &cbor.Rat{Rat: big.NewRat(3, 4)},
87+
Tau: &cbor.Rat{Rat: big.NewRat(5, 6)},
88+
ProtocolMajor: 8,
89+
ProtocolMinor: 0,
90+
MinUtxoValue: 1000000,
10291
}
10392

10493
expectedUtxorpc := &cardano.PParams{

ledger/allegra/rules.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func UtxoValidateFeeTooSmallUtxo(
7474
tx,
7575
slot,
7676
ls,
77-
&tmpPparams.ShelleyProtocolParameters,
77+
tmpPparams,
7878
)
7979
}
8080

@@ -119,7 +119,7 @@ func UtxoValidateValueNotConservedUtxo(
119119
tx,
120120
slot,
121121
ls,
122-
&tmpPparams.ShelleyProtocolParameters,
122+
tmpPparams,
123123
)
124124
}
125125

@@ -137,7 +137,7 @@ func UtxoValidateOutputTooSmallUtxo(
137137
tx,
138138
slot,
139139
ls,
140-
&tmpPparams.ShelleyProtocolParameters,
140+
tmpPparams,
141141
)
142142
}
143143

@@ -164,6 +164,6 @@ func UtxoValidateMaxTxSizeUtxo(
164164
tx,
165165
slot,
166166
ls,
167-
&tmpPparams.ShelleyProtocolParameters,
167+
tmpPparams,
168168
)
169169
}

ledger/allegra/rules_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,8 @@ func TestUtxoValidateFeeTooSmallUtxo(t *testing.T) {
203203
}
204204
testTx.SetCbor(testTxCbor)
205205
testProtocolParams := &allegra.AllegraProtocolParameters{
206-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
207-
MinFeeA: 7,
208-
MinFeeB: 53,
209-
},
206+
MinFeeA: 7,
207+
MinFeeB: 53,
210208
}
211209
testLedgerState := test.MockLedgerState{}
212210
testSlot := uint64(0)
@@ -526,9 +524,7 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
526524
}
527525
testSlot := uint64(0)
528526
testProtocolParams := &allegra.AllegraProtocolParameters{
529-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
530-
KeyDeposit: uint(testStakeDeposit),
531-
},
527+
KeyDeposit: uint(testStakeDeposit),
532528
}
533529
// Exact amount
534530
t.Run(
@@ -675,9 +671,7 @@ func TestUtxoValidateOutputTooSmallUtxo(t *testing.T) {
675671
testLedgerState := test.MockLedgerState{}
676672
testSlot := uint64(0)
677673
testProtocolParams := &allegra.AllegraProtocolParameters{
678-
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
679-
MinUtxoValue: 100000,
680-
},
674+
MinUtxoValue: 100000,
681675
}
682676
// Good
683677
t.Run(

ledger/alonzo/pparams.go

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,24 @@ import (
2424
)
2525

2626
type AlonzoProtocolParameters struct {
27-
mary.MaryProtocolParameters
27+
cbor.StructAsArray
28+
MinFeeA uint
29+
MinFeeB uint
30+
MaxBlockBodySize uint
31+
MaxTxSize uint
32+
MaxBlockHeaderSize uint
33+
KeyDeposit uint
34+
PoolDeposit uint
35+
MaxEpoch uint
36+
NOpt uint
37+
A0 *cbor.Rat
38+
Rho *cbor.Rat
39+
Tau *cbor.Rat
40+
Decentralization *cbor.Rat
41+
ExtraEntropy common.Nonce
42+
ProtocolMajor uint
43+
ProtocolMinor uint
44+
MinUtxoValue uint
2845
MinPoolCost uint64
2946
AdaPerUtxoByte uint64
3047
CostModels map[uint][]int64
@@ -39,9 +56,55 @@ type AlonzoProtocolParameters struct {
3956
func (p *AlonzoProtocolParameters) Update(
4057
paramUpdate *AlonzoProtocolParameterUpdate,
4158
) {
42-
p.MaryProtocolParameters.Update(
43-
&paramUpdate.MaryProtocolParameterUpdate,
44-
)
59+
if paramUpdate.MinFeeA != nil {
60+
p.MinFeeA = *paramUpdate.MinFeeA
61+
}
62+
if paramUpdate.MinFeeB != nil {
63+
p.MinFeeB = *paramUpdate.MinFeeB
64+
}
65+
if paramUpdate.MaxBlockBodySize != nil {
66+
p.MaxBlockBodySize = *paramUpdate.MaxBlockBodySize
67+
}
68+
if paramUpdate.MaxTxSize != nil {
69+
p.MaxTxSize = *paramUpdate.MaxTxSize
70+
}
71+
if paramUpdate.MaxBlockHeaderSize != nil {
72+
p.MaxBlockHeaderSize = *paramUpdate.MaxBlockHeaderSize
73+
}
74+
if paramUpdate.KeyDeposit != nil {
75+
p.KeyDeposit = *paramUpdate.KeyDeposit
76+
}
77+
if paramUpdate.PoolDeposit != nil {
78+
p.PoolDeposit = *paramUpdate.PoolDeposit
79+
}
80+
if paramUpdate.MaxEpoch != nil {
81+
p.MaxEpoch = *paramUpdate.MaxEpoch
82+
}
83+
if paramUpdate.NOpt != nil {
84+
p.NOpt = *paramUpdate.NOpt
85+
}
86+
if paramUpdate.A0 != nil {
87+
p.A0 = paramUpdate.A0
88+
}
89+
if paramUpdate.Rho != nil {
90+
p.Rho = paramUpdate.Rho
91+
}
92+
if paramUpdate.Tau != nil {
93+
p.Tau = paramUpdate.Tau
94+
}
95+
if paramUpdate.Decentralization != nil {
96+
p.Decentralization = paramUpdate.Decentralization
97+
}
98+
if paramUpdate.ProtocolVersion != nil {
99+
p.ProtocolMajor = paramUpdate.ProtocolVersion.Major
100+
p.ProtocolMinor = paramUpdate.ProtocolVersion.Minor
101+
}
102+
if paramUpdate.ExtraEntropy != nil {
103+
p.ExtraEntropy = *paramUpdate.ExtraEntropy
104+
}
105+
if paramUpdate.MinUtxoValue != nil {
106+
p.MinUtxoValue = *paramUpdate.MinUtxoValue
107+
}
45108
if paramUpdate.MinPoolCost != nil {
46109
p.MinPoolCost = *paramUpdate.MinPoolCost
47110
}
@@ -100,20 +163,45 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) {
100163
}
101164

102165
type AlonzoProtocolParameterUpdate struct {
103-
mary.MaryProtocolParameterUpdate
104-
MinPoolCost *uint64 `cbor:"16,keyasint"`
105-
AdaPerUtxoByte *uint64 `cbor:"17,keyasint"`
106-
CostModels map[uint][]int64 `cbor:"18,keyasint"`
107-
ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"`
108-
MaxTxExUnits *common.ExUnits `cbor:"20,keyasint"`
109-
MaxBlockExUnits *common.ExUnits `cbor:"21,keyasint"`
110-
MaxValueSize *uint `cbor:"22,keyasint"`
111-
CollateralPercentage *uint `cbor:"23,keyasint"`
112-
MaxCollateralInputs *uint `cbor:"24,keyasint"`
166+
cbor.DecodeStoreCbor
167+
MinFeeA *uint `cbor:"0,keyasint"`
168+
MinFeeB *uint `cbor:"1,keyasint"`
169+
MaxBlockBodySize *uint `cbor:"2,keyasint"`
170+
MaxTxSize *uint `cbor:"3,keyasint"`
171+
MaxBlockHeaderSize *uint `cbor:"4,keyasint"`
172+
KeyDeposit *uint `cbor:"5,keyasint"`
173+
PoolDeposit *uint `cbor:"6,keyasint"`
174+
MaxEpoch *uint `cbor:"7,keyasint"`
175+
NOpt *uint `cbor:"8,keyasint"`
176+
A0 *cbor.Rat `cbor:"9,keyasint"`
177+
Rho *cbor.Rat `cbor:"10,keyasint"`
178+
Tau *cbor.Rat `cbor:"11,keyasint"`
179+
Decentralization *cbor.Rat `cbor:"12,keyasint"`
180+
ExtraEntropy *common.Nonce `cbor:"13,keyasint"`
181+
ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"`
182+
MinUtxoValue *uint `cbor:"15,keyasint"`
183+
MinPoolCost *uint64 `cbor:"16,keyasint"`
184+
AdaPerUtxoByte *uint64 `cbor:"17,keyasint"`
185+
CostModels map[uint][]int64 `cbor:"18,keyasint"`
186+
ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"`
187+
MaxTxExUnits *common.ExUnits `cbor:"20,keyasint"`
188+
MaxBlockExUnits *common.ExUnits `cbor:"21,keyasint"`
189+
MaxValueSize *uint `cbor:"22,keyasint"`
190+
CollateralPercentage *uint `cbor:"23,keyasint"`
191+
MaxCollateralInputs *uint `cbor:"24,keyasint"`
113192
}
114193

115-
func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
116-
return u.UnmarshalCbor(data, u)
194+
func (AlonzoProtocolParameterUpdate) IsProtocolParameterUpdate() {}
195+
196+
func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(cborData []byte) error {
197+
type tAlonzoProtocolParameterUpdate AlonzoProtocolParameterUpdate
198+
var tmp tAlonzoProtocolParameterUpdate
199+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
200+
return err
201+
}
202+
*u = AlonzoProtocolParameterUpdate(tmp)
203+
u.SetCbor(cborData)
204+
return nil
117205
}
118206

119207
func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
@@ -203,6 +291,22 @@ func UpgradePParams(
203291
prevPParams mary.MaryProtocolParameters,
204292
) AlonzoProtocolParameters {
205293
return AlonzoProtocolParameters{
206-
MaryProtocolParameters: prevPParams,
294+
MinFeeA: prevPParams.MinFeeA,
295+
MinFeeB: prevPParams.MinFeeB,
296+
MaxBlockBodySize: prevPParams.MaxBlockBodySize,
297+
MaxTxSize: prevPParams.MaxTxSize,
298+
MaxBlockHeaderSize: prevPParams.MaxBlockHeaderSize,
299+
KeyDeposit: prevPParams.KeyDeposit,
300+
PoolDeposit: prevPParams.PoolDeposit,
301+
MaxEpoch: prevPParams.MaxEpoch,
302+
NOpt: prevPParams.NOpt,
303+
A0: prevPParams.A0,
304+
Rho: prevPParams.Rho,
305+
Tau: prevPParams.Tau,
306+
Decentralization: prevPParams.Decentralization,
307+
ExtraEntropy: prevPParams.ExtraEntropy,
308+
ProtocolMajor: prevPParams.ProtocolMajor,
309+
ProtocolMinor: prevPParams.ProtocolMinor,
310+
MinUtxoValue: prevPParams.MinUtxoValue,
207311
}
208312
}

0 commit comments

Comments
 (0)