Sometimes I lose array items in Ethereum smart-contract

alex_romanovalex_romanov Member Posts: 2
I have found strange problem in my smart contract. I use testrpc (v4.1.3) and last MetaMask as local dev environment. Please, see code below (it is short version, just place with problem):
struct PayRecord {
    address sender;
    uint256 sum;
    uint256 blockNumber;
    uint256 status;
}

event PaymentPlaced(address senderAddress, uint256 blockNumber, uint256 payIndex, string guid);

PayRecord[] public payments;

function payForSomething(string guid) payable {
    uint256 newLength = payments.push(PayRecord(msg.sender, msg.value, block.number, 0));
    PaymentPlaced(msg.sender, block.number, newLength-1, guid);
}

function changeSomething(uint256 paymentIndex) {
    if(payments[paymentIndex].status == 0){
        payments[paymentIndex].status == 1;
    }
}
Often everything works fine:
1. user calls function payForSomething()
2. user gets event PaymentPlaced in browser
3. user calls changeSomething()

but...

SOMETIMES I have next problem:
1. user calls payForSomething()
2. user gets event PaymentPlaced (for example: blockNumber = 10, payIndex = 5).
3. user calls changeSomething() - and gets error "invalid opcode"
4. if user calls payForSomething() again - he gets event PaymentPlaced with data (blockNumber == 15, payIndex = 5)

The same array length, different block numbers, two insertion events...

So...Sometimes it happens, sometimes everything works fine. I always have only one user, only I works with this smart-contract (I'm still developing it).

Why do I lose items from array?

Answers

Sign In or Register to comment.