Skip to content

Commit 7870818

Browse files
committed
feat(protocol): Added networks map and selector. Implemented the definition part & constats initilization for testnet shape
Signed-off-by: Akhil Repala <[email protected]>
1 parent c2506b4 commit 7870818

File tree

1 file changed

+164
-4
lines changed

1 file changed

+164
-4
lines changed

internal/handshake/protocol/network.go

Lines changed: 164 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package protocol
22

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.
54

65
import (
76
"math/big"
@@ -57,6 +56,7 @@ type KeyPrefix struct {
5756
XPrivKey uint32
5857
XPubKey58 string
5958
XPrivKey58 string
59+
CoinType uint32
6060
}
6161

6262
type POWParams struct {
@@ -152,10 +152,13 @@ type Network struct {
152152

153153
func (n *Network) init() {
154154
n.onceInit.Do(func() {
155+
if n.timeSource == nil {
156+
n.timeSource = &systemTime{}
157+
}
155158
var mask uint32
156159
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)
159162
}
160163
}
161164
n.UnknownBitsMask = ^mask
@@ -186,10 +189,167 @@ func (n *Network) ByBit(bit uint32) *Deployment {
186189
return nil
187190
}
188191

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+
189202
func (n *Network) Now() time.Time {
203+
n.ensureTime()
190204
return n.timeSource.Now()
191205
}
192206

193207
func (n *Network) Ms() int64 {
208+
n.ensureTime()
194209
return n.timeSource.Ms()
195210
}
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

Comments
 (0)