Skip to content

Commit

Permalink
improve the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Nov 12, 2024
1 parent 90a858a commit 6c872cd
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion e2etest/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 12 additions & 18 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion state/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 9 additions & 15 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions test/mock/mock_factory/mock_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c872cd

Please sign in to comment.