Skip to content

Commit

Permalink
fix: sort addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
rictorlome committed Nov 26, 2024
1 parent 7fca80c commit 2569ad8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
120 changes: 59 additions & 61 deletions src/utils/addressMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
import { address } from '../fixtures/common';
import { Address } from '../serializable/fxs/common';
import { AddressMap, AddressMaps, matchOwners } from './addressMap';
Expand Down Expand Up @@ -245,67 +245,65 @@ describe('AddressMaps', () => {
});

describe('matchOwners', () => {
it('matches owners', () => {
const owner1 = address();
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
const wrongOwner = OutputOwners.fromNative(
[hexToBuffer('0x12345123451234512345')],
0n,
5,
);
const locked = OutputOwners.fromNative(
ownerAddresses,
9999999999999999999999999999999999n,
5,
);
const owner1 = address();
const owner2 = Address.fromHex('7db97c7cece249c2b98bdc0226cc4c2a57bf52fc');
const ownerAddresses: Uint8Array[] = [owner1.toBytes(), owner2.toBytes()];
const goodOwner = OutputOwners.fromNative(ownerAddresses, 0n, 1);
const threasholdTooHigh = OutputOwners.fromNative(ownerAddresses, 0n, 5);
const wrongOwner = OutputOwners.fromNative(
[hexToBuffer('0x12345123451234512345')],
0n,
5,
);
const locked = OutputOwners.fromNative(
ownerAddresses,
9999999999999999999999999999999999n,
5,
);

const specs = [
{
testCase: goodOwner,
expectedSigIndices: [0],
expectedAddressMap: new AddressMap([[owner1, 0]]),
},
{
testCase: threasholdTooHigh,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: locked,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: wrongOwner,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: goodOwner,
sigindices: [1],
expectedSigIndices: [1],
expectedAddressMap: new AddressMap([[owner2, 1]]),
},
{
testCase: goodOwner,
sigindices: [2],
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
];
const specs = [
{
testCase: goodOwner,
expectedSigIndices: [0],
expectedAddressMap: new AddressMap([[owner2, 0]]),
},
{
testCase: threasholdTooHigh,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: locked,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: wrongOwner,
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
{
testCase: goodOwner,
sigindices: [1],
expectedSigIndices: [1],
expectedAddressMap: new AddressMap([[owner1, 1]]),
},
{
testCase: goodOwner,
sigindices: [2],
expectedSigIndices: undefined,
expectedAddressMap: undefined,
},
];

specs.forEach((spec) => {
const result = matchOwners(
spec.testCase,
addressesFromBytes(ownerAddresses),
50n,
spec.sigindices,
);
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
});
test.each(specs)('matchOwners($testCase, $sigIndices)', (spec) => {
const result = matchOwners(
spec.testCase,
addressesFromBytes(ownerAddresses),
50n,
spec.sigindices,
);
expect(result?.sigIndicies).toEqual(spec.expectedSigIndices);
expect(result?.addressMap).toEqual(spec.expectedAddressMap);
});
});
4 changes: 3 additions & 1 deletion src/utils/addressesFromBytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Address } from '../serializable/fxs/common';
import { bytesCompare } from './bytesCompare';

export function addressesFromBytes(bytes: readonly Uint8Array[]): Address[] {
return bytes.map((b) => new Address(b));
const sortedBytes = bytes.toSorted(bytesCompare);
return sortedBytes.map((b) => new Address(b));
}

0 comments on commit 2569ad8

Please sign in to comment.