Skip to content

Commit 2856c2f

Browse files
authored
Merge pull request #928 from ava-labs/fix/sort-addresses
fix: sort addresses
2 parents 31f4c62 + 9b0907e commit 2856c2f

File tree

2 files changed

+73
-62
lines changed

2 files changed

+73
-62
lines changed

src/utils/addressMap.test.ts

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
22
import { address } from '../fixtures/common';
33
import { Address } from '../serializable/fxs/common';
44
import { AddressMap, AddressMaps, matchOwners } from './addressMap';
@@ -245,67 +245,76 @@ describe('AddressMaps', () => {
245245
});
246246

247247
describe('matchOwners', () => {
248-
it('matches owners', () => {
249-
const owner1 = address();
250-
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
251-
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
252-
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
253-
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
254-
const wrongOwner = OutputOwners.fromNative(
255-
[hexToBuffer('0x12345123451234512345')],
256-
0n,
257-
5,
258-
);
259-
const locked = OutputOwners.fromNative(
260-
ownerAddresses,
261-
9999999999999999999999999999999999n,
262-
5,
263-
);
248+
const owner1 = address();
249+
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
250+
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
251+
// NOTE: the ownerAddresses will be sorted in the OutputOwners -- owner2 is at index 0.
252+
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
253+
const goodOwnerMultisig = OutputOwners.fromNative(ownerAddresses, 0n, 2);
254+
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
255+
const wrongOwner = OutputOwners.fromNative(
256+
[hexToBuffer('0x12345123451234512345')],
257+
0n,
258+
5,
259+
);
260+
const locked = OutputOwners.fromNative(
261+
ownerAddresses,
262+
9999999999999999999999999999999999n,
263+
5,
264+
);
264265

265-
const specs = [
266-
{
267-
testCase: goodOwner,
268-
expectedSigIndices: [0],
269-
expectedAddressMap: new AddressMap([[owner1, 0]]),
270-
},
271-
{
272-
testCase: threasholdTooHigh,
273-
expectedSigIndices: undefined,
274-
expectedAddressMap: undefined,
275-
},
276-
{
277-
testCase: locked,
278-
expectedSigIndices: undefined,
279-
expectedAddressMap: undefined,
280-
},
281-
{
282-
testCase: wrongOwner,
283-
expectedSigIndices: undefined,
284-
expectedAddressMap: undefined,
285-
},
286-
{
287-
testCase: goodOwner,
288-
sigindices: [1],
289-
expectedSigIndices: [1],
290-
expectedAddressMap: new AddressMap([[owner2, 1]]),
291-
},
292-
{
293-
testCase: goodOwner,
294-
sigindices: [2],
295-
expectedSigIndices: undefined,
296-
expectedAddressMap: undefined,
297-
},
298-
];
266+
const specs = [
267+
{
268+
testCase: goodOwner,
269+
expectedSigIndices: [0],
270+
expectedAddressMap: new AddressMap([[owner2, 0]]),
271+
},
272+
{
273+
testCase: threasholdTooHigh,
274+
expectedSigIndices: undefined,
275+
expectedAddressMap: undefined,
276+
},
277+
{
278+
testCase: locked,
279+
expectedSigIndices: undefined,
280+
expectedAddressMap: undefined,
281+
},
282+
{
283+
testCase: wrongOwner,
284+
expectedSigIndices: undefined,
285+
expectedAddressMap: undefined,
286+
},
287+
{
288+
testCase: goodOwner,
289+
sigindices: [1],
290+
expectedSigIndices: [1],
291+
expectedAddressMap: new AddressMap([[owner1, 1]]),
292+
},
293+
{
294+
testCase: goodOwnerMultisig,
295+
sigindices: [0, 1],
296+
expectedSigIndices: [0, 1],
297+
expectedAddressMap: new AddressMap([
298+
[owner2, 0],
299+
[owner1, 1],
300+
]),
301+
},
302+
{
303+
testCase: goodOwner,
304+
sigindices: [2],
305+
expectedSigIndices: undefined,
306+
expectedAddressMap: undefined,
307+
},
308+
];
299309

300-
specs.forEach((spec) => {
301-
const result = matchOwners(
302-
spec.testCase,
303-
addressesFromBytes(ownerAddresses),
304-
50n,
305-
spec.sigindices,
306-
);
307-
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
308-
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
309-
});
310+
test.each(specs)('matchOwners($testCase, $sigIndices)', (spec) => {
311+
const result = matchOwners(
312+
spec.testCase,
313+
addressesFromBytes(ownerAddresses),
314+
50n,
315+
spec.sigindices,
316+
);
317+
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
318+
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
310319
});
311320
});

src/utils/addressesFromBytes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Address } from '../serializable/fxs/common';
2+
import { bytesCompare } from './bytesCompare';
23

34
export function addressesFromBytes(bytes: readonly Uint8Array[]): Address[] {
4-
return bytes.map((b) => new Address(b));
5+
const sortedBytes = bytes.toSorted(bytesCompare);
6+
return sortedBytes.map((b) => new Address(b));
57
}

0 commit comments

Comments
 (0)