Skip to content

Commit

Permalink
Remove admin and its operations from rewarding protocol (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjshen14 committed Mar 14, 2019
1 parent 24abf03 commit 4d6efae
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 637 deletions.
8 changes: 0 additions & 8 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ func (elp *Envelope) Proto() *iotextypes.ActionCore {
actCore.Action = &iotextypes.ActionCore_SettleDeposit{SettleDeposit: act.Proto()}
case *GrantReward:
actCore.Action = &iotextypes.ActionCore_GrantReward{GrantReward: act.Proto()}
case *SetReward:
actCore.Action = &iotextypes.ActionCore_SetReward{SetReward: act.Proto()}
case *ClaimFromRewardingFund:
actCore.Action = &iotextypes.ActionCore_ClaimFromRewardingFund{ClaimFromRewardingFund: act.Proto()}
case *DepositToRewardingFund:
Expand Down Expand Up @@ -219,12 +217,6 @@ func (elp *Envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
return err
}
elp.payload = act
case pbAct.GetSetReward() != nil:
act := &SetReward{}
if err := act.LoadProto(pbAct.GetSetReward()); err != nil {
return err
}
elp.payload = act
case pbAct.GetClaimFromRewardingFund() != nil:
act := &ClaimFromRewardingFund{}
if err := act.LoadProto(pbAct.GetClaimFromRewardingFund()); err != nil {
Expand Down
112 changes: 0 additions & 112 deletions action/protocol/rewarding/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package rewarding

import (
"bytes"
"context"
"math/big"

Expand All @@ -21,7 +20,6 @@ import (

// admin stores the admin data of the rewarding protocol
type admin struct {
admin address.Address
blockReward *big.Int
epochReward *big.Int
numDelegatesForEpochReward uint64
Expand All @@ -30,7 +28,6 @@ type admin struct {
// Serialize serializes admin state into bytes
func (a admin) Serialize() ([]byte, error) {
gen := rewardingpb.Admin{
Admin: a.admin.Bytes(),
BlockReward: a.blockReward.String(),
EpochReward: a.epochReward.String(),
NumDelegatesForEpochReward: a.numDelegatesForEpochReward,
Expand All @@ -44,10 +41,6 @@ func (a *admin) Deserialize(data []byte) error {
if err := proto.Unmarshal(data, &gen); err != nil {
return err
}
var err error
if a.admin, err = address.FromBytes(gen.Admin); err != nil {
return err
}
blockReward, ok := big.NewInt(0).SetString(gen.BlockReward, 10)
if !ok {
return errors.New("failed to set block reward")
Expand Down Expand Up @@ -97,7 +90,6 @@ func (e *exempt) Deserialize(data []byte) error {
func (p *Protocol) Initialize(
ctx context.Context,
sm protocol.StateManager,
adminAddr address.Address,
initBalance *big.Int,
blockReward *big.Int,
epochReward *big.Int,
Expand All @@ -118,7 +110,6 @@ func (p *Protocol) Initialize(
sm,
adminKey,
&admin{
admin: adminAddr,
blockReward: blockReward,
epochReward: epochReward,
numDelegatesForEpochReward: numDelegatesForEpochReward,
Expand All @@ -145,36 +136,6 @@ func (p *Protocol) Initialize(
)
}

// Admin returns the address of current admin
func (p *Protocol) Admin(
_ context.Context,
sm protocol.StateManager,
) (address.Address, error) {
admin := admin{}
if err := p.state(sm, adminKey, &admin); err != nil {
return nil, err
}
return admin.admin, nil
}

// SetAdmin sets a new admin address. Only the current admin could make this change
func (p *Protocol) SetAdmin(
ctx context.Context,
sm protocol.StateManager,
addr address.Address,
) error {
raCtx := protocol.MustGetRunActionsCtx(ctx)
if err := p.assertAdminPermission(raCtx, sm); err != nil {
return err
}
a := admin{}
if err := p.state(sm, adminKey, &a); err != nil {
return err
}
a.admin = addr
return p.putState(sm, adminKey, &a)
}

// BlockReward returns the block reward amount
func (p *Protocol) BlockReward(
_ context.Context,
Expand All @@ -187,15 +148,6 @@ func (p *Protocol) BlockReward(
return a.blockReward, nil
}

// SetBlockReward sets the block reward amount for the block rewarding. Only the current admin could make this change
func (p *Protocol) SetBlockReward(
ctx context.Context,
sm protocol.StateManager,
amount *big.Int,
) error {
return p.setReward(ctx, sm, amount, true)
}

// EpochReward returns the epoch reward amount
func (p *Protocol) EpochReward(
_ context.Context,
Expand All @@ -208,16 +160,6 @@ func (p *Protocol) EpochReward(
return a.epochReward, nil
}

// SetEpochReward sets the epoch reward amount shared by all beneficiaries in an epoch. Only the current admin could
// make this change
func (p *Protocol) SetEpochReward(
ctx context.Context,
sm protocol.StateManager,
amount *big.Int,
) error {
return p.setReward(ctx, sm, amount, false)
}

// NumDelegatesForEpochReward returns the number of candidates sharing an epoch reward
func (p *Protocol) NumDelegatesForEpochReward(
_ context.Context,
Expand All @@ -230,70 +172,16 @@ func (p *Protocol) NumDelegatesForEpochReward(
return a.numDelegatesForEpochReward, nil
}

// SetNumDelegatesForEpochReward sets the number of candidates sharing an epoch reward
func (p *Protocol) SetNumDelegatesForEpochReward(
ctx context.Context,
sm protocol.StateManager,
num uint64,
) error {
raCtx := protocol.MustGetRunActionsCtx(ctx)
if err := p.assertAdminPermission(raCtx, sm); err != nil {
return err
}
a := admin{}
if err := p.state(sm, adminKey, &a); err != nil {
return err
}
a.numDelegatesForEpochReward = num
return p.putState(sm, adminKey, &a)
}

func (p *Protocol) assertAmount(amount *big.Int) error {
if amount.Cmp(big.NewInt(0)) >= 0 {
return nil
}
return errors.Errorf("reward amount %s shouldn't be negative", amount.String())
}

func (p *Protocol) assertAdminPermission(raCtx protocol.RunActionsCtx, sm protocol.StateManager) error {
a := admin{}
if err := p.state(sm, adminKey, &a); err != nil {
return err
}
if bytes.Equal(a.admin.Bytes(), raCtx.Caller.Bytes()) {
return nil
}
return errors.Errorf("%s is not the rewarding protocol admin", raCtx.Caller.String())
}

func (p *Protocol) assertZeroBlockHeight(height uint64) error {
if height != 0 {
return errors.Errorf("current block height %d is not zero", height)
}
return nil
}

func (p *Protocol) setReward(
ctx context.Context,
sm protocol.StateManager,
amount *big.Int,
blockLevel bool,
) error {
raCtx := protocol.MustGetRunActionsCtx(ctx)
if err := p.assertAdminPermission(raCtx, sm); err != nil {
return err
}
if err := p.assertAmount(amount); err != nil {
return err
}
a := admin{}
if err := p.state(sm, adminKey, &a); err != nil {
return err
}
if blockLevel {
a.blockReward = amount
} else {
a.epochReward = amount
}
return p.putState(sm, adminKey, &a)
}
121 changes: 0 additions & 121 deletions action/protocol/rewarding/admin_test.go

This file was deleted.

15 changes: 0 additions & 15 deletions action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ func (p *Protocol) Handle(
) (*action.Receipt, error) {
// TODO: simplify the boilerplate
switch act := act.(type) {
case *action.SetReward:
switch act.RewardType() {
case action.BlockReward:
si := sm.Snapshot()
if err := p.SetBlockReward(ctx, sm, act.Amount()); err != nil {
return p.settleAction(ctx, sm, action.FailureReceiptStatus, si)
}
return p.settleAction(ctx, sm, action.SuccessReceiptStatus, si)
case action.EpochReward:
si := sm.Snapshot()
if err := p.SetEpochReward(ctx, sm, act.Amount()); err != nil {
return p.settleAction(ctx, sm, action.FailureReceiptStatus, si)
}
return p.settleAction(ctx, sm, action.SuccessReceiptStatus, si)
}
case *action.DepositToRewardingFund:
si := sm.Snapshot()
if err := p.Deposit(ctx, sm, act.Amount()); err != nil {
Expand Down
6 changes: 0 additions & 6 deletions action/protocol/rewarding/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func testProtocol(t *testing.T, test func(*testing.T, context.Context, factory.F
p.Initialize(
ctx,
ws,
testaddress.Addrinfo["alfa"],
big.NewInt(0),
big.NewInt(10),
big.NewInt(100),
Expand All @@ -110,7 +109,6 @@ func testProtocol(t *testing.T, test func(*testing.T, context.Context, factory.F
p.Initialize(
ctx,
ws,
testaddress.Addrinfo["alfa"],
big.NewInt(0),
big.NewInt(10),
big.NewInt(100),
Expand All @@ -131,9 +129,6 @@ func testProtocol(t *testing.T, test func(*testing.T, context.Context, factory.F
)
ws, err = stateDB.NewWorkingSet()
require.NoError(t, err)
adminAddr, err := p.Admin(ctx, ws)
require.NoError(t, err)
assert.Equal(t, testaddress.Addrinfo["alfa"].Bytes(), adminAddr.Bytes())
blockReward, err := p.BlockReward(ctx, ws)
require.NoError(t, err)
assert.Equal(t, big.NewInt(10), blockReward)
Expand Down Expand Up @@ -188,7 +183,6 @@ func TestProtocol_Handle(t *testing.T) {
require.NoError(t, p.Initialize(
ctx,
ws,
identityset.Address(0),
big.NewInt(1000000),
big.NewInt(10),
big.NewInt(100),
Expand Down
Loading

0 comments on commit 4d6efae

Please sign in to comment.