-
Notifications
You must be signed in to change notification settings - Fork 580
/
acknowledgement.go
73 lines (63 loc) · 2.61 KB
/
acknowledgement.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
package types
import (
"fmt"
"reflect"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// ackErrorString defines a string constant included in error acknowledgements
// NOTE: Changing this const is state machine breaking as acknowledgements are written into state.
ackErrorString = "error handling packet: see events for details"
)
// NewResultAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Result
// type in the Response field.
func NewResultAcknowledgement(result []byte) Acknowledgement {
return Acknowledgement{
Response: &Acknowledgement_Result{
Result: result,
},
}
}
// NewErrorAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Error
// type in the Response field.
// NOTE: Acknowledgements are written into state and thus, changes made to error strings included in packet acknowledgements
// risk an app hash divergence when nodes in a network are running different patch versions of software.
func NewErrorAcknowledgement(err error) Acknowledgement {
// the ABCI code is included in the abcitypes.ResponseDeliverTx hash
// constructed in Tendermint and is therefore deterministic
_, code, _ := sdkerrors.ABCIInfo(err, false) // discard non-determinstic codespace and log values
return Acknowledgement{
Response: &Acknowledgement_Error{
Error: fmt.Sprintf("ABCI code: %d: %s", code, ackErrorString),
},
}
}
// ValidateBasic performs a basic validation of the acknowledgement
func (ack Acknowledgement) ValidateBasic() error {
switch resp := ack.Response.(type) {
case *Acknowledgement_Result:
if len(resp.Result) == 0 {
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement result cannot be empty")
}
case *Acknowledgement_Error:
if strings.TrimSpace(resp.Error) == "" {
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement error cannot be empty")
}
default:
return sdkerrors.Wrapf(ErrInvalidAcknowledgement, "unsupported acknowledgement response field type %T", resp)
}
return nil
}
// Success implements the Acknowledgement interface. The acknowledgement is
// considered successful if it is a ResultAcknowledgement. Otherwise it is
// considered a failed acknowledgement.
func (ack Acknowledgement) Success() bool {
return reflect.TypeOf(ack.Response) == reflect.TypeOf(((*Acknowledgement_Result)(nil)))
}
// Acknowledgement implements the Acknowledgement interface. It returns the
// acknowledgement serialised using JSON.
func (ack Acknowledgement) Acknowledgement() []byte {
return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&ack))
}