Skip to content

Commit

Permalink
fix: improve logical conditions syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed Nov 20, 2023
1 parent 138ad38 commit 8dc0357
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 57 deletions.
17 changes: 17 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ All submissions, including submissions by project members, require review. We us

If you find a bug or want to request a new feature, please open a new issue. When filing a bug report, please provide a clear description of the problem, including the expected behavior and the actual behavior.

# Errors handling conventions
Errors handling is delegated to `errors` pacakge.
Follow all code snapshots and convetion related to it.

Below a brief description of main conventions:

## Input validation
The typical flow to handle errors is the following:

```typescript
import { errors } from 'vechain-sdk';

function some_function(input: any) {
assert(valid_input(input), ERROR_CODE, ERROR_MESSAGE);
}
```

# Code of Conduct

We are committed to providing a welcoming and inclusive environment for everyone who contributes to or interacts with the vechain SDK project. To ensure a positive experience for our community, we have established a [Code of Conduct](CODE_OF_CONDUCT.md) that outlines our expectations for behavior. We encourage all contributors, maintainers, and users to familiarize themselves with this code, as it reflects our commitment to creating a diverse and respectful community. Thank you for helping us maintain a welcoming and collaborative space for all.
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/certificate/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ function encode(cert: Certificate): string {
function verify(cert: Certificate): void {
// No signature
assert(
!(cert.signature === undefined || cert.signature === null),
cert.signature !== undefined && cert.signature !== null,
CERTIFICATE.CERTIFICATE_NOT_SIGNED,
'The certificate not signed.',
"Can't verify the certificate, signature not found",
{ cert }
);

// Invalid signature
assert(
!(
!dataUtils.isHexString(cert.signature as string) ||
(cert.signature as string).length % 2 !== 0
),
dataUtils.isHexString(cert.signature as string) &&
(cert.signature as string).length % 2 === 0,
CERTIFICATE.CERTIFICATE_INVALID_SIGNATURE_FORMAT,
'Invalid signature format.',
{ cert }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const assertCompactFixedHexBlobBuffer = (
);

assert(
!(buffer.length !== 0 && buffer[0] === 0),
buffer.length === 0 || buffer[0] !== 0,
RLP.INVALID_RLP,
'expected no leading zero bytes',
{
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/encoding/rlp/helpers/numerickind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const validateNumericKindData = (data: RLPInput, context: string): bigint => {
*/
const _validateNumericKindNumber = (num: number, context: string): void => {
assert(
!(!Number.isSafeInteger(num) || num < 0),
Number.isSafeInteger(num) && num >= 0,
RLP.INVALID_RLP,
'expected integer',
{
Expand Down Expand Up @@ -71,15 +71,15 @@ const _validateNumericKindString = (str: string, context: string): void => {

// Ensure the string is either a hex or decimal number.
assert(
!(!isHex && !isDecimal),
isHex || isDecimal,
RLP.INVALID_RLP,
'expected non-negative integer in hex or dec string',
{ str, context }
);

// Ensure hex numbers are of a valid length.
assert(
!(isHex && str.length <= 2),
!isHex || str.length > 2,
RLP.INVALID_RLP,
'expected valid hex string number',
{ str, context }
Expand All @@ -105,7 +105,7 @@ const assertValidNumericKindBuffer = (
): void => {
// If maxBytes is defined, ensure buffer length is within bounds.
assert(
!(maxBytes !== undefined && buf.length > maxBytes),
maxBytes === undefined || buf.length <= maxBytes,
RLP.INVALID_RLP,
`expected less than ${maxBytes} bytes`,
{ maxBytes, context }
Expand Down Expand Up @@ -144,7 +144,7 @@ const encodeBigIntToBuffer = (
}

assert(
!(maxBytes !== undefined && hex.length > maxBytes * 2),
maxBytes === undefined || hex.length <= maxBytes * 2,
RLP.INVALID_RLP,
`expected number in ${maxBytes} bytes`,
{ maxBytes, hex, context }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/encoding/rlp/rlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const _unpackData = (
// ScalarKind: Direct decoding using the provided method.
if (kind instanceof RLP.ScalarKind) {
assert(
!(!Buffer.isBuffer(packed) && !(packed instanceof Uint8Array)),
Buffer.isBuffer(packed) || packed instanceof Uint8Array,
RLPError.INVALID_RLP,
'expected buffer',
{ context }
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/mnemonic/mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ function generate(
): string[] {
// Strange edge case in wordlist size
assert(
!(
wordlistSize !== undefined &&
!MNEMONIC_WORDLIST_ALLOWED_SIZES.includes(wordlistSize)
),
wordlistSize === undefined ||
MNEMONIC_WORDLIST_ALLOWED_SIZES.includes(wordlistSize),
HDNODE.INVALID_HDNODE_MNEMONICS,
'Invalid wordlist size given as input. Allowed sizes are 12, 15, 18, 21, 24',
{ wordlistSize }
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/secp256k1/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ function recover(msgHash: Buffer, sig: Buffer): Buffer {
);

assert(
!(!Buffer.isBuffer(sig) || sig.length !== SIGNATURE_LENGTH),
Buffer.isBuffer(sig) && sig.length === SIGNATURE_LENGTH,
SECP256K1.INVALID_SECP256k1_SIGNATURE,
'Invalid signature given as input. Length must be 65 bytes',
{ sig }
);

const recovery = sig[64];
assert(
!(recovery !== 0 && recovery !== 1),
recovery === 0 || recovery === 1,
SECP256K1.INVALID_SECP256k1_SIGNATURE_RECOVERY,
'Invalid signature recovery given as input. Signature bytes in position 64 must be 0 or 1',
{ recovery }
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ class Transaction {
public getSignatureHash(delegateFor?: string): Buffer {
// Correct delegateFor address
assert(
!(
delegateFor !== undefined &&
!addressUtils.isAddress(delegateFor)
),
delegateFor === undefined || addressUtils.isAddress(delegateFor),
ADDRESS.INVALID_ADDRESS,
'Invalid address given as input as delegateFor parameter.',
{ delegateFor }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/bloom/bloom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const isInBloom = (bloom: string, k: number, data: string): boolean => {
);

assert(
!(!Number.isInteger(k) || k <= 0),
Number.isInteger(k) && k > 0,
BLOOM.INVALID_K,
'Invalid k. It should be a positive integer.',
{ k }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const encodeBytes32String = (
*/
const decodeBytes32String = (value: string): string => {
assert(
!(!isHexString(value) || removePrefix(value).length !== 64),
isHexString(value) && removePrefix(value).length === 64,
DATA.INVALID_DATA_TYPE,
`Failed to decode value ${value} to string. Value is not a valid hex string or it is not 64 characters long`,
{ value }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function intrinsicGas(clauses: TransactionClause[]): number {
function _calculateDataUsedGas(data: string): number {
// Invalid data
assert(
!(data !== '' && !dataUtils.isHexString(data)),
data === '' || dataUtils.isHexString(data),
DATA.INVALID_DATA_TYPE,
'Invalid data type. Data should be an hexadecimal string.',
{ data }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/units/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const parseUnits = (
decimals: WEI_UNITS | number | bigint
): bigint => {
assert(
!(typeof value === 'string' && !dataUtils.isNumeric(value)),
typeof value !== 'string' || dataUtils.isNumeric(value),
DATA.INVALID_DATA_TYPE,
`The value "${value}" is not a valid hexadecimal string.`,
{ value }
Expand Down
12 changes: 5 additions & 7 deletions packages/network/src/clients/thor-client/nodes/nodes-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ class NodesClient {
response: BlockDetail | null
): number => {
assert(
!(
response === null ||
response === undefined ||
typeof response !== 'object' ||
!('timestamp' in response) ||
typeof response.timestamp !== 'number'
),
response !== null &&

This comment has been minimized.

Copy link
@pierobassa

pierobassa Nov 20, 2023

Member

response != null can simplify

response !== undefined &&
typeof response === 'object' &&
'timestamp' in response &&
typeof response.timestamp === 'number',
DATA.INVALID_DATA_TYPE,
'Invalid block format returned from node. The block must be an object with a timestamp key present of type number',
{ response }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ class AccountsClient {
);

assert(
!(
options?.revision != null &&
!revisionUtils.isRevisionAccount(options.revision)
),
options?.revision === undefined ||
options?.revision === null ||
revisionUtils.isRevisionAccount(options.revision),
DATA.INVALID_DATA_TYPE,
'Invalid revision. The revision must be a string representing a block number or block id.',
{ revision: options?.revision }
Expand Down Expand Up @@ -87,10 +86,9 @@ class AccountsClient {
);

assert(
!(
options?.revision != null &&
!revisionUtils.isRevisionAccount(options.revision)
),
options?.revision === undefined ||
options?.revision === null ||
revisionUtils.isRevisionAccount(options.revision),
DATA.INVALID_DATA_TYPE,
'Invalid revision. The revision must be a string representing a block number or block id.',
{ revision: options?.revision }
Expand Down Expand Up @@ -131,18 +129,17 @@ class AccountsClient {
);

assert(
!(
options?.revision != null &&
!revisionUtils.isRevisionAccount(options.revision)
),
options?.revision === undefined ||
options?.revision === null ||
revisionUtils.isRevisionAccount(options.revision),
DATA.INVALID_DATA_TYPE,
'Invalid revision. The revision must be a string representing a block number or block id.',
{ revision: options?.revision }
);

// The position represents a slot in the VM storage. Each slot is 32 bytes.
assert(
!(!dataUtils.isHexString(position) || position.length !== 66),
dataUtils.isHexString(position) && position.length === 66,
DATA.INVALID_DATA_TYPE,
'Invalid `position`. The position must be a hex string of 32 bytes (66 characters including `0x` prefix).',
{ position }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class BlocksClient {
options?: BlockInputOptions
): Promise<BlockDetail | null> {
assert(
!(revision != null && !revisionUtils.isRevisionBlock(revision)),
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.',
{ revision }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ class TransactionsClient {

// Invalid head
assert(
!(
options?.head !== undefined &&
!dataUtils.isThorId(options?.head, true)
),
options?.head === undefined ||
dataUtils.isThorId(options?.head, true),
DATA.INVALID_DATA_TYPE,
'Invalid head given as input. Input must be an hex string of length 64.',
{ head: options?.head }
Expand Down Expand Up @@ -95,10 +93,8 @@ class TransactionsClient {

// Invalid head
assert(
!(
options?.head !== undefined &&
!dataUtils.isThorId(options?.head, true)
),
options?.head === undefined ||
dataUtils.isThorId(options?.head, true),
DATA.INVALID_DATA_TYPE,
'Invalid head given as input. Input must be an hex string of length 64.',
{ head: options?.head }
Expand Down Expand Up @@ -174,7 +170,9 @@ class TransactionsClient {
} = options ?? {};

assert(
!(revision != null && !revisionUtils.isRevisionAccount(revision)),
revision === undefined ||
revision === null ||
revisionUtils.isRevisionAccount(revision),
DATA.INVALID_DATA_TYPE,
'Invalid revision given as input. Input must be a valid revision (i.e., a block number or block ID).',
{ revision }
Expand Down

0 comments on commit 8dc0357

Please sign in to comment.