I decided to test out a proof-of-concept for what I figure would be the ideal application of contract programming -- a bounty for finding large prime numbers. I put together this quick-and-dirty implementation in Etherscripter and then tidied it up by hand.
But why won't it compile?
init:
data bounty = msg.data[0]
data minimum = msg.data[1]
data killswitch = msg.sender
data results[]
code:
if tx.origin == self.killswitch:
# Shutting down contract.
suicide(self.killswitch)
else:
candidate = msg.data[0]
if (candidate % 2) == 0 or candidate < self.minimum or (self.results[candidate] % 2) == 1 or self.results[candidate] == candidate:
# This number is too small, even, or has already been tested.
stop
else:
if self.results[candidate]:
# Even number in storage means restore previous progress.
factor = (self.results[candidate] + 1)
else:
factor = 3
while (factor * factor) <= candidate:
if (candidate % factor) == 0:
# Found a factor, so this number isn't prime. Sorry!
self.results[candidate] = factor
stop
else:
# Save progress in case we run out of gas. Use an even number to indicate we're not done.
self.results[candidate] = (factor + 1)
factor = (factor + 2)
# Congratulations, you found a new prime! Here's your reward.
self.results[candidate] = candidate
send(tx.origin, self.bounty, (msg.gas - 100))
Also, is there a way to get a more useful error message than this -- say, one that points to a specific line of Serpent code?
$ serpent compile primebounty.se
compile ['primebounty.se'] {}
Error (file "main", line 1, char 0): Invalid argument count or LLL function: (init (seq (data (set 'bounty (mload (. (get 'msg) (get 'data))))) (data (set 'minimum (mload (add (. (get 'msg) (get 'data)) 32)))) (data (set 'killswitch (caller)))))
Traceback (most recent call last):
File "/usr/local/bin/serpent", line 9, in
load_entry_point('ethereum-serpent==2.0.1', 'console_scripts', 'serpent')()
File "/usr/local/bin/serpent.py", line 225, in main
o = globals()[cmd](*args, **kwargs)
File "/usr/local/bin/serpent.py", line 92, in
compile = lambda code, **kwargs: pyext.compile(strtobytes(pre_transform(code, kwargs)))
Exception: Error (file "main", line 1, char 0): Invalid argument count or LLL function: (init (seq (data (set 'bounty (mload (. (get 'msg) (get 'data))))) (data (set 'minimum (mload (add (. (get 'msg) (get 'data)) 32)))) (data (set 'killswitch (caller)))))
Comments