Skip to content

Commit

Permalink
[mint] deadline for mint block (#4487)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Nov 19, 2024
1 parent 6d606aa commit b2e6537
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
3 changes: 3 additions & 0 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type (
PersistStakingPatchBlock uint64 `yaml:"persistStakingPatchBlock"`
// FactoryDBType is the type of factory db
FactoryDBType string `yaml:"factoryDBType"`
// MintTimeout is the timeout for minting
MintTimeout time.Duration `yaml:"-"`
}
)

Expand Down Expand Up @@ -119,6 +121,7 @@ var (
StreamingBlockBufferSize: 200,
PersistStakingPatchBlock: 19778037,
FactoryDBType: db.DBBolt,
MintTimeout: 1500 * time.Millisecond, // valued with block accept ttl - 500ms(tolerate network delay)
}

// ErrConfig config error
Expand Down
6 changes: 5 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,11 @@ func (builder *Builder) createBlockchain(forSubChain, forTest bool) blockchain.B
chainOpts = append(chainOpts, blockchain.BlockValidatorOption(builder.cs.factory))
}

return blockchain.NewBlockchain(builder.cfg.Chain, builder.cfg.Genesis, builder.cs.blockdao, factory.NewMinter(builder.cs.factory, builder.cs.actpool), chainOpts...)
var mintOpts []factory.MintOption
if builder.cfg.Consensus.Scheme == config.RollDPoSScheme {
mintOpts = append(mintOpts, factory.WithTimeoutOption(builder.cfg.Chain.MintTimeout))
}
return blockchain.NewBlockchain(builder.cfg.Chain, builder.cfg.Genesis, builder.cs.blockdao, factory.NewMinter(builder.cs.factory, builder.cs.actpool, mintOpts...), chainOpts...)
}

func (builder *Builder) buildNodeInfoManager() error {
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func New(configPaths []string, _plugins []string, validates ...Validate) (Config
if err := cfg.Chain.SetProducerPrivKey(); err != nil {
return Config{}, errors.Wrap(err, "failed to set producer private key")
}
// set default value for mint timeout
cfg.Chain.MintTimeout = Default.Chain.MintTimeout

// set network master key to private key
if cfg.Network.MasterKey == "" {
Expand Down
4 changes: 3 additions & 1 deletion e2etest/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/mohae/deepcopy"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -548,10 +549,11 @@ func TestStartExistingBlockchain(t *testing.T) {
}

func newTestConfig() (config.Config, error) {
cfg := config.Default
cfg := deepcopy.Copy(config.Default).(config.Config)
cfg.Chain.TrieDBPath = _triePath
cfg.Chain.ChainDBPath = _dBPath
cfg.Chain.BlobStoreDBPath = _blobPath
cfg.Chain.MintTimeout = 0
cfg.ActPool.MinGasPriceStr = "0"
cfg.Consensus.Scheme = config.NOOPScheme
cfg.Network.Port = testutil.RandomPort()
Expand Down
4 changes: 3 additions & 1 deletion e2etest/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/require"

"github.com/iotexproject/go-pkgs/hash"
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestStakingContract(t *testing.T) {
}
}

cfg := config.Default
cfg := deepcopy.Copy(config.Default).(config.Config)
initDBPaths(require, &cfg)

defer func() {
Expand All @@ -196,6 +197,7 @@ func TestStakingContract(t *testing.T) {
cfg.ActPool.MinGasPriceStr = "0"
cfg.Chain.TrieDBPatchFile = ""
cfg.Chain.ProducerPrivKey = "a000000000000000000000000000000000000000000000000000000000000000"
cfg.Chain.MintTimeout = 0
cfg.Consensus.Scheme = config.RollDPoSScheme
cfg.Genesis.NumDelegates = 1
cfg.Genesis.NumSubEpochs = 10
Expand Down
29 changes: 25 additions & 4 deletions state/factory/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,45 @@ package factory

import (
"context"
"time"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/actpool"
"github.com/iotexproject/iotex-core/v2/blockchain"
"github.com/iotexproject/iotex-core/v2/blockchain/block"
)

type MintOption func(*minter)

// WithTimeoutOption sets the timeout for NewBlockBuilder
func WithTimeoutOption(timeout time.Duration) MintOption {
return func(m *minter) {
m.timeout = timeout
}
}

type minter struct {
f Factory
ap actpool.ActPool
f Factory
ap actpool.ActPool
timeout time.Duration
}

// NewMinter creates a wrapper instance
func NewMinter(f Factory, ap actpool.ActPool) blockchain.BlockBuilderFactory {
return &minter{f: f, ap: ap}
func NewMinter(f Factory, ap actpool.ActPool, opts ...MintOption) blockchain.BlockBuilderFactory {
m := &minter{f: f, ap: ap}
for _, opt := range opts {
opt(m)
}
return m
}

// NewBlockBuilder implements the BlockMinter interface
func (m *minter) NewBlockBuilder(ctx context.Context, sign func(action.Envelope) (*action.SealedEnvelope, error)) (*block.Builder, error) {
if m.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, protocol.MustGetBlockCtx(ctx).BlockTimeStamp.Add(m.timeout))
defer cancel()
}
return m.f.NewBlockBuilder(ctx, m.ap, sign)
}
22 changes: 22 additions & 0 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"math/big"
"sort"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -40,6 +41,13 @@ var (
},
[]string{"type"},
)
_mintAbility = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "iotex_mint_ability",
Help: "IoTeX Mint Ability",
},
[]string{"type"},
)

errInvalidSystemActionLayout = errors.New("system action layout is invalid")
errUnfoldTxContainer = errors.New("failed to unfold tx container")
Expand All @@ -48,6 +56,7 @@ var (

func init() {
prometheus.MustRegister(_stateDBMtc)
prometheus.MustRegister(_mintAbility)
}

type (
Expand Down Expand Up @@ -658,10 +667,23 @@ func (ws *workingSet) pickAndRunActions(
fCtx = protocol.MustGetFeatureCtx(ctx)
blobCnt = uint64(0)
blobLimit = params.MaxBlobGasPerBlock / params.BlobTxBlobGasPerBlob
deadline *time.Time
fullGas = blkCtx.GasLimit
)
if ap != nil {
if dl, ok := ctx.Deadline(); ok {
deadline = &dl
}
actionIterator := actioniterator.NewActionIterator(ap.PendingActionMap())
for {
if deadline != nil && time.Now().After(*deadline) {
duration := time.Since(blkCtx.BlockTimeStamp)
log.L().Warn("Stop processing actions due to deadline, please consider increasing hardware", zap.Time("deadline", *deadline), zap.Duration("duration", duration), zap.Int("actions", len(executedActions)), zap.Uint64("gas", fullGas-blkCtx.GasLimit))
_mintAbility.WithLabelValues("action").Set(float64(len(executedActions)))
_mintAbility.WithLabelValues("gas").Set(float64(fullGas - blkCtx.GasLimit))
_mintAbility.WithLabelValues("duration").Set(float64(duration.Milliseconds()))
break
}
nextAction, ok := actionIterator.Next()
if !ok {
break
Expand Down

0 comments on commit b2e6537

Please sign in to comment.