/
prepare.go
221 lines (202 loc) · 6.68 KB
/
prepare.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
package keeper
import (
"errors"
"fmt"
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
"github.com/datachainlab/cross/x/core/atomic/protocol/tpc/types"
atomictypes "github.com/datachainlab/cross/x/core/atomic/types"
txtypes "github.com/datachainlab/cross/x/core/tx/types"
crosstypes "github.com/datachainlab/cross/x/core/types"
xcctypes "github.com/datachainlab/cross/x/core/xcc/types"
"github.com/datachainlab/cross/x/packets"
)
func (k Keeper) SendPrepare(
ctx sdk.Context,
packetSender packets.PacketSender,
txID crosstypes.TxID,
transactions []txtypes.ResolvedContractTransaction,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
) error {
if len(transactions) == 0 {
return errors.New("the number of contract transactions must be greater than 1")
} else if !timeoutHeight.IsZero() && uint64(ctx.BlockHeight()) >= timeoutHeight.GetRevisionHeight() {
return fmt.Errorf("the given timeoutHeight is in the past: current=%v timeout=%v", ctx.BlockHeight(), timeoutHeight.GetRevisionHeight())
} else if timeoutTimestamp != 0 && uint64(ctx.BlockTime().Unix()) >= timeoutTimestamp {
return fmt.Errorf("the given timeoutTimestamp is in the past: current=%v timeout=%v", ctx.BlockTime().Unix(), timeoutTimestamp)
} else if _, found := k.GetCoordinatorState(ctx, txID); found {
return fmt.Errorf("txID '%X' already exists", txID)
}
// NOTE: packet-timeout isn't supported in the two-phase commit
packetTimeoutHeight := clienttypes.NewHeight(
clienttypes.ParseChainID(ctx.ChainID()),
math.MaxUint64,
)
var channels []xcctypes.ChannelInfo
for i, tx := range transactions {
data := types.NewPacketDataPrepare(
txID,
tx,
crosstypes.TxIndex(i),
)
xcc, err := tx.GetCrossChainChannel(k.cdc)
if err != nil {
return err
}
ci, err := k.xccResolver.ResolveCrossChainChannel(ctx, xcc)
if err != nil {
return err
}
ch, found := k.ChannelKeeper().GetChannel(ctx, ci.Port, ci.Channel)
if !found {
return sdkerrors.Wrap(channeltypes.ErrChannelNotFound, ci.String())
}
if err := k.SendPacket(
ctx,
packetSender,
&data,
ci.Port, ci.Channel, ch.Counterparty.PortId, ch.Counterparty.ChannelId,
packetTimeoutHeight, 0,
); err != nil {
return err
}
channels = append(channels, *ci)
}
cs := atomictypes.NewCoordinatorState(
txtypes.COMMIT_PROTOCOL_TPC,
atomictypes.COORDINATOR_PHASE_PREPARE,
channels,
)
k.SetCoordinatorState(ctx, txID, cs)
return nil
}
func (k Keeper) ReceivePacketPrepare(
ctx sdk.Context,
destPort,
destChannel string,
data types.PacketDataPrepare,
) (*txtypes.ContractCallResult, *types.PacketAcknowledgementPrepare, error) {
// validate packet data upon receiving
if err := data.ValidateBasic(); err != nil {
return nil, nil, err
}
_, found := k.ChannelKeeper().GetChannel(ctx, destPort, destChannel)
if !found {
return nil, nil, fmt.Errorf("channel(port=%v channel=%v) not found", destPort, destChannel)
}
if _, ok := k.GetContractTransactionState(ctx, data.TxId, data.TxIndex); ok {
return nil, nil, fmt.Errorf("txID '%x' already exists", data.TxId)
}
var prepareResult atomictypes.PrepareResult
res, err := k.cm.PrepareCommit(ctx, data.TxId, data.TxIndex, data.Tx)
if err != nil {
k.Logger(ctx).Info("failed to prepare a commit", "error", err.Error())
prepareResult = atomictypes.PREPARE_RESULT_FAILED
} else {
prepareResult = atomictypes.PREPARE_RESULT_OK
}
txState := atomictypes.NewContractTransactionState(
atomictypes.CONTRACT_TRANSACTION_STATUS_PREPARE,
prepareResult,
xcctypes.ChannelInfo{Channel: destChannel, Port: destPort},
)
k.SetContractTransactionState(ctx, data.TxId, data.TxIndex, txState)
return res, types.NewPacketAcknowledgementPayload(prepareResult), nil
}
func (k Keeper) HandlePacketAcknowledgementPrepare(
ctx sdk.Context,
sourcePort string,
sourceChannel string,
ack types.PacketAcknowledgementPrepare,
txID crosstypes.TxID,
txIndex crosstypes.TxIndex,
ps packets.PacketSender,
) (*sdk.Result, error) {
state, err := k.receivePrepareAcknowledgement(
ctx,
sourcePort, sourceChannel,
ack,
txID, txIndex,
)
if err != nil {
return nil, err
}
if state.GoCommit && state.GoAbort {
panic("fatal error")
} else if state.GoCommit || state.GoAbort {
if err := k.SendCommit(
ctx,
ps,
txID,
state.GoCommit,
); err != nil {
return nil, err
}
} else if state.AlreadyCommitted {
// nop
} else { // wait for more acks
// nop
}
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}
func (k Keeper) receivePrepareAcknowledgement(
ctx sdk.Context,
sourcePort string,
sourceChannel string,
ack types.PacketAcknowledgementPrepare,
txID crosstypes.TxID,
txIndex crosstypes.TxIndex,
) (*receivePrepareState, error) {
if err := ack.ValidateBasic(); err != nil {
return nil, err
}
cs, found := k.GetCoordinatorState(ctx, txID)
if !found {
return nil, fmt.Errorf("txID '%x' not found", txID)
} else if cs.Phase == atomictypes.COORDINATOR_PHASE_UNKNOWN {
return nil, fmt.Errorf("coordinator status should not be '%v'", atomictypes.COORDINATOR_PHASE_UNKNOWN.String())
} else if cs.IsConfirmedALLPrepares() {
return nil, errors.New("all transactions are already confirmed")
}
_, found = k.ChannelKeeper().GetChannel(ctx, sourcePort, sourceChannel)
if !found {
return nil, sdkerrors.Wrap(channeltypes.ErrChannelNotFound, sourceChannel)
}
if err := cs.Confirm(txIndex, xcctypes.ChannelInfo{Port: sourcePort, Channel: sourceChannel}); err != nil {
return nil, err
}
var state receivePrepareState
switch cs.Phase {
case atomictypes.COORDINATOR_PHASE_PREPARE:
switch ack.Result {
case atomictypes.PREPARE_RESULT_OK:
if cs.IsConfirmedALLPrepares() {
cs.Decision = atomictypes.COORDINATOR_DECISION_COMMIT
state.GoCommit = true
} else {
// nop
}
case atomictypes.PREPARE_RESULT_FAILED:
cs.Decision = atomictypes.COORDINATOR_DECISION_ABORT
state.GoAbort = true
default:
panic(fmt.Sprintf("unexpected result %v", ack.Result))
}
case atomictypes.COORDINATOR_PHASE_COMMIT:
state.AlreadyCommitted = true
default:
panic(fmt.Sprintf("unexpected phase %v", cs.Phase))
}
k.SetCoordinatorState(ctx, txID, *cs)
return &state, nil
}
// receivePrepareState keeps a packet receiving state
type receivePrepareState struct {
AlreadyCommitted bool // AlreadyCommitted indicates a boolean whether the tx had already reach the commit phase
GoCommit bool // GoCommit indicates a boolean whether the tx should be committed at next step
GoAbort bool // GoAbort indicates a boolean whether the tx should be aborted at next step
}