|
| 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 shelley |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "math/big" |
| 20 | + |
| 21 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 22 | +) |
| 23 | + |
| 24 | +type ShelleyProtocolParameters struct { |
| 25 | + cbor.StructAsArray |
| 26 | + MinFeeA uint |
| 27 | + MinFeeB uint |
| 28 | + MaxBlockBodySize uint |
| 29 | + MaxTxSize uint |
| 30 | + MaxBlockHeaderSize uint |
| 31 | + KeyDeposit uint |
| 32 | + PoolDeposit uint |
| 33 | + MaxEpoch uint |
| 34 | + NOpt uint |
| 35 | + A0 *cbor.Rat |
| 36 | + Rho *cbor.Rat |
| 37 | + Tau *cbor.Rat |
| 38 | + Decentralization *cbor.Rat |
| 39 | + Nonce *Nonce |
| 40 | + ProtocolMajor uint |
| 41 | + ProtocolMinor uint |
| 42 | + MinUtxoValue uint |
| 43 | +} |
| 44 | + |
| 45 | +func (p *ShelleyProtocolParameters) Update(paramUpdate *ShelleyProtocolParameterUpdate) { |
| 46 | + if paramUpdate.MinFeeA != nil { |
| 47 | + p.MinFeeA = *paramUpdate.MinFeeA |
| 48 | + } |
| 49 | + if paramUpdate.MinFeeB != nil { |
| 50 | + p.MinFeeB = *paramUpdate.MinFeeB |
| 51 | + } |
| 52 | + if paramUpdate.MaxBlockBodySize != nil { |
| 53 | + p.MaxBlockBodySize = *paramUpdate.MaxBlockBodySize |
| 54 | + } |
| 55 | + if paramUpdate.MaxTxSize != nil { |
| 56 | + p.MaxTxSize = *paramUpdate.MaxTxSize |
| 57 | + } |
| 58 | + if paramUpdate.MaxBlockHeaderSize != nil { |
| 59 | + p.MaxBlockHeaderSize = *paramUpdate.MaxBlockHeaderSize |
| 60 | + } |
| 61 | + if paramUpdate.KeyDeposit != nil { |
| 62 | + p.KeyDeposit = *paramUpdate.KeyDeposit |
| 63 | + } |
| 64 | + if paramUpdate.PoolDeposit != nil { |
| 65 | + p.PoolDeposit = *paramUpdate.PoolDeposit |
| 66 | + } |
| 67 | + if paramUpdate.MaxEpoch != nil { |
| 68 | + p.MaxEpoch = *paramUpdate.MaxEpoch |
| 69 | + } |
| 70 | + if paramUpdate.NOpt != nil { |
| 71 | + p.NOpt = *paramUpdate.NOpt |
| 72 | + } |
| 73 | + if paramUpdate.A0 != nil { |
| 74 | + p.A0 = paramUpdate.A0 |
| 75 | + } |
| 76 | + if paramUpdate.Rho != nil { |
| 77 | + p.Rho = paramUpdate.Rho |
| 78 | + } |
| 79 | + if paramUpdate.Tau != nil { |
| 80 | + p.Tau = paramUpdate.Tau |
| 81 | + } |
| 82 | + if paramUpdate.Decentralization != nil { |
| 83 | + p.Decentralization = paramUpdate.Decentralization |
| 84 | + } |
| 85 | + if paramUpdate.ProtocolVersion != nil { |
| 86 | + p.ProtocolMajor = paramUpdate.ProtocolVersion.Major |
| 87 | + p.ProtocolMinor = paramUpdate.ProtocolVersion.Minor |
| 88 | + } |
| 89 | + if paramUpdate.Nonce != nil { |
| 90 | + p.Nonce = paramUpdate.Nonce |
| 91 | + } |
| 92 | + if paramUpdate.MinUtxoValue != nil { |
| 93 | + p.MinUtxoValue = *paramUpdate.MinUtxoValue |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (p *ShelleyProtocolParameters) UpdateFromGenesis(genesis *ShelleyGenesis) { |
| 98 | + genesisParams := genesis.ProtocolParameters |
| 99 | + p.MinFeeA = genesisParams.MinFeeA |
| 100 | + p.MinFeeB = genesisParams.MinFeeB |
| 101 | + p.MaxBlockBodySize = genesisParams.MaxBlockBodySize |
| 102 | + p.MaxTxSize = genesisParams.MaxTxSize |
| 103 | + p.MaxBlockHeaderSize = genesisParams.MaxBlockHeaderSize |
| 104 | + p.KeyDeposit = genesisParams.KeyDeposit |
| 105 | + p.PoolDeposit = genesisParams.PoolDeposit |
| 106 | + p.MaxEpoch = genesisParams.MaxEpoch |
| 107 | + p.NOpt = genesisParams.NOpt |
| 108 | + if genesisParams.A0 != nil { |
| 109 | + p.A0 = &cbor.Rat{Rat: new(big.Rat).Set(genesisParams.A0.Rat)} |
| 110 | + } |
| 111 | + if genesisParams.Rho != nil { |
| 112 | + p.Rho = &cbor.Rat{Rat: new(big.Rat).Set(genesisParams.Rho.Rat)} |
| 113 | + } |
| 114 | + if genesisParams.Tau != nil { |
| 115 | + p.Tau = &cbor.Rat{Rat: new(big.Rat).Set(genesisParams.Tau.Rat)} |
| 116 | + } |
| 117 | + if genesisParams.Decentralization != nil { |
| 118 | + p.Decentralization = &cbor.Rat{Rat: new(big.Rat).Set(genesisParams.Decentralization.Rat)} |
| 119 | + } |
| 120 | + p.ProtocolMajor = genesisParams.ProtocolVersion.Major |
| 121 | + p.ProtocolMinor = genesisParams.ProtocolVersion.Minor |
| 122 | + p.MinUtxoValue = genesisParams.MinUtxoValue |
| 123 | + // TODO: |
| 124 | + //p.Nonce *cbor.Rat |
| 125 | +} |
| 126 | + |
| 127 | +type ShelleyProtocolParametersProtocolVersion struct { |
| 128 | + cbor.StructAsArray |
| 129 | + Major uint |
| 130 | + Minor uint |
| 131 | +} |
| 132 | + |
| 133 | +type ShelleyProtocolParameterUpdate struct { |
| 134 | + cbor.DecodeStoreCbor |
| 135 | + MinFeeA *uint `cbor:"0,keyasint"` |
| 136 | + MinFeeB *uint `cbor:"1,keyasint"` |
| 137 | + MaxBlockBodySize *uint `cbor:"2,keyasint"` |
| 138 | + MaxTxSize *uint `cbor:"3,keyasint"` |
| 139 | + MaxBlockHeaderSize *uint `cbor:"4,keyasint"` |
| 140 | + KeyDeposit *uint `cbor:"5,keyasint"` |
| 141 | + PoolDeposit *uint `cbor:"6,keyasint"` |
| 142 | + MaxEpoch *uint `cbor:"7,keyasint"` |
| 143 | + NOpt *uint `cbor:"8,keyasint"` |
| 144 | + A0 *cbor.Rat `cbor:"9,keyasint"` |
| 145 | + Rho *cbor.Rat `cbor:"10,keyasint"` |
| 146 | + Tau *cbor.Rat `cbor:"11,keyasint"` |
| 147 | + Decentralization *cbor.Rat `cbor:"12,keyasint"` |
| 148 | + Nonce *Nonce `cbor:"13,keyasint"` |
| 149 | + ProtocolVersion *ShelleyProtocolParametersProtocolVersion `cbor:"14,keyasint"` |
| 150 | + MinUtxoValue *uint `cbor:"15,keyasint"` |
| 151 | +} |
| 152 | + |
| 153 | +func (ShelleyProtocolParameterUpdate) IsProtocolParameterUpdate() {} |
| 154 | + |
| 155 | +func (u *ShelleyProtocolParameterUpdate) UnmarshalCBOR(data []byte) error { |
| 156 | + return u.UnmarshalCbor(data, u) |
| 157 | +} |
| 158 | + |
| 159 | +const ( |
| 160 | + NonceType0 = 0 |
| 161 | + NonceType1 = 1 |
| 162 | +) |
| 163 | + |
| 164 | +var NeutralNonce = Nonce{ |
| 165 | + Type: NonceType0, |
| 166 | +} |
| 167 | + |
| 168 | +type Nonce struct { |
| 169 | + cbor.StructAsArray |
| 170 | + Type uint |
| 171 | + Value [32]byte |
| 172 | +} |
| 173 | + |
| 174 | +func (n *Nonce) UnmarshalCBOR(data []byte) error { |
| 175 | + nonceType, err := cbor.DecodeIdFromList(data) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + n.Type = uint(nonceType) |
| 181 | + |
| 182 | + switch nonceType { |
| 183 | + case NonceType0: |
| 184 | + // Value uses default value |
| 185 | + case NonceType1: |
| 186 | + if err := cbor.DecodeGeneric(data, n); err != nil { |
| 187 | + fmt.Printf("Nonce decode error: %+v\n", data) |
| 188 | + return err |
| 189 | + } |
| 190 | + default: |
| 191 | + return fmt.Errorf("unsupported nonce type %d", nonceType) |
| 192 | + } |
| 193 | + return nil |
| 194 | +} |
0 commit comments