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
Conversation
koseoyoung
commented
Nov 12, 2019
•
edited
edited
- move initialize() from blockchain.go to state factory
- 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 { |
There was a problem hiding this comment.
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
)
state/factory/statedb.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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
)
state/factory/util.go
Outdated
return createRewardingGenesisStates(ctx, ws, registry, cfg) | ||
} | ||
|
||
func createAccountGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error { |
There was a problem hiding this comment.
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
)
state/factory/util.go
Outdated
return ap.Initialize(ctx, ws, addrs, balances) | ||
} | ||
|
||
func createRewardingGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error { |
There was a problem hiding this comment.
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
)
state/factory/util.go
Outdated
) | ||
} | ||
|
||
func createPollGenesisStates(ctx context.Context, ws WorkingSet, registry *protocol.Registry, cfg config.Config) error { |
There was a problem hiding this comment.
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
)
state/factory/factory.go
Outdated
@@ -399,3 +401,24 @@ func (sf *factory) accountState(encodedAddr string) (*state.Account, error) { | |||
} | |||
return &account, nil | |||
} | |||
|
|||
//Initialize initializes the state factory |
There was a problem hiding this comment.
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
)
state/factory/statedb.go
Outdated
@@ -282,3 +282,24 @@ func (sdb *stateDB) accountState(encodedAddr string) (*state.Account, error) { | |||
} | |||
return &account, nil | |||
} | |||
|
|||
//Initialize initializes the state db |
There was a problem hiding this comment.
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
)
state/factory/util.go
Outdated
"github.com/pkg/errors" | ||
) | ||
|
||
//CreateGenesisStates initialize the states |
There was a problem hiding this comment.
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
)
state/factory/util.go
Outdated
) | ||
|
||
//CreateGenesisStates initialize the states | ||
func CreateGenesisStates(cfg config.Config, registry *protocol.Registry, ws WorkingSet) error { |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
)
"github.com/iotexproject/iotex-address/address" | |
"github.com/iotexproject/iotex-address/address" | |
"github.com/iotexproject/iotex-proto/golang/iotextypes" | |
"github.com/pkg/errors" | |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
state/factory/util.go
Outdated
) | ||
|
||
// CreateGenesisStates initialize the states | ||
func CreateGenesisStates(cfg config.Config, registry *protocol.Registry, sm protocol.StateManager) error { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
)
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StateManager
state/factory/util.go
Outdated
) | ||
|
||
// initializeFactory initializes the factory | ||
func initializeFactory(sf Factory, cfg config.Config, registry *protocol.Registry) error { |
There was a problem hiding this comment.
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
)
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)
blockchain/blockchain.go
Outdated
@@ -694,23 +691,7 @@ func (bc *blockchain) blockFooterByHeight(height uint64) (*block.Footer, error) | |||
} | |||
|
|||
func (bc *blockchain) startEmptyBlockchain() error { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
state/factory/factory.go
Outdated
|
||
// Initialize initializes the state factory | ||
func (sf *factory) Initialize(cfg config.Config, registry *protocol.Registry) error { | ||
return initializeFactory(sf, cfg, registry) |
There was a problem hiding this comment.
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()
a390ccb
to
e7169a8
Compare
} | ||
|
||
// Initialize initializes the state db | ||
func (sdb *stateDB) Initialize(cfg config.Config, registry *protocol.Registry) error { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
)