Skip to content

Commit

Permalink
fix: circular dependencies
Browse files Browse the repository at this point in the history
Closes #873.
  • Loading branch information
erictaylor committed Oct 24, 2024
1 parent 5710ba7 commit e442228
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 129 deletions.
2 changes: 1 addition & 1 deletion src/serializable/fxs/pvm/convertSubnetValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Codec } from '../../codec';
import { serializable } from '../../common/types';
import { BigIntPr, Bytes } from '../../primitives';
import { TypeSymbols } from '../../constants';
import { ProofOfPossession } from '../../pvm';
import { ProofOfPossession } from '../../pvm/proofOfPossession';
import { NodeId } from '../common';
import { PChainOwner } from './pChainOwner';

Expand Down
2 changes: 1 addition & 1 deletion src/serializable/pvm/increaseBalanceTx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pack, unpack } from '../../utils/struct';
import { BaseTx } from '../avax';
import { BaseTx } from '../avax/baseTx';
import type { Codec } from '../codec';
import { serializable } from '../common/types';
import { TypeSymbols } from '../constants';
Expand Down
9 changes: 8 additions & 1 deletion src/serializable/pvm/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { testPVMCodec } from '../../fixtures/codec';
import { validator, validatorBytes } from '../../fixtures/pvm';
import { testSerialization } from '../../fixtures/utils/serializable';
import { Validator } from './validator';

testSerialization('Validator', Validator, validator, validatorBytes);
testSerialization(
'Validator',
Validator,
validator,
validatorBytes,
testPVMCodec,
);
71 changes: 70 additions & 1 deletion src/utils/addressMap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { jest } from '@jest/globals';
import { address } from '../fixtures/common';
import { Address } from '../serializable/fxs/common';
import { AddressMap, AddressMaps } from './addressMap';
import { AddressMap, AddressMaps, matchOwners } from './addressMap';
import { addressesFromBytes } from './addressesFromBytes';
import { OutputOwners } from '../serializable';
import { hexToBuffer } from './buffer';

describe('AddressMap', () => {
const testAddress1 = address();
Expand Down Expand Up @@ -240,3 +243,69 @@ 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 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,
},
];

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);
});
});
});
43 changes: 41 additions & 2 deletions src/utils/addressMap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
import type { TransferableInput } from '../serializable';
import type { OutputOwners, TransferableInput } from '../serializable';
import type { Utxo } from '../serializable/avax/utxo';
import { Address } from '../serializable/fxs/common';
import { addressesFromBytes } from './addressesFromBytes';
import { hexToBuffer } from './buffer';
import { matchOwners } from './matchOwners';

export type MatchOwnerResult = {
sigIndicies: number[];
addressMap: AddressMap;
};
export const matchOwners = (
owners: OutputOwners,
inputAddrs: Address[],
minIssuanceTime: bigint,
sigindices?: number[],
): MatchOwnerResult | undefined => {
if (owners.locktime.value() > minIssuanceTime) {
return undefined;
}

const inputAddrSet = new Set(inputAddrs.map((a) => a.toString()));
const addressMap = owners.addrs.reduce((agg, addr, i) => {
if (
agg.size() < owners.threshold.value() &&
inputAddrSet.has(addr.value())
) {
// only add actual signer addresses if sigindices are known
if (sigindices?.length && !sigindices.includes(i)) {
return agg;
}

return agg.set(addr, i);
}
return agg;
}, new AddressMap());

if (addressMap.size() < owners.threshold.value()) {
return undefined;
}

return {
sigIndicies: Array.from(addressMap.values()),
addressMap: addressMap,
};
};

export class AddressMap {
constructor(initialData: [Address, number][] = []) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/builderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransferableInput } from '../serializable/avax';
import type { Utxo } from '../serializable/avax/utxo';
import { matchOwners } from './addressMap';
import { addressesFromBytes } from './addressesFromBytes';
import { matchOwners } from './matchOwners';
import { isTransferOut } from './typeGuards';

type GetImportedInputsFromUtxosOutput = {
Expand Down
73 changes: 0 additions & 73 deletions src/utils/matchOwners.spec.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/utils/matchOwners.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/vms/evm/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
} from '../../serializable/fxs/secp256k1';
import { BigIntPr, Int } from '../../serializable/primitives';
import { addressesFromBytes } from '../../utils';
import { AddressMap, AddressMaps } from '../../utils/addressMap';
import { AddressMap, AddressMaps, matchOwners } from '../../utils/addressMap';
import { costCorethTx } from '../../utils/costs';
import { matchOwners } from '../../utils/matchOwners';
import { compareEVMOutputs } from '../../utils/sort';
import { EVMUnsignedTx } from '../common/evmUnsignedTx';
import type { UnsignedTx } from '../common/unsignedTx';
Expand Down
2 changes: 1 addition & 1 deletion src/vms/pvm/etna-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import {
addressesFromBytes,
bytesCompare,
isTransferOut,
matchOwners,
} from '../../../utils';
import { matchOwners } from '../../../utils/matchOwners';
import { compareTransferableOutputs } from '../../../utils/sort';
import { baseTxUnsafePvm, UnsignedTx } from '../../common';
import { addDimensions, createDimensions } from '../../common/fees/dimensions';
Expand Down
2 changes: 1 addition & 1 deletion src/vms/pvm/txs/fee/complexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
isStakeableLockIn,
isStakeableLockOut,
isTransferOut,
} from '../../../../utils';
} from '../../../../utils/typeGuards';
import type { Dimensions } from '../../../common/fees/dimensions';
import {
FeeDimensions,
Expand Down
4 changes: 2 additions & 2 deletions src/vms/utils/calculateSpend/utils/verifySignaturesMatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MatchOwnerResult } from '../../../../utils/matchOwners';
import { matchOwners } from '../../../../utils/matchOwners';
import type { Address, TransferOutput } from '../../../../serializable';
import type { MatchOwnerResult } from '../../../../utils';
import { matchOwners } from '../../../../utils';

export type verifySigMatchItem<T> = Required<{
sigData: MatchOwnerResult;
Expand Down

0 comments on commit e442228

Please sign in to comment.