0

This question already answers that in order to get the size of an array programmaticaly, a getter should be defined.

However, assuming that there isn't a getter, is there a way to find out size of a dynamic array in any other way? For example looking at the bytecode.

3 Answers 3

1

Bytecode only includes the original contract code. All the subsequent data is in transactions and I assume you mean the size of an array which is modified after contract deployment.

So I already gave you a hint. The array is modified only in the constructor (upon deployment) and in transactions. Therefore you "only" need to look at the constructor code and go through the transactions. This is basically what nodes do already (at least the more full ones) - they execute all transactions to get the new state.

Also as the current state is always known you should be able to find out in some way how many entries there are in an array but I'm not sure how this could be done.

None of the aforementioned ways are easy (at least if done by hand) - probably some reverse-engineering is required.

1

Sure, this is possible. Size of dynamic array is stored in contract's storage along with array elements. Particular address in storage, where the size of array is stored, depends on how the array is defined. See this page for details. You may use this function to read smart contract's storage.

3
  • I guess the size if a mapping instead is truly unknown by looking at contract storage, since it doesn't store the size, isn't it? Commented Aug 13, 2019 at 14:01
  • Right. Mappings are different and there is no way to count mapping elements, or iterate through mapping keys. Commented Aug 14, 2019 at 8:28
  • Just another question.. Let's say a smartcontract has only a mapping in its storage, and only a function which can add a single element to the mapping. I can still count the number of transaction made to that sc in order to understand how many element are in right? Commented Aug 16, 2019 at 13:57
1

Yes, it is possible, but the means to do it would probably be bad form.

  1. Read contract storage directly, as Mikhail suggests. This involves finding your way around.
  2. Probe it. Attempts to access array elements beyond the length boundary will fail.

It's nice to know there are possibilities in the case of a production contract that cannot be amended.

On the other hand, if you buy into the idea that the contract state should be discoverable, then why omit a way to discover this important information?

Hope it helps.

2
  • Probing assumes that there is a getter for array element by index, but the question is about the situatiion where there is no getter. Commented Aug 13, 2019 at 5:37
  • You might be right. I interpreted it to mean there is no getter for length, itself. Commented Aug 13, 2019 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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