-
Notifications
You must be signed in to change notification settings - Fork 136
/
keeper.go
230 lines (188 loc) · 6.24 KB
/
keeper.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
package keeper
import (
"context"
"errors"
"fmt"
"strconv"
"cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/VolumeFi/whoops"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
xchain "github.com/palomachain/paloma/internal/x-chain"
keeperutil "github.com/palomachain/paloma/util/keeper"
"github.com/palomachain/paloma/util/liblog"
"github.com/palomachain/paloma/util/slice"
"github.com/palomachain/paloma/x/scheduler/types"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey store.KVStoreService
account types.AccountKeeper
EvmKeeper types.EvmKeeper
ider keeperutil.IDGenerator
Chains map[xchain.Type]xchain.Bridge
}
func NewKeeper(
cdc codec.BinaryCodec,
storeKey store.KVStoreService,
account types.AccountKeeper,
evmKeeper types.EvmKeeper,
chains []xchain.Bridge,
) *Keeper {
cm := slice.MustMakeMapKeys(chains, func(c xchain.Bridge) xchain.Type {
return c.XChainType()
})
k := &Keeper{
cdc: cdc,
storeKey: storeKey,
account: account,
EvmKeeper: evmKeeper,
Chains: cm,
}
k.ider = keeperutil.NewIDGenerator(k, nil)
return k
}
func (k Keeper) GetAccount(ctx context.Context, addr sdk.AccAddress) authtypes.AccountI {
return k.account.GetAccount(ctx, addr)
}
func (k Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return liblog.FromSDKLogger(sdkCtx.Logger()).With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) ModuleName() string {
return types.ModuleName
}
func (k Keeper) PreJobExecution(ctx context.Context, job *types.Job) error {
return k.EvmKeeper.PreJobExecution(ctx, job)
}
// store returns default store for this keeper!
func (k Keeper) Store(ctx context.Context) storetypes.KVStore {
return runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx))
}
func (k Keeper) jobsStore(ctx context.Context) storetypes.KVStore {
s := runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx))
return prefix.NewStore(s, types.KeyPrefix("jobs"))
}
func (k Keeper) AddNewJob(ctx context.Context, job *types.Job) error {
if k.JobIDExists(ctx, job.GetID()) {
return types.ErrJobWithIDAlreadyExists.Wrap(job.GetID())
}
err := k.saveJob(ctx, job)
if err != nil {
return err
}
keeperutil.EmitEvent(k, ctx, "JobAdded",
sdk.NewAttribute("id", job.GetID()),
)
return nil
}
func (k Keeper) saveJob(ctx context.Context, job *types.Job) error {
if job.GetOwner().Empty() {
return types.ErrInvalid.Wrap("owner can't be empty when adding a new job")
}
if err := job.ValidateBasic(); err != nil {
return err
}
router := job.GetRouting()
chain, ok := k.Chains[router.GetChainType()]
if !ok {
supportedChains := slice.FromMapKeys(k.Chains)
return types.ErrInvalid.Wrapf("chain type %s is not supported. supported chains: %s", router.GetChainType(), supportedChains)
}
// unmarshaling now to test if the payload is correct
err := chain.VerifyJob(
ctx,
job.GetDefinition(),
job.GetPayload(),
router.GetChainReferenceID(),
)
if err != nil {
return whoops.Wrap(err, types.ErrInvalid)
}
return keeperutil.Save(k.jobsStore(ctx), k.cdc, []byte(job.GetID()), job)
}
func (k Keeper) JobIDExists(ctx context.Context, jobID string) bool {
return k.jobsStore(ctx).Has([]byte(jobID))
}
func (k Keeper) GetJob(ctx context.Context, jobID string) (*types.Job, error) {
job, err := keeperutil.Load[*types.Job](k.jobsStore(ctx), k.cdc, []byte(jobID))
if errors.Is(err, keeperutil.ErrNotFound) {
return nil, types.ErrJobNotFound.Wrap("job id: " + jobID)
}
return job, nil
}
func (k Keeper) ExecuteJob(ctx context.Context, jobID string, payload []byte, senderAddress sdk.AccAddress, contractAddr sdk.AccAddress) (uint64, error) {
job, err := k.GetJob(ctx, jobID)
if err != nil {
liblog.FromSDKLogger(k.Logger(ctx)).WithError(err).WithFields("job-id", jobID).Error("Job not found.")
return 0, err
}
// Hook to trigger a valset update attempt
err = k.PreJobExecution(ctx, job)
if err != nil {
// If we have an error here, don't exit. Go ahead and schedule the job.
// Paloma will try to push the valset update again with the next job.
liblog.FromSDKLogger(k.Logger(ctx)).WithError(err).WithFields("job-id", jobID).Error("failed to run PreJobExecution hook")
}
return k.ScheduleNow(ctx, jobID, payload, senderAddress, contractAddr)
}
func (k Keeper) ScheduleNow(ctx context.Context, jobID string, in []byte, senderAddress sdk.AccAddress, contractAddress sdk.AccAddress) (uint64, error) {
job, err := k.GetJob(ctx, jobID)
if err != nil {
k.Logger(ctx).Error("couldn't schedule a job", "job_id", jobID, "err", err)
return 0, err
}
router := job.GetRouting()
chain := k.Chains[router.GetChainType()]
payload := job.GetPayload()
k.Logger(ctx).Info(
"scheduling a job",
"job_id", jobID,
"chain_type", router.GetChainType(),
"chain_reference_id", router.GetChainReferenceID(),
)
if len(in) > 0 && !job.GetIsPayloadModifiable() {
k.Logger(ctx).Error(
"couldn't modify a job's payload as the payload is not modifiable for the job",
"job_id", jobID,
"err", err,
)
return 0, types.ErrCannotModifyJobPayload.Wrapf("jobID: %s", jobID)
}
if job.GetIsPayloadModifiable() && in != nil {
payload = in
}
jcfg := &xchain.JobConfiguration{
Definition: job.GetDefinition(),
Payload: payload,
SenderAddress: senderAddress,
ContractAddress: contractAddress,
RefID: router.GetChainReferenceID(),
Requirements: xchain.JobRequirements{
EnforceMEVRelay: job.EnforceMEVRelay,
},
}
msgID, err := chain.ExecuteJob(ctx, jcfg)
if err != nil {
liblog.FromSDKLogger(k.Logger(ctx)).WithError(err).WithFields(
"job_id", jobID,
"payload", payload,
"chain_type", router.GetChainType(),
"chain_reference_id", router.GetChainReferenceID(),
"enforce_mev_relay", job.EnforceMEVRelay,
).Error("couldn't execute a job")
return 0, err
}
keeperutil.EmitEvent(k, ctx, "JobScheduler",
sdk.NewAttribute("job-id", jobID),
sdk.NewAttribute("msg-id", strconv.FormatUint(msgID, 10)),
sdk.NewAttribute("chainType", router.GetChainType()),
sdk.NewAttribute("chainReferenceID", router.GetChainReferenceID()),
)
return msgID, nil
}