New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting bytes32 to int always returning 0 #34

Closed
aakilfernandes opened this Issue Aug 27, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@aakilfernandes
Copy link

aakilfernandes commented Aug 27, 2015

Not sure if I this is a bug or I misunderstand how it should work, but the following function returns 0 for all blocks

block.blockhash(blockIndex) returns a bytes32 so I was expecting a uint256

function getHashOfBlock(uint blockIndex) constant returns(uint){
    return uint(block.blockhash(blockIndex));
}
@LianaHus

This comment has been minimized.

Copy link
Contributor

LianaHus commented Aug 28, 2015

http://gavwood.com/paper.pdf page 25.
"0 is left on the stack if the looked for block number is greater than the current block number
or more than 256 blocks behind the current block"
Please check if this is your case. I think this is not because of conversion but because the blockhash returns 0.

@aakilfernandes

This comment has been minimized.

Copy link

aakilfernandes commented Aug 28, 2015

Just checked again, its for blocks I have in my chain already (even the current block)

Here's the live contract you can test against

https://github.com/etherpot/contract/blob/master/app/contracts/lotto.sol#L105-L107
https://etherchain.org/account/0x539f2912831125c9b86451420bc0d37b219587f9

You can reproduce by putting the following lines in your geth console

> var LottoAbi = [{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"}],"name":"getPot","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"buyer","type":"address"}],"name":"getTicketsCountByBuyer","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"subpotIndex","type":"uint256"}],"name":"getIsCashed","outputs":[{"name":"","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"buyer","type":"address"}],"name":"getBuyers","outputs":[{"name":"","type":"address[]"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"}],"name":"getSubpotsCount","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"subpotIndex","type":"uint256"}],"name":"calculateWinner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":true,"inputs":[],"name":"getRoundIndex","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"blockIndex","type":"uint256"}],"name":"getHashOfBlock","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"getBlocksPerRound","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"getTicketPrice","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"}],"name":"getSubpot","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"subpotIndex","type":"uint256"}],"name":"cash","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"roundIndex","type":"uint256"},{"name":"subpotIndex","type":"uint256"}],"name":"getDecisionBlockNumber","outputs":[{"name":"","type":"uint256"}],"type":"function"}]

> var Lotto = web3.eth.contract(LottoAbi).at('0x539f2912831125c9b86451420bc0d37b219587f9')

> Lotto.getHashOfBlock(0)
'0'    
@aakilfernandes

This comment has been minimized.

Copy link

aakilfernandes commented Aug 28, 2015

This might shine some light on the issue: I'm not having the problem on my local test chain but only when I deploy to the main chain.

@aakilfernandes

This comment has been minimized.

Copy link

aakilfernandes commented Aug 28, 2015

A little more context: I've tried deploying with both embark and truffle. Same issue. (Truffle deployed contract here: 0xbea678e58758ab86fc731e30139b34e106f3ca57)

@aakilfernandes

This comment has been minimized.

Copy link

aakilfernandes commented Aug 30, 2015

256 blocks behind the current block

sorry, totally missed that part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment