ceremonyclient/node/app/node.go

209 lines
4.4 KiB
Go
Raw Normal View History

2023-09-03 23:47:09 +00:00
package app
import (
"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
"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"
2024-10-17 04:51:27 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/token"
2023-10-28 02:23:55 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/keys"
"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
}
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(
logger *zap.Logger,
2024-06-08 11:32:45 +00:00
dataProofStore store.DataProofStore,
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,
pubSub p2p.PubSub,
2024-10-17 04:51:27 +00:00
tokenExecutionEngine *token.TokenExecutionEngine,
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)
2024-10-17 04:51:27 +00:00
if tokenExecutionEngine != nil {
execEngines[tokenExecutionEngine.GetName()] = tokenExecutionEngine
}
2023-09-03 23:47:09 +00:00
return &Node{
logger,
2024-06-08 11:32:45 +00:00
dataProofStore,
clockStore,
2024-10-14 21:41:40 +00:00
coinStore,
2023-10-28 02:23:55 +00:00
keyManager,
pubSub,
2023-09-03 23:47:09 +00:00
execEngines,
engine,
}, nil
}
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
}
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)
_, 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)
}
idx, idxProof, idxCommit, idxKP := GetOutputs(o)
2024-06-08 11:32:45 +00:00
ip := sha3.Sum512(idxProof)
v, err := dataProver.VerifyRaw(
ip[:],
idxCommit,
int(idx),
idxKP,
2024-10-14 01:37:19 +00:00
nearestApplicablePowerOfTwo(uint64(parallelism)),
)
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)
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
}
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)
}
}
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
}
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)
}
func (n *Node) GetExecutionEngines() []execution.ExecutionEngine {
list := []execution.ExecutionEngine{}
for _, e := range n.execEngines {
list = append(list, e)
}
return list
}