blockbench
blockbench copied to clipboard
Running CPUheavy contract
The array in cpuheavy contract(blockbench-master/benchmark/contracts/ethereum/cpuheavy.sol) is initialized in descending order. And in function quicksort(),i and j are initialized after the if condition.So it will always return the descending ordered array.How could it enter the while loop since the value of i and j will be always 0.
function quickSort(uint[] arr, uint left, uint right) internal { if (i == j) return; uint i = left; uint j = right;
uint pivot = arr[left + (right - left) / 2];
while (i <= j) {
while (arr[i] < pivot) i++;
while (pivot < arr[j]) j--;
if (i <= j) {
(arr[i], arr[j]) = (arr[j], arr[i]);
i++;
j--;
}
}
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
}
thanks for spotting this. it was a mistake when uploading the source. has been fixed. the source code should be similar to the Sorter.go Hyperledger's contract.
When I tried running with the new contract(CPUheavy), there is error (Error: VM Exception while processing transaction: invalid opcode) for some integer values. eg:- for 4,5,10
@ijingo @bulldosers @dloghin can you reproduce this?
contract Sorter { event finish(uint size, uint signature); function sort(uint size, uint signature) { uint[] memory data = new uint; for (uint x = 0; x < data.length; x++) { data[x] = size-x; } quickSort(data, 0, data.length - 1); finish(size, signature); } function quickSort(uint[] arr, uint left, uint right) internal {
uint i = left;
uint j = right;
if (i == j) return;
uint pivot = arr[left + (right - left) / 2];
while (i <= j) {
while (arr[i] < pivot) i++;
while (pivot < arr[j]) j--;
if (i <= j) {
(arr[i], arr[j]) = (arr[j], arr[i]);
i++;
j--;
}
}
if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); } } We have removed the if condition at the end of the code and now its working fine for all the inputs.
Why is event used to calculate the time taken for sorting ,is there any specific reason behind using event? When running deploy.js the event is listened twice, why it is so ?