Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate 500 dummy contracts #304

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion src/store/selectors/contracts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,111 @@
import { createSelector } from 'reselect';
import { v4 as uuidv4 } from 'uuid';
import * as nodeCrypto from 'crypto';
import sortBy from 'lodash/sortBy';

function createRandomContract() {
const contract = {
id: generateContractAddress(),
price: Math.floor(Math.random() * 100000000).toString(),
speed: Math.floor(
Math.random() * (1500000000000000 - 50000000000000 + 1) + 50000000000000
),
length: Math.floor(Math.random() * (28800 - 3600 + 1) + 3600), // random rounded number between 1 and 8 hours
buyer: '0x0000000000000000000000000000000000000000',
seller: generateContractAddress(),
timestamp:
Date.now() -
Math.floor(Math.random() * (3 * 7 * 24 * 60 * 60 * 1000)).toString(), // random timestamp within the last 3 weeks
Comment on lines +6 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createRandomContract function generates a contract with random values. However, it does not ensure that the generated contract is valid before returning it. If an invalid contract is generated, it will be logged to the console, but the function will still return undefined. This could lead to potential issues downstream where the returned value is used without checking for undefined. To improve this, you should ensure that a valid contract is always returned. You could do this by generating a new contract in a loop until a valid one is created.

state: '0',
encryptedPoolData: '',
limit: '0',
isDead: false,
balance: (Math.random() * 10000000).toString(),
stats: {
successCount: Math.floor(Math.random() * 10).toString(),
failCount: Math.floor(Math.random() * 10).toString()
},
hasFutureTerms: false,
futureTerms: null,
history: [],
version: '0'
};
if (isValidDummyContract(contract)) {
return contract;
} else {
console.log('Invalid dummy contract generated');
}
}

function generateContractAddress() {
// Generate a UUID
const uniqueId = uuidv4();

// Hash the UUID using SHA-256 to get a predictable length
const hash = nodeCrypto.createHash('sha256');
hash.update(uniqueId);
const hashedUniqueId = hash.digest('hex'); // This will create a 64 characters hexadecimal string

// Get the first 40 characters to conform to the Ethereum address style
const address = '0x' + hashedUniqueId.substring(0, 40);

return address; // this is synchronous and returns immediately
Comment on lines +41 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generateContractAddress function generates a contract address by hashing a UUID and taking the first 40 characters. However, this does not guarantee that the generated address is unique. If two UUIDs have the same first 40 characters when hashed, they will generate the same contract address. This could lead to potential issues if uniqueness of contract addresses is required. To improve this, you should ensure that the generated contract address is unique. You could do this by keeping track of all generated addresses and generating a new one if a collision occurs.

}

function generateDummyContracts() {
let newContracts = {};

for (let i = 0; i < 5; i++) {
const newContract = createRandomContract(); // synchronous call
if (newContract) {
newContracts[newContract.id] = newContract; // Check to prevent any issue if a contract was not created properly
}
}
return newContracts;
}
Comment on lines +55 to +65

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generateDummyContracts function generates a fixed number of dummy contracts and stores them in an object using their IDs as keys. However, it does not handle the case where createRandomContract returns undefined. If this happens, an undefined key will be added to the newContracts object, which could lead to potential issues when trying to access the contracts by their IDs later on. To improve this, you should check if newContract is undefined before adding it to newContracts.


function isValidDummyContract(contract) {
// Regular expression to validate if a string is a hexadecimal number
const hexRegExp = /^0x[a-fA-F0-9]{40}$/;

return (
hexRegExp.test(contract.id) &&
contract.price > 0 &&
contract.speed >= 50000000000000 && // strictly for dummy data
contract.speed <= 1500000000000000 && // strictly for dummy data
hexRegExp.test(contract.seller) &&
hexRegExp.test(contract.buyer)
);
}

// Cache the contracts to prevent regeneration on refresh
let cachedDummyContracts = null;

function getDummyContracts() {
if (!cachedDummyContracts) {
// If contracts haven't been created yet, generate them
cachedDummyContracts = generateDummyContracts();
}
return cachedDummyContracts;
}
Comment on lines +84 to +90

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getDummyContracts function uses a global variable cachedDummyContracts to cache the generated contracts. This approach can lead to issues in a multi-threaded environment where multiple threads might access and modify this variable simultaneously, leading to data races.
To avoid this, consider using a more thread-safe approach to cache your data. For example, you could use a caching library that provides thread-safe operations, or you could implement your own synchronization mechanism to ensure that only one thread can access this variable at a time.


// Returns the array of transactions of the current chain/wallet/address.
// The items are mapped to contain properties useful for rendering.
export const getContracts = state => state.contracts;
export const getContracts = state => {
const dummyContracts = getDummyContracts();
const realContracts = state.contracts;

const combinedActiveContracts = {
...realContracts.actives,
...dummyContracts
};

const combinedContracts = {
...realContracts,
actives: combinedActiveContracts
};
return combinedContracts;
};

export const getActiveContracts = createSelector(getContracts, contractsData =>
sortBy(Object.values(contractsData.actives), 'timestamp')
Expand Down
Loading