From 6f9c7309df8051ae6751e3b157ba4def6d12647f Mon Sep 17 00:00:00 2001 From: dustinxie Date: Mon, 11 Nov 2024 21:32:39 -0800 Subject: [PATCH] improve the interface --- action/protocol/execution/protocol_test.go | 2 +- api/coreservice.go | 4 +-- chainservice/builder.go | 2 +- e2etest/staking_test.go | 2 +- state/factory/factory.go | 30 +++++++++------------- state/factory/factory_test.go | 2 +- state/factory/statedb.go | 24 +++++++---------- test/mock/mock_factory/mock_factory.go | 27 ++++++++++++++----- 8 files changed, 48 insertions(+), 45 deletions(-) diff --git a/action/protocol/execution/protocol_test.go b/action/protocol/execution/protocol_test.go index fea3a505f8..a33e343e31 100644 --- a/action/protocol/execution/protocol_test.go +++ b/action/protocol/execution/protocol_test.go @@ -293,7 +293,7 @@ func readExecution( GetBlockTime: getBlockTimeForTest, DepositGasFunc: rewarding.DepositGas, }) - ws, err := sf.WorkingSetNotWritable(ctx, 0, false) + ws, err := sf.WorkingSet(ctx) if err != nil { return nil, nil, err } diff --git a/api/coreservice.go b/api/coreservice.go index 08fcbabc08..1625d55519 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -1820,7 +1820,7 @@ func (core *coreService) ReadContractStorage(ctx context.Context, addr address.A if err != nil { return nil, status.Error(codes.Internal, err.Error()) } - ws, err := core.sf.WorkingSetNotWritable(ctx, 0, false) + ws, err := core.sf.WorkingSet(ctx) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -1973,7 +1973,7 @@ func (core *coreService) simulateExecution(ctx context.Context, addr address.Add GetBlockTime: core.getBlockTime, DepositGasFunc: rewarding.DepositGas, }) - ws, err := core.sf.WorkingSetNotWritable(ctx, 0, false) + ws, err := core.sf.WorkingSet(ctx) if err != nil { return nil, nil, status.Error(codes.Internal, err.Error()) } diff --git a/chainservice/builder.go b/chainservice/builder.go index dfd6e88824..bf6c589a45 100644 --- a/chainservice/builder.go +++ b/chainservice/builder.go @@ -735,7 +735,7 @@ func (builder *Builder) registerRollDPoSProtocol() error { GetBlockTime: getBlockTime, DepositGasFunc: rewarding.DepositGas, }) - ws, err := factory.WorkingSetNotWritable(ctx, 0, false) + ws, err := factory.WorkingSet(ctx) if err != nil { return nil, err } diff --git a/e2etest/staking_test.go b/e2etest/staking_test.go index 2969e21ad1..3fc4680145 100644 --- a/e2etest/staking_test.go +++ b/e2etest/staking_test.go @@ -125,7 +125,7 @@ func TestStakingContract(t *testing.T) { GetBlockTime: fakeGetBlockTime, DepositGasFunc: rewarding.DepositGas, }) - ws, err := sf.WorkingSetNotWritable(ctx, 0, false) + ws, err := sf.WorkingSet(ctx) if err != nil { return nil, err } diff --git a/state/factory/factory.go b/state/factory/factory.go index 3380b2f965..edd913e16d 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 IoTeX Foundation +// Copyright (c) 2024 IoTeX Foundation // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. @@ -89,7 +89,8 @@ type ( DeleteTipBlock(context.Context, *block.Block) error StateAtHeight(uint64, interface{}, ...protocol.StateOption) error StatesAtHeight(uint64, ...protocol.StateOption) (state.Iterator, error) - WorkingSetNotWritable(context.Context, uint64, bool) (protocol.StateManager, error) + WorkingSet(context.Context) (protocol.StateManager, error) + WorkingSetAtHeight(context.Context, uint64) (protocol.StateManager, error) } // factory implements StateFactory interface, tracks changes to account/contract and batch-commits to DB @@ -380,23 +381,16 @@ func (sf *factory) NewBlockBuilder( return blkBuilder, nil } -func (sf *factory) WorkingSetNotWritable(ctx context.Context, height uint64, isArchive bool) (protocol.StateManager, error) { - var ( - ws *workingSet - err error - ) - // TODO: make the workingset not writable, cannot call commit() to write its content to DB +func (sf *factory) WorkingSet(ctx context.Context) (protocol.StateManager, error) { sf.mutex.Lock() - if isArchive { - ws, err = sf.newWorkingSet(ctx, height+1) - } else { - ws, err = sf.newWorkingSet(ctx, sf.currentChainHeight+1) - } - sf.mutex.Unlock() - if err != nil { - return nil, errors.Wrap(err, "failed to obtain working set from state factory") - } - return ws, nil + defer sf.mutex.Unlock() + return sf.newWorkingSet(ctx, sf.currentChainHeight+1) +} + +func (sf *factory) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) { + sf.mutex.Lock() + defer sf.mutex.Unlock() + return sf.newWorkingSet(ctx, height+1) } // PutBlock persists all changes in RunActions() into the DB diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index 37385baadd..97ebf293fa 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -1224,7 +1224,7 @@ func testSimulateExecution(ctx context.Context, sf Factory, t *testing.T) { }, DepositGasFunc: rewarding.DepositGas, }) - ws, err := sf.WorkingSetNotWritable(ctx, 0, false) + ws, err := sf.WorkingSet(ctx) require.NoError(err) _, _, err = evm.SimulateExecution(ctx, ws, addr, elp) require.NoError(err) diff --git a/state/factory/statedb.go b/state/factory/statedb.go index c0a3543a4f..7da193a25f 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -259,21 +259,15 @@ func (sdb *stateDB) NewBlockBuilder( return blkBuilder, nil } -func (sdb *stateDB) WorkingSetNotWritable(ctx context.Context, height uint64, isArchive bool) (protocol.StateManager, error) { - var ( - ws *workingSet - err error - ) - if !isArchive { - sdb.mutex.RLock() - height = sdb.currentChainHeight - sdb.mutex.RUnlock() - } - ws, err = sdb.newWorkingSet(ctx, height+1) - if err != nil { - return nil, errors.Wrap(err, "failed to obtain working set from state factory") - } - return ws, nil +func (sdb *stateDB) WorkingSet(ctx context.Context) (protocol.StateManager, error) { + sdb.mutex.RLock() + height := sdb.currentChainHeight + sdb.mutex.RUnlock() + return sdb.newWorkingSet(ctx, height+1) +} + +func (sdb *stateDB) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) { + return sdb.newWorkingSet(ctx, height+1) } // PutBlock persists all changes in RunActions() into the DB diff --git a/test/mock/mock_factory/mock_factory.go b/test/mock/mock_factory/mock_factory.go index 54d105f882..4e5f71671e 100644 --- a/test/mock/mock_factory/mock_factory.go +++ b/test/mock/mock_factory/mock_factory.go @@ -247,17 +247,32 @@ func (mr *MockFactoryMockRecorder) Validate(arg0, arg1 interface{}) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Validate", reflect.TypeOf((*MockFactory)(nil).Validate), arg0, arg1) } -// WorkingSetNotWritable mocks base method. -func (m *MockFactory) WorkingSetNotWritable(arg0 context.Context, arg1 uint64, arg2 bool) (protocol.StateManager, error) { +// WorkingSet mocks base method. +func (m *MockFactory) WorkingSet(arg0 context.Context) (protocol.StateManager, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "WorkingSetNotWritable", arg0, arg1, arg2) + ret := m.ctrl.Call(m, "WorkingSet", arg0) ret0, _ := ret[0].(protocol.StateManager) ret1, _ := ret[1].(error) return ret0, ret1 } -// WorkingSetNotWritable indicates an expected call of WorkingSetNotWritable. -func (mr *MockFactoryMockRecorder) WorkingSetNotWritable(arg0, arg1, arg2 interface{}) *gomock.Call { +// WorkingSet indicates an expected call of WorkingSet. +func (mr *MockFactoryMockRecorder) WorkingSet(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WorkingSetNotWritable", reflect.TypeOf((*MockFactory)(nil).WorkingSetNotWritable), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WorkingSet", reflect.TypeOf((*MockFactory)(nil).WorkingSet), arg0) +} + +// WorkingSetAtHeight mocks base method. +func (m *MockFactory) WorkingSetAtHeight(arg0 context.Context, arg1 uint64) (protocol.StateManager, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WorkingSetAtHeight", arg0, arg1) + ret0, _ := ret[0].(protocol.StateManager) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// WorkingSetAtHeight indicates an expected call of WorkingSetAtHeight. +func (mr *MockFactoryMockRecorder) WorkingSetAtHeight(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WorkingSetAtHeight", reflect.TypeOf((*MockFactory)(nil).WorkingSetAtHeight), arg0, arg1) }