|
1 | 1 | package protocol |
2 | 2 |
|
3 | | -// Shapes only: constants & configs from hsd's protocol/networks.js. |
4 | | -// No sockets, no consensus logic. |
| 3 | +// Shapes only - constants & configs from hsd's protocol/networks.js. |
5 | 4 |
|
6 | 5 | import ( |
7 | 6 | "math/big" |
@@ -57,6 +56,7 @@ type KeyPrefix struct { |
57 | 56 | XPrivKey uint32 |
58 | 57 | XPubKey58 string |
59 | 58 | XPrivKey58 string |
| 59 | + CoinType uint32 |
60 | 60 | } |
61 | 61 |
|
62 | 62 | type POWParams struct { |
@@ -152,10 +152,13 @@ type Network struct { |
152 | 152 |
|
153 | 153 | func (n *Network) init() { |
154 | 154 | n.onceInit.Do(func() { |
| 155 | + if n.timeSource == nil { |
| 156 | + n.timeSource = &systemTime{} |
| 157 | + } |
155 | 158 | var mask uint32 |
156 | 159 | for _, d := range n.Deploys { |
157 | | - if d.Bit >= 0 && d.Bit < 32 { |
158 | | - mask |= (1 << uint32(d.Bit)) |
| 160 | + if d.Bit < 32 { |
| 161 | + mask |= (1 << d.Bit) |
159 | 162 | } |
160 | 163 | } |
161 | 164 | n.UnknownBitsMask = ^mask |
@@ -186,10 +189,167 @@ func (n *Network) ByBit(bit uint32) *Deployment { |
186 | 189 | return nil |
187 | 190 | } |
188 | 191 |
|
| 192 | +func (n *Network) ensureTime() { |
| 193 | + if n.timeSource == nil { |
| 194 | + n.timeSource = &systemTime{} |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func (n *Network) SetTimeSource(ts TimeSource) { |
| 199 | + n.timeSource = ts |
| 200 | +} |
| 201 | + |
189 | 202 | func (n *Network) Now() time.Time { |
| 203 | + n.ensureTime() |
190 | 204 | return n.timeSource.Now() |
191 | 205 | } |
192 | 206 |
|
193 | 207 | func (n *Network) Ms() int64 { |
| 208 | + n.ensureTime() |
194 | 209 | return n.timeSource.Ms() |
195 | 210 | } |
| 211 | + |
| 212 | +func bi(hex string) *big.Int { |
| 213 | + n := new(big.Int) |
| 214 | + if _, ok := n.SetString(hex, 16); !ok { |
| 215 | + panic("invalid hex for big.Int: " + hex) |
| 216 | + } |
| 217 | + return n |
| 218 | +} |
| 219 | + |
| 220 | +var Networks = map[NetworkType]*Network{ |
| 221 | + Mainnet: mainNet(), |
| 222 | + Testnet: testNet(), |
| 223 | + Regtest: regTest(), |
| 224 | + Simnet: simNet(), |
| 225 | +} |
| 226 | + |
| 227 | +func SelectNetwork(t NetworkType) *Network { |
| 228 | + return Networks[t] |
| 229 | +} |
| 230 | + |
| 231 | +func testNet() *Network { |
| 232 | + const ( |
| 233 | + targetSpacing = uint32(10 * 60) |
| 234 | + targetWindow = uint32(144) |
| 235 | + ) |
| 236 | + blocksPerDay := uint32((24 * 60 * 60) / targetSpacing) |
| 237 | + targetTimespan := targetWindow * targetSpacing |
| 238 | + |
| 239 | + n := &Network{ |
| 240 | + Type: "testnet", |
| 241 | + Seeds: []string{"hs-testnet.bcoin.ninja"}, |
| 242 | + Magic: 0, // Need to modify from genesis.testnet.magic |
| 243 | + Port: 13038, |
| 244 | + BrontidePort: 45806, |
| 245 | + |
| 246 | + CheckpointMap: map[uint32]string{}, |
| 247 | + LastCheckpoint: 0, |
| 248 | + |
| 249 | + HalvingInterval: 170000, |
| 250 | + CoinbaseMaturity: 100, |
| 251 | + |
| 252 | + POW: POWParams{ |
| 253 | + Limit: bi("00000000ffff0000000000000000000000000000000000000000000000000000"), |
| 254 | + Bits: 0x1d00ffff, |
| 255 | + Chainwork: bi("0000000000000000000000000000000000000000000000000000000000000000"), |
| 256 | + TargetWindow: targetWindow, |
| 257 | + TargetSpacing: targetSpacing, |
| 258 | + BlocksPerDay: blocksPerDay, |
| 259 | + TargetTimespan: targetTimespan, |
| 260 | + MinActual: targetTimespan / 4, |
| 261 | + MaxActual: targetTimespan * 4, |
| 262 | + TargetReset: true, |
| 263 | + NoRetargeting: false, |
| 264 | + }, |
| 265 | + |
| 266 | + Names: NamesParams{ |
| 267 | + AuctionStart: uint32(0.25 * float32(blocksPerDay)), |
| 268 | + RolloutInterval: uint32(0.25 * float32(blocksPerDay)), |
| 269 | + LockupPeriod: uint32(0.25 * float32(blocksPerDay)), |
| 270 | + RenewalWindow: 30 * blocksPerDay, |
| 271 | + RenewalPeriod: 7 * blocksPerDay, |
| 272 | + RenewalMaturity: 1 * blocksPerDay, |
| 273 | + ClaimPeriod: 90 * blocksPerDay, |
| 274 | + AlexaLockupPeriod: 180 * blocksPerDay, |
| 275 | + ClaimFrequency: 2 * blocksPerDay, |
| 276 | + BiddingPeriod: 1 * blocksPerDay, |
| 277 | + RevealPeriod: 2 * blocksPerDay, |
| 278 | + TreeInterval: blocksPerDay >> 2, |
| 279 | + TransferLockup: 2 * blocksPerDay, |
| 280 | + AuctionMaturity: (1 + 2 + 4) * blocksPerDay, |
| 281 | + NoRollout: false, |
| 282 | + NoReserved: false, |
| 283 | + }, |
| 284 | + |
| 285 | + Block: BlockLimits{ |
| 286 | + PruneAfterHeight: 1000, |
| 287 | + KeepBlocks: 10000, |
| 288 | + MaxTipAge: 12 * 60 * 60, |
| 289 | + SlowHeight: 0, |
| 290 | + }, |
| 291 | + |
| 292 | + GoosigStop: 20 * blocksPerDay, |
| 293 | + ActivationThreshold: 1512, |
| 294 | + MinerWindow: 2016, |
| 295 | + |
| 296 | + Deployments: map[string]Deployment{ |
| 297 | + "hardening": {Name: "hardening", Bit: 0, StartTime: 1581638400, Timeout: 1707868800, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 298 | + "icannlockup": {Name: "icannlockup", Bit: 1, StartTime: 1691625600, Timeout: 1703980800, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 299 | + "airstop": {Name: "airstop", Bit: 2, StartTime: 1751328000, Timeout: 1759881600, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 300 | + "testdummy": {Name: "testdummy", Bit: 28, StartTime: 1199145601, Timeout: 1230767999, Threshold: -1, Window: -1, Required: false, Force: true}, |
| 301 | + }, |
| 302 | + Deploys: []Deployment{ |
| 303 | + {Name: "hardening", Bit: 0, StartTime: 1581638400, Timeout: 1707868800, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 304 | + {Name: "icannlockup", Bit: 1, StartTime: 1691625600, Timeout: 1703980800, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 305 | + {Name: "airstop", Bit: 2, StartTime: 1751328000, Timeout: 1759881600, Threshold: -1, Window: -1, Required: false, Force: false}, |
| 306 | + {Name: "testdummy", Bit: 28, StartTime: 1199145601, Timeout: 1230767999, Threshold: -1, Window: -1, Required: false, Force: true}, |
| 307 | + }, |
| 308 | + |
| 309 | + KeyPrefix: KeyPrefix{ |
| 310 | + Privkey: 0xef, |
| 311 | + XPubKey: 0x043587cf, |
| 312 | + XPrivKey: 0x04358394, |
| 313 | + XPubKey58: "tpub", |
| 314 | + XPrivKey58: "tprv", |
| 315 | + CoinType: 5354, |
| 316 | + }, |
| 317 | + AddressPrefix: "ts", |
| 318 | + ClaimPrefix: "hns-testnet:", |
| 319 | + RequireStandard: false, |
| 320 | + |
| 321 | + RPCPort: 13037, |
| 322 | + WalletPort: 13039, |
| 323 | + NSPort: 15349, |
| 324 | + RSPort: 15350, |
| 325 | + |
| 326 | + MinRelay: 1000, |
| 327 | + FeeRate: 20000, |
| 328 | + MaxFeeRate: 60000, |
| 329 | + |
| 330 | + IdentityKeyHex: "", |
| 331 | + SelfConnect: false, |
| 332 | + RequestMempool: true, |
| 333 | + |
| 334 | + DeflationHeight: 0, |
| 335 | + TxStartHeight: 0, |
| 336 | + } |
| 337 | + n.ensureTime() |
| 338 | + n.init() |
| 339 | + return n |
| 340 | +} |
| 341 | + |
| 342 | +func mainNet() *Network { |
| 343 | + // Need to implement |
| 344 | + return &Network{} |
| 345 | +} |
| 346 | + |
| 347 | +func regTest() *Network { |
| 348 | + // Need to implement |
| 349 | + return &Network{} |
| 350 | +} |
| 351 | + |
| 352 | +func simNet() *Network { |
| 353 | + // Need to implement |
| 354 | + return &Network{} |
| 355 | +} |
0 commit comments