|
| 1 | +// Copyright 2024 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package allegra_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/hex" |
| 19 | + "math/big" |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 24 | + "github.com/blinklabs-io/gouroboros/ledger/allegra" |
| 25 | + "github.com/blinklabs-io/gouroboros/ledger/shelley" |
| 26 | +) |
| 27 | + |
| 28 | +func TestAllegraProtocolParamsUpdate(t *testing.T) { |
| 29 | + testDefs := []struct { |
| 30 | + startParams allegra.AllegraProtocolParameters |
| 31 | + updateCbor string |
| 32 | + expectedParams allegra.AllegraProtocolParameters |
| 33 | + }{ |
| 34 | + { |
| 35 | + startParams: allegra.AllegraProtocolParameters{ |
| 36 | + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ |
| 37 | + Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + updateCbor: "a10cd81e82090a", |
| 41 | + expectedParams: allegra.AllegraProtocolParameters{ |
| 42 | + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ |
| 43 | + Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + startParams: allegra.AllegraProtocolParameters{ |
| 49 | + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ |
| 50 | + ProtocolMajor: 3, |
| 51 | + }, |
| 52 | + }, |
| 53 | + updateCbor: "a10e820400", |
| 54 | + expectedParams: allegra.AllegraProtocolParameters{ |
| 55 | + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ |
| 56 | + ProtocolMajor: 4, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, testDef := range testDefs { |
| 62 | + cborBytes, err := hex.DecodeString(testDef.updateCbor) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("unexpected error: %s", err) |
| 65 | + } |
| 66 | + var tmpUpdate allegra.AllegraProtocolParameterUpdate |
| 67 | + if _, err := cbor.Decode(cborBytes, &tmpUpdate); err != nil { |
| 68 | + t.Fatalf("unexpected error: %s", err) |
| 69 | + } |
| 70 | + tmpParams := testDef.startParams |
| 71 | + tmpParams.Update(&tmpUpdate) |
| 72 | + if !reflect.DeepEqual(tmpParams, testDef.expectedParams) { |
| 73 | + t.Fatalf("did not get expected params:\n got: %#v\n wanted: %#v", tmpParams, testDef.expectedParams) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments