blockbench icon indicating copy to clipboard operation
blockbench copied to clipboard

Running CPUheavy contract

Open Ranjananair opened this issue 6 years ago • 5 comments

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);
}

}

Ranjananair avatar Mar 09 '18 12:03 Ranjananair

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.

ug93tad avatar Mar 09 '18 13:03 ug93tad

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

GayuUser avatar Mar 12 '18 12:03 GayuUser

@ijingo @bulldosers @dloghin can you reproduce this?

ug93tad avatar Mar 12 '18 13:03 ug93tad

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.

GayuUser avatar Mar 13 '18 09:03 GayuUser

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 ?

GayuUser avatar Mar 13 '18 10:03 GayuUser