Go-ethereum: Insufficient funds error caused by zero balance , but getBalance is not 0

Created on 12 Nov 2017  Â·  3Comments  Â·  Source: ethereum/go-ethereum

When attempting to send transaction, geth returns error: Insufficient funds for gas * price + value.

2173 is similar with my problem, but i found the balance is 0 when send tx while getBalance is 230000000000000000000

System information

⚡ leonzhao☯ node# private geth version
Geth
Version: 1.7.2-stable
Git Commit: 1db4ecdc0b9e828ff65777fb466fc7c1d04e0de9
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.2
Operating System: linux
GOPATH=/home/leonzhao/workspace/go
GOROOT=/usr/local/go

Code snippet

Here's the code that I print the actual account and the balance when send transaction

    // Transactor should have enough funds to cover the costs
    // cost == V + GP * GL
    fmt.Printf("From: %x, Balance: %d\n", from, pool.currentState.GetBalance(from))
    fmt.Printf("Cost: %d\n", tx.Cost())
    if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
        return ErrInsufficientFunds
    }

Expected behaviour

Calling eth.sendTransaction with a gas price as recommended by eth.gasPrice should successfully build and broadcast a transaction to the network.

Actual behaviour

Calling eth.sendTransaction with a gas price as recommended by eth.gasPrice returns the following error:

