Skip to content

Commit

Permalink
Merge pull request #327 from libotony/solo
Browse files Browse the repository at this point in the history
improve solo
  • Loading branch information
qianbin committed Jan 20, 2020
2 parents a8cf0fe + 3f3557c commit 865620f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
12 changes: 6 additions & 6 deletions cmd/thor/node/bandwidth.go → cmd/thor/bandwidth/bandwidth.go
Expand Up @@ -3,7 +3,7 @@
// 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 node
package bandwidth

import (
"sync"
Expand All @@ -13,19 +13,19 @@ import (
"github.com/vechain/thor/thor"
)

// bandwidth is gas per second.
type bandwidth struct {
// Bandwidth is gas per second.
type Bandwidth struct {
value uint64 // gas per second
lock sync.Mutex
}

func (b *bandwidth) Value() uint64 {
func (b *Bandwidth) Value() uint64 {
b.lock.Lock()
defer b.lock.Unlock()
return b.value
}

func (b *bandwidth) Update(header *block.Header, elapsed time.Duration) (uint64, bool) {
func (b *Bandwidth) Update(header *block.Header, elapsed time.Duration) (uint64, bool) {
b.lock.Lock()
defer b.lock.Unlock()

Expand All @@ -52,7 +52,7 @@ func (b *bandwidth) Update(header *block.Header, elapsed time.Duration) (uint64,
return b.value, true
}

func (b *bandwidth) SuggestGasLimit() uint64 {
func (b *Bandwidth) SuggestGasLimit() uint64 {
b.lock.Lock()
defer b.lock.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion cmd/thor/flags.go
Expand Up @@ -87,7 +87,7 @@ var (
gasLimitFlag = cli.IntFlag{
Name: "gas-limit",
Value: 10000000,
Usage: "block gas limit",
Usage: "block gas limit(adaptive if set to 0)",
}
importMasterKeyFlag = cli.BoolFlag{
Name: "import",
Expand Down
7 changes: 7 additions & 0 deletions cmd/thor/main.go
Expand Up @@ -89,6 +89,7 @@ func main() {
Usage: "client runs in solo mode for test & dev",
Flags: []cli.Flag{
dataDirFlag,
cacheFlag,
apiAddrFlag,
apiCorsFlag,
apiTimeoutFlag,
Expand All @@ -103,6 +104,7 @@ func main() {
skipLogsFlag,
txPoolLimitFlag,
txPoolLimitPerAccountFlag,
disablePrunerFlag,
},
Action: soloAction,
},
Expand Down Expand Up @@ -305,6 +307,11 @@ func soloAction(ctx *cli.Context) error {

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

if !ctx.Bool(disablePrunerFlag.Name) {
pruner := pruner.New(mainDB, repo)
defer func() { log.Info("stopping pruner..."); pruner.Stop() }()
}

return solo.New(repo,
state.NewStater(mainDB),
logDB,
Expand Down
3 changes: 2 additions & 1 deletion cmd/thor/node/node.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/vechain/thor/block"
"github.com/vechain/thor/cache"
"github.com/vechain/thor/chain"
"github.com/vechain/thor/cmd/thor/bandwidth"
"github.com/vechain/thor/co"
"github.com/vechain/thor/comm"
"github.com/vechain/thor/consensus"
Expand Down Expand Up @@ -52,7 +53,7 @@ type Node struct {
targetGasLimit uint64
skipLogs bool
logDBFailed bool
bandwidth bandwidth
bandwidth bandwidth.Bandwidth
}

func New(
Expand Down
13 changes: 12 additions & 1 deletion cmd/thor/solo/solo.go
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/vechain/thor/block"
"github.com/vechain/thor/chain"
"github.com/vechain/thor/cmd/thor/bandwidth"
"github.com/vechain/thor/co"
"github.com/vechain/thor/genesis"
"github.com/vechain/thor/logdb"
Expand All @@ -27,7 +28,7 @@ import (
"github.com/vechain/thor/txpool"
)

var log = log15.New()
var log = log15.New("pkg", "solo")

// Solo mode is the standalone client without p2p server
type Solo struct {
Expand All @@ -37,6 +38,7 @@ type Solo struct {
logDB *logdb.LogDB
bestBlockCh chan *block.Block
gasLimit uint64
bandwidth bandwidth.Bandwidth
onDemand bool
skipLogs bool
}
Expand Down Expand Up @@ -134,6 +136,11 @@ func (s *Solo) packing(pendingTxs tx.Transactions) error {
}
}()

if s.gasLimit == 0 {
suggested := s.bandwidth.SuggestGasLimit()
s.packer.SetTargetGasLimit(suggested)
}

flow, err := s.packer.Mock(best.Header(), uint64(time.Now().Unix()), s.gasLimit)
if err != nil {
return errors.WithMessage(err, "mock packer")
Expand Down Expand Up @@ -185,6 +192,10 @@ func (s *Solo) packing(pendingTxs tx.Transactions) error {

commitElapsed := mclock.Now() - startTime - execElapsed

if v, updated := s.bandwidth.Update(b.Header(), time.Duration(execElapsed+commitElapsed)); updated {
log.Debug("bandwidth updated", "gps", v)
}

blockID := b.Header().ID()
log.Info("📦 new block packed",
"txs", len(receipts),
Expand Down
7 changes: 6 additions & 1 deletion packer/packer.go
Expand Up @@ -148,6 +148,11 @@ func (p *Packer) Mock(parent *block.Header, targetTime uint64, gasLimit uint64)
features |= tx.DelegationFeature
}

gl := gasLimit
if gasLimit == 0 {
gl = p.gasLimit(parent.GasLimit())
}

rt := runtime.New(
p.repo.NewChain(parent.ID()),
state,
Expand All @@ -156,7 +161,7 @@ func (p *Packer) Mock(parent *block.Header, targetTime uint64, gasLimit uint64)
Signer: p.nodeMaster,
Number: parent.Number() + 1,
Time: targetTime,
GasLimit: gasLimit,
GasLimit: gl,
TotalScore: parent.TotalScore() + 1,
},
p.forkConfig)
Expand Down

0 comments on commit 865620f

Please sign in to comment.