Skip to content

Commit

Permalink
[staking] unexport namespace (#3551)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Jul 19, 2022
1 parent 2ed5b03 commit c216396
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 50 deletions.
10 changes: 5 additions & 5 deletions action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

const (
// TODO: it works only for one instance per protocol definition now
_protocolID = "rewarding"
_v2Namespace = "Rewarding"
_protocolID = "rewarding"
_v2RewardingNamespace = "Rewarding"
)

var (
Expand Down Expand Up @@ -332,7 +332,7 @@ func (p *Protocol) stateV1(sm protocol.StateReader, key []byte, value interface{

func (p *Protocol) stateV2(sm protocol.StateReader, key []byte, value interface{}) (uint64, error) {
k := append(p.keyPrefix, key...)
return sm.State(value, protocol.KeyOption(k), protocol.NamespaceOption(_v2Namespace))
return sm.State(value, protocol.KeyOption(k), protocol.NamespaceOption(_v2RewardingNamespace))
}

func (p *Protocol) putState(ctx context.Context, sm protocol.StateManager, key []byte, value interface{}) error {
Expand All @@ -350,7 +350,7 @@ func (p *Protocol) putStateV1(sm protocol.StateManager, key []byte, value interf

func (p *Protocol) putStateV2(sm protocol.StateManager, key []byte, value interface{}) error {
k := append(p.keyPrefix, key...)
_, err := sm.PutState(value, protocol.KeyOption(k), protocol.NamespaceOption(_v2Namespace))
_, err := sm.PutState(value, protocol.KeyOption(k), protocol.NamespaceOption(_v2RewardingNamespace))
return err
}

Expand All @@ -373,7 +373,7 @@ func (p *Protocol) deleteStateV1(sm protocol.StateManager, key []byte) error {

func (p *Protocol) deleteStateV2(sm protocol.StateManager, key []byte) error {
k := append(p.keyPrefix, key...)
_, err := sm.DelState(protocol.KeyOption(k), protocol.NamespaceOption(_v2Namespace))
_, err := sm.DelState(protocol.KeyOption(k), protocol.NamespaceOption(_v2RewardingNamespace))
if errors.Cause(err) == state.ErrStateNotExist {
// don't care if not exist
return nil
Expand Down
6 changes: 3 additions & 3 deletions action/protocol/staking/bucket_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (bp *BucketPool) Copy(enableSMStorage bool) *BucketPool {
// Sync syncs the data from state manager
func (bp *BucketPool) Sync(sm protocol.StateManager) error {
if bp.enableSMStorage {
_, err := sm.State(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err := sm.State(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
// get stashed total amount
Expand All @@ -138,7 +138,7 @@ func (bp *BucketPool) CreditPool(sm protocol.StateManager, amount *big.Int) erro
}

if bp.enableSMStorage {
_, err := sm.PutState(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err := sm.PutState(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
return sm.Load(_protocolID, _stakingBucketPool, bp.total)
Expand All @@ -148,7 +148,7 @@ func (bp *BucketPool) CreditPool(sm protocol.StateManager, amount *big.Int) erro
func (bp *BucketPool) DebitPool(sm protocol.StateManager, amount *big.Int, newBucket bool) error {
bp.total.AddBalance(amount, newBucket)
if bp.enableSMStorage {
_, err := sm.PutState(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err := sm.PutState(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
return sm.Load(_protocolID, _stakingBucketPool, bp.total)
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/bucket_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ func TestBucketPool(t *testing.T) {
}

if !testGreenland && v.postGreenland {
_, err = sm.PutState(c.BaseView().bucketPool.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err = sm.PutState(c.BaseView().bucketPool.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
r.NoError(err)
testGreenland = true
}
}

// verify state has been created successfully
var b totalAmount
_, err = sm.State(&b, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err = sm.State(&b, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
r.NoError(err)
r.Equal(total, b.amount)
r.Equal(count, b.count)
Expand Down
24 changes: 12 additions & 12 deletions action/protocol/staking/candidate_statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (csm *candSM) updateBucket(index uint64, bucket *VoteBucket) error {

_, err := csm.PutState(
bucket,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(bucketKey(index)))
return err
}
Expand All @@ -205,7 +205,7 @@ func (csm *candSM) putBucket(bucket *VoteBucket) (uint64, error) {
var tc totalBucketCount
if _, err := csm.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return 0, err
}
Expand All @@ -215,21 +215,21 @@ func (csm *candSM) putBucket(bucket *VoteBucket) (uint64, error) {
bucket.Index = index
if _, err := csm.PutState(
bucket,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(bucketKey(index))); err != nil {
return 0, err
}
tc.count++
_, err := csm.PutState(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey))
return index, err
}

func (csm *candSM) delBucket(index uint64) error {
_, err := csm.DelState(
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(bucketKey(index)))
return err
}
Expand Down Expand Up @@ -272,14 +272,14 @@ func (csm *candSM) putBucketIndex(addr address.Address, prefix byte, index uint6
)
if _, err := csm.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return err
}
bis.addBucketIndex(index)
_, err := csm.PutState(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key))
return err
}
Expand All @@ -295,7 +295,7 @@ func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint6
)
if _, err := csm.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key)); err != nil {
return err
}
Expand All @@ -304,12 +304,12 @@ func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint6
var err error
if len(bis) == 0 {
_, err = csm.DelState(
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key))
} else {
_, err = csm.PutState(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key))
}
return err
Expand All @@ -320,7 +320,7 @@ func (csm *candSM) delVoterBucketIndex(addr address.Address, index uint64) error
}

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

Expand All @@ -329,7 +329,7 @@ func (csm *candSM) putCandBucketIndex(addr address.Address, index uint64) error
}

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

Expand Down
16 changes: 8 additions & 8 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *candSR) getTotalBucketCount() (uint64, error) {
var tc totalBucketCount
_, err := c.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey))
return tc.count, err
}
Expand All @@ -215,14 +215,14 @@ func (c *candSR) getBucket(index uint64) (*VoteBucket, error) {
)
if _, err = c.State(
&vb,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(bucketKey(index))); err != nil {
return nil, err
}
var tc totalBucketCount
if _, err := c.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return nil, err
}
Expand All @@ -234,7 +234,7 @@ func (c *candSR) getBucket(index uint64) (*VoteBucket, error) {

func (c *candSR) getAllBuckets() ([]*VoteBucket, uint64, error) {
height, iter, err := c.States(
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeysOption(func() ([][]byte, error) {
// TODO (zhi): fix potential racing issue
count, err := c.getTotalBucketCount()
Expand Down Expand Up @@ -285,7 +285,7 @@ func (c *candSR) getBucketIndices(addr address.Address, prefix byte) (*BucketInd
)
height, err := c.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key))
if err != nil {
return nil, height, err
Expand All @@ -306,12 +306,12 @@ func (c *candSR) getCandidate(name address.Address) (*Candidate, uint64, error)
return nil, 0, ErrNilParameters
}
var d Candidate
height, err := c.State(&d, protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(name.Bytes()))
height, err := c.State(&d, protocol.NamespaceOption(_candidateNameSpace), protocol.KeyOption(name.Bytes()))
return &d, height, err
}

func (c *candSR) getAllCandidates() (CandidateList, uint64, error) {
height, iter, err := c.States(protocol.NamespaceOption(CandidateNameSpace))
height, iter, err := c.States(protocol.NamespaceOption(_candidateNameSpace))
if err != nil {
return nil, height, err
}
Expand All @@ -336,7 +336,7 @@ func (c *candSR) NewBucketPool(enableSMStorage bool) (*BucketPool, error) {
}

if bp.enableSMStorage {
switch _, err := c.State(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey)); errors.Cause(err) {
switch _, err := c.State(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey)); errors.Cause(err) {
case nil:
return &bp, nil
case state.ErrStateNotExist:
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
csr := newCandidateStateReader(sm)
_, err := sm.PutState(
&totalBucketCount{count: 0},
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey),
)
require.NoError(err)
Expand Down Expand Up @@ -2658,7 +2658,7 @@ func initAll(t *testing.T, ctrl *gomock.Controller) (protocol.StateManager, *Pro
csm := newCandidateStateManager(sm)
_, err := sm.PutState(
&totalBucketCount{count: 0},
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey),
)
require.NoError(err)
Expand Down
10 changes: 5 additions & 5 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const (
// _protocolID is the protocol ID
_protocolID = "staking"

// StakingNameSpace is the bucket name for staking state
StakingNameSpace = "Staking"
// _stakingNameSpace is the bucket name for staking state
_stakingNameSpace = "Staking"

// CandidateNameSpace is the bucket name for candidate state
CandidateNameSpace = "Candidate"
// _candidateNameSpace is the bucket name for candidate state
_candidateNameSpace = "Candidate"
)

const (
Expand Down Expand Up @@ -239,7 +239,7 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
if err != nil {
return err
}
if _, err = sm.PutState(csr.BaseView().bucketPool.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey)); err != nil {
if _, err = sm.PutState(csr.BaseView().bucketPool.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey)); err != nil {
return err
}
}
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestProtocol(t *testing.T) {
csmTemp := newCandidateStateManager(sm)
_, err := sm.PutState(
&totalBucketCount{count: 0},
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey),
)
r.NoError(err)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestCreatePreStates(t *testing.T) {
_, err = NewCandidateStateManager(sm, true)
require.Error(err)
require.NoError(p.CreatePreStates(ctx, sm))
_, err = sm.State(nil, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err = sm.State(nil, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
require.EqualError(errors.Cause(err), state.ErrStateNotExist.Error())
ctx = protocol.WithBlockCtx(
ctx,
Expand All @@ -216,7 +216,7 @@ func TestCreatePreStates(t *testing.T) {
},
)
require.NoError(p.CreatePreStates(ctx, sm))
_, err = sm.State(nil, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err = sm.State(nil, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
require.EqualError(errors.Cause(err), state.ErrStateNotExist.Error())
ctx = protocol.WithBlockCtx(
ctx,
Expand All @@ -226,7 +226,7 @@ func TestCreatePreStates(t *testing.T) {
)
require.NoError(p.CreatePreStates(ctx, sm))
total := &totalAmount{}
_, err = sm.State(total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
_, err = sm.State(total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
require.NoError(err)
}

Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/read_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func getTotalStakedAmount(ctx context.Context, csr CandidateStateReader) (*big.I
if featureCtx.ReadStateFromDB(csr.Height()) {
// after Greenland, read state from db
var total totalAmount
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
h, err := csr.SR().State(&total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
if err != nil {
return nil, h, err
}
Expand All @@ -92,7 +92,7 @@ func getActiveBucketsCount(ctx context.Context, csr CandidateStateReader) (uint6
if featureCtx.ReadStateFromDB(csr.Height()) {
// after Greenland, read state from db
var total totalAmount
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
h, err := csr.SR().State(&total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
if err != nil {
return 0, h, err
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/vote_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGetPutStaking(t *testing.T) {
csr := newCandidateStateReader(sm)
sm.PutState(
&totalBucketCount{count: 0},
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey),
)

Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/vote_reviser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestVoteReviser(t *testing.T) {
csr := newCandidateStateReader(sm)
_, err := sm.PutState(
&totalBucketCount{count: 0},
protocol.NamespaceOption(StakingNameSpace),
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(TotalBucketKey),
)
r.NoError(err)
Expand Down
Loading

0 comments on commit c216396

Please sign in to comment.