Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pedro/add_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed May 22, 2024
2 parents 9c90d3d + e39091e commit 449388b
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 158 deletions.
52 changes: 30 additions & 22 deletions api/doc/thor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ info:
RESTful API to access VechainThor Network
[Project Home](https://github.com/vechain/thor)
⚠️ <b>Note:</b> The examples given in this specification are optimized for mainnet.
license:
name: LGPL 3.0
Expand Down Expand Up @@ -220,6 +222,8 @@ paths:
description: |
This endpoint allows you to send a transaction to the blockchain. The transaction must be signed and RLP encoded.
⚠️ <b>Note:</b> The example values provided for this endpoint are optimized for mainnet.
The below is a TypeScript example of how to sign and RLP encode a transaction using the `thor-devkit` library:
```typescript
Expand Down Expand Up @@ -650,6 +654,8 @@ paths:
This endpoint allows you to create a tracer for a specific clause. Tracers are instrumental in monitoring and analyzing the execution flow within the EVM.

You can customize the tracer using various options to tailor it to your specific debugging needs.

⚠️ <b>Note:</b> The example values provided for this endpoint are optimized for mainnet.
requestBody:
required: true
content:
Expand Down Expand Up @@ -715,6 +721,8 @@ paths:
description: |
The endpoint retrieves storage entries related to a particular clause execution and contract address. This
could be useful for inspecting or analyzing storage changes.
⚠️ <b>Note:</b> The example values provided for this endpoint are optimized for mainnet.
requestBody:
required: true
content:
Expand Down Expand Up @@ -865,13 +873,13 @@ components:
- $ref: '#/components/schemas/Tx'
- properties:
meta:
$ref: '#/components/schemas/TxMeta'
$ref: '#/components/schemas/TxMeta'
title: GetTxResponse
- allOf:
- $ref: '#/components/schemas/RawTx'
- properties:
meta:
$ref: '#/components/schemas/TxMeta'
$ref: '#/components/schemas/TxMeta'
title: GetRawTxResponse
example:
id: '0x4de71f2d588aa8a1ea00fe8312d92966da424d9939a511fc0be81e65fad52af8'
Expand Down Expand Up @@ -950,25 +958,25 @@ components:
example: '0x284bba50ef777889ff1a367ed0b38d5e5626714477c40de38d71cedd6f9fa477'

ExpandedBlockResponse:
title: ExpandedBlockResponse
type: object
description: |
The response will contain information about the block identified by the provided `revision`. The `transactions`
field contains an array of transactions, expanded to include their receipts.
allOf:
- $ref: '#/components/schemas/Block'
- $ref: '#/components/schemas/IsTrunk'
- $ref: '#/components/schemas/IsFinalized'
- properties:
transactions:
description: All included transactions, expanded, to include their receipts
type: array
nullable: false
minItems: 0
items:
allOf:
- $ref: '#/components/schemas/Tx'
- $ref: '#/components/schemas/Receipt'
title: ExpandedBlockResponse
type: object
description: |
The response will contain information about the block identified by the provided `revision`. The `transactions`
field contains an array of transactions, expanded to include their receipts.
allOf:
- $ref: '#/components/schemas/Block'
- $ref: '#/components/schemas/IsTrunk'
- $ref: '#/components/schemas/IsFinalized'
- properties:
transactions:
description: All included transactions, expanded, to include their receipts
type: array
nullable: false
minItems: 0
items:
allOf:
- $ref: '#/components/schemas/Tx'
- $ref: '#/components/schemas/Receipt'

EventLogFilterRequest:
type: object
Expand Down Expand Up @@ -1669,7 +1677,7 @@ components:
to:
type: string
description: |
The recipient of the call. Null indicates contract deployment.
The recipient of the call. Null indicates contract deployment.
example: '0x0000000000000000000000000000456e65726779'
nullable: true
pattern: '^0x[0-9a-f]{40}$'
Expand Down
5 changes: 5 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ var (
Name: "on-demand",
Usage: "create new block when there is pending transaction",
}
blockInterval = cli.IntFlag{
Name: "block-interval",
Value: 10,
Usage: "choose a custom block interval for solo mode (seconds)",
}
persistFlag = cli.BoolFlag{
Name: "persist",
Usage: "blockchain data storage option, if set data will be saved to disk",
Expand Down
22 changes: 14 additions & 8 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func main() {
apiAllowCustomTracerFlag,
apiLogsEnabledFlag,
onDemandFlag,
blockInterval,
persistFlag,
gasLimitFlag,
verbosityFlag,
Expand Down Expand Up @@ -207,7 +208,7 @@ func defaultAction(ctx *cli.Context) error {
txPool := txpool.New(repo, state.NewStater(mainDB), txpoolOpt)
defer func() { log.Info("closing tx pool..."); txPool.Close() }()

p2pcom, err := newP2PComm(ctx, repo, txPool, instanceDir)
p2pCommunicator, err := newP2PCommunicator(ctx, repo, txPool, instanceDir)
if err != nil {
return err
}
Expand All @@ -223,7 +224,7 @@ func defaultAction(ctx *cli.Context) error {
txPool,
logDB,
bftEngine,
p2pcom.comm,
p2pCommunicator.Communicator(),
ctx.String(apiCorsFlag.Name),
uint32(ctx.Int(apiBacktraceLimitFlag.Name)),
uint64(ctx.Int(apiCallGasLimitFlag.Name)),
Expand All @@ -240,12 +241,12 @@ func defaultAction(ctx *cli.Context) error {
}
defer func() { log.Info("stopping API server..."); srvCloser() }()

printStartupMessage2(gene, apiURL, p2pcom.enode, metricsURL)
printStartupMessage2(gene, apiURL, p2pCommunicator.Enode(),metricsURL)

Check failure on line 244 in cmd/thor/main.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

File is not `goimports`-ed (goimports)

if err := p2pcom.Start(); err != nil {
if err := p2pCommunicator.Start(); err != nil {
return err
}
defer p2pcom.Stop()
defer p2pCommunicator.Stop()

optimizer := optimizer.New(mainDB, repo, !ctx.Bool(disablePrunerFlag.Name))
defer func() { log.Info("stopping optimizer..."); optimizer.Stop() }()
Expand All @@ -258,7 +259,7 @@ func defaultAction(ctx *cli.Context) error {
logDB,
txPool,
filepath.Join(instanceDir, "tx.stash"),
p2pcom.comm,
p2pCommunicator.Communicator(),
uint64(ctx.Int(targetGasLimitFlag.Name)),
skipLogs,
forkConfig).Run(exitSignal)
Expand Down Expand Up @@ -370,8 +371,12 @@ func soloAction(ctx *cli.Context) error {
srvCloser()
}()

printStartupMessage1(gene, repo, nil, instanceDir, forkConfig)
printStartupMessage2(gene, apiURL, "", metricsURL)
blockInterval := ctx.Int(blockInterval.Name)
if blockInterval == 0 {
return errors.New("block-interval cannot be zero")
}

printSoloStartupMessage(gene, repo, instanceDir, apiURL, forkConfig, metricsURL)

optimizer := optimizer.New(mainDB, repo, !ctx.Bool(disablePrunerFlag.Name))
defer func() { log.Info("stopping optimizer..."); optimizer.Stop() }()
Expand All @@ -383,6 +388,7 @@ func soloAction(ctx *cli.Context) error {
uint64(ctx.Int(gasLimitFlag.Name)),
ctx.Bool(onDemandFlag.Name),
skipLogs,
uint64(blockInterval),
forkConfig).Run(exitSignal)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/thor/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (n *Node) processBlock(newBlock *block.Block, stats *blockStats) (bool, err
stats.UpdateQueued(1)
case err == errBFTRejected:
// TODO: capture metrics
log.Debug(fmt.Sprintf("block rejected by BFT engine: %v", newBlock.Header()))
log.Debug(fmt.Sprintf("block rejected by BFT engine: \n %v\n", newBlock.Header()))
case consensus.IsCritical(err):
msg := fmt.Sprintf(`failed to process block due to consensus failure \n%v\n`, newBlock.Header())
log.Error(msg, "err", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/thor/bootstrap.go → cmd/thor/p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

package main
package p2p

import (
"github.com/ethereum/go-ethereum/p2p/discover"
)

const remoteBootstrapList = "https://vechain.github.io/bootstraps/node.list"
const remoteDiscoveryNodesList = "https://vechain.github.io/bootstraps/node.list"

var fallbackBootstrapNodes = []*discover.Node{
var fallbackDiscoveryNodes = []*discover.Node{
discover.MustParseNode("enode://797fdd968592ca3b59a143f1aa2f152913499d4bb469f2bd5b62dfb1257707b4cb0686563fe144ee2088b1cc4f174bd72df51dbeb7ec1c5b6a8d8599c756f38b@107.150.112.22:55555"),
discover.MustParseNode("enode://3eae6740af6180bb015309f7a07ff7405d6f1f9f1e5a9f2fabbd36b0c00b862521e63ff23573ffdb9035f2237c26513cb9f02454f9ada993e60b99ffc187bb54@107.150.112.21:55555"),
discover.MustParseNode("enode://12e90ad91b7c9abe1788cdd7804b1ea48f2983a99320c62a6aaa9ee71148ec9eb0a30ccb1c66acc46d27adcb8e636f141366d1894e631b93dfdfd416309be929@152.32.151.143:55555"),
Expand Down
137 changes: 137 additions & 0 deletions cmd/thor/p2p/p2p.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) 2024 The VeChainThor developers

// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

package p2p

import (
"crypto/ecdsa"
"fmt"
"os"
"path/filepath"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors"
"github.com/vechain/thor/v2/comm"
"github.com/vechain/thor/v2/p2psrv"
)

type P2P struct {
comm *comm.Communicator
p2pSrv *p2psrv.Server
peersCachePath string
enode string
}

func New(
communicator *comm.Communicator,
privateKey *ecdsa.PrivateKey,
instanceDir string,
nat nat.Interface,
version string,
maxPeers int,
listenPort int,
listenAddr string,
allowedPeers []*discover.Node,
cachedPeers []*discover.Node,
bootstrapNodes []*discover.Node,
) *P2P {
// known peers will be loaded/stored from/in this file
peersCachePath := filepath.Join(instanceDir, "peers.cache")

// default option setting
// no known nodes for p2p connection
// use the hardcoded fallbackDiscoveryNodes for discovery only
opts := &p2psrv.Options{
Name: common.MakeName("thor", version),
PrivateKey: privateKey,
MaxPeers: maxPeers,
ListenAddr: listenAddr,
DiscoveryNodes: fallbackDiscoveryNodes,
RemoteDiscoveryList: remoteDiscoveryNodesList,
NAT: nat,
}

// allowed peers flag will only allow p2psrv to connect to the designated peers
if len(allowedPeers) > 0 {
opts.NoDiscovery = true // disable discovery
opts.DiscoveryNodes = nil
opts.KnownNodes = allowedPeers
} else {
// boot nodes flag will overwrite the default bootstrap nodes and also disable remote bootstrap
// bootstrap nodes will be connected for discovery and for p2p protocols
if len(bootstrapNodes) > 0 {
opts.RemoteDiscoveryList = "" // disable remote bootstrap
opts.DiscoveryNodes = bootstrapNodes // discovery nodes are unused given that known nodes are used for both node discovery and p2p protocol
opts.KnownNodes = bootstrapNodes // bootstrap nodes will be used for discovery and p2p
}

// cached peers will be appended to existing or flag-set bootnodes
if len(cachedPeers) > 0 {
opts.KnownNodes = dedupNodeSlice(opts.KnownNodes, cachedPeers)
}
}

return &P2P{
comm: communicator,
p2pSrv: p2psrv.New(opts),
peersCachePath: peersCachePath,
enode: fmt.Sprintf("enode://%x@[extip]:%v", discover.PubkeyID(&privateKey.PublicKey).Bytes(), listenPort),
}
}

func (p *P2P) Start() error {
log.Info("starting P2P networking")
if err := p.p2pSrv.Start(p.comm.Protocols(), p.comm.DiscTopic()); err != nil {
return errors.Wrap(err, "start P2P server")
}
p.comm.Start()
return nil
}

func (p *P2P) Stop() {
log.Info("stopping communicator...")
p.comm.Stop()

log.Info("stopping P2P server...")
p.p2pSrv.Stop()

log.Info("saving peers cache...")
nodes := p.p2pSrv.KnownNodes()
data, err := rlp.EncodeToBytes(nodes)
if err != nil {
log.Warn("failed to encode cached peers", "err", err)
return
}
if err := os.WriteFile(p.peersCachePath, data, 0600); err != nil {
log.Warn("failed to write peers cache", "err", err)
}
}

func (p *P2P) Communicator() *comm.Communicator {
return p.comm
}

func (p *P2P) Enode() string {
return p.enode
}

func dedupNodeSlice(slice1, slice2 p2psrv.Nodes) p2psrv.Nodes {
foundMap := map[string]bool{}
var dedupedSlice p2psrv.Nodes

for _, item := range append(slice1, slice2...) {
if _, ok := foundMap[item.ID.String()]; ok {
continue
}
foundMap[item.ID.String()] = true
dedupedSlice = append(dedupedSlice, item)
}

return dedupedSlice
}
Loading

0 comments on commit 449388b

Please sign in to comment.