0

I am a newbie to this field so I apologize in advance if this seems trivial.

Say I have written some code in Solidity, for instance something like the quick sort code (https://gist.github.com/subhodi/b3b86cc13ad2636420963e692a4d896f).

Now I want to locally run it with my evm virtual machine (due to certain reason, we prefer to run it locally with virtual machine, instead of using some web services...), so I would use commands like:

solc --bin --optimize -o . qs.sol
./evm --debug --code $(cat QuickSort.bin) run

So after executing the above commands, I do see some logs printed out; however, I am still unclear about:

  1. How exactly can I specify a function to execute? In the above quick sort case I only have one "public" function, but what if I have many?

  2. How to pass in parameters into the evm when executing certain functions?

  3. Is there any convenient way I can expect to read some output? For instance can evm return a sorted array and somehow print it out, or write into some log files?

2 Answers 2

1

Some theory basics are confusing you.

Everything "runs" on the EVM which is a distributed state machine. You can set up a local private blockchain or ganache-cli or even Remix to "run" locally in an isolated context.

Your constant function sort() is read-only and therefore will always run locally in all contexts. constant is deprecated. You could use pure.

To use the function, you would sign and call sort(arguments[]) using Web3. It should return a sorted array if everything goes as planned.

I would caution that this is not an ideal project for getting acquainted with Ethereum and also possibly a horrible idea for a production system owing to the gas cost and inherent limitations of performing this sort process on-chain. Sort should be avoided if at all possible. It usually is possible. https://blog.b9lab.com/the-joy-of-minimalism-in-smart-contract-design-2303010c8b09

Hope it helps.

1
  • A private network is a valid test environment but it's a tricky thing when you're just starting out. It initializing with your own genesis block, disabling peer-discovery (so it doesn't go looking), ensuring something is mining (or else nothing will ever happen), then the usual process of deploying contracts and interacting. Oct 26, 2018 at 18:23
2

Regarding your first two points: Parameters are passed via so-called "Call Data". Call data is retrieved via certain EVM opcodes (CALLDATACOPY, CALLDATASIZE, CALLDATALOAD). In your testing environment, using the evm tool, you may provide call data with the --input parameter, eg: --input 11223344 will provide 4 bytes of data, 0x11223344.

The public ABI of Ethereum contracts specify that the first 4 bytes of call data are your public function selector (calculated as the top 4-bytes of the Keccak256 hash of a public function normalized prototype). That hash is provided by solc when you compile your source.

That should answer points 1 and 2. You will find more information on public ABI here: https://solidity.readthedocs.io/en/latest/abi-spec.html

Your Answer

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

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