forked from PlatONnetwork/PlatON-Go
/
delegate_reward_contract.go
147 lines (120 loc) · 5.27 KB
/
delegate_reward_contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package vm
import (
"fmt"
"math/big"
"github.com/PlatONnetwork/PlatON-Go/x/reward"
"github.com/PlatONnetwork/PlatON-Go/x/xutil"
"github.com/PlatONnetwork/PlatON-Go/common/vm"
"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/log"
"github.com/PlatONnetwork/PlatON-Go/p2p/discover"
"github.com/PlatONnetwork/PlatON-Go/params"
"github.com/PlatONnetwork/PlatON-Go/x/plugin"
)
const (
TxWithdrawDelegateReward = 5000
FuncNameWithdrawDelegateReward = "WithdrawDelegateReward"
QueryDelegateReward = 5100
FuncNameDelegateReward = "QueryDelegateReward"
)
type DelegateRewardContract struct {
Plugin *plugin.RewardMgrPlugin
stkPlugin *plugin.StakingPlugin
Contract *Contract
Evm *EVM
}
func (rc *DelegateRewardContract) RequiredGas(input []byte) uint64 {
return params.DelegateRewardGas
}
func (rc *DelegateRewardContract) Run(input []byte) ([]byte, error) {
return execPlatonContract(input, rc.FnSigns())
}
func (rc *DelegateRewardContract) FnSigns() map[uint16]interface{} {
return map[uint16]interface{}{
// Set
TxWithdrawDelegateReward: rc.withdrawDelegateReward,
// Get
QueryDelegateReward: rc.getDelegateReward,
}
}
func (rc *DelegateRewardContract) CheckGasPrice(gasPrice *big.Int, fcode uint16) error {
return nil
}
func (rc *DelegateRewardContract) withdrawDelegateReward() ([]byte, error) {
from := rc.Contract.CallerAddress
txHash := rc.Evm.StateDB.TxHash()
blockNum := rc.Evm.BlockNumber
blockHash := rc.Evm.BlockHash
state := rc.Evm.StateDB
log.Debug("Call withdrawDelegateReward of DelegateRewardContract", "blockNumber", blockNum.Uint64(),
"blockHash", blockHash.TerminalString(), "txHash", txHash.Hex(), "from", from, "gas", rc.Contract.Gas)
if !rc.Contract.UseGas(params.WithdrawDelegateRewardGas) {
return nil, ErrOutOfGas
}
list, err := rc.stkPlugin.GetDelegatesInfo(blockHash, from)
if err != nil {
return txResultHandler(vm.DelegateRewardPoolAddr, rc.Evm, "withdrawDelegateReward", "",
TxWithdrawDelegateReward, int(common.InternalError.Code)), err
}
if len(list) == 0 {
log.Debug("Call withdrawDelegateReward of DelegateRewardContract,the delegates info list is empty", "blockNumber", blockNum.Uint64(),
"blockHash", blockHash.TerminalString(), "txHash", txHash.Hex(), "from", from.String())
return txResultHandler(vm.DelegateRewardPoolAddr, rc.Evm, FuncNameWithdrawDelegateReward, reward.ErrDelegationNotFound.Msg, TxWithdrawDelegateReward, int(reward.ErrDelegationNotFound.Code)), nil
}
if !rc.Contract.UseGas(params.WithdrawDelegateNodeGas * uint64(len(list))) {
return nil, ErrOutOfGas
}
currentEpoch := xutil.CalculateEpoch(blockNum.Uint64())
unCalEpoch := 0
delegationInfoWithRewardPerList := make([]*plugin.DelegationInfoWithRewardPerList, 0)
for _, stakingNode := range list {
delegateRewardPerList, err := rc.Plugin.GetDelegateRewardPerList(blockHash, stakingNode.NodeID, stakingNode.StakeBlockNumber, uint64(stakingNode.Delegation.DelegateEpoch), currentEpoch-1)
if err != nil {
log.Error("Failed to withdrawDelegateReward",
"txHash", txHash.Hex(), "blockNumber", blockNum, "err", err)
return nil, err
}
if len(delegateRewardPerList) > 0 {
// the begin of delegation have not reward
if stakingNode.Delegation.Released.Cmp(common.Big0) == 0 && stakingNode.Delegation.RestrictingPlan.Cmp(common.Big0) == 0 && uint64(stakingNode.Delegation.DelegateEpoch) == delegateRewardPerList[0].Epoch {
delegateRewardPerList = delegateRewardPerList[1:]
}
}
unCalEpoch += len(delegateRewardPerList)
delegationInfoWithRewardPerList = append(delegationInfoWithRewardPerList, plugin.NewDelegationInfoWithRewardPerList(stakingNode, delegateRewardPerList))
}
if !rc.Contract.UseGas(params.WithdrawDelegateEpochGas * uint64(unCalEpoch)) {
return nil, ErrOutOfGas
}
if txHash == common.ZeroHash {
return nil, nil
}
reward, err := rc.Plugin.WithdrawDelegateReward(blockHash, blockNum.Uint64(), from, delegationInfoWithRewardPerList, state)
if err != nil {
if bizErr, ok := err.(*common.BizError); ok {
return txResultHandler(vm.DelegateRewardPoolAddr, rc.Evm, FuncNameWithdrawDelegateReward,
bizErr.Error(), TxWithdrawDelegateReward, int(bizErr.Code)), nil
} else {
log.Error("Failed to withdraw delegateReward ", "txHash", txHash,
"blockNumber", blockNum, "err", err, "account", from)
return nil, err
}
}
return txResultHandlerWithRes(vm.DelegateRewardPoolAddr, rc.Evm, FuncNameWithdrawDelegateReward, "", TxWithdrawDelegateReward, int(common.NoErr.Code), reward), nil
}
func (rc *DelegateRewardContract) getDelegateReward(address common.Address, nodeIDs []discover.NodeID) ([]byte, error) {
state := rc.Evm.StateDB
blockNum := rc.Evm.BlockNumber
blockHash := rc.Evm.BlockHash
res, err := rc.Plugin.GetDelegateReward(blockHash, blockNum.Uint64(), address, nodeIDs, state)
if err != nil {
if err == reward.ErrDelegationNotFound {
return callResultHandler(rc.Evm, fmt.Sprintf("getDelegateReward, account: %s", address.String()),
res, reward.ErrDelegationNotFound), nil
}
return callResultHandler(rc.Evm, fmt.Sprintf("getDelegateReward, account: %s", address.String()),
res, common.InternalError.Wrap(err.Error())), nil
}
return callResultHandler(rc.Evm, fmt.Sprintf("getDelegateReward, account: %s", address.String()),
res, nil), nil
}