0

Assume a struct Node and an array of Nodes allNodes[]. I was successful in adding Node to the array but when I tried to use a removeNode() method (which uses the delete method in arrays), the value of allNodes.length remains the same. Is it because blockchains are "immutable". Doesn't immutable mean that when a transaction is made (even remove Node()), it cannot be undone?

1

It's possible, but it doesn't do exactly what you (probably) expect.

It signals "no important data here" and helps keep things efficient at the platform level but it doesn't reorganize the array at the application level, by which I mean it doesn't shift all the elements above the deleted row down one row. Length doesn't change.

Immutability has an impact here. No matter what we do, we are only appending to the previous state. Nothing is truly deleted. Even the transactions that created, updated and ultimately deleted the "row" are part of the immutable history. "Delete" can't be about obliterating data, but it might be about reorganizing it.

Have a look over here: Are there well-solved and simple storage patterns for Solidity?

"Mapped Structs with Delete-enabled Index" does a logical delete. It's appropriate for situations where a high percentage of rows will be deleted and you want to avoid looping over garbage.

The much simpler "Mapping with Struct" is more common. It sets a "bit" to indicate a logical delete.

Hope it helps.

  • Thank you sir....but is it legal to manually increase/decrease the allNodes.length variable? Like allNodes.length ++ or allNodes.length -- ... – yobro97 Jun 21 '17 at 18:14
  • Yes. Possibly someone will chime in some certainty about all the edge cases around ++. I haven't used it enough to be certain and I don't wish to mislead you. Probably a matter of taste - I like to push() because it leaves no doubt about what exists in the new row. – Rob Hitchens - B9lab Jun 21 '17 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.