-
Notifications
You must be signed in to change notification settings - Fork 9
/
value_encoder.go
139 lines (110 loc) · 3.61 KB
/
value_encoder.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
package collections
import (
"fmt"
"math/big"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
cosmosproto "github.com/cosmos/gogoproto/proto"
"github.com/gogo/protobuf/proto"
)
var (
AccAddressValueEncoder ValueEncoder[sdk.AccAddress] = accAddressValueEncoder{}
DecValueEncoder ValueEncoder[math.LegacyDec] = decValueEncoder{}
IntValueEncoder ValueEncoder[math.Int] = intValueEncoder{}
Uint64ValueEncoder ValueEncoder[uint64] = uint64Value{}
)
// ProtoValueEncoder returns a protobuf value encoder given the codec.BinaryCodec.
// It's used to convert a specific protobuf object into bytes representation and convert
// the protobuf object bytes representation into the concrete object.
func ProtoValueEncoder[V any, PV interface {
*V
cosmosproto.Message
}](cdc codec.BinaryCodec) ValueEncoder[V] {
return protoValueEncoder[V, PV]{
cdc: cdc,
}
}
type protoValueEncoder[V any, PV interface {
*V
cosmosproto.Message
}] struct {
cdc codec.BinaryCodec
}
func (p protoValueEncoder[V, PV]) Name() string { return proto.MessageName(PV(new(V))) }
func (p protoValueEncoder[V, PV]) Encode(value V) []byte { return p.cdc.MustMarshal(PV(&value)) }
func (p protoValueEncoder[V, PV]) Stringify(v V) string { return PV(&v).String() }
func (p protoValueEncoder[V, PV]) Decode(b []byte) V {
v := PV(new(V))
p.cdc.MustUnmarshal(b, v)
return *v
}
// DecValueEncoder ValueEncoder[math.LegacyDec]
type decValueEncoder struct{}
func (d decValueEncoder) Encode(value math.LegacyDec) []byte {
b, err := value.Marshal()
if err != nil {
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
}
return b
}
func (d decValueEncoder) Decode(b []byte) math.LegacyDec {
dec := new(math.LegacyDec)
err := dec.Unmarshal(b)
if err != nil {
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
}
return *dec
}
func (d decValueEncoder) Stringify(value math.LegacyDec) string {
return value.String()
}
func (d decValueEncoder) Name() string {
return "math.LegacyDec"
}
// AccAddressValueEncoder ValueEncoder[sdk.AccAddress]
type accAddressValueEncoder struct{}
func (a accAddressValueEncoder) Encode(value sdk.AccAddress) []byte { return value }
func (a accAddressValueEncoder) Decode(b []byte) sdk.AccAddress { return b }
func (a accAddressValueEncoder) Stringify(value sdk.AccAddress) string { return value.String() }
func (a accAddressValueEncoder) Name() string { return "sdk.AccAddress" }
// IntValueEncoder ValueEncoder[sdk.Int]
type intValueEncoder struct{}
func (intValueEncoder) Encode(value math.Int) []byte {
return IntKeyEncoder.Encode(value)
}
func (intValueEncoder) Decode(b []byte) math.Int {
_, got := IntKeyEncoder.Decode(b)
return got
}
func (intValueEncoder) Stringify(value math.Int) string {
return IntKeyEncoder.Stringify(value)
}
func (intValueEncoder) Name() string {
return "math.Int"
}
// IntKeyEncoder
var IntKeyEncoder KeyEncoder[math.Int] = intKeyEncoder{}
type intKeyEncoder struct{}
const maxIntKeyLen = math.MaxBitLen / 8
func (intKeyEncoder) Encode(key math.Int) []byte {
if key.IsNil() {
panic("cannot encode invalid math.Int")
}
if key.IsNegative() {
panic("cannot encode negative math.Int")
}
i := key.BigInt()
be := i.Bytes()
padded := make([]byte, maxIntKeyLen)
copy(padded[maxIntKeyLen-len(be):], be)
return padded
}
func (intKeyEncoder) Decode(b []byte) (int, math.Int) {
if len(b) != maxIntKeyLen {
panic("invalid key length")
}
i := new(big.Int).SetBytes(b)
return maxIntKeyLen, math.NewIntFromBigInt(i)
}
func (intKeyEncoder) Stringify(key math.Int) string { return key.String() }