-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcommon.go
More file actions
55 lines (49 loc) · 2.29 KB
/
common.go
File metadata and controls
55 lines (49 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package helpers
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/pokt-network/pocket/logger"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
"github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
)
// TECHDEBT: Accept reading this from `Datadir` and/or as a flag.
var genesisPath = runtime.GetEnv("GENESIS_PATH", "build/config/genesis.json")
// FetchPeerstore retrieves the providers from the CLI context and uses them to retrieve the address book for the current height
func FetchPeerstore(cmd *cobra.Command) (types.Peerstore, error) {
bus, err := GetBusFromCmd(cmd)
if err != nil {
return nil, err
}
// TECHDEBT(#811): Remove type casting once Peerstore is available as a submodule
pstoreProvider, err := peerstore_provider.GetPeerstoreProvider(bus)
if err != nil {
return nil, err
}
currentHeightProvider := bus.GetCurrentHeightProvider()
height := currentHeightProvider.CurrentHeight()
pstore, err := pstoreProvider.GetStakedPeerstoreAtHeight(height)
if err != nil {
return nil, fmt.Errorf("retrieving peerstore at height %d", height)
}
// Inform the client's main P2P that a the blockchain is at a new height so it can, if needed, update its view of the validator set
if err := sendConsensusNewHeightEventToP2PModule(height, bus); err != nil {
return nil, errors.New("sending consensus new height event")
}
return pstore, nil
}
// sendConsensusNewHeightEventToP2PModule mimicks the consensus module sending a ConsensusNewHeightEvent to the p2p module
// This is necessary because the debug client is not a validator and has no consensus module but it has to update the peerstore
// depending on the changes in the validator set.
// TODO(#613): Make the debug client mimic a full node.
// TECHDEBT: This may no longer be required (https://github.com/pokt-network/pocket/pull/891/files#r1262710098)
func sendConsensusNewHeightEventToP2PModule(height uint64, bus modules.Bus) error {
newHeightEvent, err := messaging.PackMessage(&messaging.ConsensusNewHeightEvent{Height: height})
if err != nil {
logger.Global.Fatal().Err(err).Msg("Failed to pack consensus new height event")
}
return bus.GetP2PModule().HandleEvent(newHeightEvent.Content)
}