Skip to content

Commit

Permalink
[staking] clean up internal interface (#3404)
Browse files Browse the repository at this point in the history
Co-authored-by: Haaai <[email protected]>
  • Loading branch information
dustinxie and Liuhaai authored May 27, 2022
1 parent 180ab52 commit a1bc494
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 61 deletions.
24 changes: 0 additions & 24 deletions action/protocol/staking/bucket_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,3 @@ func AddrKeyWithPrefix(addr address.Address, prefix byte) []byte {
copy(key[1:], k)
return key
}

func (csr *candSR) VoterBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return csr.getBucketIndices(addr, _voterIndex)
}

func (csm *candSM) PutVoterBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _voterIndex, index)
}

func (csm *candSM) DelVoterBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _voterIndex, index)
}

func (csr *candSR) CandBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return csr.getBucketIndices(addr, _candIndex)
}

func (csm *candSM) PutCandBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _candIndex, index)
}

func (csm *candSM) DelCandBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _candIndex, index)
}
20 changes: 10 additions & 10 deletions action/protocol/staking/bucket_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,26 @@ func TestGetPutBucketIndex(t *testing.T) {

// put buckets and get
for i, e := range tests {
_, _, err := csr.(*candSR).VoterBucketIndices(e.voterAddr)
_, _, err := csr.voterBucketIndices(e.voterAddr)
if i == 0 {
require.Equal(state.ErrStateNotExist, errors.Cause(err))
}
_, _, err = csr.(*candSR).CandBucketIndices(e.candAddr)
_, _, err = csr.candBucketIndices(e.candAddr)
if i == 0 {
require.Equal(state.ErrStateNotExist, errors.Cause(err))
}

// put voter bucket index
require.NoError(csm.(*candSM).PutVoterBucketIndex(e.voterAddr, e.index))
bis, _, err := csr.(*candSR).VoterBucketIndices(e.voterAddr)
require.NoError(csm.putVoterBucketIndex(e.voterAddr, e.index))
bis, _, err := csr.voterBucketIndices(e.voterAddr)
require.NoError(err)
bucketIndices := *bis
require.Equal(e.voterIndexSize, len(bucketIndices))
require.Equal(bucketIndices[e.voterIndexSize-1], e.index)

// put candidate bucket index
require.NoError(csm.(*candSM).PutCandBucketIndex(e.candAddr, e.index))
bis, _, err = csr.(*candSR).CandBucketIndices(e.candAddr)
require.NoError(csm.putCandBucketIndex(e.candAddr, e.index))
bis, _, err = csr.candBucketIndices(e.candAddr)
require.NoError(err)
bucketIndices = *bis
require.Equal(e.candIndexSize, len(bucketIndices))
Expand All @@ -152,8 +152,8 @@ func TestGetPutBucketIndex(t *testing.T) {

for _, e := range tests {
// delete voter bucket index
require.NoError(csm.(*candSM).DelVoterBucketIndex(e.voterAddr, e.index))
bis, _, err := csr.(*candSR).VoterBucketIndices(e.voterAddr)
require.NoError(csm.delVoterBucketIndex(e.voterAddr, e.index))
bis, _, err := csr.voterBucketIndices(e.voterAddr)
if e.voterIndexSize != indexSize {
bucketIndices := *bis
require.Equal(indexSize-e.voterIndexSize, len(bucketIndices))
Expand All @@ -162,8 +162,8 @@ func TestGetPutBucketIndex(t *testing.T) {
}

// delete candidate bucket index
require.NoError(csm.(*candSM).DelCandBucketIndex(e.candAddr, e.index))
bis, _, err = csr.(*candSR).CandBucketIndices(e.candAddr)
require.NoError(csm.delCandBucketIndex(e.candAddr, e.index))
bis, _, err = csr.candBucketIndices(e.candAddr)
if e.candIndexSize != indexSize {
bucketIndices := *bis
require.Equal(indexSize-e.candIndexSize, len(bucketIndices))
Expand Down
30 changes: 25 additions & 5 deletions action/protocol/staking/candidate_statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ type (
}
// CandidateSet related to setting candidates
CandidateSet interface {
delCandidate(name address.Address) error
putCandidate(d *Candidate) error
delCandidate(name address.Address) error
putVoterBucketIndex(addr address.Address, index uint64) error
delVoterBucketIndex(addr address.Address, index uint64) error
putCandBucketIndex(addr address.Address, index uint64) error
delCandBucketIndex(addr address.Address, index uint64) error
}
// CandidateStateManager is candidate state manager on top of StateManager
CandidateStateManager interface {
Expand Down Expand Up @@ -236,11 +240,11 @@ func (csm *candSM) putBucketAndIndex(bucket *VoteBucket) (uint64, error) {
return 0, errors.Wrap(err, "failed to put bucket")
}

if err := csm.PutVoterBucketIndex(bucket.Owner, index); err != nil {
if err := csm.putVoterBucketIndex(bucket.Owner, index); err != nil {
return 0, errors.Wrap(err, "failed to put bucket index")
}

if err := csm.PutCandBucketIndex(bucket.Candidate, index); err != nil {
if err := csm.putCandBucketIndex(bucket.Candidate, index); err != nil {
return 0, errors.Wrap(err, "failed to put candidate index")
}
return index, nil
Expand All @@ -251,11 +255,11 @@ func (csm *candSM) delBucketAndIndex(owner, cand address.Address, index uint64)
return errors.Wrap(err, "failed to delete bucket")
}

if err := csm.DelVoterBucketIndex(owner, index); err != nil {
if err := csm.delVoterBucketIndex(owner, index); err != nil {
return errors.Wrap(err, "failed to delete bucket index")
}

if err := csm.DelCandBucketIndex(cand, index); err != nil {
if err := csm.delCandBucketIndex(cand, index); err != nil {
return errors.Wrap(err, "failed to delete candidate index")
}
return nil
Expand All @@ -280,6 +284,10 @@ func (csm *candSM) putBucketIndex(addr address.Address, prefix byte, index uint6
return err
}

func (csm *candSM) putVoterBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _voterIndex, index)
}

func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint64) error {
var (
bis BucketIndices
Expand Down Expand Up @@ -307,12 +315,24 @@ func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint6
return err
}

func (csm *candSM) delVoterBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _voterIndex, index)
}

func (csm *candSM) putCandidate(d *Candidate) error {
_, err := csm.PutState(d, protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(d.Owner.Bytes()))
return err
}

func (csm *candSM) putCandBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _candIndex, index)
}

func (csm *candSM) delCandidate(name address.Address) error {
_, err := csm.DelState(protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(name.Bytes()))
return err
}

func (csm *candSM) delCandBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _candIndex, index)
}
14 changes: 12 additions & 2 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type (
getAllBuckets() ([]*VoteBucket, uint64, error)
getBucketsWithIndices(indices BucketIndices) ([]*VoteBucket, error)
getBucketIndices(addr address.Address, prefix byte) (*BucketIndices, uint64, error)
voterBucketIndices(addr address.Address) (*BucketIndices, uint64, error)
candBucketIndices(addr address.Address) (*BucketIndices, uint64, error)
}
// CandidateGet related to obtaining Candidate
CandidateGet interface {
Expand Down Expand Up @@ -291,6 +293,14 @@ func (c *candSR) getBucketIndices(addr address.Address, prefix byte) (*BucketInd
return &bis, height, nil
}

func (c *candSR) voterBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return c.getBucketIndices(addr, _voterIndex)
}

func (c *candSR) candBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return c.getBucketIndices(addr, _candIndex)
}

func (c *candSR) getCandidate(name address.Address) (*Candidate, uint64, error) {
if name == nil {
return nil, 0, ErrNilParameters
Expand Down Expand Up @@ -371,7 +381,7 @@ func (c *candSR) readStateBucketsByVoter(ctx context.Context, req *iotexapi.Read
return nil, 0, err
}

indices, height, err := c.VoterBucketIndices(voter)
indices, height, err := c.voterBucketIndices(voter)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
Expand All @@ -396,7 +406,7 @@ func (c *candSR) readStateBucketsByCandidate(ctx context.Context, req *iotexapi.
return &iotextypes.VoteBucketList{}, 0, nil
}

indices, height, err := c.CandBucketIndices(cand.Owner)
indices, height, err := c.candBucketIndices(cand.Owner)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ func (p *Protocol) handleChangeCandidate(ctx context.Context, act *action.Change
}

// update bucket index
if err := csm.(*candSM).DelCandBucketIndex(bucket.Candidate, act.BucketIndex()); err != nil {
if err := csm.delCandBucketIndex(bucket.Candidate, act.BucketIndex()); err != nil {
return log, errors.Wrapf(err, "failed to delete candidate bucket index for candidate %s", bucket.Candidate.String())
}
if err := csm.(*candSM).PutCandBucketIndex(candidate.Owner, act.BucketIndex()); err != nil {
if err := csm.putCandBucketIndex(candidate.Owner, act.BucketIndex()); err != nil {
return log, errors.Wrapf(err, "failed to put candidate bucket index for candidate %s", candidate.Owner.String())
}
// update bucket
Expand Down Expand Up @@ -395,10 +395,10 @@ func (p *Protocol) handleTransferStake(ctx context.Context, act *action.Transfer
}

// update bucket index
if err := csm.(*candSM).DelVoterBucketIndex(bucket.Owner, act.BucketIndex()); err != nil {
if err := csm.delVoterBucketIndex(bucket.Owner, act.BucketIndex()); err != nil {
return log, errors.Wrapf(err, "failed to delete voter bucket index for voter %s", bucket.Owner.String())
}
if err := csm.(*candSM).PutVoterBucketIndex(newOwner, act.BucketIndex()); err != nil {
if err := csm.putVoterBucketIndex(newOwner, act.BucketIndex()); err != nil {
return log, errors.Wrapf(err, "failed to put candidate bucket index for voter %s", act.VoterAddress().String())
}

Expand Down
32 changes: 16 additions & 16 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
require.Equal(test.amount, cLog.Amount.String())

// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(candidateAddr)
bucketIndices, _, err := csr.candBucketIndices(candidateAddr)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(stakerAddr)
bucketIndices, _, err = csr.voterBucketIndices(stakerAddr)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -1109,10 +1109,10 @@ func TestProtocol_HandleUnstake(t *testing.T) {
if test.err == nil && test.status == iotextypes.ReceiptStatus_Success {
// test bucket index and bucket
csr = newCandidateStateReader(sm)
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(candidate.Owner)
bucketIndices, _, err := csr.candBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(candidate.Owner)
bucketIndices, _, err = csr.voterBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -1343,9 +1343,9 @@ func TestProtocol_HandleWithdrawStake(t *testing.T) {
require.Equal(test.amount, wLog.Amount.String())

// test bucket index and bucket
_, _, err := csr.(*candSR).CandBucketIndices(candidate.Owner)
_, _, err := csr.candBucketIndices(candidate.Owner)
require.Error(err)
_, _, err = csr.(*candSR).VoterBucketIndices(candidate.Owner)
_, _, err = csr.voterBucketIndices(candidate.Owner)
require.Error(err)

// test staker's account
Expand Down Expand Up @@ -1624,10 +1624,10 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) {

if test.err == nil && test.status == iotextypes.ReceiptStatus_Success {
// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(identityset.Address(1))
bucketIndices, _, err := csr.candBucketIndices(identityset.Address(1))
require.NoError(err)
require.Equal(2, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(identityset.Address(1))
bucketIndices, _, err = csr.voterBucketIndices(identityset.Address(1))
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -1825,10 +1825,10 @@ func TestProtocol_HandleTransferStake(t *testing.T) {

if test.err == nil && test.status == iotextypes.ReceiptStatus_Success {
// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(candidate2.Owner)
bucketIndices, _, err := csr.candBucketIndices(candidate2.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(test.to)
bucketIndices, _, err = csr.voterBucketIndices(test.to)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -2058,10 +2058,10 @@ func TestProtocol_HandleConsignmentTransfer(t *testing.T) {

if test.status == iotextypes.ReceiptStatus_Success {
// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(cand2.Owner)
bucketIndices, _, err := csr.candBucketIndices(cand2.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(test.to)
bucketIndices, _, err = csr.voterBucketIndices(test.to)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -2330,10 +2330,10 @@ func TestProtocol_HandleRestake(t *testing.T) {

if test.err == nil && test.status == iotextypes.ReceiptStatus_Success {
// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(candidate.Owner)
bucketIndices, _, err := csr.candBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(candidate.Owner)
bucketIndices, _, err = csr.voterBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down Expand Up @@ -2550,10 +2550,10 @@ func TestProtocol_HandleDepositToStake(t *testing.T) {
require.Equal(test.amount, dLog.Amount.String())

// test bucket index and bucket
bucketIndices, _, err := csr.(*candSR).CandBucketIndices(candidate.Owner)
bucketIndices, _, err := csr.candBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
bucketIndices, _, err = csr.(*candSR).VoterBucketIndices(candidate.Owner)
bucketIndices, _, err = csr.voterBucketIndices(candidate.Owner)
require.NoError(err)
require.Equal(1, len(*bucketIndices))
indices := *bucketIndices
Expand Down

0 comments on commit a1bc494

Please sign in to comment.