/
keeper.go
80 lines (68 loc) · 2.48 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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
basekeeper "github.com/datachainlab/cross/x/core/atomic/protocol/base/keeper"
simplekeeper "github.com/datachainlab/cross/x/core/atomic/protocol/simple/keeper"
tpckeeper "github.com/datachainlab/cross/x/core/atomic/protocol/tpc/keeper"
"github.com/datachainlab/cross/x/core/atomic/types"
txtypes "github.com/datachainlab/cross/x/core/tx/types"
xcctypes "github.com/datachainlab/cross/x/core/xcc/types"
"github.com/datachainlab/cross/x/packets"
)
var _ txtypes.TxRunner = (*Keeper)(nil)
type Keeper struct {
baseKeeper basekeeper.Keeper
simpleKeeper simplekeeper.Keeper
tpcKeeper tpckeeper.Keeper
packetMiddleware packets.PacketMiddleware
packetSender packets.PacketSender
}
func NewKeeper(
cdc codec.Codec,
storeKey sdk.StoreKey,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
cm txtypes.ContractManager,
xccResolver xcctypes.XCCResolver,
packetMiddleware packets.PacketMiddleware,
) Keeper {
baseKeeper := basekeeper.NewKeeper(cdc, storeKey, channelKeeper, portKeeper, scopedKeeper)
simpleKeeper := simplekeeper.NewKeeper(cdc, cm, xccResolver, baseKeeper)
tpcKeeper := tpckeeper.NewKeeper(cdc, cm, xccResolver, baseKeeper)
return Keeper{
baseKeeper: baseKeeper,
simpleKeeper: simpleKeeper,
tpcKeeper: tpcKeeper,
packetSender: packets.NewBasicPacketSender(channelKeeper),
packetMiddleware: packetMiddleware,
}
}
func (k Keeper) RunTx(ctx sdk.Context, tx txtypes.Tx, ps packets.PacketSender) error {
// Run a transaction
switch tx.CommitProtocol {
case txtypes.COMMIT_PROTOCOL_SIMPLE:
err := k.simpleKeeper.SendCall(ctx, ps, tx.Id, tx.ContractTransactions, tx.TimeoutHeight, tx.TimeoutTimestamp)
if err != nil {
return sdkerrors.Wrap(types.ErrFailedInitiateTx, err.Error())
}
case txtypes.COMMIT_PROTOCOL_TPC:
err := k.tpcKeeper.SendPrepare(ctx, ps, tx.Id, tx.ContractTransactions, tx.TimeoutHeight, tx.TimeoutTimestamp)
if err != nil {
return sdkerrors.Wrap(types.ErrFailedInitiateTx, err.Error())
}
default:
return fmt.Errorf("unknown commit protocol '%v'", tx.CommitProtocol)
}
return nil
}
func (k Keeper) SimpleKeeper() simplekeeper.Keeper {
return k.simpleKeeper
}
func (k Keeper) TPCKeeper() tpckeeper.Keeper {
return k.tpcKeeper
}