Skip to content

Commit 1a979c1

Browse files
committed
v1.4.18-patch-2
1 parent 5252a9a commit 1a979c1

File tree

63 files changed

+966
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+966
-53
lines changed

nekryptology/pkg/vdf/wesolowski.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ func GenerateVDFWithStopChan(seed []byte, iterations, int_size_bits uint32, stop
8989
func GenerateVDFIteration(seed, x_blob []byte, iterations, int_size_bits uint32) ([]byte, []byte) {
9090
int_size := (int_size_bits + 16) >> 4
9191
D := iqc.CreateDiscriminant(seed, int_size_bits)
92-
x, _ := iqc.NewClassGroupFromBytesDiscriminant(x_blob[:(2*int_size)], D)
92+
x, ok := iqc.NewClassGroupFromBytesDiscriminant(x_blob[:(2*int_size)], D)
93+
if !ok {
94+
return nil, nil
95+
}
9396

9497
y, proof := calculateVDF(D, x, iterations, int_size_bits, nil)
9598

@@ -105,18 +108,36 @@ func VerifyVDF(seed, proof_blob []byte, iterations, int_size_bits uint32) bool {
105108

106109
D := iqc.CreateDiscriminant(seed, int_size_bits)
107110
x := iqc.NewClassGroupFromAbDiscriminant(big.NewInt(2), big.NewInt(1), D)
108-
y, _ := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[:(2*int_size)], D)
109-
proof, _ := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[2*int_size:], D)
111+
y, ok := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[:(2*int_size)], D)
112+
if !ok {
113+
return false
114+
}
115+
116+
proof, ok := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[2*int_size:], D)
117+
if !ok {
118+
return false
119+
}
110120

111121
return verifyProof(x, y, proof, iterations)
112122
}
113123

114124
func VerifyVDFIteration(seed, x_blob, proof_blob []byte, iterations, int_size_bits uint32) bool {
115125
int_size := (int_size_bits + 16) >> 4
116126
D := iqc.CreateDiscriminant(seed, int_size_bits)
117-
x, _ := iqc.NewClassGroupFromBytesDiscriminant(x_blob[:(2*int_size)], D)
118-
y, _ := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[:(2*int_size)], D)
119-
proof, _ := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[2*int_size:], D)
127+
x, ok := iqc.NewClassGroupFromBytesDiscriminant(x_blob[:(2*int_size)], D)
128+
if !ok {
129+
return false
130+
}
131+
132+
y, ok := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[:(2*int_size)], D)
133+
if !ok {
134+
return false
135+
}
136+
137+
proof, ok := iqc.NewClassGroupFromBytesDiscriminant(proof_blob[2*int_size:], D)
138+
if !ok {
139+
return false
140+
}
120141

121142
return verifyProof(x, y, proof, iterations)
122143
}

