-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: dev
Are you sure you want to change the base?
Conversation
src/store/selectors/contracts.js
Outdated
function getRandomHex(size = 40) { | ||
let result = []; | ||
let characters = 'ABCDEF0123456789'; | ||
let charactersLength = characters.length; | ||
for (let i = 0; i < size; i++) { | ||
result.push( | ||
characters.charAt(Math.floor(Math.random() * charactersLength)) | ||
); | ||
} | ||
return '0x' + result.join(''); | ||
} |
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.
The getRandomHex
function generates a random hexadecimal string, which is used as an ID for contracts. This approach can lead to collisions where two contracts might end up with the same ID. This could cause serious issues in your application, such as overwriting data or retrieving incorrect data.
To mitigate this, consider using a more reliable method for generating unique IDs. There are many libraries available that can generate unique IDs, such as UUID or nanoid. These libraries are designed to minimize the chance of collisions.
src/store/selectors/contracts.js
Outdated
function createRandomContract() { | ||
const contract = { | ||
id: getRandomHex(), | ||
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: getRandomHex(), | ||
timestamp: | ||
Date.now() - | ||
Math.floor(Math.random() * (3 * 7 * 24 * 60 * 60 * 1000)).toString(), // random timestamp within the last 3 weeks | ||
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' | ||
}; | ||
|
||
return contract; | ||
} |
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.
The createRandomContract
function generates a contract with random values. However, it does not validate these values. For example, it does not check if the generated price
, speed
, length
, timestamp
, and balance
are within acceptable ranges or formats. This could lead to contracts with invalid or unrealistic data.
Consider adding validation checks to ensure that the generated values meet your application's requirements. For example, you could check if the price
is a positive number, if the timestamp
is a valid date, etc.
function getDummyContracts() { | ||
if (!cachedDummyContracts) { | ||
// If contracts haven't been created yet, generate them | ||
cachedDummyContracts = generateDummyContracts(); | ||
} | ||
return cachedDummyContracts; | ||
} |
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.
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.
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 |
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.
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.
// 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 |
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.
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; | ||
} |
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.
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
.
No description provided.