Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly initialize state factory #1647

Merged
merged 3 commits into from Nov 15, 2019
Merged

Conversation

dustinxie
Copy link
Member

it could happen that bc.tipHeight > 0 while state factory DB file has been deleted
sf.Initialize() should be called based upon sf.Height() == 0, but not bc.tipHeight == 0

Test plan:
make test && make minicluster

@dustinxie dustinxie requested a review from a team as a code owner November 13, 2019 22:41
if bc.tipHeight == 0 {
return bc.sf.Initialize(bc.config, bc.registry)
if sfHeight == 0 {
if err := bc.sf.Initialize(bc.config, bc.registry); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shadow: declaration of "err" shadows declaration at line 305 (from govet)

@codecov
Copy link

codecov bot commented Nov 13, 2019

Codecov Report

Merging #1647 into master will increase coverage by 0.04%.
The diff coverage is 27.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1647      +/-   ##
==========================================
+ Coverage   54.22%   54.26%   +0.04%     
==========================================
  Files         155      155              
  Lines       13480    13504      +24     
==========================================
+ Hits         7309     7328      +19     
  Misses       5226     5226              
- Partials      945      950       +5
Impacted Files Coverage Δ
state/factory/util.go 0% <0%> (ø) ⬆️
state/factory/factory.go 54.95% <38.88%> (+0.45%) ⬆️
state/factory/statedb.go 60.58% <42.1%> (-0.96%) ⬇️
blockchain/blockchain.go 42.17% <46.66%> (+0.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b65179...8eaf00d. Read the comment docs.

@koseoyoung
Copy link
Contributor

koseoyoung commented Nov 14, 2019

If we want to handle that corner case, I think it would be better to move the sf.initialize() into sf.start() so that it can initialize by itself if needed

@dustinxie
Copy link
Member Author

If we want to handle that corner case, I think it would be better to move the sf.initialize() into sf.start() so that it can initialize by itself if needed

fixed in latest push

@@ -29,46 +28,33 @@ import (
)

// createGenesisStates initialize the genesis states
func createGenesisStates(cfg config.Config, registry *protocol.Registry, ws WorkingSet) error {
func createGenesisStates(ctx context.Context, cfg config.Config, ws WorkingSet) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hugeParam: cfg is heavy (1488 bytes); consider passing it by pointer (from gocritic)

return err
}
_ = ws.UpdateBlockLevelInfo(0)
return nil
}

func createAccountGenesisStates(ctx context.Context, sm protocol.StateManager, registry *protocol.Registry, cfg config.Config) error {
p, ok := registry.Find(account.ProtocolID)
func createAccountGenesisStates(ctx context.Context, sm protocol.StateManager, cfg config.Config) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hugeParam: cfg is heavy (1488 bytes); consider passing it by pointer (from gocritic)

@@ -80,8 +66,9 @@ func createAccountGenesisStates(ctx context.Context, sm protocol.StateManager, r
return ap.Initialize(ctx, sm, addrs, balances)
}

func createRewardingGenesisStates(ctx context.Context, sm protocol.StateManager, registry *protocol.Registry, cfg config.Config) error {
p, ok := registry.Find(rewarding.ProtocolID)
func createRewardingGenesisStates(ctx context.Context, sm protocol.StateManager, cfg config.Config) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hugeParam: cfg is heavy (1488 bytes); consider passing it by pointer (from gocritic)


"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"go.uber.org/zap"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File is not goimports-ed with -local github.com/iotexproject/iotex-core (from goimports)

Suggested change
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"

@@ -160,6 +158,17 @@ func (sf *factory) Start(ctx context.Context) error {
if err := sf.dao.Start(ctx); err != nil {
return err
}
// check factory height
_, err := sf.dao.Get(AccountKVNameSpace, []byte(CurrentHeightKey))
if err != nil && errors.Cause(err) == db.ErrNotExist {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch errors.Cause(err) {
    case db.ErrNotExist:
       ...
    case nil:
      break
    default:
      return err
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in latest push

case nil:
break
case db.ErrNotExist:
if err := sf.dao.Put(AccountKVNameSpace, []byte(CurrentHeightKey), byteutil.Uint64ToBytes(0)); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shadow: declaration of "err" shadows declaration at line 162 (from govet)

return errors.Wrap(err, "failed to init factory's height")
}
// init the state factory
if err := sf.initialize(ctx); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shadow: declaration of "err" shadows declaration at line 162 (from govet)

case nil:
break
case db.ErrNotExist:
if err := sdb.dao.Put(AccountKVNameSpace, []byte(CurrentHeightKey), byteutil.Uint64ToBytes(0)); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shadow: declaration of "err" shadows declaration at line 109 (from govet)

return errors.Wrap(err, "failed to init statedb's height")
}
// init the state factory
if err := sdb.initialize(ctx); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shadow: declaration of "err" shadows declaration at line 109 (from govet)

return errors.Wrap(err, "failed to init factory's height")
}
// init the state factory
if err = sf.initialize(ctx); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sloppyReassign: re-assignment to err can be replaced with err := sf.initialize(ctx) (from gocritic)

return errors.Wrap(err, "failed to init statedb's height")
}
// init the state factory
if err = sdb.initialize(ctx); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sloppyReassign: re-assignment to err can be replaced with err := sdb.initialize(ctx) (from gocritic)

@dustinxie dustinxie merged commit ca564f7 into iotexproject:master Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants