-
Notifications
You must be signed in to change notification settings - Fork 183
/
handler.go
233 lines (201 loc) · 7.27 KB
/
handler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package feesplit
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
sdkerrors "github.com/okex/exchain/libs/cosmos-sdk/types/errors"
tmtypes "github.com/okex/exchain/libs/tendermint/types"
"github.com/okex/exchain/x/feesplit/keeper"
"github.com/okex/exchain/x/feesplit/types"
)
// NewHandler defines the fees module handler instance
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx.SetEventManager(sdk.NewEventManager())
if !tmtypes.HigherThanVenus3(ctx.BlockHeight()) {
errMsg := fmt.Sprintf("feesplt module not support at height %d", ctx.BlockHeight())
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
params := k.GetParams(ctx)
if !params.EnableFeeSplit {
return nil, types.ErrFeeSplitDisabled
}
switch msg := msg.(type) {
case types.MsgRegisterFeeSplit:
return handleMsgRegisterFeeSplit(ctx, msg, k, params)
case types.MsgUpdateFeeSplit:
return handleMsgUpdateFeeSplit(ctx, msg, k)
case types.MsgCancelFeeSplit:
return handleMsgCancelFeeSplit(ctx, msg, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
}
}
}
// handleMsgRegisterFeeSplit registers a contract to receive transaction fees
func handleMsgRegisterFeeSplit(
ctx sdk.Context,
msg types.MsgRegisterFeeSplit,
k keeper.Keeper,
params types.Params,
) (*sdk.Result, error) {
contract := common.HexToAddress(msg.ContractAddress)
if k.IsFeeSplitRegistered(ctx, contract) {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitAlreadyRegistered,
"contract is already registered %s", contract,
)
}
deployer := sdk.MustAccAddressFromBech32(msg.DeployerAddress)
deployerAccount, isExist := k.GetEthAccount(ctx, common.BytesToAddress(deployer))
if !isExist {
return nil, sdkerrors.Wrapf(
types.ErrFeeAccountNotFound,
"deployer account not found %s", msg.DeployerAddress,
)
}
if deployerAccount != nil && deployerAccount.IsContract() {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitDeployerIsNotEOA,
"deployer cannot be a contract %s", msg.DeployerAddress,
)
}
// contract must already be deployed, to avoid spam registrations
contractAccount, _ := k.GetEthAccount(ctx, contract)
if contractAccount == nil || !contractAccount.IsContract() {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitNoContractDeployed,
"no contract code found at address %s", msg.ContractAddress,
)
}
if msg.WithdrawerAddress == "" {
msg.WithdrawerAddress = msg.DeployerAddress
}
withdrawer := sdk.MustAccAddressFromBech32(msg.WithdrawerAddress)
derivedContract := common.BytesToAddress(deployer)
// the contract can be directly deployed by an EOA or created through one
// or more factory contracts. If it was deployed by an EOA account, then
// msg.Nonces contains the EOA nonce for the deployment transaction.
// If it was deployed by one or more factories, msg.Nonces contains the EOA
// nonce for the origin factory contract, then the nonce of the factory
// for the creation of the next factory/contract.
for _, nonce := range msg.Nonces {
ctx.GasMeter().ConsumeGas(
params.AddrDerivationCostCreate,
"fee split registration: address derivation CREATE opcode",
)
derivedContract = crypto.CreateAddress(derivedContract, nonce)
}
if contract != derivedContract {
return nil, sdkerrors.Wrapf(
types.ErrDerivedNotMatched,
"not contract deployer or wrong nonce: expected %s instead of %s",
derivedContract, msg.ContractAddress,
)
}
// prevent storing the same address for deployer and withdrawer
feeSplit := types.NewFeeSplit(contract, deployer, withdrawer)
k.SetFeeSplit(ctx, feeSplit)
k.SetDeployerMap(ctx, deployer, contract)
k.SetWithdrawerMap(ctx, withdrawer, contract)
k.Logger(ctx).Debug(
"registering contract for transaction fees",
"contract", msg.ContractAddress, "deployer", msg.DeployerAddress,
"withdraw", msg.WithdrawerAddress,
)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterFeeSplit,
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
sdk.NewAttribute(types.AttributeKeyWithdrawerAddress, msg.WithdrawerAddress),
),
},
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
// handleMsgUpdateFeeSplit updates the withdraw address of a given FeeSplit. If the given
// withdraw address is empty or the same as the deployer address, the withdraw
// address is removed.
func handleMsgUpdateFeeSplit(
ctx sdk.Context,
msg types.MsgUpdateFeeSplit,
k keeper.Keeper,
) (*sdk.Result, error) {
contract := common.HexToAddress(msg.ContractAddress)
feeSplit, found := k.GetFeeSplit(ctx, contract)
if !found {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitContractNotRegistered,
"contract %s is not registered", msg.ContractAddress,
)
}
// error if the msg deployer address is not the same as the fee's deployer
if !sdk.MustAccAddressFromBech32(msg.DeployerAddress).Equals(feeSplit.DeployerAddress) {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrUnauthorized,
"%s is not the contract deployer", msg.DeployerAddress,
)
}
var withdrawer sdk.AccAddress
withdrawer = sdk.MustAccAddressFromBech32(msg.WithdrawerAddress)
// fee split with the given withdraw address is already registered
if withdrawer.Equals(feeSplit.WithdrawerAddress) {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitAlreadyRegistered,
"fee split with withdraw address %s", msg.WithdrawerAddress,
)
}
k.DeleteWithdrawerMap(ctx, feeSplit.WithdrawerAddress, contract)
k.SetWithdrawerMap(ctx, withdrawer, contract)
// update fee split
feeSplit.WithdrawerAddress = withdrawer
k.SetFeeSplit(ctx, feeSplit)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeUpdateFeeSplit,
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyWithdrawerAddress, msg.WithdrawerAddress),
),
},
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
// handleMsgCancelFeeSplit deletes the FeeSplit for a given contract
func handleMsgCancelFeeSplit(
ctx sdk.Context,
msg types.MsgCancelFeeSplit,
k keeper.Keeper,
) (*sdk.Result, error) {
contract := common.HexToAddress(msg.ContractAddress)
fee, found := k.GetFeeSplit(ctx, contract)
if !found {
return nil, sdkerrors.Wrapf(
types.ErrFeeSplitContractNotRegistered,
"contract %s is not registered", msg.ContractAddress,
)
}
if !sdk.MustAccAddressFromBech32(msg.DeployerAddress).Equals(fee.DeployerAddress) {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrUnauthorized,
"%s is not the contract deployer", msg.DeployerAddress,
)
}
k.DeleteFeeSplit(ctx, fee)
k.DeleteDeployerMap(ctx, fee.DeployerAddress, contract)
k.DeleteWithdrawerMap(ctx, fee.WithdrawerAddress, contract)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeCancelFeeSplit,
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
),
},
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}