@@ -75,38 +75,44 @@ func (p *Protocol) Handle(
75
75
case * action.SetReward :
76
76
switch act .RewardType () {
77
77
case action .BlockReward :
78
+ si := sm .Snapshot ()
78
79
if err := p .SetBlockReward (ctx , sm , act .Amount ()); err != nil {
79
- return p .settleAction (ctx , sm , 1 ), nil
80
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
80
81
}
81
- return p .settleAction (ctx , sm , 0 ), nil
82
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
82
83
case action .EpochReward :
84
+ si := sm .Snapshot ()
83
85
if err := p .SetEpochReward (ctx , sm , act .Amount ()); err != nil {
84
- return p .settleAction (ctx , sm , 1 ), nil
86
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
85
87
}
86
- return p .settleAction (ctx , sm , 0 ), nil
88
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
87
89
}
88
90
case * action.DepositToRewardingFund :
91
+ si := sm .Snapshot ()
89
92
if err := p .Deposit (ctx , sm , act .Amount ()); err != nil {
90
- return p .settleAction (ctx , sm , 1 ), nil
93
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
91
94
}
92
- return p .settleAction (ctx , sm , 0 ), nil
95
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
93
96
case * action.ClaimFromRewardingFund :
97
+ si := sm .Snapshot ()
94
98
if err := p .Claim (ctx , sm , act .Amount ()); err != nil {
95
- return p .settleAction (ctx , sm , 1 ), nil
99
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
96
100
}
97
- return p .settleAction (ctx , sm , 0 ), nil
101
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
98
102
case * action.GrantReward :
99
103
switch act .RewardType () {
100
104
case action .BlockReward :
105
+ si := sm .Snapshot ()
101
106
if err := p .GrantBlockReward (ctx , sm ); err != nil {
102
- return p .settleAction (ctx , sm , 1 ), nil
107
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
103
108
}
104
- return p .settleAction (ctx , sm , 0 ), nil
109
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
105
110
case action .EpochReward :
111
+ si := sm .Snapshot ()
106
112
if err := p .GrantEpochReward (ctx , sm ); err != nil {
107
- return p .settleAction (ctx , sm , 1 ), nil
113
+ return p .settleAction (ctx , sm , action . FailureReceiptStatus , si )
108
114
}
109
- return p .settleAction (ctx , sm , 0 ), nil
115
+ return p .settleAction (ctx , sm , action . SuccessReceiptStatus , si )
110
116
}
111
117
}
112
118
return nil , nil
@@ -166,16 +172,22 @@ func (p *Protocol) settleAction(
166
172
ctx context.Context ,
167
173
sm protocol.StateManager ,
168
174
status uint64 ,
169
- ) * action.Receipt {
175
+ si int ,
176
+ ) (* action.Receipt , error ) {
170
177
raCtx := protocol .MustGetRunActionsCtx (ctx )
178
+ if status == action .FailureReceiptStatus {
179
+ if err := sm .Revert (si ); err != nil {
180
+ return nil , err
181
+ }
182
+ }
171
183
gasFee := big .NewInt (0 ).Mul (raCtx .GasPrice , big .NewInt (0 ).SetUint64 (raCtx .IntrinsicGas ))
172
184
if err := DepositGas (ctx , sm , gasFee , raCtx .Registry ); err != nil {
173
- p . createReceipt ( 1 , raCtx . ActionHash , raCtx . IntrinsicGas )
185
+ return nil , err
174
186
}
175
187
if err := p .increaseNonce (sm , raCtx .Caller , raCtx .Nonce ); err != nil {
176
- return p . createReceipt ( 1 , raCtx . ActionHash , raCtx . IntrinsicGas )
188
+ return nil , err
177
189
}
178
- return p .createReceipt (status , raCtx .ActionHash , raCtx .IntrinsicGas )
190
+ return p .createReceipt (status , raCtx .ActionHash , raCtx .IntrinsicGas ), nil
179
191
}
180
192
181
193
func (p * Protocol ) increaseNonce (sm protocol.StateManager , addr address.Address , nonce uint64 ) error {
@@ -194,7 +206,7 @@ func (p *Protocol) createReceipt(status uint64, actHash hash.Hash256, gasConsume
194
206
// TODO: need to review the fields
195
207
return & action.Receipt {
196
208
ReturnValue : nil ,
197
- Status : 0 ,
209
+ Status : status ,
198
210
ActHash : actHash ,
199
211
GasConsumed : gasConsumed ,
200
212
ContractAddress : p .addr .String (),
0 commit comments