@@ -23,18 +23,21 @@ import "solidity-cborutils/contracts/CBOR.sol";
23
23
24
24
import "./BigIntCbor.sol " ;
25
25
import "./FilecoinCbor.sol " ;
26
+ import "./BytesCbor.sol " ;
26
27
27
28
import "../types/MinerTypes.sol " ;
28
29
import "../types/CommonTypes.sol " ;
29
30
30
31
import "../utils/CborDecode.sol " ;
31
32
import "../utils/Misc.sol " ;
33
+ import "../utils/Errors.sol " ;
32
34
33
35
/// @title This library is a set of functions meant to handle CBOR parameters serialization and return values deserialization for Miner actor exported methods.
34
36
/// @author Zondax AG
35
37
library MinerCBOR {
36
38
using CBOR for CBOR.CBORBuffer;
37
39
using CBORDecoder for bytes ;
40
+ using BytesCBOR for bytes ;
38
41
using BigIntCBOR for * ;
39
42
using FilecoinCBOR for * ;
40
43
@@ -67,7 +70,9 @@ library MinerCBOR {
67
70
uint len;
68
71
69
72
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
70
- assert (len == 2 );
73
+ if (! (len == 2 )) {
74
+ revert Errors.InvalidArrayLength (2 , len);
75
+ }
71
76
72
77
(ret.owner.data, byteIdx) = rawResp.readBytes (byteIdx);
73
78
@@ -89,26 +94,32 @@ library MinerCBOR {
89
94
uint len;
90
95
91
96
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
92
- assert (len == 2 );
97
+ if (! (len == 2 )) {
98
+ revert Errors.InvalidArrayLength (2 , len);
99
+ }
93
100
94
101
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
95
- assert (len == 2 );
102
+ if (! (len == 2 )) {
103
+ revert Errors.InvalidArrayLength (2 , len);
104
+ }
96
105
97
106
(ret.active.beneficiary.data, byteIdx) = rawResp.readBytes (byteIdx);
98
107
99
108
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
100
- assert (len == 3 );
109
+ if (! (len == 3 )) {
110
+ revert Errors.InvalidArrayLength (3 , len);
111
+ }
101
112
102
113
(tmp, byteIdx) = rawResp.readBytes (byteIdx);
103
114
if (tmp.length > 0 ) {
104
- ret.active.term.quota = tmp.deserializeBigInt ();
115
+ ret.active.term.quota = tmp.deserializeBytesBigInt ();
105
116
} else {
106
117
ret.active.term.quota = CommonTypes.BigInt (new bytes (0 ), false );
107
118
}
108
119
109
120
(tmp, byteIdx) = rawResp.readBytes (byteIdx);
110
121
if (tmp.length > 0 ) {
111
- ret.active.term.used_quota = tmp.deserializeBigInt ();
122
+ ret.active.term.used_quota = tmp.deserializeBytesBigInt ();
112
123
} else {
113
124
ret.active.term.used_quota = CommonTypes.BigInt (new bytes (0 ), false );
114
125
}
@@ -117,13 +128,15 @@ library MinerCBOR {
117
128
118
129
if (! rawResp.isNullNext (byteIdx)) {
119
130
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
120
- assert (len == 5 );
131
+ if (! (len == 5 )) {
132
+ revert Errors.InvalidArrayLength (5 , len);
133
+ }
121
134
122
135
(ret.proposed.new_beneficiary.data, byteIdx) = rawResp.readBytes (byteIdx);
123
136
124
137
(tmp, byteIdx) = rawResp.readBytes (byteIdx);
125
138
if (tmp.length > 0 ) {
126
- ret.proposed.new_quota = tmp.deserializeBigInt ();
139
+ ret.proposed.new_quota = tmp.deserializeBytesBigInt ();
127
140
} else {
128
141
ret.proposed.new_quota = CommonTypes.BigInt (new bytes (0 ), false );
129
142
}
@@ -149,7 +162,9 @@ library MinerCBOR {
149
162
uint leni;
150
163
151
164
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
152
- assert (len == 1 );
165
+ if (len != 1 ) {
166
+ revert Errors.InvalidArrayLength (1 , len);
167
+ }
153
168
154
169
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
155
170
vesting_funds = new MinerTypes.VestingFunds [](len);
@@ -161,7 +176,7 @@ library MinerCBOR {
161
176
(epoch, byteIdx) = rawResp.readChainEpoch (byteIdx);
162
177
(tmp, byteIdx) = rawResp.readBytes (byteIdx);
163
178
164
- amount = tmp.deserializeBigInt ();
179
+ amount = tmp.deserializeBytesBigInt ();
165
180
vesting_funds[i] = MinerTypes.VestingFunds (epoch, amount);
166
181
}
167
182
}
@@ -171,20 +186,25 @@ library MinerCBOR {
171
186
/// @return cbor serialized data as bytes
172
187
function serializeChangeWorkerAddressParams (MinerTypes.ChangeWorkerAddressParams memory params ) internal pure returns (bytes memory ) {
173
188
uint256 capacity = 0 ;
189
+ uint64 addressCount = uint64 (params.new_control_addresses.length );
190
+
191
+ // Safety check to prevent silent truncation
192
+ require (params.new_control_addresses.length == addressCount, "Address count exceeds uint64 limit " );
174
193
175
194
capacity += Misc.getPrefixSize (2 );
176
195
capacity += Misc.getBytesSize (params.new_worker.data);
177
- capacity += Misc.getPrefixSize (uint256 (params.new_control_addresses.length ));
178
- for (uint64 i = 0 ; i < params.new_control_addresses.length ; i++ ) {
196
+ capacity += Misc.getPrefixSize (addressCount);
197
+
198
+ for (uint64 i = 0 ; i < addressCount; i++ ) {
179
199
capacity += Misc.getBytesSize (params.new_control_addresses[i].data);
180
200
}
181
201
CBOR.CBORBuffer memory buf = CBOR.create (capacity);
182
202
183
203
buf.startFixedArray (2 );
184
204
buf.writeBytes (params.new_worker.data);
185
- buf.startFixedArray (uint64 (params.new_control_addresses. length ) );
205
+ buf.startFixedArray (addressCount );
186
206
187
- for (uint64 i = 0 ; i < params.new_control_addresses. length ; i++ ) {
207
+ for (uint64 i = 0 ; i < addressCount ; i++ ) {
188
208
buf.writeBytes (params.new_control_addresses[i].data);
189
209
}
190
210
@@ -222,7 +242,9 @@ library MinerCBOR {
222
242
uint len;
223
243
224
244
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
225
- assert (len == 1 );
245
+ if (len != 1 ) {
246
+ revert Errors.InvalidArrayLength (1 , len);
247
+ }
226
248
227
249
(len, byteIdx) = rawResp.readFixedArray (byteIdx);
228
250
multi_addrs = new CommonTypes.FilAddress [](len);
0 commit comments