Skip to content

Redefine view #4668

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion action/protocol/staking/bucket_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func TestBucketPool(t *testing.T) {
}

view, _, err := CreateBaseView(sm, false)
view.contractsStake = &contractStakeView{}
r.NoError(err)
r.NoError(sm.WriteView(_protocolID, view))
pool = view.bucketPool
Expand Down
5 changes: 2 additions & 3 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ func CreateBaseView(sr protocol.StateReader, enableSMStorage bool) (*ViewData, u
}

return &ViewData{
candCenter: center,
bucketPool: pool,
contractsStake: &contractStakeView{},
candCenter: center,
bucketPool: pool,
}, height, nil
}

Expand Down
21 changes: 4 additions & 17 deletions action/protocol/staking/viewdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"math/big"

"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/pkg/errors"
)

type (
Expand All @@ -22,7 +21,6 @@ type (
Clone() ContractStakeView
CreatePreStates(ctx context.Context) error
Handle(ctx context.Context, receipt *action.Receipt) error
Commit()
BucketsByCandidate(ownerAddr address.Address) ([]*VoteBucket, error)
}
// ViewData is the data that need to be stored in protocol's view
Expand Down Expand Up @@ -81,7 +79,6 @@ func (v *ViewData) Commit(ctx context.Context, sr protocol.StateReader) error {
if err := v.bucketPool.Commit(sr); err != nil {
return err
}
v.contractsStake.Commit()
v.snapshots = []Snapshot{}

return nil
Expand All @@ -93,13 +90,15 @@ func (v *ViewData) IsDirty() bool {

func (v *ViewData) Snapshot() int {
snapshot := len(v.snapshots)
clone := v.contractsStake.Clone()
v.snapshots = append(v.snapshots, Snapshot{
size: v.candCenter.size,
changes: v.candCenter.change.size(),
amount: new(big.Int).Set(v.bucketPool.total.amount),
count: v.bucketPool.total.count,
contractsStake: v.contractsStake.Clone(),
contractsStake: v.contractsStake,
})
v.contractsStake = clone
Copy link
Member

Choose a reason for hiding this comment

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

the contractStake may have too many wraps, because there is no reset for it after 1 block committed

return snapshot
}

Expand Down Expand Up @@ -175,15 +174,3 @@ func (csv *contractStakeView) Handle(ctx context.Context, receipt *action.Receip
}
return nil
}

func (csv *contractStakeView) Commit() {
if csv.v1 != nil {
csv.v1.Commit()
}
if csv.v2 != nil {
csv.v2.Commit()
}
if csv.v3 != nil {
csv.v3.Commit()
}
}
10 changes: 4 additions & 6 deletions action/protocol/staking/viewdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/v2/test/identityset"
"github.com/iotexproject/iotex-core/v2/test/mock/mock_chainmanager"
"github.com/stretchr/testify/require"
)

func TestViewData_Clone(t *testing.T) {
Expand Down Expand Up @@ -62,10 +61,9 @@ func prepareViewData(t *testing.T) (*ViewData, int) {
},
}
viewData := &ViewData{
candCenter: candCenter,
bucketPool: bucketPool,
snapshots: []Snapshot{},
contractsStake: &contractStakeView{},
candCenter: candCenter,
bucketPool: bucketPool,
snapshots: []Snapshot{},
}
return viewData, viewData.Snapshot()
}
Expand Down
4 changes: 2 additions & 2 deletions blockindex/contractstaking/bucket_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (bi *bucketInfo) Deserialize(b []byte) error {
return bi.loadProto(&m)
}

// clone clones the bucket info
func (bi *bucketInfo) clone() *bucketInfo {
// Clone clones the bucket info
func (bi *bucketInfo) Clone() *bucketInfo {
delegate := bi.Delegate
if delegate != nil {
delegate, _ = address.FromBytes(delegate.Bytes())
Expand Down
Loading