Skip to content

Commit c2506b4

Browse files
committed
feat(protocol): Added handshake network shapes & constants with main, testnet, regtest and simnet
Signed-off-by: Akhil Repala <[email protected]>
1 parent 7c6d8dd commit c2506b4

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package protocol
2+
3+
// Shapes only: constants & configs from hsd's protocol/networks.js.
4+
// No sockets, no consensus logic.
5+
6+
import (
7+
"math/big"
8+
"sort"
9+
"strings"
10+
"sync"
11+
"time"
12+
)
13+
14+
type NetworkType string
15+
16+
const (
17+
Mainnet NetworkType = "main"
18+
Testnet NetworkType = "testnet"
19+
Regtest NetworkType = "regtest"
20+
Simnet NetworkType = "simnet"
21+
)
22+
23+
type TimeSource interface {
24+
Now() time.Time
25+
Ms() int64
26+
}
27+
28+
type systemTime struct{}
29+
30+
func (t *systemTime) Now() time.Time {
31+
return time.Now()
32+
}
33+
34+
func (t *systemTime) Ms() int64 {
35+
return time.Now().UnixMilli()
36+
}
37+
38+
type Checkpoint struct {
39+
Height uint32
40+
Hash string
41+
}
42+
43+
type Deployment struct {
44+
Name string
45+
Bit uint32
46+
StartTime uint32
47+
Timeout uint32
48+
Threshold int32
49+
Window int32
50+
Required bool
51+
Force bool
52+
}
53+
54+
type KeyPrefix struct {
55+
Privkey uint32
56+
XPubKey uint32
57+
XPrivKey uint32
58+
XPubKey58 string
59+
XPrivKey58 string
60+
}
61+
62+
type POWParams struct {
63+
Limit *big.Int
64+
Bits uint32
65+
Chainwork *big.Int
66+
TargetWindow uint32
67+
TargetSpacing uint32
68+
BlocksPerDay uint32
69+
TargetTimespan uint32
70+
MinActual uint32
71+
MaxActual uint32
72+
TargetReset bool
73+
NoRetargeting bool
74+
}
75+
76+
type BlockLimits struct {
77+
PruneAfterHeight uint32
78+
KeepBlocks uint32
79+
MaxTipAge uint32
80+
SlowHeight uint32
81+
}
82+
83+
type NamesParams struct {
84+
AuctionStart uint32
85+
RolloutInterval uint32
86+
LockupPeriod uint32
87+
RenewalWindow uint32
88+
RenewalPeriod uint32
89+
RenewalMaturity uint32
90+
ClaimPeriod uint32
91+
AlexaLockupPeriod uint32
92+
ClaimFrequency uint32
93+
BiddingPeriod uint32
94+
RevealPeriod uint32
95+
TreeInterval uint32
96+
TransferLockup uint32
97+
AuctionMaturity uint32
98+
NoRollout bool
99+
NoReserved bool
100+
}
101+
102+
type Network struct {
103+
Type string
104+
Seeds []string
105+
Magic uint32
106+
Port uint16
107+
BrontidePort uint16
108+
CheckpointMap map[uint32]string
109+
LastCheckpoint uint32
110+
Checkpoints []Checkpoint
111+
112+
HalvingInterval uint32
113+
CoinbaseMaturity uint32
114+
GenesisHash string
115+
GenesisBlockHex string
116+
UnknownBitsMask uint32
117+
118+
POW POWParams
119+
Names NamesParams
120+
GoosigStop uint32
121+
Block BlockLimits
122+
123+
ActivationThreshold uint32
124+
MinerWindow uint32
125+
126+
Deployments map[string]Deployment
127+
Deploys []Deployment
128+
129+
KeyPrefix KeyPrefix
130+
AddressPrefix string
131+
ClaimPrefix string
132+
133+
RequireStandard bool
134+
MinRelay uint32
135+
FeeRate uint32
136+
MaxFeeRate uint32
137+
138+
RPCPort uint16
139+
WalletPort uint16
140+
NSPort uint16
141+
RSPort uint16
142+
143+
IdentityKeyHex string
144+
SelfConnect bool
145+
RequestMempool bool
146+
147+
DeflationHeight uint32
148+
TxStartHeight uint32
149+
timeSource TimeSource
150+
onceInit sync.Once
151+
}
152+
153+
func (n *Network) init() {
154+
n.onceInit.Do(func() {
155+
var mask uint32
156+
for _, d := range n.Deploys {
157+
if d.Bit >= 0 && d.Bit < 32 {
158+
mask |= (1 << uint32(d.Bit))
159+
}
160+
}
161+
n.UnknownBitsMask = ^mask
162+
n.Checkpoints = n.checkpointsFromMap()
163+
sort.Slice(n.Checkpoints, func(i, j int) bool {
164+
return n.Checkpoints[i].Height < n.Checkpoints[j].Height
165+
})
166+
})
167+
}
168+
169+
func (n *Network) checkpointsFromMap() []Checkpoint {
170+
if len(n.CheckpointMap) == 0 {
171+
return nil
172+
}
173+
out := make([]Checkpoint, 0, len(n.CheckpointMap))
174+
for h, hash := range n.CheckpointMap {
175+
out = append(out, Checkpoint{Height: h, Hash: strings.ToLower(hash)})
176+
}
177+
return out
178+
}
179+
180+
func (n *Network) ByBit(bit uint32) *Deployment {
181+
for i := range n.Deploys {
182+
if n.Deploys[i].Bit == bit {
183+
return &n.Deploys[i]
184+
}
185+
}
186+
return nil
187+
}
188+
189+
func (n *Network) Now() time.Time {
190+
return n.timeSource.Now()
191+
}
192+
193+
func (n *Network) Ms() int64 {
194+
return n.timeSource.Ms()
195+
}

0 commit comments

Comments
 (0)