/
types.go
225 lines (190 loc) · 6.22 KB
/
types.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
package types
import (
"bytes"
fmt "fmt"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/gogo/protobuf/proto"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
authtypes "github.com/datachainlab/cross/x/core/auth/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 NewTx(id crosstypes.TxID, commitProtocol CommitProtocol, ctxs []ResolvedContractTransaction, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) Tx {
return Tx{
Id: id,
CommitProtocol: commitProtocol,
ContractTransactions: ctxs,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
}
}
func (tx Tx) ValidateBasic() error {
return nil
}
// TxRunner defines the expected transaction runner
type TxRunner interface {
// RunTx executes a given transaction
RunTx(ctx sdk.Context, tx Tx, ps packets.PacketSender) error
}
// CallResultType is a type of CallResult
type CallResultType = uint8
const (
// ConstantValueCallResultType indicates a type of constant value
ConstantValueCallResultType CallResultType = iota + 1
)
// CallResult wraps an actual value
type CallResult interface {
proto.Message
Type() CallResultType
Key() []byte
Value() []byte
GetCrossChainChannel(m codec.Codec) xcctypes.XCC
WithCrossChainChannel(m codec.Codec, xcc xcctypes.XCC) CallResult
}
var _ CallResult = (*ConstantValueCallResult)(nil)
// NewConstantValueCallResult returns ConstantValueObject
func NewConstantValueCallResult(xcc xcctypes.XCC, key []byte, value []byte) ConstantValueCallResult {
anyXCC, err := xcctypes.PackCrossChainChannel(xcc)
if err != nil {
panic(err)
}
return ConstantValueCallResult{
CrossChainChannel: *anyXCC,
K: key,
V: value,
}
}
// Type implements CallResult.Type
func (ConstantValueCallResult) Type() CallResultType {
return ConstantValueCallResultType
}
// GetCrossChainChannel implements CallResult.GetCrossChainChannel
func (r ConstantValueCallResult) GetCrossChainChannel(m codec.Codec) xcctypes.XCC {
xcc, err := xcctypes.UnpackCrossChainChannel(m, r.CrossChainChannel)
if err != nil {
panic(err)
}
return xcc
}
// WithChainID implements CallResult.WithCrossChainChannel
func (r ConstantValueCallResult) WithCrossChainChannel(m codec.Codec, xcc xcctypes.XCC) CallResult {
anyXCC, err := xcctypes.PackCrossChainChannel(xcc)
if err != nil {
panic(err)
}
r.CrossChainChannel = *anyXCC
return &r
}
// Key implements CallResult.Key
func (r ConstantValueCallResult) Key() []byte {
return r.K
}
// Evaluate returns a constant value
func (r ConstantValueCallResult) Value() []byte {
return r.V
}
// CallResolverProvider is a provider of CallResultResolver
type CallResolverProvider func(m codec.Codec, results []CallResult) (CallResolver, error)
// DefaultCallResolverProvider returns a default implements of CallResolverProvider
func DefaultCallResolverProvider() CallResolverProvider {
return func(m codec.Codec, results []CallResult) (CallResolver, error) {
return NewSequentialResolver(m, results), nil
}
}
// CallResolver resolves a given key to CallResult
type CallResolver interface {
Resolve(xcc xcctypes.XCC, key []byte) (CallResult, error)
}
// SequentialResolver is a resolver that resolves a CallResult in sequential
type SequentialResolver struct {
m codec.Codec
seq uint8
results []CallResult
}
var _ CallResolver = (*SequentialResolver)(nil)
// NewSequentialResolver returns SequentialResolver
func NewSequentialResolver(m codec.Codec, results []CallResult) *SequentialResolver {
return &SequentialResolver{m: m, seq: 0, results: results}
}
// Resolve implements ObjectResolver.Resolve
// If success, resolver increments the internal sequence
func (r *SequentialResolver) Resolve(xcc xcctypes.XCC, key []byte) (CallResult, error) {
if len(r.results) <= int(r.seq) {
return nil, fmt.Errorf("result not found: seq=%X", r.seq)
}
obj := r.results[r.seq]
if !bytes.Equal(obj.Key(), key) {
return nil, fmt.Errorf("keys mismatch: %X != %X", obj.Key(), key)
}
if objXCC := obj.GetCrossChainChannel(r.m); !objXCC.Equal(xcc) {
return nil, fmt.Errorf("cross-chain channel mismatch: %v != %v", objXCC, xcc)
}
r.seq++
return obj, nil
}
// FakeResolver is a resolver that always fails to resolve an result
type FakeResolver struct{}
var _ CallResolver = (*FakeResolver)(nil)
// NewFakeResolver returns FakeResolver
func NewFakeResolver() FakeResolver {
return FakeResolver{}
}
// Resolve implements CallResolver.Resolve
func (FakeResolver) Resolve(xcc xcctypes.XCC, key []byte) (CallResult, error) {
panic(fmt.Errorf("FakeResolver cannot resolve any results, but received '%v' '%X'", xcc, key))
}
func NewResolvedContractTransaction(anyXCC *codectypes.Any, signers []authtypes.Account, callInfo ContractCallInfo, returnValue *ReturnValue, results []CallResult) ResolvedContractTransaction {
anyResults, err := PackCallResults(results)
if err != nil {
panic(err)
}
return ResolvedContractTransaction{
CrossChainChannel: anyXCC,
Signers: signers,
CallInfo: callInfo,
ReturnValue: returnValue,
CallResults: anyResults,
}
}
func (tx ResolvedContractTransaction) ValidateBasic() error {
return nil
}
func (tx ResolvedContractTransaction) UnpackCallResults(m codec.Codec) []CallResult {
results, err := UnpackCallResults(m, tx.CallResults)
if err != nil {
panic(err)
}
return results
}
func (tx ResolvedContractTransaction) GetCrossChainChannel(m codec.Codec) (xcctypes.XCC, error) {
var xcc xcctypes.XCC
if err := m.UnpackAny(tx.CrossChainChannel, &xcc); err != nil {
return nil, err
}
return xcc, nil
}
type ContractCallInfo []byte
func NewReturnValue(v []byte) *ReturnValue {
rv := ReturnValue{Value: v}
return &rv
}
func (rv *ReturnValue) IsNil() bool {
if rv == nil {
return true
}
return false
}
func (rv *ReturnValue) Equal(other *ReturnValue) bool {
if rv.IsNil() && other.IsNil() {
return true
} else if rv.IsNil() && !other.IsNil() {
return false
} else if !rv.IsNil() && other.IsNil() {
return false
} else {
return bytes.Equal(rv.Value, other.Value)
}
}