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

Move state factory related code from blockchain to sf #1642

Merged
merged 9 commits into from Nov 13, 2019

Conversation

koseoyoung
Copy link
Contributor

@koseoyoung koseoyoung commented Nov 12, 2019

  1. move initialize() from blockchain.go to state factory
  2. handle dependency loop issue in unit tests

@@ -399,3 +401,24 @@ func (sf *factory) accountState(encodedAddr string) (*state.Account, error) {
}
return &account, nil
}

//Initialize initializes the state factory
func (sf *factory) Initialize(cfg config.Config, registry *protocol.Registry) error {

Choose a reason for hiding this comment

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

406-424 lines are duplicate of state/factory/statedb.go:287-305 (from dupl)

@@ -282,3 +282,24 @@ func (sdb *stateDB) accountState(encodedAddr string) (*state.Account, error) {
}
return &account, nil
}

//Initialize initializes the state db
func (sdb *stateDB) Initialize(cfg config.Config, registry *protocol.Registry) error {

Choose a reason for hiding this comment

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

287-305 lines are duplicate of state/factory/factory.go:406-424 (from dupl)

return createRewardingGenesisStates(ctx, ws, registry, cfg)
}

func createAccountGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error {

Choose a reason for hiding this comment

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

ws can be github.com/iotexproject/iotex-core/action/protocol.StateManager (from interfacer)

return ap.Initialize(ctx, ws, addrs, balances)
}

func createRewardingGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error {

Choose a reason for hiding this comment

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

ws can be github.com/iotexproject/iotex-core/action/protocol.StateManager (from interfacer)

)
}

func createPollGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error {

Choose a reason for hiding this comment

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

ws can be github.com/iotexproject/iotex-core/action/protocol.StateManager (from interfacer)

@@ -399,3 +401,24 @@ func (sf *factory) accountState(encodedAddr string) (*state.Account, error) {
}
return &account, nil
}

//Initialize initializes the state factory

Choose a reason for hiding this comment

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

commentFormatting: put a space between // and comment text (from gocritic)

@@ -282,3 +282,24 @@ func (sdb *stateDB) accountState(encodedAddr string) (*state.Account, error) {
}
return &account, nil
}

//Initialize initializes the state db

Choose a reason for hiding this comment

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

commentFormatting: put a space between // and comment text (from gocritic)

"github.com/pkg/errors"
)

//CreateGenesisStates initialize the states

Choose a reason for hiding this comment

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

commentFormatting: put a space between // and comment text (from gocritic)

)

//CreateGenesisStates initialize the states
func CreateGenesisStates(cfg config.Config, registry *protocol.Registry, 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)

"go.uber.org/zap"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"

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-address/address"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"

@koseoyoung koseoyoung marked this pull request as ready for review November 12, 2019 22:44
@koseoyoung koseoyoung requested a review from a team as a code owner November 12, 2019 22:44
@codecov
Copy link

codecov bot commented Nov 12, 2019

Codecov Report

Merging #1642 into master will decrease coverage by 0.49%.
The diff coverage is 8.58%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #1642     +/-   ##
=========================================
- Coverage   54.72%   54.23%   -0.5%     
=========================================
  Files         154      155      +1     
  Lines       13459    13480     +21     
=========================================
- Hits         7366     7311     -55     
- Misses       5138     5224     +86     
+ Partials      955      945     -10
Impacted Files Coverage Δ
state/factory/util.go 0% <0%> (ø)
action/protocol/execution/evm/evmstatedbadapter.go 52.57% <0%> (-0.24%) ⬇️
state/factory/factory.go 54.5% <22.58%> (-2.86%) ⬇️
state/factory/statedb.go 61.53% <24%> (-3.29%) ⬇️
blockchain/blockchain.go 41.98% <33.33%> (-1.23%) ⬇️
action/protocol/poll/protocol.go 35.75% <0%> (-1.27%) ⬇️

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 db4a207...e7169a8. Read the comment docs.

)

// CreateGenesisStates initialize the states
func CreateGenesisStates(cfg config.Config, registry *protocol.Registry, sm protocol.StateManager) 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 createRewardingGenesisStates(ctx, sm, registry, cfg)
}

func createAccountGenesisStates(ctx context.Context, sm protocol.StateManager, registry *protocol.Registry, 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)

return ap.Initialize(ctx, sm, addrs, balances)
}