node/config/engine.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ type EngineConfig struct {
88
PendingCommitWorkers int64 `yaml:"pendingCommitWorkers"`
99
MinimumPeersRequired int `yaml:"minimumPeersRequired"`
1010
StatsMultiaddr string `yaml:"statsMultiaddr"`
11+
// Sets the fmt.Sprintf format string to use as the listen multiaddrs for
12+
// data worker processes
13+
DataWorkerBaseListenMultiaddr string `yaml:"dataWorkerBaseListenMultiaddr"`
14+
// Sets the starting port number to use as the listen port for data worker
15+
// processes, incrementing by 1 until n-1, n = cores. (Example: a 4 core
16+
// system, base listen port of 40000 will listen on 40000, 40001, 40002)
17+
DataWorkerBaseListenPort uint16 `yaml:"dataWorkerBaseListenPort"`
18+
DataWorkerMemoryLimit int64 `yaml:"dataWorkerMemoryLimit"`
1119

1220
// Values used only for testing – do not override these in production, your
1321
// node will get kicked out

node/config/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ func FormatVersion(version []byte) string {
2929
}
3030

3131
func GetPatchNumber() byte {
32-
return 0x01
32+
return 0x02
3333
}

node/consensus/master/broadcast_messaging.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,19 @@ func (e *MasterClockConsensusEngine) handleSelfTestReport(
116116
peerID []byte,
117117
any *anypb.Any,
118118
) error {
119-
if bytes.Equal(peerID, e.pubSub.GetPeerID()) {
120-
return nil
121-
}
122-
123119
report := &protobufs.SelfTestReport{}
124120
if err := any.UnmarshalTo(report); err != nil {
125121
return errors.Wrap(err, "handle self test report")
126122
}
127123

124+
if bytes.Equal(peerID, e.pubSub.GetPeerID()) {
125+
info := e.peerInfoManager.GetPeerInfo(peerID)
126+
info.LastSeen = time.Now().UnixMilli()
127+
info.DifficultyMetric = report.DifficultyMetric
128+
info.MasterHeadFrame = report.MasterHeadFrame
129+
return nil
130+
}
131+
128132
// minimum proof size is one timestamp, one vdf proof, must match one fewer
129133
// than core count
130134
if len(report.Proof) < 516+8 ||

node/consensus/master/master_clock_consensus_engine.go

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/rand"
88
"encoding/binary"
99
"encoding/hex"
10+
"fmt"
1011
"io"
1112
"math/big"
1213
"sync"
@@ -15,9 +16,12 @@ import (
1516
"github.com/iden3/go-iden3-crypto/poseidon"
1617
"github.com/libp2p/go-libp2p/core/peer"
1718
"github.com/mr-tron/base58"
19+
"github.com/multiformats/go-multiaddr"
20+
mn "github.com/multiformats/go-multiaddr/net"
1821
"github.com/pkg/errors"
1922
"go.uber.org/zap"
2023
"google.golang.org/grpc"
24+
"google.golang.org/grpc/credentials/insecure"
2125
"source.quilibrium.com/quilibrium/monorepo/node/config"
2226
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
2327
qtime "source.quilibrium.com/quilibrium/monorepo/node/consensus/time"
@@ -68,6 +72,7 @@ type MasterClockConsensusEngine struct {
6872
verifyTestCh chan verifyChallenge
6973
currentReceivingSyncPeers int
7074
currentReceivingSyncPeersMx sync.Mutex
75+
engineConfig *config.EngineConfig
7176
}
7277

7378
var _ consensus.ConsensusEngine = (*MasterClockConsensusEngine)(nil)
@@ -131,6 +136,7 @@ func NewMasterClockConsensusEngine(
131136
frameValidationCh: make(chan *protobufs.ClockFrame),
132137
bandwidthTestCh: make(chan []byte),
133138
verifyTestCh: make(chan verifyChallenge),
139+
engineConfig: engineConfig,
134140
}
135141

136142
e.addPeerManifestReport(e.pubSub.GetPeerID(), report)
@@ -241,6 +247,16 @@ func (e *MasterClockConsensusEngine) Start() <-chan error {
241247
time.Sleep(30 * time.Second)
242248
difficultyMetric := int64(100000)
243249
skew := (difficultyMetric * 12) / 10
250+
parallelism := e.report.Cores - 1
251+
252+
if parallelism < 3 {
253+
panic("invalid system configuration, minimum system configuration must be four cores")
254+
}
255+
256+
clients, err := e.createParallelDataClients(int(parallelism))
257+
if err != nil {
258+
panic(err)
259+
}
244260

245261
for {
246262
head, err := e.masterTimeReel.Head()
@@ -250,23 +266,49 @@ func (e *MasterClockConsensusEngine) Start() <-chan error {
250266

251267
e.report.MasterHeadFrame = head.FrameNumber
252268
e.report.DifficultyMetric = difficultyMetric
253-
parallelism := e.report.Cores - 1
254269

255270
challenge := binary.BigEndian.AppendUint64(
256271
[]byte{},
257272
e.report.MasterHeadFrame,
258273
)
259274
challenge = append(challenge, e.pubSub.GetPeerID()...)
260275

261-
ts, proofs, nextDifficultyMetric, err :=
262-
e.frameProver.CalculateChallengeProof(
263-
challenge,
264-
parallelism,
265-
skew,
266-
)
267-
if err != nil {
268-
panic(err)
276+
proofs := make([][]byte, parallelism)
277+
nextMetrics := make([]int64, parallelism)
278+
279+
wg := sync.WaitGroup{}
280+
wg.Add(int(parallelism))
281+
282+
ts := time.Now().UnixMilli()
283+
for i := uint32(0); i < parallelism; i++ {
284+
i := i
285+
go func() {
286+
resp, err :=
287+
clients[i].CalculateChallengeProof(
288+
context.Background(),
289+
&protobufs.ChallengeProofRequest{
290+
Challenge: challenge,
291+
Core: i,
292+
Skew: skew,
293+
NowMs: ts,
294+
},
295+
)
296+
if err != nil {
297+
panic(err)
298+
}
299+
300+
proofs[i], nextMetrics[i] = resp.Output, resp.NextSkew
301+
wg.Done()
302+
}()
269303
}
304+
wg.Wait()
305+
nextDifficultySum := uint64(0)
306+
for i := 0; i < int(parallelism); i++ {
307+
nextDifficultySum += uint64(nextMetrics[i])
308+
}
309+
310+
nextDifficultyMetric := int64(nextDifficultySum / uint64(parallelism))
311+
270312
e.logger.Info(
271313
"recalibrating difficulty metric",
272314
zap.Int64("previous_difficulty_metric", difficultyMetric),
@@ -336,6 +378,64 @@ func (e *MasterClockConsensusEngine) Start() <-chan error {
336378
return errChan
337379
}
338380

381+
func (e *MasterClockConsensusEngine) createParallelDataClients(
382+
paralellism int,
383+
) ([]protobufs.DataIPCServiceClient, error) {
384+
e.logger.Info(
385+
"connecting to data worker processes",
386+
zap.Int("parallelism", paralellism),
387+
)
388+
389+
if e.engineConfig.DataWorkerBaseListenMultiaddr == "" {
390+
e.engineConfig.DataWorkerBaseListenMultiaddr = "/ip4/127.0.0.1/tcp/%d"
391+
}
392+
393+
if e.engineConfig.DataWorkerBaseListenPort == 0 {
394+
e.engineConfig.DataWorkerBaseListenPort = 40000
395+
}
396+
397+
clients := make([]protobufs.DataIPCServiceClient, paralellism)
398+
399+
for i := 0; i < paralellism; i++ {
400+
ma, err := multiaddr.NewMultiaddr(
401+
fmt.Sprintf(
402+
e.engineConfig.DataWorkerBaseListenMultiaddr,
403+
int(e.engineConfig.DataWorkerBaseListenPort)+i,
404+
),
405+
)
406+
if err != nil {
407+
panic(err)
408+
}
409+
410+
_, addr, err := mn.DialArgs(ma)
411+
if err != nil {
412+
panic(err)
413+
}
414+
415+
conn, err := grpc.Dial(
416+
addr,
417+
grpc.WithTransportCredentials(
418+
insecure.NewCredentials(),
419+
),
420+
grpc.WithDefaultCallOptions(
421+
grpc.MaxCallSendMsgSize(10*1024*1024),
422+
grpc.MaxCallRecvMsgSize(10*1024*1024),
423+
),
424+
)
425+
if err != nil {
426+
panic(err)
427+
}
428+
429+
clients[i] = protobufs.NewDataIPCServiceClient(conn)
430+
}
431+
432+
e.logger.Info(
433+
"connected to data worker processes",
434+
zap.Int("parallelism", paralellism),
435+
)
436+
return clients, nil
437+
}
438+
339439
func (e *MasterClockConsensusEngine) PerformValidation(
340440
ctx context.Context,
341441
msg *protobufs.ValidationMessage,

node/crypto/frame_prover.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ type FrameProver interface {
5353
) bool
5454
CalculateChallengeProof(
5555
challenge []byte,
56-
parallelism uint32,
56+
core uint32,
5757
skew int64,
58-
) (int64, [][]byte, int64, error)
58+
nowMs int64,
59+
) ([]byte, int64, error)
5960
VerifyChallengeProof(
6061
challenge []byte,
6162
timestamp int64,

node/crypto/wesolowski_frame_prover.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"crypto/rand"
77
"encoding/binary"
88
"math/big"
9-
"sync"
109
"time"
1110

1211
"github.com/cloudflare/circl/sign/ed448"
@@ -601,43 +600,31 @@ func (w *WesolowskiFrameProver) VerifyWeakRecursiveProof(
601600

602601
func (w *WesolowskiFrameProver) CalculateChallengeProof(
603602
challenge []byte,
604-
parallelism uint32,
603+
core uint32,
605604
skew int64,
606-
) (int64, [][]byte, int64, error) {
607-
now := time.Now()
608-
nowMs := now.UnixMilli()
605+
nowMs int64,
606+
) ([]byte, int64, error) {
609607
input := binary.BigEndian.AppendUint64([]byte{}, uint64(nowMs))
610608
input = append(input, challenge...)
611-
outputs := make([][]byte, parallelism)
612-
613-
wg := sync.WaitGroup{}
614-
wg.Add(int(parallelism))
615609

616610
// 4.5 minutes = 270 seconds, one increment should be ten seconds
617611
proofDuration := 270 * 1000
618612
calibratedDifficulty := (int64(proofDuration) * 10000) / skew
619-
for i := uint32(0); i < parallelism; i++ {
620-
i := i
621-
go func() {
622-
instanceInput := binary.BigEndian.AppendUint32([]byte{}, i)
623-
instanceInput = append(instanceInput, input...)
624-
b := sha3.Sum256(instanceInput)
625-
v := vdf.New(uint32(calibratedDifficulty), b)
626-
627-
v.Execute()
628-
o := v.GetOutput()
613+
instanceInput := binary.BigEndian.AppendUint32([]byte{}, core)
614+
instanceInput = append(instanceInput, input...)
615+
b := sha3.Sum256(instanceInput)
616+
v := vdf.New(uint32(calibratedDifficulty), b)
629617

630-
outputs[i] = make([]byte, 516)
631-
copy(outputs[i][:], o[:])
632-
wg.Done()
633-
}()
634-
}
618+
v.Execute()
619+
o := v.GetOutput()
635620

636-
wg.Wait()
621+
output := make([]byte, 516)
622+
copy(output[:], o[:])
623+
now := time.UnixMilli(nowMs)
637624
after := time.Since(now)
638625
nextSkew := (skew * after.Milliseconds()) / int64(proofDuration)
639626

640-
return nowMs, outputs, nextSkew, nil
627+
return output, nextSkew, nil
641628
}
642629

643630
func (w *WesolowskiFrameProver) VerifyChallengeProof(

node/crypto/wesolowski_frame_prover_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ func TestMasterProve(t *testing.T) {
3030
func TestChallengeProof(t *testing.T) {
3131
l, _ := zap.NewProduction()
3232
w := crypto.NewWesolowskiFrameProver(l)
33-
now, proofs, nextSkew, err := w.CalculateChallengeProof([]byte{0x01, 0x02, 0x03}, 3, 120000)
33+
now := time.Now().UnixMilli()
34+
proofs, nextSkew, err := w.CalculateChallengeProof([]byte{0x01, 0x02, 0x03}, 0, 120000, now)
3435
assert.NoError(t, err)
35-
assert.True(t, w.VerifyChallengeProof([]byte{0x01, 0x02, 0x03}, now, 100000, proofs))
36-
now, proofs, _, err = w.CalculateChallengeProof([]byte{0x01, 0x02, 0x03}, 3, nextSkew*12/10)
36+
assert.True(t, w.VerifyChallengeProof([]byte{0x01, 0x02, 0x03}, now, 100000, [][]byte{proofs}))
37+
now = time.Now().UnixMilli()
38+
proofs, _, err = w.CalculateChallengeProof([]byte{0x01, 0x02, 0x03}, 0, nextSkew*12/10, now)
3739
assert.NoError(t, err)
38-
assert.True(t, w.VerifyChallengeProof([]byte{0x01, 0x02, 0x03}, now, nextSkew, proofs))
40+
assert.True(t, w.VerifyChallengeProof([]byte{0x01, 0x02, 0x03}, now, nextSkew, [][]byte{proofs}))
3941
}

0 commit comments

Comments
 (0)