/
smart_contract_deployment.go
449 lines (402 loc) · 14.8 KB
/
smart_contract_deployment.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package keeper
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/VolumeFi/whoops"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
xchain "github.com/palomachain/paloma/internal/x-chain"
keeperutil "github.com/palomachain/paloma/util/keeper"
"github.com/palomachain/paloma/util/liblog"
consensustypes "github.com/palomachain/paloma/x/consensus/types"
"github.com/palomachain/paloma/x/evm/keeper/deployment"
"github.com/palomachain/paloma/x/evm/types"
)
var lastSmartContractKey = []byte{0x1}
func (k Keeper) AllSmartContractsDeployments(ctx sdk.Context) ([]*types.SmartContractDeployment, error) {
_, res, err := keeperutil.IterAll[*types.SmartContractDeployment](
k.provideSmartContractDeploymentStore(ctx),
k.cdc,
)
return res, err
}
func (k Keeper) HasAnySmartContractDeployment(ctx context.Context, chainReferenceID string) (found bool) {
if err := keeperutil.IterAllFnc(
k.provideSmartContractDeploymentStore(ctx),
k.cdc,
func(keyArg []byte, item *types.SmartContractDeployment) bool {
if item.ChainReferenceID == chainReferenceID {
found = true
return false
}
return true
},
); err != nil {
k.Logger(ctx).Error(
"error getting smart contract from deployment store by chain Ref",
"err", err,
"chainReferenceID", chainReferenceID,
)
}
return
}
func (k Keeper) DeleteSmartContractDeploymentByContractID(ctx context.Context, smartContractID uint64, chainReferenceID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
_, key := k.getSmartContractDeploymentByContractID(sdkCtx, smartContractID, chainReferenceID)
if key == nil {
return
}
k.Logger(ctx).Info("removing a smart contract deployment", "smart-contract-id", smartContractID, "chain-reference-id", chainReferenceID)
k.provideSmartContractDeploymentStore(sdkCtx).Delete(key)
}
func (k Keeper) updateSmartContractDeployment(ctx context.Context, smartContractID uint64, chainReferenceID string, deployment *types.SmartContractDeployment) error {
if deployment == nil {
return fmt.Errorf("deployment must not be nil")
}
logger := k.Logger(ctx).WithFields("smart-contract-id", smartContractID, "chain-reference-id", chainReferenceID, "new-deployment", deployment)
logger.Debug("Updating deployment record...")
v, key := k.getSmartContractDeploymentByContractID(ctx, smartContractID, chainReferenceID)
if key == nil {
logger.WithError(keeperutil.ErrNotFound).Error("No deployment found for given contract and chain ID.")
return keeperutil.ErrNotFound
}
logger = logger.WithFields("current-deployment", v)
if err := keeperutil.Save(k.provideSmartContractDeploymentStore(ctx), k.cdc, key, deployment); err != nil {
logger.WithError(err).Error("Failed to update smart contract deployment record.")
return err
}
logger.Debug("Updated contract deployment record.")
return nil
}
func (k Keeper) GetLastCompassContract(ctx context.Context) (*types.SmartContract, error) {
kv := k.provideLastCompassContractStore(ctx)
id := kv.Get(lastSmartContractKey)
return keeperutil.Load[*types.SmartContract](k.provideSmartContractStore(ctx), k.cdc, id)
}
func (k Keeper) SetAsCompassContract(ctx context.Context, smartContract *types.SmartContract) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
k.Logger(sdkCtx).Info("setting smart contract as the latest one", "smart-contract-id", smartContract.GetId())
err := k.setAsLastCompassContract(ctx, smartContract)
if err != nil {
return fmt.Errorf("failed to set contract as last smart contract: %w", err)
}
err = k.tryDeployingSmartContractToAllChains(ctx, smartContract)
if err != nil {
// that's ok. it will try to deploy it on every end blocker
if !errors.Is(err, ErrConsensusNotAchieved) {
return fmt.Errorf("failed to deploy smart contract to all chains: %w", err)
}
}
return nil
}
func (k Keeper) SaveNewSmartContract(ctx context.Context, abiJSON string, bytecode []byte) (*types.SmartContract, error) {
smartContract := &types.SmartContract{
Id: k.ider.IncrementNextID(ctx, "smart-contract"),
AbiJSON: abiJSON,
Bytecode: bytecode,
}
k.Logger(ctx).Info("saving new smart contract", "smart-contract-id", smartContract.GetId())
err := k.createSmartContract(ctx, smartContract)
if err != nil {
return nil, err
}
return smartContract, nil
}
func (k Keeper) TryDeployingLastCompassContractToAllChains(ctx context.Context) {
smartContract, err := k.GetLastCompassContract(ctx)
if err != nil {
k.Logger(ctx).Error("error while getting latest smart contract", "err", err)
return
}
err = k.tryDeployingSmartContractToAllChains(ctx, smartContract)
if err != nil {
k.Logger(ctx).Error("error while trying to deploy smart contract to all chains",
"err", err,
"smart-contract-id", smartContract.GetId(),
)
return
}
k.Logger(ctx).Info("trying to deploy smart contract to all chains",
"smart-contract-id", smartContract.GetId(),
)
}
func (k Keeper) AddSmartContractExecutionToConsensus(
ctx context.Context,
chainReferenceID,
turnstoneID string,
logicCall *types.SubmitLogicCall,
) (uint64, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
requirements := &xchain.JobRequirements{
EnforceMEVRelay: logicCall.ExecutionRequirements.EnforceMEVRelay,
}
assignee, err := k.PickValidatorForMessage(ctx, chainReferenceID, requirements)
if err != nil {
return 0, err
}
return k.ConsensusKeeper.PutMessageInQueue(
ctx,
consensustypes.Queue(
ConsensusTurnstoneMessage,
xchainType,
chainReferenceID,
),
&types.Message{
ChainReferenceID: chainReferenceID,
TurnstoneID: turnstoneID,
Action: &types.Message_SubmitLogicCall{
SubmitLogicCall: logicCall,
},
Assignee: assignee,
AssignedAtBlockHeight: sdkmath.NewInt(sdkCtx.BlockHeight()),
}, nil)
}
func (k Keeper) deploySmartContractToChain(ctx context.Context, chainInfo *types.ChainInfo, smartContract *types.SmartContract) (retErr error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
defer func() {
args := []any{
"chain-reference-id", chainInfo.GetChainReferenceID(),
"smart-contract-id", smartContract.GetId(),
}
if retErr != nil {
args = append(args, "err", retErr)
}
if r := recover(); r != nil {
args = append(args, "panic", r)
k.Logger(ctx).Error("recovered panic! Error adding a message to deploy smart contract to chain", args...)
}
if retErr != nil {
k.Logger(ctx).Error("error adding a message to deploy smart contract to chain", args...)
} else {
k.Logger(ctx).Info("added a new smart contract deployment to queue", args...)
}
}()
logger := k.Logger(ctx)
contractABI, err := abi.JSON(strings.NewReader(smartContract.GetAbiJSON()))
if err != nil {
return err
}
snapshot, err := k.Valset.GetCurrentSnapshot(ctx)
var totalShares sdkmath.Int
if snapshot != nil {
totalShares = snapshot.TotalShares
}
logger.Info(
"get current snapshot",
"snapshot-id", snapshot.GetId(),
"validators-size", len(snapshot.GetValidators()),
"total-shares", totalShares,
)
if err != nil {
if errors.Is(err, keeperutil.ErrNotFound) {
logger.With("error", err).Info("cannot deploy due to no consensus")
return nil
}
return err
}
valset := transformSnapshotToCompass(snapshot, chainInfo.GetChainReferenceID(), logger)
logger.Info("returning valset info for deploy smart contract to chain",
"valset-id", valset.ValsetID,
"valset-validator-size", len(valset.Validators),
"valset-power-size", len(valset.Powers),
)
if !isEnoughToReachConsensus(valset) {
k.Logger(ctx).Info(
"skipping deployment as there are not enough validators to form a consensus",
"chain-id", chainInfo.GetChainReferenceID(),
"smart-contract-id", smartContract.GetId(),
"valset-id", valset.GetValsetID(),
)
return whoops.WrapS(
ErrConsensusNotAchieved,
"cannot build a valset. valset-id: %d, chain-reference-id: %s, smart-contract-id: %d",
valset.GetValsetID(), chainInfo.GetChainReferenceID(), smartContract.GetId(),
)
}
uniqueID := generateSmartContractID(ctx)
k.createSmartContractDeployment(ctx, smartContract, chainInfo, uniqueID[:])
lastEventNonce, err := k.Gravity.GetLastObservedEventNonce(ctx)
if err != nil {
return fmt.Errorf("failed to get last observed event nonce: %w", err)
}
// set the smart contract constructor arguments
logger.Info(
"transform valset to abi valset",
"valset-id", valset.GetValsetID(),
"validators-size", len(valset.GetValidators()),
"power-size", len(valset.GetPowers()),
)
input, err := contractABI.Pack("", uniqueID, (&big.Int{}).SetUint64(lastEventNonce), types.TransformValsetToABIValset(valset))
if err != nil {
return err
}
vals, err := contractABI.Constructor.Inputs.Unpack(input)
logger.Debug("[deploySmartContractToChain] UNPACK",
"ERR", err,
"ARGS", vals,
)
if err != nil {
return err
}
logger.Info(
"smart contract deployment constructor input",
"x-chain-type", xchainType,
"chain-reference-id", chainInfo.GetChainReferenceID(),
"constructor-input", input,
)
assignee, err := k.PickValidatorForMessage(ctx, chainInfo.GetChainReferenceID(), nil)
if err != nil {
return err
}
_, err = k.ConsensusKeeper.PutMessageInQueue(
ctx,
consensustypes.Queue(
ConsensusTurnstoneMessage,
xchainType,
chainInfo.GetChainReferenceID(),
),
&types.Message{
ChainReferenceID: chainInfo.GetChainReferenceID(),
Action: &types.Message_UploadSmartContract{
UploadSmartContract: &types.UploadSmartContract{
Id: smartContract.GetId(),
Bytecode: smartContract.GetBytecode(),
Abi: smartContract.GetAbiJSON(),
ConstructorInput: input,
},
},
Assignee: assignee,
AssignedAtBlockHeight: sdkmath.NewInt(sdkCtx.BlockHeight()),
}, nil)
return err
}
func (k Keeper) getSmartContract(ctx context.Context, id uint64) (*types.SmartContract, error) {
return keeperutil.Load[*types.SmartContract](k.provideSmartContractStore(ctx), k.cdc, keeperutil.Uint64ToByte(id))
}
func (k Keeper) createSmartContract(ctx context.Context, smartContract *types.SmartContract) error {
return keeperutil.Save(k.provideSmartContractStore(ctx), k.cdc, keeperutil.Uint64ToByte(smartContract.GetId()), smartContract)
}
func (k Keeper) setAsLastCompassContract(ctx context.Context, smartContract *types.SmartContract) error {
kv := k.provideLastCompassContractStore(ctx)
kv.Set(lastSmartContractKey, keeperutil.Uint64ToByte(smartContract.GetId()))
return nil
}
func (k Keeper) tryDeployingSmartContractToAllChains(ctx context.Context, smartContract *types.SmartContract) error {
var g whoops.Group
chainInfos, err := k.GetAllChainInfos(ctx)
if err != nil {
return err
}
for _, chainInfo := range chainInfos {
k.Logger(ctx).Info("trying to deploy smart contract to EVM chain", "smart-contract-id", smartContract.GetId(), "chain-reference-id", chainInfo.GetChainReferenceID())
if k.HasAnySmartContractDeployment(ctx, chainInfo.GetChainReferenceID()) {
// TODO: Only wait if the status is IN_FLIGHT
// TODO: We probably want to still delete the deployment in case of error AS LONG as we haven't sent a move ownership message
// we are already deploying to this chain. Lets wait it out.
continue
}
if chainInfo.GetActiveSmartContractID() >= smartContract.GetId() {
// the chain has the newer version of the chain, so skipping the "old" smart contract upgrade
continue
}
k.Logger(ctx).Info("deploying smart contracts actually",
"smart-contract-id", smartContract.GetId(),
"chain-reference-id", chainInfo.GetChainReferenceID())
g.Add(k.deploySmartContractToChain(ctx, chainInfo, smartContract))
}
if g.Err() {
return g
}
return nil
}
func (k Keeper) createSmartContractDeployment(
ctx context.Context,
smartContract *types.SmartContract,
chainInfo *types.ChainInfo,
uniqueID []byte,
) *types.SmartContractDeployment {
if foundItem, _ := k.getSmartContractDeploymentByContractID(ctx, smartContract.GetId(), chainInfo.GetChainReferenceID()); foundItem != nil {
k.Logger(ctx).Error(
"smart contract is already deploying",
"smart-contract-id", smartContract.GetId(),
"smart-contract-status", foundItem.GetStatus(),
"chain-reference-id", chainInfo.GetChainReferenceID(),
)
return foundItem
}
item := &types.SmartContractDeployment{
SmartContractID: smartContract.GetId(),
ChainReferenceID: chainInfo.GetChainReferenceID(),
Status: types.SmartContractDeployment_IN_FLIGHT,
UniqueID: uniqueID,
}
id := k.ider.IncrementNextID(ctx, "smart-contract-deploying")
if err := keeperutil.Save(
k.provideSmartContractDeploymentStore(ctx),
k.cdc,
keeperutil.Uint64ToByte(id),
item,
); err != nil {
k.Logger(ctx).Error("error setting smart contract in deployment store", "err", err)
}
k.Logger(ctx).Info("setting smart contract in deployment state", "smart-contract-id", smartContract.GetId(), "chain-reference-id", chainInfo.GetChainReferenceID())
return item
}
func (k Keeper) getSmartContractDeploymentByContractID(ctx context.Context, smartContractID uint64, chainReferenceID string) (res *types.SmartContractDeployment, key []byte) {
if err := keeperutil.IterAllFnc(
k.provideSmartContractDeploymentStore(ctx),
k.cdc,
func(keyArg []byte, item *types.SmartContractDeployment) bool {
if item.ChainReferenceID == chainReferenceID && item.SmartContractID == smartContractID {
res = item
key = keyArg
return false
}
return true
},
); err != nil {
k.Logger(ctx).Error(
"error getting smart contract from deployment store by contractID, chainRef",
"err", err,
"smartContractID", smartContractID,
"chainReferenceID", chainReferenceID,
)
}
return
}
func (k Keeper) provideSmartContractDeploymentStore(ctx context.Context) storetypes.KVStore {
s := runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx))
return prefix.NewStore(s, []byte("smart-contract-deployment"))
}
func (k Keeper) provideSmartContractStore(ctx context.Context) storetypes.KVStore {
kvstore := runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx))
return prefix.NewStore(kvstore, []byte("smart-contracts"))
}
func (k Keeper) provideLastCompassContractStore(ctx context.Context) storetypes.KVStore {
kvstore := runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx))
return prefix.NewStore(kvstore, []byte("latest-smart-contract"))
}
func provideDeploymentCacheBootstrapper(k *Keeper) func(context.Context, *deployment.Cache) {
return func(ctx context.Context, c *deployment.Cache) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
d, err := k.AllSmartContractsDeployments(sdkCtx)
if err != nil {
liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("Failed to load smart contract deployments")
// This should only happen once during cache initialisation.
// A better approach would be to use the cache as a wrapper that gets cold data on demand.
panic(err)
}
for _, v := range d {
for _, t := range v.Erc20Transfers {
c.Add(ctx, v.GetChainReferenceID(), t.GetMsgID())
}
}
}
}