2023-09-03 23:47:09 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-03-08 05:05:04 +00:00
|
|
|
"bytes"
|
2023-09-03 23:47:09 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-10-09 04:52:19 +00:00
|
|
|
"go.uber.org/zap"
|
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"
|
2023-09-03 23:47:09 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution"
|
2024-02-13 07:04:56 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/ceremony"
|
2024-03-08 05:05:04 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/ceremony/application"
|
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"
|
2024-03-08 05:05:04 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/tries"
|
2023-09-03 23:47:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
2023-10-09 04:52:19 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
clockStore store.ClockStore
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager keys.KeyManager
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub p2p.PubSub
|
2023-09-03 23:47:09 +00:00
|
|
|
execEngines map[string]execution.ExecutionEngine
|
|
|
|
engine consensus.ConsensusEngine
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
clockStore store.ClockStore,
|
2023-10-28 02:23:55 +00:00
|
|
|
keyManager keys.KeyManager,
|
2023-10-09 04:52:19 +00:00
|
|
|
pubSub p2p.PubSub,
|
2023-09-25 02:43:35 +00:00
|
|
|
ceremonyExecutionEngine *ceremony.CeremonyExecutionEngine,
|
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)
|
2023-09-25 02:43:35 +00:00
|
|
|
if ceremonyExecutionEngine != nil {
|
|
|
|
execEngines[ceremonyExecutionEngine.GetName()] = ceremonyExecutionEngine
|
2023-09-03 23:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Node{
|
2023-10-09 04:52:19 +00:00
|
|
|
logger,
|
|
|
|
clockStore,
|
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-03-08 05:05:04 +00:00
|
|
|
func (n *Node) RunRepair() {
|
|
|
|
intrinsicFilter := append(
|
|
|
|
p2p.GetBloomFilter(application.CEREMONY_ADDRESS, 256, 3),
|
|
|
|
p2p.GetBloomFilterIndices(application.CEREMONY_ADDRESS, 65536, 24)...,
|
|
|
|
)
|
|
|
|
n.logger.Info("check store and repair if needed, this may take a few minutes")
|
|
|
|
proverTrie := &tries.RollingFrecencyCritbitTrie{}
|
|
|
|
head, err := n.clockStore.GetLatestDataClockFrame(intrinsicFilter, proverTrie)
|
|
|
|
if err == nil && head != nil {
|
|
|
|
for head != nil && head.FrameNumber != 0 {
|
|
|
|
prev := head
|
2024-03-12 07:45:20 +00:00
|
|
|
head, err = n.clockStore.GetStagedDataClockFrame(
|
2024-03-08 05:05:04 +00:00
|
|
|
intrinsicFilter,
|
|
|
|
head.FrameNumber-1,
|
|
|
|
head.ParentSelector,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
compare, _, err := n.clockStore.GetDataClockFrame(
|
|
|
|
intrinsicFilter,
|
|
|
|
prev.FrameNumber-1,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(head.Output, compare.Output) {
|
|
|
|
n.logger.Warn(
|
|
|
|
"repairing frame",
|
|
|
|
zap.Uint64("frame_number", head.FrameNumber),
|
|
|
|
)
|
2024-03-12 07:45:20 +00:00
|
|
|
head, err = n.clockStore.GetStagedDataClockFrame(
|
2024-03-08 05:05:04 +00:00
|
|
|
intrinsicFilter,
|
|
|
|
prev.FrameNumber-1,
|
|
|
|
prev.ParentSelector,
|
2024-03-12 07:45:20 +00:00
|
|
|
true,
|
2024-03-08 05:05:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txn, err := n.clockStore.NewTransaction()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
selector, err := head.GetSelector()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.clockStore.CommitDataClockFrame(
|
|
|
|
intrinsicFilter,
|
|
|
|
head.FrameNumber,
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
|
|
|
proverTrie,
|
|
|
|
txn,
|
|
|
|
true,
|
|
|
|
)
|
2024-03-08 05:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = txn.Commit(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.logger.Info("check complete")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|