> eth.getBalance(eth.accounts[0])
230000000000000000000
> eth.estimateGas({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"})
21000
> eth.gasPrice
18000000000
> eth.estimateGas({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"}) * eth.gasPrice + 0x1000
378000000004096
> eth.sendTransaction({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"})
From: 5d277e1aaf29f49699efffb2ccc368a50d5163ff, Balance: 0
Cost: 1620000000004096
Error: insufficient funds for gas * price + value
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at <anonymous>:1:1

 ```
#### Question: 
1. Why does the balance is `230000000000000000000` after mined, but it is `0` when i send tx
2. Why does the account `5d277e1aaf29f49699efffb2ccc368a50d5163ff` printed not same with the actual account `0x2cdec91d4e97114f50806ccb033b8fcd777614e0`
3. Why does  (gas * price + value)  is different? `378000000004096`  vs `Cost: 1620000000004096`

#### Steps to reproduce the behaviour
`genesis.json`

{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty" : "0x400",
"gasLimit" : "2100000",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : {
}
}


⚡ leonzhao☯ node# private geth --datadir ./data04 init ./genesis.json
WARN [11-12|09:07:24] No etherbase set and no accounts found as default
INFO [11-12|09:07:24] Allocated cache and file handles database=/home/leonzhao/workspace/github/ethereum/private/data04/geth/chaindata cache=16 handles=16
INFO [11-12|09:07:24] Writing custom genesis block
INFO [11-12|09:07:24] Successfully wrote genesis state database=chaindata hash=8c6e45…eb10f7
INFO [11-12|09:07:24] Allocated cache and file handles database=/home/leonzhao/workspace/github/ethereum/private/data04/geth/lightchaindata cache=16 handles=16
INFO [11-12|09:07:24] Writing custom genesis block
INFO [11-12|09:07:24] Successfully wrote genesis state database=lightchaindata

⚡ leonzhao☯ node# private geth --datadir ./data04 --networkid 1018 --ipcdisable --port 61921 --rpcport 8301 --bootnodes "enode://f3c57bc9e0e374974301f53b94f0a704bb950ffde34145e36c6749281ab64a0604bd4030b1cfd42b44f22f405247cd85f4f8ac8e46fffa99aed4d681a4effc83@100.120.184.204:61920" console
WARN [11-12|09:09:46] No etherbase set and no accounts found as default
INFO [11-12|09:09:46] Starting peer-to-peer node instance=Geth/v1.7.2-stable-1db4ecdc/linux-amd64/go1.9.2
INFO [11-12|09:09:46] Allocated cache and file handles database=/home/leonzhao/workspace/github/ethereum/private/data04/geth/chaindata cache=128 handles=1024
WARN [11-12|09:09:46] Upgrading database to use lookup entries
INFO [11-12|09:09:46] Initialised chain configuration config="{ChainID: 0 Homestead: 0 DAO: DAOSupport: false EIP150: EIP155: 0 EIP158: 0 Byzantium: Engine: unknown}"
INFO [11-12|09:09:46] Disk storage enabled for ethash caches dir=/home/leonzhao/workspace/github/ethereum/private/data04/geth/ethash count=3
INFO [11-12|09:09:46] Disk storage enabled for ethash DAGs dir=/home/leonzhao/.ethash count=2
INFO [11-12|09:09:46] Initialising Ethereum protocol versions="[63 62]" network=1018
INFO [11-12|09:09:46] Loaded most recent local header number=0 hash=8c6e45…eb10f7 td=1024
INFO [11-12|09:09:46] Loaded most recent local full block number=0 hash=8c6e45…eb10f7 td=1024
INFO [11-12|09:09:46] Loaded most recent local fast block number=0 hash=8c6e45…eb10f7 td=1024
INFO [11-12|09:09:46] Regenerated local transaction journal transactions=0 accounts=0
INFO [11-12|09:09:46] Starting P2P networking
INFO [11-12|09:09:46] Database deduplication successful deduped=0
INFO [11-12|09:09:46] UDP listener up self=enode://7e52a1a10a177767b2c25d7d0efe4215386873f4a96faf48d27e2ce0f604ee64cf64dcd76c4faf73a3c13a14813979f977fffea36f5e921fb70d0dff9ea8276f@100.120.184.204:61921
INFO [11-12|09:09:46] RLPx listener up self=enode://7e52a1a10a177767b2c25d7d0efe4215386873f4a96faf48d27e2ce0f604ee64cf64dcd76c4faf73a3c13a14813979f977fffea36f5e921fb70d0dff9ea8276f@100.120.184.204:61921
INFO [11-12|09:09:46] Mapped network port proto=udp extport=61921 intport=61921 interface=NAT-PMP(192.168.1.1)
INFO [11-12|09:09:46] Mapped network port proto=tcp extport=61921 intport=61921 interface=NAT-PMP(192.168.1.1)
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.2-stable-1db4ecdc/linux-amd64/go1.9.2
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

admin.peers
[{
caps: ["eth/63"],
id: "f3c57bc9e0e374974301f53b94f0a704bb950ffde34145e36c6749281ab64a0604bd4030b1cfd42b44f22f405247cd85f4f8ac8e46fffa99aed4d681a4effc83",
name: "Geth/v1.7.2-stable-1db4ecdc/linux-amd64/go1.9.2",
network: {
localAddress: "192.168.1.104:61921",
remoteAddress: "100.120.184.204:51032"
},
protocols: {
eth: {
difficulty: 1024,
head: "0x8c6e45a523c80e9dde88005b37f44f4d46c857226f972f729794123a82eb10f7",
version: 63
}
}
}]
personal.newAccount()
Passphrase:
Repeat passphrase:
"0x2cdec91d4e97114f50806ccb033b8fcd777614e0"
miner.start(4)
INFO [11-12|09:11:08] Updated mining threads threads=4
INFO [11-12|09:11:08] Transaction pool price threshold updated price=18000000000
INFO [11-12|09:11:08] Starting mining operation
null
INFO [11-12|09:11:08] Commit new mining work number=1 txs=0 uncles=0 elapsed=262.567µs
INFO [11-12|09:11:34] Successfully sealed new block number=1 hash=eff680…e7d880
INFO [11-12|09:11:34] 🔨 mined potential block number=1 hash=eff680…e7d880
INFO [11-12|09:11:34] Commit new mining work number=2 txs=0 uncles=0 elapsed=276.505µs
INFO [11-12|09:11:35] Successfully sealed new block number=2 hash=1e0096…041e22
INFO [11-12|09:11:35] 🔨 mined potential block number=2 hash=1e0096…041e22
INFO [11-12|09:11:35] Commit new mining work number=3 txs=0 uncles=0 elapsed=147.292µs
......
......
INFO [11-12|09:12:31] 🔗 block reached canonical chain number=40 hash=7a7f7d…91fb44
INFO [11-12|09:12:31] 🔨 mined potential block number=45 hash=536af7…e4d1dc
INFO [11-12|09:12:31] Commit new mining work number=46 txs=0 uncles=0 elapsed=173.282µs
miner.stop(INFO [11-12|09:12:32] Successfully sealed new block number=46 hash=5abfa8…945f79
INFO [11-12|09:12:32] 🔗 block reached canonical chain number=41 hash=1cee66…45a5f9
INFO [11-12|09:12:32] 🔨 mined potential block number=46 hash=5abfa8…945f79
INFO [11-12|09:12:32] Commit new mining work number=47 txs=0 uncles=0 elapsed=156.68µs
miner.stop()
true
personal.listAccounts
["0x2cdec91d4e97114f50806ccb033b8fcd777614e0"]
personal.unlockAccount(personal.listAccounts[0])
Unlock account 0x2cdec91d4e97114f50806ccb033b8fcd777614e0
Passphrase:
true
eth.getBalance(eth.accounts[0])
230000000000000000000
eth.estimateGas({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"})
21000
eth.gasPrice
18000000000
eth.estimateGas({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"}) * eth.gasPrice + 0x1000
378000000004096
eth.sendTransaction({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"})
From: 5d277e1aaf29f49699efffb2ccc368a50d5163ff, Balance: 0
Cost: 1620000000004096
Error: insufficient funds for gas * price + value
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at :1:1

```

Backtrace

[backtrace]

Most helpful comment

I've solved this issue. The reason is that the chainId is 0 in genesis.json. It works fine when I set chainId any other number but not zero.

"config": {
          "chainId": 0,
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0
      },

All 3 comments

If you don't specify an exact gas limit amount, I think geth defaults to 90K, not an estimate (I believe this to be a legacy value from the era when we didn't have a good enough gas estimation). That might explain a lot of issues.

Ahh i see. The actual cost is 1620000000004096 = 90*1000*1000*18000000000(eth.gasPrice) + 0x1024 .
But i'm very confused the balance is zero while eth.getBalance = 230000000000000000000, which caused Error: Insufficient funds

> eth.getBalance(eth.accounts[0])
- 230000000000000000000
> eth.sendTransaction({from: "0x2cdec91d4e97114f50806ccb033b8fcd777614e0", to: "0x750f3c3070fbd324650b7875d65b571aa4cf94d7", value: "0x1000"})
From: 5d277e1aaf29f49699efffb2ccc368a50d5163ff, Balance: 0
Cost: 1620000000004096
Error: insufficient funds for gas * price + value
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at <anonymous>:1:1

and the account address is not same:
0x2cdec91d4e97114f50806ccb033b8fcd777614e0 vs 5d277e1aaf29f49699efffb2ccc368a50d5163ff

code snippet (core/tx_pool.go )

    // Transactor should have enough funds to cover the costs
    // cost == V + GP * GL
    fmt.Printf("From: %x, Balance: %d\n", from, pool.currentState.GetBalance(from))
    fmt.Printf("Cost: %d\n", tx.Cost())
    if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
        return ErrInsufficientFunds
    }

I've solved this issue. The reason is that the chainId is 0 in genesis.json. It works fine when I set chainId any other number but not zero.

"config": {
          "chainId": 0,
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0
      },
Was this page helpful?
0 / 5 - 0 ratings