2023-09-03 23:47:09 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-10-12 18:55:17 +00:00
|
|
|
"encoding/binary"
|
2023-09-03 23:47:09 +00:00
|
|
|
"errors"
|
2024-06-08 11:32:45 +00:00
|
|
|
"fmt"
|
2023-09-03 23:47:09 +00:00
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
"go.uber.org/zap"
|
2024-06-08 11:32:45 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2023-09-03 23:47:09 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/consensus"
|
2024-03-04 03:20:24 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/consensus/master"
|
2024-06-08 11:32:45 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/crypto"
|
2023-09-03 23:47:09 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution"
|
2023-10-28 02:23:55 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/keys"
|
2023-10-09 04:52:19 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/p2p"
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/store"
|
2023-09-03 23:47:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
2024-06-08 11:32:45 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
dataProofStore store.DataProofStore
|
|
|
|
clockStore store.ClockStore
|
2024-10-14 21:41:40 +00:00
|
|
|
coinStore store.CoinStore
|
2024-06-08 11:32:45 +00:00
|
|
|
keyManager keys.KeyManager
|
|
|
|
pubSub p2p.PubSub
|
|
|
|
execEngines map[string]execution.ExecutionEngine
|
|
|
|
engine consensus.ConsensusEngine
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 01:38:20 +00:00
|
|
|
type DHTNode struct {
|
|
|
|
pubSub p2p.PubSub
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDHTNode(
|
|
|
|
pubSub p2p.PubSub,
|
|
|
|
) (*DHTNode, error) {
|
|
|
|
return &DHTNode{
|
|
|
|
pubSub: pubSub,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-03 23:47:09 +00:00
|
|
|
func newNode(
|
2023-10-09 04:52:19 +00:00
|
|
|
logger *zap.Logger,
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProofStore store.DataProofStore,
|
2023-10-09 04:52:19 +00:00
|
|
|
clockStore store.ClockStore,
|
2024-10-14 21:41:40 +00:00
|
|
|
coinStore store.CoinStore,
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager keys.KeyManager,
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub p2p.PubSub,
|
2024-05-25 05:22:50 +00:00
|
|
|
// execution engines wire in here
|
2023-09-03 23:47:09 +00:00
|
|
|
engine consensus.ConsensusEngine,
|
|
|
|
) (*Node, error) {
|
|
|
|
if engine == nil {
|
|
|
|
return nil, errors.New("engine must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
execEngines := make(map[string]execution.ExecutionEngine)
|
|
|
|
|
|
|
|
return &Node{
|
2023-10-09 04:52:19 +00:00
|
|
|
logger,
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProofStore,
|
2023-10-09 04:52:19 +00:00
|
|
|
clockStore,
|
2024-10-14 21:41:40 +00:00
|
|
|
coinStore,
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager,
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub,
|
2023-09-03 23:47:09 +00:00
|
|
|
execEngines,
|
|
|
|
engine,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
func GetOutputs(output []byte) (
|
|
|
|
index uint32,
|
|
|
|
indexProof []byte,
|
|
|
|
kzgCommitment []byte,
|
|
|
|
kzgProof []byte,
|
|
|
|
) {
|
|
|
|
index = binary.BigEndian.Uint32(output[:4])
|
|
|
|
indexProof = output[4:520]
|
|
|
|
kzgCommitment = output[520:594]
|
|
|
|
kzgProof = output[594:668]
|
|
|
|
return index, indexProof, kzgCommitment, kzgProof
|
|
|
|
}
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
func nearestApplicablePowerOfTwo(number uint64) uint64 {
|
|
|
|
power := uint64(128)
|
|
|
|
if number > 2048 {
|
|
|
|
power = 65536
|
|
|
|
} else if number > 1024 {
|
|
|
|
power = 2048
|
|
|
|
} else if number > 128 {
|
|
|
|
power = 1024
|
2024-10-12 18:55:17 +00:00
|
|
|
}
|
|
|
|
return power
|
|
|
|
}
|
|
|
|
|
2024-06-08 11:32:45 +00:00
|
|
|
func (n *Node) VerifyProofIntegrity() {
|
|
|
|
i, _, _, e := n.dataProofStore.GetLatestDataTimeProof(n.pubSub.GetPeerID())
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
2024-07-29 17:46:36 +00:00
|
|
|
|
2024-06-08 11:32:45 +00:00
|
|
|
dataProver := crypto.NewKZGInclusionProver(n.logger)
|
|
|
|
wesoProver := crypto.NewWesolowskiFrameProver(n.logger)
|
|
|
|
|
|
|
|
for j := int(i); j >= 0; j-- {
|
|
|
|
fmt.Println(j)
|
2024-10-12 18:55:17 +00:00
|
|
|
_, parallelism, input, o, err := n.dataProofStore.GetDataTimeProof(n.pubSub.GetPeerID(), uint32(j))
|
2024-06-08 11:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-10-12 18:55:17 +00:00
|
|
|
idx, idxProof, idxCommit, idxKP := GetOutputs(o)
|
2024-06-08 11:32:45 +00:00
|
|
|
|
|
|
|
ip := sha3.Sum512(idxProof)
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
v, err := dataProver.VerifyRaw(
|
|
|
|
ip[:],
|
|
|
|
idxCommit,
|
|
|
|
int(idx),
|
|
|
|
idxKP,
|
2024-10-14 01:37:19 +00:00
|
|
|
nearestApplicablePowerOfTwo(uint64(parallelism)),
|
2024-10-12 18:55:17 +00:00
|
|
|
)
|
2024-06-08 11:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v {
|
2024-07-29 17:46:36 +00:00
|
|
|
panic(fmt.Sprintf("bad kzg proof at increment %d", j))
|
2024-06-08 11:32:45 +00:00
|
|
|
}
|
|
|
|
wp := []byte{}
|
|
|
|
wp = append(wp, n.pubSub.GetPeerID()...)
|
|
|
|
wp = append(wp, input...)
|
|
|
|
fmt.Printf("%x\n", wp)
|
2024-10-12 18:55:17 +00:00
|
|
|
v = wesoProver.VerifyPreDuskChallengeProof(wp, uint32(j), idx, idxProof)
|
2024-06-08 11:32:45 +00:00
|
|
|
if !v {
|
2024-07-29 17:46:36 +00:00
|
|
|
panic(fmt.Sprintf("bad weso proof at increment %d", j))
|
2024-03-08 05:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-08 11:32:45 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 01:38:20 +00:00
|
|
|
func (d *DHTNode) Start() {
|
|
|
|
<-d.quit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DHTNode) Stop() {
|
|
|
|
go func() {
|
|
|
|
d.quit <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-09-03 23:47:09 +00:00
|
|
|
func (n *Node) Start() {
|
|
|
|
err := <-n.engine.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add config mapping to engine name/frame registration
|
|
|
|
for _, e := range n.execEngines {
|
|
|
|
n.engine.RegisterExecutor(e, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Stop() {
|
|
|
|
err := <-n.engine.Stop(false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 04:52:19 +00:00
|
|
|
|
|
|
|
func (n *Node) GetLogger() *zap.Logger {
|
|
|
|
return n.logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) GetClockStore() store.ClockStore {
|
|
|
|
return n.clockStore
|
|
|
|
}
|
|
|
|
|
2024-10-14 21:41:40 +00:00
|
|
|
func (n *Node) GetCoinStore() store.CoinStore {
|
|
|
|
return n.coinStore
|
|
|
|
}
|
|
|
|
|
2024-06-08 11:32:45 +00:00
|
|
|
func (n *Node) GetDataProofStore() store.DataProofStore {
|
|
|
|
return n.dataProofStore
|
|
|
|
}
|
|
|
|
|
2023-10-28 02:23:55 +00:00
|
|
|
func (n *Node) GetKeyManager() keys.KeyManager {
|
|
|
|
return n.keyManager
|
|
|
|
}
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
func (n *Node) GetPubSub() p2p.PubSub {
|
|
|
|
return n.pubSub
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
func (n *Node) GetMasterClock() *master.MasterClockConsensusEngine {
|
|
|
|
return n.engine.(*master.MasterClockConsensusEngine)
|
|
|
|
}
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
func (n *Node) GetExecutionEngines() []execution.ExecutionEngine {
|
|
|
|
list := []execution.ExecutionEngine{}
|
|
|
|
for _, e := range n.execEngines {
|
|
|
|
list = append(list, e)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|