Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Jul 8, 2024
2 parents 741aa85 + ef93676 commit 7dd0500
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 32 deletions.
65 changes: 65 additions & 0 deletions cmd/thor/bandwidth/bandwidth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 bandwidth

import (
"crypto/rand"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/thor"
)

func TestBandwidth(t *testing.T) {
bandwidth := Bandwidth{
value: 0,
lock: sync.Mutex{},
}

val := bandwidth.Value()

assert.Equal(t, uint64(0), val)
}

func GetMockHeader(t *testing.T) *block.Header {
var sig [65]byte
rand.Read(sig[:])

block := new(block.Builder).Build().WithSignature(sig[:])
h := block.Header()
return h
}

func TestBandwithUpdate(t *testing.T) {
bandwidth := Bandwidth{
value: 0,
lock: sync.Mutex{},
}

block := new(block.Builder).ParentID(thor.Bytes32{1}).Timestamp(1).GasLimit(100000).Beneficiary(thor.Address{1}).GasUsed(11234).TotalScore(1).StateRoot(thor.Bytes32{1}).ReceiptsRoot(thor.Bytes32{1}).Build()
header := block.Header()

bandwidth.Update(header, 1)
val := bandwidth.Value()

assert.Equal(t, uint64(11234000000000), val)
}

func TestBandwidthSuggestGasLimit(t *testing.T) {
bandwidth := Bandwidth{
value: 0,
lock: sync.Mutex{},
}

block := new(block.Builder).ParentID(thor.Bytes32{1}).Timestamp(1).GasLimit(100000).Beneficiary(thor.Address{1}).GasUsed(11234).TotalScore(1).StateRoot(thor.Bytes32{1}).ReceiptsRoot(thor.Bytes32{1}).Build()
header := block.Header()
bandwidth.Update(header, 1)
val := bandwidth.SuggestGasLimit()

assert.Equal(t, uint64(5617000000000), val)
}
28 changes: 14 additions & 14 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ var (
Value: "",
Usage: "comma separated list of domains from which to accept cross origin requests to API",
}
apiTimeoutFlag = cli.IntFlag{
apiTimeoutFlag = cli.Uint64Flag{
Name: "api-timeout",
Value: 10000,
Usage: "API request timeout value in milliseconds",
}
apiCallGasLimitFlag = cli.IntFlag{
apiCallGasLimitFlag = cli.Uint64Flag{
Name: "api-call-gas-limit",
Value: 50000000,
Usage: "limit contract call gas",
}
apiBacktraceLimitFlag = cli.IntFlag{
apiBacktraceLimitFlag = cli.Uint64Flag{
Name: "api-backtrace-limit",
Value: 1000,
Usage: "limit the distance between 'position' and best block for subscriptions APIs",
Expand All @@ -64,7 +64,7 @@ var (
Name: "api-allow-custom-tracer",
Usage: "allow custom JS tracer to be used tracer API",
}
apiLogsLimitFlag = cli.IntFlag{
apiLogsLimitFlag = cli.Uint64Flag{
Name: "api-logs-limit",
Value: 1000,
Usage: "limit the number of logs returned by /logs API",
Expand All @@ -73,17 +73,17 @@ var (
Name: "enable-api-logs",
Usage: "enables API requests logging",
}
verbosityFlag = cli.IntFlag{
verbosityFlag = cli.Uint64Flag{
Name: "verbosity",
Value: int(log15.LvlInfo),
Value: uint64(log15.LvlInfo),
Usage: "log verbosity (0-9)",
}
maxPeersFlag = cli.IntFlag{
maxPeersFlag = cli.Uint64Flag{
Name: "max-peers",
Usage: "maximum number of P2P network peers (P2P network disabled if set to 0)",
Value: 25,
}
p2pPortFlag = cli.IntFlag{
p2pPortFlag = cli.Uint64Flag{
Name: "p2p-port",
Value: 11235,
Usage: "P2P network listening port",
Expand All @@ -110,7 +110,7 @@ var (
Name: "export",
Usage: "export master key to keystore",
}
targetGasLimitFlag = cli.IntFlag{
targetGasLimitFlag = cli.Uint64Flag{
Name: "target-gas-limit",
Value: 0,
Usage: "target block gas limit (adaptive if set to 0)",
Expand All @@ -128,7 +128,7 @@ var (
Usage: "verify log db at startup",
Hidden: true,
}
cacheFlag = cli.IntFlag{
cacheFlag = cli.Uint64Flag{
Name: "cache",
Usage: "megabytes of ram allocated to trie nodes cache",
Value: 4096,
Expand All @@ -152,7 +152,7 @@ var (
Name: "on-demand",
Usage: "create new block when there is pending transaction",
}
blockInterval = cli.IntFlag{
blockInterval = cli.Uint64Flag{
Name: "block-interval",
Value: 10,
Usage: "choose a custom block interval for solo mode (seconds)",
Expand All @@ -161,17 +161,17 @@ var (
Name: "persist",
Usage: "blockchain data storage option, if set data will be saved to disk",
}
gasLimitFlag = cli.IntFlag{
gasLimitFlag = cli.Uint64Flag{
Name: "gas-limit",
Value: 40_000_000,
Usage: "block gas limit(adaptive if set to 0)",
}
txPoolLimitFlag = cli.IntFlag{
txPoolLimitFlag = cli.Uint64Flag{
Name: "txpool-limit",
Value: 10000,
Usage: "set tx limit in pool",
}
txPoolLimitPerAccountFlag = cli.IntFlag{
txPoolLimitPerAccountFlag = cli.Uint64Flag{
Name: "txpool-limit-per-account",
Value: 16,
Usage: "set tx limit per account in pool",
Expand Down
43 changes: 28 additions & 15 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ func defaultAction(ctx *cli.Context) error {

defer func() { log.Info("exited") }()

initLogger(ctx)
lvl, err := readIntFromUInt64Flag(ctx.Uint64(verbosityFlag.Name))
if err != nil {
return errors.Wrap(err, "parse verbosity flag")
}
initLogger(log15.Lvl(lvl))

// enable metrics as soon as possible
metricsURL := ""
Expand Down Expand Up @@ -230,14 +234,14 @@ func defaultAction(ctx *cli.Context) error {
p2pCommunicator.Communicator(),
forkConfig,
ctx.String(apiCorsFlag.Name),
uint32(ctx.Int(apiBacktraceLimitFlag.Name)),
uint64(ctx.Int(apiCallGasLimitFlag.Name)),
uint32(ctx.Uint64(apiBacktraceLimitFlag.Name)),
ctx.Uint64(apiCallGasLimitFlag.Name),
ctx.Bool(pprofFlag.Name),
skipLogs,
ctx.Bool(apiAllowCustomTracerFlag.Name),
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
uint64(ctx.Int(apiLogsLimitFlag.Name)),
ctx.Uint64(apiLogsLimitFlag.Name),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand Down Expand Up @@ -266,7 +270,7 @@ func defaultAction(ctx *cli.Context) error {
txPool,
filepath.Join(instanceDir, "tx.stash"),
p2pCommunicator.Communicator(),
uint64(ctx.Int(targetGasLimitFlag.Name)),
ctx.Uint64(targetGasLimitFlag.Name),
skipLogs,
forkConfig).Run(exitSignal)
}
Expand All @@ -275,7 +279,11 @@ func soloAction(ctx *cli.Context) error {
exitSignal := handleExitSignal()
defer func() { log.Info("exited") }()

initLogger(ctx)
lvl, err := readIntFromUInt64Flag(ctx.Uint64(verbosityFlag.Name))
if err != nil {
return errors.Wrap(err, "parse verbosity flag")
}
initLogger(log15.Lvl(lvl))

// enable metrics as soon as possible
metricsURL := ""
Expand Down Expand Up @@ -309,7 +317,6 @@ func soloAction(ctx *cli.Context) error {
var mainDB *muxdb.MuxDB
var logDB *logdb.LogDB
var instanceDir string
var err error

if ctx.Bool(persistFlag.Name) {
if instanceDir, err = makeInstanceDir(ctx, gene); err != nil {
Expand Down Expand Up @@ -344,8 +351,14 @@ func soloAction(ctx *cli.Context) error {
}

txPoolOption := defaultTxPoolOptions
txPoolOption.Limit = ctx.Int(txPoolLimitFlag.Name)
txPoolOption.LimitPerAccount = ctx.Int(txPoolLimitPerAccountFlag.Name)
txPoolOption.Limit, err = readIntFromUInt64Flag(ctx.Uint64(txPoolLimitFlag.Name))
if err != nil {
return errors.Wrap(err, "parse txpool-limit flag")
}
txPoolOption.LimitPerAccount, err = readIntFromUInt64Flag(ctx.Uint64(txPoolLimitPerAccountFlag.Name))
if err != nil {
return errors.Wrap(err, "parse txpool-limit-per-account flag")
}

txPool := txpool.New(repo, state.NewStater(mainDB), txPoolOption)
defer func() { log.Info("closing tx pool..."); txPool.Close() }()
Expand All @@ -360,14 +373,14 @@ func soloAction(ctx *cli.Context) error {
&solo.Communicator{},
forkConfig,
ctx.String(apiCorsFlag.Name),
uint32(ctx.Int(apiBacktraceLimitFlag.Name)),
uint64(ctx.Int(apiCallGasLimitFlag.Name)),
uint32(ctx.Uint64(apiBacktraceLimitFlag.Name)),
ctx.Uint64(apiCallGasLimitFlag.Name),
ctx.Bool(pprofFlag.Name),
skipLogs,
ctx.Bool(apiAllowCustomTracerFlag.Name),
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
uint64(ctx.Int(apiLogsLimitFlag.Name)),
ctx.Uint64(apiLogsLimitFlag.Name),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand All @@ -380,7 +393,7 @@ func soloAction(ctx *cli.Context) error {
srvCloser()
}()

blockInterval := ctx.Int(blockInterval.Name)
blockInterval := ctx.Uint64(blockInterval.Name)
if blockInterval == 0 {
return errors.New("block-interval cannot be zero")
}
Expand All @@ -394,10 +407,10 @@ func soloAction(ctx *cli.Context) error {
state.NewStater(mainDB),
logDB,
txPool,
uint64(ctx.Int(gasLimitFlag.Name)),
ctx.Uint64(gasLimitFlag.Name),
ctx.Bool(onDemandFlag.Name),
skipLogs,
uint64(blockInterval),
blockInterval,
forkConfig).Run(exitSignal)
}

Expand Down
15 changes: 12 additions & 3 deletions cmd/thor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ import (

var devNetGenesisID = genesis.NewDevnet().ID()

func initLogger(ctx *cli.Context) {
logLevel := ctx.Int(verbosityFlag.Name)
log15.Root().SetHandler(log15.LvlFilterHandler(log15.Lvl(logLevel), log15.StderrHandler))
func initLogger(lvl log15.Lvl) {
log15.Root().SetHandler(log15.LvlFilterHandler(lvl, log15.StderrHandler))
// set go-ethereum log lvl to Warn
ethLogHandler := ethlog.NewGlogHandler(ethlog.StreamHandler(os.Stderr, ethlog.TerminalFormat(true)))
ethLogHandler.Verbosity(ethlog.LvlWarn)
Expand Down Expand Up @@ -712,3 +711,13 @@ func parseNodeList(list string) ([]*discover.Node, error) {

return nodes, nil
}

func readIntFromUInt64Flag(val uint64) (int, error) {
i := int(val)

if i < 0 {
return 0, fmt.Errorf("invalid value %d ", val)
}

return i, nil
}
6 changes: 6 additions & 0 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ git clone https://github.com/vechain/thor.git
cd thor
```

It is recommended to use the latest stable release. To checkout the latest stable release, run:

```shell
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
```

To see a list of all available commands, run `make help`

### Building
Expand Down

0 comments on commit 7dd0500

Please sign in to comment.