-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor revision utils and centralize reusable assertions (#324)
* refactor: move revisionUtils to core * refactor: centralize reusable high level assertions * test: added units for assertions utils
- v1.0.0
- v1.0.0-rc6
- v1.0.0-rc.5
- v1.0.0-rc.4
- v1.0.0-rc.1
- v1.0.0-beta-1
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- 2.0.0-beta.1
- 1.0.0-rc.3
- 1.0.0-rc.2
- 1.0.0-beta-3
- 1.0.0-beta-2
- 1.0.0-beta.32
- 1.0.0-beta.31
- 1.0.0-beta.30
- 1.0.0-beta.29
- 1.0.0-beta.27
- 1.0.0-beta.26
- 1.0.0-beta.24
- 1.0.0-beta.23
- 1.0.0-beta.22
- 1.0.0-beta.21
- 1.0.0-beta.20
- 1.0.0-beta.19
- 1.0.0-beta.18
- 1.0.0-beta.17
- 1.0.0-beta.16
- 1.0.0-beta.15
- 1.0.0-beta.14
- 1.0.0-beta.13
- 1.0.0-beta.12
- 1.0.0-beta.11
- 1.0.0-beta.10
- 1.0.0-beta.9
- 1.0.0-beta.8
- 1.0.0-beta.7
- 1.0.0-beta.6
- 1.0.0-beta.5
- 1.0.0-beta.4
1 parent
a4edb21
commit e9580d0
Showing
21 changed files
with
527 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DATA, assert } from '@vechainfoundation/vechain-sdk-errors'; | ||
import { addressUtils } from '../../address'; | ||
|
||
/** | ||
* Assert if address is valid | ||
* | ||
* @param address - Address to assert | ||
*/ | ||
function assertIsAddress(address: string): void { | ||
assert( | ||
addressUtils.isAddress(address), | ||
DATA.INVALID_DATA_TYPE, | ||
'Invalid address. The address must be 20 bytes (a 42 characters hex string with a `0x` prefix.)', | ||
{ address } | ||
); | ||
} | ||
|
||
export { assertIsAddress }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { DATA, assert } from '@vechainfoundation/vechain-sdk-errors'; | ||
import { revisionUtils } from '../revision'; | ||
|
||
/** | ||
* Assert if a given revision is valid. | ||
* A valid revision is a string representing a block number or block id. | ||
* | ||
* @param revision - Revision to assert | ||
*/ | ||
function assertIsRevisionForBlock(revision?: string | number): void { | ||
assert( | ||
revision === undefined || | ||
revision === null || | ||
revisionUtils.isRevisionBlock(revision), | ||
DATA.INVALID_DATA_TYPE, | ||
'Invalid revision. The revision must be a string representing a block number or block id (also "best" is accepted which represents the best block & "finalized" for the finalized block).', | ||
{ revision } | ||
); | ||
} | ||
|
||
function assertIsRevisionForAccount(revision?: string | number): void { | ||
assert( | ||
revision === undefined || | ||
revision === null || | ||
revisionUtils.isRevisionAccount(revision), | ||
DATA.INVALID_DATA_TYPE, | ||
'Invalid revision. The revision must be a string representing a block number or block id (also "best" is accepted which represents the best block).', | ||
{ revision } | ||
); | ||
} | ||
|
||
export { assertIsRevisionForBlock, assertIsRevisionForAccount }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './account-assertions'; | ||
export * from './block-assertions'; | ||
export * from './transaction-assertions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...es/network/src/utils/revision/revision.ts → packages/core/src/utils/revision/revision.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/core/tests/utils/assertions/account-assertions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { accountAssertionsTests } from './fixture'; | ||
import { assertIsAddress } from '../../../src'; | ||
import { InvalidDataTypeError } from '@vechainfoundation/vechain-sdk-errors'; | ||
|
||
/** | ||
* Account assertions | ||
* | ||
* @group unit/utils-assertions | ||
*/ | ||
describe('Account assertions', () => { | ||
/** | ||
* Assert is address test suite | ||
*/ | ||
describe('assertIsAddress', () => { | ||
/** | ||
* Valid addresses test cases | ||
*/ | ||
accountAssertionsTests.assertIsAddress.valid.forEach(({ value }) => { | ||
test(`should not throw error for assertIsAddress of ${value}`, () => { | ||
// Expect assertIsAddress to not throw | ||
expect(() => { | ||
assertIsAddress(value); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
|
||
/** | ||
* Invalid addresses test cases | ||
*/ | ||
accountAssertionsTests.assertIsAddress.invalid.forEach(({ value }) => { | ||
test(`hould throw error for assertIsAddress of ${value}`, () => { | ||
// Expect assertIsAddress to throw | ||
expect(() => { | ||
assertIsAddress(value); | ||
}).toThrowError(InvalidDataTypeError); | ||
}); | ||
}); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/core/tests/utils/assertions/block-assertions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { blockAssertionsTests } from './fixture'; | ||
import { | ||
assertIsRevisionForAccount, | ||
assertIsRevisionForBlock | ||
} from '../../../src'; | ||
|
||
/** | ||
* Block assertions | ||
* | ||
* @group unit/utils-assertions | ||
*/ | ||
describe('Block assertions', () => { | ||
/** | ||
* Assert is valid revision | ||
*/ | ||
describe('assertIsRevision', () => { | ||
/** | ||
* Valid account endpoint revisions | ||
*/ | ||
blockAssertionsTests.assertIsRevisionForAccount.valid.forEach( | ||
({ value }) => { | ||
test(`should not throw error for assertIsRevision of ${value}`, () => { | ||
expect(() => { | ||
assertIsRevisionForAccount(value); | ||
}).not.toThrow(); | ||
}); | ||
} | ||
); | ||
|
||
/** | ||
* Invalid account endpoint revisions | ||
*/ | ||
blockAssertionsTests.assertIsRevisionForAccount.invalid.forEach( | ||
({ value }) => { | ||
test(`should throw error for assertIsRevision of ${value}`, () => { | ||
expect(() => { | ||
assertIsRevisionForAccount(value); | ||
}).toThrow(); | ||
}); | ||
} | ||
); | ||
}); | ||
|
||
/** | ||
* Assert is valid block revision | ||
*/ | ||
describe('assertIsRevisionForBlock', () => { | ||
/** | ||
* Valid block endpoint revisions | ||
*/ | ||
blockAssertionsTests.assertIsRevisionForBlock.valid.forEach( | ||
({ value }) => { | ||
test(`should not throw error for assertIsRevision of ${value}`, () => { | ||
expect(() => { | ||
assertIsRevisionForBlock(value); | ||
}).not.toThrow(); | ||
}); | ||
} | ||
); | ||
|
||
/** | ||
* Invalid block endpoint revisions | ||
*/ | ||
blockAssertionsTests.assertIsRevisionForBlock.invalid.forEach( | ||
({ value }) => { | ||
test(`should throw error for assertIsRevision of ${value}`, () => { | ||
expect(() => { | ||
assertIsRevisionForBlock(value); | ||
}).toThrow(); | ||
}); | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { Transaction, TransactionHandler } from '../../../src'; | ||
import { signer, transactions } from '../../transaction/fixture'; | ||
|
||
/** | ||
* Generates a random buffer of the specified length | ||
* | ||
* @param length - The length of the buffer to generate | ||
* @returns A random buffer of the specified length | ||
*/ | ||
const generateRandomBytes = (length: number): Buffer => { | ||
const buffer = Buffer.alloc(length); | ||
for (let i = 0; i < length; i++) { | ||
// Generate a random byte | ||
buffer[i] = Math.floor(Math.random() * 256); | ||
} | ||
|
||
return buffer; | ||
}; | ||
|
||
/** | ||
* Generates a random valid address | ||
* | ||
* @returns A random valid address of 20 bytes | ||
*/ | ||
const generateRandomValidAddress = (): string => { | ||
const buffer = generateRandomBytes(20); | ||
|
||
return '0x' + buffer.toString('hex'); | ||
}; | ||
|
||
/** | ||
* Generates a random valid transaction ID | ||
* | ||
* @returns A random valid transaction ID of 32 bytes | ||
*/ | ||
const generateRandomTransactionID = (): string => { | ||
const buffer = generateRandomBytes(32); | ||
|
||
return '0x' + buffer.toString('hex'); | ||
}; | ||
|
||
/** | ||
* Generates a random valid transaction head | ||
* | ||
* @returns - A random valid transaction head of 32 bytes | ||
*/ | ||
const generateRandomTransactionHead = (): string => { | ||
return generateRandomTransactionID(); | ||
}; | ||
|
||
/** | ||
* Account assertion tests | ||
*/ | ||
const accountAssertionsTests = { | ||
assertIsAddress: { | ||
valid: [ | ||
{ | ||
value: generateRandomValidAddress() | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: '0x' | ||
}, | ||
{ | ||
value: '0x0' | ||
} | ||
] | ||
} | ||
}; | ||
|
||
/** | ||
* Transaction assertion tests | ||
*/ | ||
const transactionAssertionsTests = { | ||
assertValidTransactionID: { | ||
valid: [ | ||
{ | ||
value: generateRandomTransactionID() | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: '0x' | ||
}, | ||
{ | ||
value: '0x0' | ||
} | ||
] | ||
}, | ||
assertValidTransactionHead: { | ||
valid: [ | ||
{ | ||
value: generateRandomTransactionHead() | ||
}, | ||
{ | ||
value: undefined | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: '0x' | ||
}, | ||
{ | ||
value: '0x0' | ||
} | ||
] | ||
}, | ||
assertIsSignedTransaction: { | ||
valid: [ | ||
{ | ||
value: TransactionHandler.sign( | ||
new Transaction({ | ||
...transactions.undelegated[0].body | ||
}), | ||
signer.privateKey | ||
) | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: new Transaction({ | ||
...transactions.undelegated[0].body | ||
}) | ||
} | ||
] | ||
} | ||
}; | ||
|
||
/** | ||
* Block assertion tests | ||
*/ | ||
const blockAssertionsTests = { | ||
assertIsRevisionForAccount: { | ||
valid: [ | ||
{ | ||
value: '0x542fd' | ||
}, | ||
{ | ||
value: '100' | ||
}, | ||
{ | ||
value: 100 | ||
}, | ||
{ | ||
value: 'best' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: 'invalid-revision' | ||
}, | ||
{ | ||
value: '0xG8656c6c6f' | ||
}, | ||
{ | ||
value: 'finalized' | ||
} | ||
] | ||
}, | ||
assertIsRevisionForBlock: { | ||
valid: [ | ||
{ | ||
value: '0x542fd' | ||
}, | ||
{ | ||
value: '100' | ||
}, | ||
{ | ||
value: 100 | ||
}, | ||
{ | ||
value: 'best' | ||
}, | ||
{ | ||
value: 'finalized' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
value: 'invalid-revision' | ||
}, | ||
{ | ||
value: '0xG8656c6c6f' | ||
} | ||
] | ||
} | ||
}; | ||
|
||
export { | ||
accountAssertionsTests, | ||
transactionAssertionsTests, | ||
blockAssertionsTests | ||
}; |
114 changes: 114 additions & 0 deletions
114
packages/core/tests/utils/assertions/transaction-assertions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { transactionAssertionsTests } from './fixture'; | ||
import { | ||
assertIsSignedTransaction, | ||
assertValidTransactionHead, | ||
assertValidTransactionID | ||
} from '../../../src'; | ||
|
||
/** | ||
* Transaction assertions | ||
* | ||
* @group unit/utils-assertions | ||
*/ | ||
describe('Transaction assertions', () => { | ||
/** | ||
* Assert valid transaction ID test suite | ||
*/ | ||
describe('assertValidTransactionID', () => { | ||
/** | ||
* Valid transaction IDs test cases | ||
*/ | ||
transactionAssertionsTests.assertValidTransactionID.valid.forEach( | ||
({ value }) => { | ||
test(`should not throw error for assertValidTransactionID of ${value}`, () => { | ||
// Expect assertValidTransactionID to not throw | ||
expect(() => { | ||
assertValidTransactionID(value); | ||
}).not.toThrow(); | ||
}); | ||
} | ||
); | ||
|
||
/** | ||
* Invalid transaction IDs test cases | ||
*/ | ||
transactionAssertionsTests.assertValidTransactionID.invalid.forEach( | ||
({ value }) => { | ||
test(`should throw error for assertValidTransactionID of ${value}`, () => { | ||
// Expect assertValidTransactionID to throw | ||
expect(() => { | ||
assertValidTransactionID(value); | ||
}).toThrowError(); | ||
}); | ||
} | ||
); | ||
}); | ||
|
||
describe('assertValidTransactionHead', () => { | ||
/** | ||
* Valid transaction heads test cases | ||
*/ | ||
transactionAssertionsTests.assertValidTransactionHead.valid.forEach( | ||
({ value }) => { | ||
test(`should not throw error for assertValidTransactionHead of ${value}`, () => { | ||
// Expect assertValidTransactionHead to not throw | ||
expect(() => { | ||
assertValidTransactionHead(value); | ||
}).not.toThrow(); | ||
}); | ||
} | ||
); | ||
|
||
/** | ||
* Invalid transaction heads test cases | ||
*/ | ||
transactionAssertionsTests.assertValidTransactionHead.invalid.forEach( | ||
({ value }) => { | ||
test(`should throw error for assertValidTransactionHead of ${value}`, () => { | ||
// Expect assertValidTransactionHead to throw | ||
expect(() => { | ||
assertValidTransactionHead(value); | ||
}).toThrowError(); | ||
}); | ||
} | ||
); | ||
}); | ||
|
||
/** | ||
* Assert is signed transaction test suite | ||
*/ | ||
describe('assertIsSignedTransaction', () => { | ||
/** | ||
* Valid signed transactions test cases | ||
*/ | ||
transactionAssertionsTests.assertIsSignedTransaction.valid.forEach( | ||
({ value }) => { | ||
test(`should not throw error for assertIsSignedTransaction of ${JSON.stringify( | ||
value | ||
)}`, () => { | ||
// Expect assertIsSignedTransaction to not throw | ||
expect(() => { | ||
assertIsSignedTransaction(value); | ||
}).not.toThrow(); | ||
}); | ||
} | ||
); | ||
|
||
/** | ||
* Invalid signed transactions test cases | ||
*/ | ||
transactionAssertionsTests.assertIsSignedTransaction.invalid.forEach( | ||
({ value }) => { | ||
test(`should throw error for assertIsSignedTransaction of ${JSON.stringify( | ||
value | ||
)}`, () => { | ||
// Expect assertIsSignedTransaction to throw | ||
expect(() => { | ||
assertIsSignedTransaction(value); | ||
}).toThrowError(); | ||
}); | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ork/tests/utils/revision/revision.test.ts → ...ore/tests/utils/revision/revision.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
packages/network/src/clients/thor-client/transactions/helpers/assertions.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/network/src/clients/thor-client/transactions/helpers/index.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
packages/network/src/clients/thorest-client/accounts/helpers/assertions.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e9580d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Coverage
Summary