Skip to content
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

return msg.sender.balance; returns unexpected value in Geth #14989

Closed
o0ragman0o opened this issue Aug 17, 2017 · 4 comments
Closed

return msg.sender.balance; returns unexpected value in Geth #14989

o0ragman0o opened this issue Aug 17, 2017 · 4 comments

Comments

@o0ragman0o
Copy link

Referring from Solidity issue #2325

Geth returning an unexpected number when calling a constant function which returns msg.sender.balance

A demonstration contract similar to the following was deployed on Rinkeby here

contract Sender{
        event SenderBal(uint _bal);
        
        // Returns correct address
        function constSenderAddr() public constant returns (address) {
            return msg.sender;
        }
        
        // Geth returns garbage number if called externally 
        function constSenderBal() constant returns (uint256) {
            return msg.sender.balance;
        }
        
        // Geth logs correct value if transaction is generated
        function senderBal() {
            SenderBal(constSenderBal());
        }
    }

System information

Geth version: geth version

  • Various versions tested up to v1.6.7

Expected behaviour

function constSenderBal() constant returns (uint256) should return the balance of the sender when called from console.

Actual behaviour

Returns a huge garbage (perhaps negative) number. Correct number is event logged by a TX calling function senderBal()

Steps to reproduce the behaviour

Behaviour seems only to present only in Geth.

@holiman
Copy link
Contributor

holiman commented Aug 31, 2017

So, regarding msg.sender and constant; if it's invoked via eth_call, the msg.sender is the coinbase or first account of the node executing the call (IIRC from when I looked into this earlier). The thing is, when eth_call is made, there's no actual signed transaction being executed, so geth is forced to use something. And coinbase is most often a better choice than 0x00000..000.

I don't know what geth could do differently, so I'll close this. If it's not working as intended, or I've misinterpreted it, please open it again.

@holiman holiman closed this as completed Aug 31, 2017
@danfinlay
Copy link

Maybe should be reopened, because the first issue did not fully represent the bug reported on remix:

The wrong result is also returned when trying to return the balance of an arbitrary address:

function balanceOfEther(address user) public constant returns (uint256) { return user.balance; }

I documented a detailed account in this comment, and it seems most like a Geth issue to me.

@diegobes
Copy link

diegobes commented Aug 9, 2018

This is related to:

eth/api_backend.go

func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
===> state.SetBalance(msg.From(), math.MaxBig256)
vmError := func() error { return nil }
context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
}

math.MaxBig256 == 115792089237316195423570985008687907853269984665640564039455084007913129639935

Is a constant in Geth( common/math/big.go)

@nuevoalex
Copy link

This issue is preventing the use of eth_calls to check if a function will fail before submitting the tx for some portion of contracts. While there is a workaround this introduces some really bad UX issues and seems like something that should be addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants