/
keeper_logic_call.go
175 lines (153 loc) · 6 KB
/
keeper_logic_call.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
package keeper
import (
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/near-aurora-bridge/module/x/nab/types"
)
/////////////////////////////
// LOGICCALLS //
/////////////////////////////
// GetOutgoingLogicCall gets an outgoing logic call
func (k Keeper) GetOutgoingLogicCall(ctx sdk.Context, invalidationID []byte, invalidationNonce uint64) *types.OutgoingLogicCall {
store := ctx.KVStore(k.storeKey)
call := types.OutgoingLogicCall{
Transfers: []*types.ERC20Token{},
Fees: []*types.ERC20Token{},
LogicContractAddress: "",
Payload: []byte{},
Timeout: 0,
InvalidationId: invalidationID,
InvalidationNonce: invalidationNonce,
Block: 0,
}
k.cdc.MustUnmarshalBinaryBare(store.Get(types.GetOutgoingLogicCallKey(invalidationID, invalidationNonce)), &call)
return &call
}
// SetOutogingLogicCall sets an outgoing logic call
func (k Keeper) SetOutgoingLogicCall(ctx sdk.Context, call *types.OutgoingLogicCall) {
store := ctx.KVStore(k.storeKey)
// Store checkpoint to prove that this logic call actually happened
checkpoint := call.GetCheckpoint(k.GetGravityID(ctx))
k.SetPastEthSignatureCheckpoint(ctx, checkpoint)
store.Set(types.GetOutgoingLogicCallKey(call.InvalidationId, call.InvalidationNonce),
k.cdc.MustMarshalBinaryBare(call))
}
// DeleteOutgoingLogicCall deletes outgoing logic calls
func (k Keeper) DeleteOutgoingLogicCall(ctx sdk.Context, invalidationID []byte, invalidationNonce uint64) {
ctx.KVStore(k.storeKey).Delete(types.GetOutgoingLogicCallKey(invalidationID, invalidationNonce))
}
// IterateOutgoingLogicCalls iterates over outgoing logic calls
func (k Keeper) IterateOutgoingLogicCalls(ctx sdk.Context, cb func([]byte, *types.OutgoingLogicCall) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyOutgoingLogicCall)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var call types.OutgoingLogicCall
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &call)
// cb returns true to stop early
if cb(iter.Key(), &call) {
break
}
}
}
// GetOutgoingLogicCalls returns the outgoing logic calls
func (k Keeper) GetOutgoingLogicCalls(ctx sdk.Context) (out []*types.OutgoingLogicCall) {
k.IterateOutgoingLogicCalls(ctx, func(_ []byte, call *types.OutgoingLogicCall) bool {
out = append(out, call)
return false
})
return
}
// CancelOutgoingLogicCalls releases all TX in the batch and deletes the batch
func (k Keeper) CancelOutgoingLogicCall(ctx sdk.Context, invalidationId []byte, invalidationNonce uint64) error {
call := k.GetOutgoingLogicCall(ctx, invalidationId, invalidationNonce)
if call == nil {
return types.ErrUnknown
}
// Delete batch since it is finished
k.DeleteOutgoingLogicCall(ctx, call.InvalidationId, call.InvalidationNonce)
// a consuming application will have to watch for this event and act on it
batchEvent := sdk.NewEvent(
types.EventTypeOutgoingLogicCallCanceled,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyInvalidationID, fmt.Sprint(call.InvalidationId)),
sdk.NewAttribute(types.AttributeKeyInvalidationNonce, fmt.Sprint(call.InvalidationNonce)),
)
ctx.EventManager().EmitEvent(batchEvent)
return nil
}
/////////////////////////////
// LOGICCONFIRMS //
/////////////////////////////
// SetLogicCallConfirm sets a logic confirm in the store
func (k Keeper) SetLogicCallConfirm(ctx sdk.Context, msg *types.MsgConfirmLogicCall) {
bytes, err := hex.DecodeString(msg.InvalidationId)
if err != nil {
panic(err)
}
acc, err := sdk.AccAddressFromBech32(msg.Orchestrator)
if err != nil {
panic(err)
}
ctx.KVStore(k.storeKey).
Set(types.GetLogicConfirmKey(bytes, msg.InvalidationNonce, acc), k.cdc.MustMarshalBinaryBare(msg))
}
// GetLogicCallConfirm gets a logic confirm from the store
func (k Keeper) GetLogicCallConfirm(ctx sdk.Context, invalidationId []byte, invalidationNonce uint64, val sdk.AccAddress) *types.MsgConfirmLogicCall {
store := ctx.KVStore(k.storeKey)
data := store.Get(types.GetLogicConfirmKey(invalidationId, invalidationNonce, val))
if data == nil {
return nil
}
out := types.MsgConfirmLogicCall{
InvalidationId: "",
InvalidationNonce: invalidationNonce,
EthSigner: "",
Orchestrator: "",
Signature: "",
}
k.cdc.MustUnmarshalBinaryBare(data, &out)
return &out
}
// DeleteLogicCallConfirm deletes a logic confirm from the store
func (k Keeper) DeleteLogicCallConfirm(
ctx sdk.Context,
invalidationID []byte,
invalidationNonce uint64,
val sdk.AccAddress) {
ctx.KVStore(k.storeKey).Delete(types.GetLogicConfirmKey(invalidationID, invalidationNonce, val))
}
// IterateLogicConfirmByInvalidationIDAndNonce iterates over all logic confirms stored by nonce
func (k Keeper) IterateLogicConfirmByInvalidationIDAndNonce(
ctx sdk.Context,
invalidationID []byte,
invalidationNonce uint64,
cb func([]byte, *types.MsgConfirmLogicCall) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyOutgoingLogicConfirm)
iter := prefixStore.Iterator(prefixRange(append(invalidationID, types.UInt64Bytes(invalidationNonce)...)))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
confirm := types.MsgConfirmLogicCall{
InvalidationId: "",
InvalidationNonce: invalidationNonce,
EthSigner: "",
Orchestrator: "",
Signature: "",
}
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &confirm)
// cb returns true to stop early
if cb(iter.Key(), &confirm) {
break
}
}
}
// GetLogicConfirmsByInvalidationIdAndNonce returns the logic call confirms
func (k Keeper) GetLogicConfirmByInvalidationIDAndNonce(ctx sdk.Context, invalidationId []byte, invalidationNonce uint64) (out []types.MsgConfirmLogicCall) {
k.IterateLogicConfirmByInvalidationIDAndNonce(ctx, invalidationId, invalidationNonce, func(_ []byte, msg *types.MsgConfirmLogicCall) bool {
out = append(out, *msg)
return false
})
return
}