Skip to content

Commit 2770f41

Browse files
Merge remote-tracking branch 'pokt/main' into refactor/consolidate-p2p-modules
* pokt/main: [Utility][Persistence][Savepoints/Rollbacks] Refactor UtilityContext into UtilityUnitOfWork (Issue #563) (#577) [Consensus] Add Quorum Certificate to the block (#593) docs(wiki): ValueError: Duplicate wiki path: devlog/template (#620) Remove unused import [Infra][P2P] Bug: "address not found in peerstore" error in localnet_debug_client when scaling up Localnet - Issue #604 (#611) [CLI][Localnet][Bug] - [localnet_client_debug] Error 137 - Issue #607 (#608) [Documentation] Fix links to RPC module docs in README (#603)
2 parents b757af8 + 19bf4d3 commit 2770f41

Some content is hidden

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

65 files changed

+2570
-2074
lines changed

.github/workflows/changelog-verify.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
const result = `<!-- validate_changelogs_review -->The changelog validation failed with the following output:
6262
6363
\`\`\`
64-
${{ needs.validate.outputs.all }}
64+
${{ fromJSON(needs.validate.outputs.all) }}
6565
\`\`\`
6666
6767
Please update the relevant CHANGELOG.md files and ensure they follow the correct format.`;

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ develop_start: ## Run all of the make commands necessary to develop on the proje
116116
make go_clean_deps && \
117117
make mockgen && \
118118
make generate_rpc_openapi && \
119+
make refresh_debug_keybase && \
119120
make build
120121

121122
.PHONY: develop_test
@@ -218,6 +219,10 @@ docker_loki_install: docker_check ## Installs the loki docker driver
218219
docker_loki_check:
219220
if [ `docker plugin ls | grep loki: | wc -l` -eq 0 ]; then make docker_loki_install; fi
220221

222+
.PHONY: refresh_debug_keybase
223+
refresh_debug_keybase: ## Refreshes the debug keybase with the latest keys from the private_keys.yaml file if necessary
224+
go run build/debug_keybase/main.go ./build/localnet/manifests/private-keys.yaml ./build/debug_keybase
225+
221226
.PHONY: clean_mocks
222227
clean_mocks: ## Use `clean_mocks` to delete mocks before recreating them. Also useful to cleanup code that was generated from a different branch
223228
$(eval modules_dir = "shared/modules")

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ If you'd like to contribute to the Pocket V1 Protocol, start by:
4444
- [Persistence Architecture](persistence/docs/README.md)
4545
- [P2P Architecture](p2p/README.md)
4646
- [APP Architecture](app/client/doc/README.md)
47-
- [RPC Architecture](app/pocket/rpc/doc/README.md)
47+
- [RPC Architecture](rpc/doc/README.md)
4848
- [Node binary Architecture](app/pocket/doc/README.md)
4949

5050
### Changelogs
@@ -55,7 +55,7 @@ If you'd like to contribute to the Pocket V1 Protocol, start by:
5555
- [Persistence Changelog](persistence/docs/CHANGELOG.md)
5656
- [P2P Changelog](p2p/CHANGELOG.md)
5757
- [APP Changelog](app/client/doc/CHANGELOG.md)
58-
- [RPC Changelog](app/pocket/rpc/doc/CHANGELOG.md)
58+
- [RPC Changelog](rpc/doc/CHANGELOG.md)
5959
- [Node binary Changelog](app/pocket/doc/CHANGELOG.md)
6060

6161
- _Coming Soon: Telemetry Changelog_

app/client/cli/debug.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ func broadcastDebugMessage(cmd *cobra.Command, debugMsg *messaging.DebugMessage)
223223

224224
pstore, err := fetchPeerstore(cmd)
225225
if err != nil {
226-
logger.Global.Fatal().Msg("Unable to retrieve the pstore")
226+
logger.Global.Fatal().Err(err).Msg("Unable to retrieve the pstore")
227227
}
228228
for _, val := range pstore.GetPeerList() {
229229
addr := val.GetAddress()
230230
if err != nil {
231231
logger.Global.Fatal().Err(err).Msg("Failed to convert validator address into pocketCrypto.Address")
232232
}
233233
if err := p2pMod.Send(addr, anyProto); err != nil {
234-
logger.Global.Fatal().Err(err).Msg("Failed to send debug message")
234+
logger.Global.Error().Err(err).Msg("Failed to send debug message")
235235
}
236236
}
237237

@@ -246,7 +246,7 @@ func sendDebugMessage(cmd *cobra.Command, debugMsg *messaging.DebugMessage) {
246246

247247
pstore, err := fetchPeerstore(cmd)
248248
if err != nil {
249-
logger.Global.Fatal().Msg("Unable to retrieve the pstore")
249+
logger.Global.Fatal().Err(err).Msg("Unable to retrieve the pstore")
250250
}
251251

252252
var validatorAddress []byte
@@ -261,7 +261,7 @@ func sendDebugMessage(cmd *cobra.Command, debugMsg *messaging.DebugMessage) {
261261
}
262262

263263
if err := p2pMod.Send(validatorAddress, anyProto); err != nil {
264-
logger.Global.Fatal().Err(err).Msg("Failed to send debug message")
264+
logger.Global.Error().Err(err).Msg("Failed to send debug message")
265265
}
266266
}
267267

@@ -286,5 +286,22 @@ func fetchPeerstore(cmd *cobra.Command) (typesP2P.Peerstore, error) {
286286
if err != nil {
287287
return nil, fmt.Errorf("retrieving peerstore at height %d", height)
288288
}
289-
return pstore, err
289+
// 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
290+
err = sendConsensusNewHeightEventToP2PModule(height, bus)
291+
if err != nil {
292+
return nil, errors.New("sending consensus new height event")
293+
}
294+
return pstore, nil
295+
}
296+
297+
// sendConsensusNewHeightEventToP2PModule mimicks the consensus module sending a ConsensusNewHeightEvent to the p2p module
298+
// This is necessary because the debug client is not a validator and has no consensus module but it has to update the peerstore
299+
// depending on the changes in the validator set.
300+
// TODO(#613): Make the debug client mimic a full node.
301+
func sendConsensusNewHeightEventToP2PModule(height uint64, bus modules.Bus) error {
302+
newHeightEvent, err := messaging.PackMessage(&messaging.ConsensusNewHeightEvent{Height: height})
303+
if err != nil {
304+
logger.Global.Fatal().Err(err).Msg("Failed to pack consensus new height event")
305+
}
306+
return bus.GetP2PModule().HandleEvent(newHeightEvent.Content)
290307
}

app/client/keybase/debug/keystore.go

+27-94
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@
33
package debug
44

55
import (
6-
"fmt"
6+
"io/ioutil"
77
"os"
8+
"path/filepath"
89

9-
"github.com/korovkin/limiter"
10+
"github.com/dgraph-io/badger/v3"
1011
"github.com/pokt-network/pocket/app/client/keybase"
1112
"github.com/pokt-network/pocket/build"
1213
"github.com/pokt-network/pocket/logger"
13-
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
14-
"gopkg.in/yaml.v2"
1514
)
1615

17-
const (
18-
// NOTE: This is the number of validators in the private-keys.yaml manifest file
19-
numValidators = 999
20-
debugKeybaseSuffix = "/.pocket/keys"
21-
22-
// Increasing this number is linearly proportional to the amount of RAM required for the debug client to start and import
23-
// pre-generated keys into the keybase. Beware that might cause OOM and process can exit with 137 status code.
24-
// 4 threads takes 350-400MiB from a few tests which sounds acceptable.
25-
debugKeybaseImportConcurrencyLimit = 4
26-
)
16+
const debugKeybaseSuffix = "/.pocket/keys"
2717

2818
var (
2919
// TODO: Allow users to override this value via `datadir` flag or env var or config file
@@ -45,81 +35,17 @@ func init() {
4535
}
4636

4737
func initializeDebugKeybase() error {
48-
var (
49-
validatorKeysPairMap map[string]string
50-
err error
51-
)
52-
53-
validatorKeysPairMap, err = parseValidatorPrivateKeysFromEmbeddedYaml()
54-
55-
if err != nil {
56-
return err
57-
}
58-
5938
// Create/Open the keybase at `$HOME/.pocket/keys`
6039
kb, err := keybase.NewKeybase(debugKeybasePath)
6140
if err != nil {
6241
return err
6342
}
6443
db := kb.GetBadgerDB()
6544

66-
// Add the keys if the keybase contains less than 999
67-
curAddr, _, err := kb.GetAll()
68-
if err != nil {
45+
if err := restoreBadgerDB(build.DebugKeybaseBackup, db); err != nil {
6946
return err
7047
}
7148

72-
// Add validator addresses if not present
73-
if len(curAddr) < numValidators {
74-
logger.Global.Debug().Msgf(fmt.Sprintf("Debug keybase initializing... Adding %d validator keys to the keybase", numValidators-len(curAddr)))
75-
76-
// Use writebatch to speed up bulk insert
77-
wb := db.NewWriteBatch()
78-
79-
// Create a channel to receive errors from goroutines
80-
errCh := make(chan error, numValidators)
81-
82-
limit := limiter.NewConcurrencyLimiter(debugKeybaseImportConcurrencyLimit)
83-
84-
for _, privHexString := range validatorKeysPairMap {
85-
limit.Execute(func() {
86-
// Import the keys into the keybase with no passphrase or hint as these are for debug purposes
87-
keyPair, err := cryptoPocket.CreateNewKeyFromString(privHexString, "", "")
88-
if err != nil {
89-
errCh <- err
90-
return
91-
}
92-
93-
// Use key address as key in DB
94-
addrKey := keyPair.GetAddressBytes()
95-
96-
// Encode KeyPair into []byte for value
97-
keypairBz, err := keyPair.Marshal()
98-
if err != nil {
99-
errCh <- err
100-
return
101-
}
102-
if err := wb.Set(addrKey, keypairBz); err != nil {
103-
errCh <- err
104-
return
105-
}
106-
})
107-
}
108-
109-
limit.WaitAndClose()
110-
111-
// Check if any goroutines returned an error
112-
select {
113-
case err := <-errCh:
114-
return err
115-
default:
116-
}
117-
118-
if err := wb.Flush(); err != nil {
119-
return err
120-
}
121-
}
122-
12349
// Close DB connection
12450
if err := kb.Stop(); err != nil {
12551
return err
@@ -128,24 +54,31 @@ func initializeDebugKeybase() error {
12854
return nil
12955
}
13056

131-
// parseValidatorPrivateKeysFromEmbeddedYaml fetches the validator private keys from the embedded build/localnet/manifests/private-keys.yaml manifest file.
132-
func parseValidatorPrivateKeysFromEmbeddedYaml() (map[string]string, error) {
57+
func restoreBadgerDB(backupData []byte, db *badger.DB) error {
58+
logger.Global.Debug().Msg("Debug keybase initializing... Restoring from the embedded backup file...")
13359

134-
// Parse the YAML file and load into the config struct
135-
var config struct {
136-
ApiVersion string `yaml:"apiVersion"`
137-
Kind string `yaml:"kind"`
138-
MetaData map[string]string `yaml:"metadata"`
139-
Type string `yaml:"type"`
140-
StringData map[string]string `yaml:"stringData"`
60+
// Create a temporary directory to store the backup data
61+
tempDir, err := ioutil.TempDir("", "badgerdb-restore")
62+
if err != nil {
63+
return err
14164
}
142-
if err := yaml.Unmarshal(build.PrivateKeysFile, &config); err != nil {
143-
return nil, err
65+
defer os.RemoveAll(tempDir)
66+
67+
// Write the backup data to a file in the temporary directory
68+
backupFilePath := filepath.Join(tempDir, "backup")
69+
if err := ioutil.WriteFile(backupFilePath, backupData, 0644); err != nil {
70+
return err
14471
}
145-
validatorKeysMap := make(map[string]string)
14672

147-
for id, privHexString := range config.StringData {
148-
validatorKeysMap[id] = privHexString
73+
backupFile, err := os.Open(backupFilePath)
74+
if err != nil {
75+
return err
14976
}
150-
return validatorKeysMap, nil
77+
defer backupFile.Close()
78+
79+
if err := db.Load(backupFile, 4); err != nil {
80+
return err
81+
}
82+
83+
return nil
15184
}

app/docs/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.5] - 2023-03-24
11+
12+
- The debug CLI now updates its peerstore mimicking the behavior of the validators via `sendConsensusNewHeightEventToP2PModule`.
13+
14+
## [0.0.0.4] - 2023-03-24
15+
16+
- Updated debug keystore initialization to use an embedded backup instead of the yaml file that has to be rehydrated every time.
17+
1018
## [0.0.0.3] - 2023-03-20
1119

1220
- Adds message routing type field labels to debug CLI actions

build/debug.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ package build
44

55
import _ "embed"
66

7-
// PrivateKeysFile is the pre-generated manifest file for LocalNet debugging
7+
// DebugKeybaseBackup is a backup of the pre-loaded debug keybase sourced from the manifest file for LocalNet debugging
88
//
9-
//go:embed localnet/manifests/private-keys.yaml
10-
var PrivateKeysFile []byte
9+
//go:embed debug_keybase/debug_keybase.bak
10+
var DebugKeybaseBackup []byte
1111

1212
func init() {
13-
if len(PrivateKeysFile) == 0 {
14-
panic("PrivateKeysFile is empty")
13+
if len(DebugKeybaseBackup) == 0 {
14+
panic("DebugKeybaseBackup is empty")
1515
}
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is used to check if the keybase dump is in sync with the YAML file. Its name is the MD5 hash of the private_keys.yaml

build/debug_keybase/debug_keybase.bak

481 KB
Binary file not shown.

0 commit comments

Comments
 (0)