func createRewardingGenesisStates(ctx context.Context, sm protocol.StateManager, registry *protocol.Registry, 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)

Copy link
Collaborator

@CoderZhi CoderZhi left a comment

Choose a reason for hiding this comment

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

quite a few comments offline, please address

ws, err := stateDB.NewWorkingSet()
require.NoError(t, err)
amount, err := p.EpochReward(ctx, ws)
testProtocol(t, func(t *testing.T, ctx context.Context, sm *mock_chainmanager.MockStateManager, p *Protocol) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

StateManager

"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
"github.com/iotexproject/iotex-election/test/mock/mock_committee"
"github.com/iotexproject/iotex-election/types"
)

func initConstruct(t *testing.T) (Protocol, context.Context, factory.WorkingSet, *types.ElectionResult) {
func initConstruct(t *testing.T) (Protocol, context.Context, *mock_chainmanager.MockStateManager, *types.ElectionResult) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

StateManager

)

func TestProtocol_Fund(t *testing.T) {
testProtocol(t, func(t *testing.T, ctx context.Context, stateDB factory.Factory, p *Protocol) {
testProtocol(t, func(t *testing.T, ctx context.Context, sm *mock_chainmanager.MockStateManager, p *Protocol) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

StateManager

)

// initializeFactory initializes the factory
func initializeFactory(sf Factory, cfg config.Config, registry *protocol.Registry) 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)

Copy link
Collaborator

@CoderZhi CoderZhi left a comment

Choose a reason for hiding this comment

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

looks good at first glance. It is a large PR, let me take a chance to look through everything in details.

)

func initMockStateManager(t *testing.T) protocol.StateManager {
Copy link
Collaborator

Choose a reason for hiding this comment

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

func initMockStateManager(ctrl gomock.Controller) protocol.StateManager {

Move

    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

out of the function

"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
"github.com/iotexproject/iotex-election/test/mock/mock_committee"
"github.com/iotexproject/iotex-election/types"
)

func initConstruct(t *testing.T) (Protocol, context.Context, factory.WorkingSet, *types.ElectionResult) {
func initConstruct(t *testing.T) (Protocol, context.Context, protocol.StateManager, *types.ElectionResult) {
require := require.New(t)
ctrl := gomock.NewController(t)
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, move it out

"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
"github.com/iotexproject/iotex-election/test/mock/mock_committee"
"github.com/iotexproject/iotex-election/types"
)

func initConstruct(t *testing.T) (Protocol, context.Context, factory.WorkingSet, *types.ElectionResult) {
func initConstruct(t *testing.T) (Protocol, context.Context, protocol.StateManager, *types.ElectionResult) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

return (Protocol, context.Context, protocol.StateManager, *types.ElectionResult, error)

@@ -694,23 +691,7 @@ func (bc *blockchain) blockFooterByHeight(height uint64) (*block.Footer, error)
}

func (bc *blockchain) startEmptyBlockchain() error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this function is only used once. We may delete it

@@ -64,6 +64,8 @@ type (

State(hash.Hash160, interface{}) error
AddActionHandlers(...protocol.ActionHandler)
// empty blockchain
Initialize(config.Config, *protocol.Registry) error
Copy link
Collaborator

Choose a reason for hiding this comment

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

the name of this interface is not accurate. could we come up with a better one?


// Initialize initializes the state factory
func (sf *factory) Initialize(cfg config.Config, registry *protocol.Registry) error {
return initializeFactory(sf, cfg, registry)
Copy link
Collaborator

Choose a reason for hiding this comment

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

as discussed offline, we need to call

sf.mu.Lock()
defer sf.mu.Unlock()

}

// Initialize initializes the state db
func (sdb *stateDB) Initialize(cfg config.Config, registry *protocol.Registry) 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)

)

// createGenesisStates initialize the genesis states
func createGenesisStates(cfg config.Config, registry *protocol.Registry, 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)

@@ -315,8 +312,9 @@ func (bc *blockchain) Start(ctx context.Context) (err error) {
if bc.tipHeight, err = bc.dao.GetTipHeight(); err != nil {
return err
}
//start empty blockchain

Choose a reason for hiding this comment

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

commentFormatting: put a space between // and comment text (from gocritic)

@koseoyoung koseoyoung merged commit 56912d6 into iotexproject:master Nov 13, 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

3 participants