-
Notifications
You must be signed in to change notification settings - Fork 39
/
context.go
301 lines (266 loc) · 7.91 KB
/
context.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package iiss
import (
"encoding/json"
"math/big"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/intconv"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/icon/icmodule"
"github.com/icon-project/goloop/icon/iiss/icutils"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service/contract"
"github.com/icon-project/goloop/service/scoredb"
"github.com/icon-project/goloop/service/scoreresult"
"github.com/icon-project/goloop/service/state"
"github.com/icon-project/goloop/service/trace"
)
func validateAmount(amount *big.Int) error {
if amount == nil || amount.Sign() < 0 {
return errors.Errorf("Invalid amount: %v", amount)
}
return nil
}
func setBalance(address module.Address, as state.AccountState, balance *big.Int) error {
if balance.Sign() < 0 {
return errors.Errorf(
"Invalid balance: address=%v balance=%v",
address, balance,
)
}
as.SetBalance(balance)
return nil
}
type worldContextImpl struct {
state.WorldContext
tlog *trace.Logger
}
func (ctx *worldContextImpl) Origin() module.Address {
return ctx.TransactionInfo().From
}
func (ctx *worldContextImpl) GetBalance(address module.Address) *big.Int {
account := ctx.GetAccountState(address.ID())
return account.GetBalance()
}
func (ctx *worldContextImpl) Logger() log.Logger {
return ctx.tlog
}
func (ctx *worldContextImpl) TraceLogger() *trace.Logger {
return ctx.tlog
}
func (ctx *worldContextImpl) onBalanceChange(opType module.OpType, from, to module.Address, amount *big.Int) {
if ctx.tlog != nil {
ctx.tlog.OnBalanceChange(opType, from, to, amount)
}
}
func (ctx *worldContextImpl) Deposit(address module.Address, amount *big.Int, opType module.OpType) error {
if err := validateAmount(amount); err != nil {
return err
}
if amount.Sign() == 0 {
return nil
}
err := ctx.addBalance(address, amount)
if err == nil {
ctx.onBalanceChange(opType, nil, address, amount)
}
return err
}
func (ctx *worldContextImpl) Withdraw(address module.Address, amount *big.Int, opType module.OpType) error {
if err := validateAmount(amount); err != nil {
return err
}
if amount.Sign() == 0 {
return nil
}
err := ctx.addBalance(address, new(big.Int).Neg(amount))
if err == nil {
ctx.onBalanceChange(opType, address, nil, amount)
}
return err
}
func (ctx *worldContextImpl) Transfer(
from module.Address, to module.Address, amount *big.Int, opType module.OpType) (err error) {
if err = validateAmount(amount); err != nil {
return
}
if amount.Sign() == 0 || from.Equal(to) {
return nil
}
// Subtract amount from the balance of "from" address
if err = ctx.addBalance(from, new(big.Int).Neg(amount)); err != nil {
return
}
// Add amount to "to" address
if err = ctx.addBalance(to, amount); err != nil {
return
}
ctx.onBalanceChange(opType, from, to, amount)
return
}
func (ctx *worldContextImpl) addBalance(address module.Address, amount *big.Int) error {
as := ctx.GetAccountState(address.ID())
ob := as.GetBalance()
return setBalance(address, as, new(big.Int).Add(ob, amount))
}
func (ctx *worldContextImpl) GetTotalSupply() *big.Int {
as := ctx.GetAccountState(state.SystemID)
tsVar := scoredb.NewVarDB(as, state.VarTotalSupply)
if ts := tsVar.BigInt(); ts != nil {
return ts
}
return icmodule.BigIntZero
}
func (ctx *worldContextImpl) AddTotalSupply(amount *big.Int) (*big.Int, error) {
as := ctx.GetAccountState(state.SystemID)
varDB := scoredb.NewVarDB(as, state.VarTotalSupply)
oldTs := varDB.BigInt()
if oldTs == nil {
oldTs = new(big.Int)
}
ts := new(big.Int).Add(oldTs, amount)
if ts.Sign() < 0 {
return nil, errors.Errorf("TotalSupply < 0")
}
return ts, varDB.Set(ts)
}
func (ctx *worldContextImpl) SetValidators(validators []module.Validator) error {
return ctx.GetValidatorState().Set(validators)
}
func (ctx *worldContextImpl) GetScoreOwner(score module.Address) (module.Address, error) {
if score == nil || !score.IsContract() {
return nil, scoreresult.InvalidParameterError.Errorf("Invalid score address")
}
as := ctx.GetAccountState(score.ID())
if icutils.IsNil(as) || !as.IsContract() {
return nil, scoreresult.InvalidParameterError.Errorf("Score not found")
}
return as.ContractOwner(), nil
}
func (ctx *worldContextImpl) SetScoreOwner(from module.Address, score module.Address, newOwner module.Address) error {
// Parameter sanity check
if from == nil {
return scoreresult.InvalidParameterError.Errorf("Invalid sender")
}
if score == nil {
return scoreresult.InvalidParameterError.Errorf("Invalid score address")
}
if !score.IsContract() {
return icmodule.IllegalArgumentError.Errorf("Invalid score address")
}
if newOwner == nil {
return scoreresult.InvalidParameterError.Errorf("Invalid owner")
}
as := ctx.GetAccountState(score.ID())
if icutils.IsNil(as) || !as.IsContract() {
return icmodule.IllegalArgumentError.Errorf("Score not found")
}
// Check if s.from is the owner of a given contract
owner := as.ContractOwner()
if owner == nil || !owner.Equal(from) {
return scoreresult.AccessDeniedError.Errorf("Invalid owner")
}
// Check if the score is active
if as.IsBlocked() {
return scoreresult.AccessDeniedError.Errorf("Blocked score")
}
if as.IsDisabled() {
return scoreresult.AccessDeniedError.Errorf("Disabled score")
}
return as.SetContractOwner(newOwner)
}
func (ctx *worldContextImpl) GetBTPContext() state.BTPContext {
as := ctx.GetAccountState(state.SystemID)
return state.NewBTPContext(ctx.WorldContext, as)
}
func NewWorldContext(ctx state.WorldContext, logger log.Logger) icmodule.WorldContext {
tlog := trace.LoggerOf(logger)
return &worldContextImpl{
WorldContext: ctx,
tlog: tlog,
}
}
type callContextImpl struct {
icmodule.WorldContext
cc contract.CallContext
from module.Address
}
func (ctx *callContextImpl) From() module.Address {
return ctx.from
}
func (ctx *callContextImpl) HandleBurn(from module.Address, amount *big.Int) error {
sign := amount.Sign()
if sign < 0 {
return errors.Errorf("Invalid amount: %v", amount)
}
if sign > 0 {
ts, err := ctx.AddTotalSupply(new(big.Int).Neg(amount))
if err != nil {
return err
}
ctx.onICXBurnedEvent(from, amount, ts)
}
return nil
}
func (ctx *callContextImpl) onICXBurnedEvent(from module.Address, amount, ts *big.Int) {
rev := ctx.Revision().Value()
if rev < icmodule.RevisionBurnV2 {
var burnSig string
if rev < icmodule.RevisionFixBurnEventSignature {
burnSig = "ICXBurned"
} else {
burnSig = "ICXBurned(int)"
}
ctx.cc.OnEvent(state.SystemAddress,
[][]byte{[]byte(burnSig)},
[][]byte{intconv.BigIntToBytes(amount)},
)
} else {
ctx.cc.OnEvent(state.SystemAddress,
[][]byte{[]byte("ICXBurnedV2(Address,int,int)"), from.Bytes()},
[][]byte{intconv.BigIntToBytes(amount), intconv.BigIntToBytes(ts)},
)
}
}
func (ctx *callContextImpl) SumOfStepUsed() *big.Int {
return ctx.cc.SumOfStepUsed()
}
func (ctx *callContextImpl) OnEvent(addr module.Address, indexed, data [][]byte) {
ctx.cc.OnEvent(addr, indexed, data)
}
func (ctx *callContextImpl) CallOnTimer(to module.Address, params []byte) error {
cc := ctx.cc
cm := cc.ContractManager()
jso := &contract.DataCallJSON{Method: "onTimer", Params: params}
callData, _ := json.Marshal(jso)
sl := cc.GetStepLimit(state.StepLimitTypeInvoke)
ch, err := cm.GetHandler(
state.SystemAddress,
to,
new(big.Int),
contract.CTypeCall,
callData,
)
if err != nil {
return err
}
if err, _, _, _ = cc.Call(ch, sl); err != nil {
return err
}
return nil
}
func (ctx *callContextImpl) Governance() module.Address {
return ctx.cc.Governance()
}
func (ctx *callContextImpl) FrameLogger() *trace.Logger {
return ctx.cc.FrameLogger()
}
func (ctx *callContextImpl) TransactionInfo() *state.TransactionInfo {
return ctx.cc.TransactionInfo()
}
func NewCallContext(cc contract.CallContext, from module.Address) icmodule.CallContext {
return &callContextImpl{
WorldContext: NewWorldContext(cc, cc.FrameLogger()),
cc: cc,
from: from,
}
}