github.com/mavryk-network/mvgo@v1.19.9/mavryk/param_test.go (about) 1 package mavryk_test 2 3 import ( 4 "testing" 5 6 "github.com/mavryk-network/mvgo/mavryk" 7 "github.com/mavryk-network/mvgo/rpc" 8 ) 9 10 type ( 11 Block = rpc.Block 12 BlockHeader = rpc.BlockHeader 13 BlockMetadata = rpc.BlockMetadata 14 VotingPeriodInfo = rpc.VotingPeriodInfo 15 LevelInfo = rpc.LevelInfo 16 ) 17 18 var ( 19 ProtoGenesis = mavryk.ProtoGenesis 20 ProtoBootstrap = mavryk.ProtoBootstrap 21 PtAtLas = mavryk.PtAtLas 22 PtBoreas = mavryk.PtBoreas 23 24 Mainnet = mavryk.Mainnet 25 NewParams = mavryk.NewParams 26 Deployments = mavryk.Deployments 27 ) 28 29 func TestParams(t *testing.T) { 30 var lastProto mavryk.ProtocolHash 31 32 // walk test blocks 33 for _, v := range paramBlocks { 34 // update test state 35 isProtoUpgrade := !lastProto.Equal(v.Protocol) 36 if isProtoUpgrade { 37 lastProto = v.Protocol 38 } 39 40 // prepare block 41 block := Block{ 42 Protocol: v.Protocol, 43 ChainId: Mainnet, 44 Header: BlockHeader{ 45 Level: v.LevelInfo.Level, 46 }, 47 Metadata: v, 48 } 49 height, cycle := block.GetLevel(), block.GetCycle() 50 51 // prepare params 52 p := NewParams(). 53 WithChainId(Mainnet). 54 WithProtocol(v.Protocol). 55 WithDeployment(Deployments[Mainnet].AtProtocol(v.Protocol)) 56 57 // load expected result 58 check := paramResults[height] 59 60 checkParams(t, p, height, cycle, check) 61 } 62 } 63 64 func TestParamsStatic(t *testing.T) { 65 for height, check := range paramResults { 66 p := NewParams().WithChainId(Mainnet).AtBlock(height) 67 checkParams(t, p, height, check.Cycle, check) 68 } 69 } 70 71 func TestDefaultParams(t *testing.T) { 72 for n, p := range map[string]*mavryk.Params{ 73 "main": mavryk.DefaultParams, 74 "base": mavryk.BasenetParams, 75 } { 76 if p.Network == "" { 77 t.Errorf("%s params: Empty network name", n) 78 } 79 if !p.ChainId.IsValid() { 80 t.Errorf("%s params: zero network id", n) 81 } 82 if !p.Protocol.IsValid() { 83 t.Errorf("%s params: zero protocol", n) 84 } 85 if have, want := p.Version, mavryk.Versions[p.Protocol]; have != want { 86 t.Errorf("%s params: version mismatch: have=%d want=%d", n, have, want) 87 } 88 if p.MinimalBlockDelay == 0 { 89 t.Errorf("%s params: zero MinimalBlockDelay", n) 90 } 91 if p.CostPerByte == 0 { 92 t.Errorf("%s params: zero CostPerByte", n) 93 } 94 if p.OriginationSize == 0 { 95 t.Errorf("%s params: zero OriginationSize", n) 96 } 97 if p.BlocksPerCycle == 0 { 98 t.Errorf("%s params: zero BlocksPerCycle", n) 99 } 100 if p.ConsensusRightsDelay == 0 { 101 t.Errorf("%s params: zero PreservedCycles", n) 102 } 103 if p.BlocksPerSnapshot == 0 { 104 t.Errorf("%s params: zero BlocksPerSnapshot", n) 105 } 106 if p.HardGasLimitPerOperation == 0 { 107 t.Errorf("%s params: zero HardGasLimitPerOperation", n) 108 } 109 if p.HardGasLimitPerBlock == 0 { 110 t.Errorf("%s params: zero HardGasLimitPerBlock", n) 111 } 112 if p.HardStorageLimitPerOperation == 0 { 113 t.Errorf("%s params: zero HardStorageLimitPerOperation", n) 114 } 115 if p.MaxOperationDataLength == 0 { 116 t.Errorf("%s params: zero MaxOperationDataLength", n) 117 } 118 if p.MaxOperationsTTL == 0 { 119 t.Errorf("%s params: zero MaxOperationsTTL", n) 120 } 121 if p.MaxOperationDataLength == 0 { 122 t.Errorf("%s params: zero MaxOperationDataLength", n) 123 } 124 if p.OperationTagsVersion < 0 || p.OperationTagsVersion > 3 { 125 t.Errorf("%s params: unknown OperationTagsVersion %d", n, p.OperationTagsVersion) 126 } 127 if p.StartHeight == 0 { 128 t.Errorf("%s params: zero StartHeight", n) 129 } 130 if p.EndHeight == 0 { 131 t.Errorf("%s params: zero EndHeight", n) 132 } 133 if p.StartHeight > p.BlocksPerCycle && p.StartCycle == 0 { 134 t.Errorf("%s params: zero StartCycle", n) 135 } 136 } 137 } 138 139 func checkParams(t *testing.T, p *mavryk.Params, height, cycle int64, check paramResult) { 140 // test param functions 141 if !p.ContainsHeight(height) { 142 t.Errorf("v%03d ContainsHeight(%d) failed", p.Version, height) 143 } 144 if !p.ContainsCycle(cycle) { 145 t.Errorf("v%03d %d ContainsCycle(%d) failed", p.Version, height, cycle) 146 } 147 if have, want := p.IsCycleStart(height), check.IsCycleStart(); have != want { 148 t.Errorf("v%03d IsCycleStart(%d) mismatch: have=%t want=%t", p.Version, height, have, want) 149 } 150 if have, want := p.IsCycleEnd(height), check.IsCycleEnd(); have != want { 151 t.Errorf("v%03d IsCycleEnd(%d) mismatch: have=%t want=%t", p.Version, height, have, want) 152 } 153 if have, want := p.CycleFromHeight(height), check.Cycle; have != want { 154 t.Errorf("v%03d CycleFromHeight(%d) mismatch: have=%d want=%d", p.Version, height, have, want) 155 } 156 cstart := p.CycleStartHeight(cycle) 157 cend := p.CycleEndHeight(cycle) 158 cpos := p.CyclePosition(height) 159 if cstart < 0 { 160 t.Errorf("v%03d %d negative cycle start %d", p.Version, height, cstart) 161 } 162 if cend < 0 { 163 t.Errorf("v%03d %d negative cycle end %d", p.Version, height, cend) 164 } 165 if cpos < 0 { 166 t.Errorf("v%03d %d negative cycle pos %d", p.Version, height, cpos) 167 } 168 if cstart >= cend { 169 t.Errorf("v%03d %d cycle start %d > end %d", p.Version, height, cstart, cend) 170 } 171 if cstart+cpos != height { 172 t.Errorf("v%03d %d cycle pos %d + start %d != height", p.Version, height, cstart, cpos) 173 } 174 175 if have, want := p.IsSnapshotBlock(height), check.IsSnapshot(); have != want { 176 t.Errorf("v%03d IsSnapshotBlock(%d) mismatch: have=%t want=%t", p.Version, height, have, want) 177 } 178 if have, want := p.SnapshotIndex(height), check.Snap; have != want { 179 t.Errorf("v%03d SnapshotIndex(%d) mismatch: have=%d want=%d", p.Version, height, have, want) 180 } 181 if have, want := p.SnapshotBlock(cycle, 0), height; have > want { 182 t.Errorf("v%03d SnapshotBlock(%d) mismatch: have=%d > want=%d", p.Version, height, have, want) 183 } 184 } 185 186 type paramResult struct { 187 Cycle int64 188 Snap int 189 Flags byte // 16 Snapshot | 8 CycleStart | 4 CycleEnd | 2 VoteStart | 1 VoteEnd 190 } 191 192 func (p paramResult) IsSnapshot() bool { 193 return (p.Flags>>4)&0x1 > 0 194 } 195 196 func (p paramResult) IsCycleStart() bool { 197 return (p.Flags>>3)&0x1 > 0 198 } 199 200 func (p paramResult) IsCycleEnd() bool { 201 return (p.Flags>>2)&0x1 > 0 202 } 203 204 func (p paramResult) IsVoteStart() bool { 205 return (p.Flags>>1)&0x1 > 0 206 } 207 208 func (p paramResult) IsVoteEnd() bool { 209 return p.Flags&0x1 > 0 210 } 211 212 var paramResults = map[int64]paramResult{ 213 0: {0, -1, 0}, // genesis 214 1: {0, -1, 8}, // bootstrap 215 5070849: {703, -1, 8 + 2}, // v018 start 216 5726208: {742, 15, 16 + 4 + 1}, // --> end 217 5726209: {743, 15, 8 + 2}, // v019 start 218 } 219 220 var paramBlocks = []BlockMetadata{ 221 { 222 // genesis 223 Protocol: ProtoGenesis, 224 NextProtocol: ProtoBootstrap, 225 LevelInfo: &LevelInfo{}, 226 VotingPeriodInfo: &VotingPeriodInfo{}, 227 }, { 228 // bootstrap 229 Protocol: ProtoBootstrap, 230 NextProtocol: PtAtLas, 231 LevelInfo: &LevelInfo{ 232 Level: 1, 233 }, 234 VotingPeriodInfo: &VotingPeriodInfo{}, 235 }, { 236 // v18 start 237 Protocol: PtAtLas, 238 NextProtocol: PtAtLas, 239 LevelInfo: &LevelInfo{ 240 Level: 5070849, 241 Cycle: 703, 242 CyclePosition: 0, 243 ExpectedCommitment: false, 244 }, 245 VotingPeriodInfo: &VotingPeriodInfo{ 246 Position: 0, 247 Remaining: 81912, 248 }, 249 }, { 250 // v18 end 251 Protocol: PtAtLas, 252 NextProtocol: PtBoreas, 253 LevelInfo: &LevelInfo{ 254 Level: 5726208, 255 Cycle: 742, 256 CyclePosition: 16383, 257 ExpectedCommitment: true, 258 }, 259 VotingPeriodInfo: &VotingPeriodInfo{ 260 Position: 81912, 261 Remaining: 0, 262 }, 263 }, { 264 // v19 start 265 Protocol: PtBoreas, 266 NextProtocol: PtBoreas, 267 LevelInfo: &LevelInfo{ 268 Level: 5726209, 269 Cycle: 743, 270 CyclePosition: 0, 271 ExpectedCommitment: false, 272 }, 273 VotingPeriodInfo: &VotingPeriodInfo{ 274 Position: 0, 275 Remaining: 81912, 276 }, 277 }, 278 }