git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit e74b3f639022db04c2ec7782c0b9320add8b05db

Kernel: move account state to the map interface

Alex Beregszaszi committed on 8/23/2016, 12:27:46 AM
Parent: dbb9b6eacbc6786a69bbe76a01aa4fb1d930ff18

Files changed

environment.jschanged
index.jschanged
environment.jsView
@@ -43,13 +43,13 @@
4343 this.state.set(address.toString(), account)
4444 }
4545
4646 getBalance (address) {
47- return this.state.get(address.toString()).balance
47+ return this.state.get(address.toString())['balance']
4848 }
4949
5050 getCode (address) {
51- return this.state.get(address.toString()).code
51+ return this.state.get(address.toString())['code']
5252 }
5353
5454 getBlockHash (height) {
5555 return blockChain.getBlock(height).hash()
index.jsView
@@ -67,22 +67,28 @@
6767 // Special case: contract deployment
6868 // FIXME: place this in the best location with the best condition checking
6969 if (address.isZero()) {
7070 if (data.length !== 0) {
71- let codeHash = sha3(data)
72- this.environment.state.set(codeHash, data);
73- this.environment.state.set(address.toString(), { balance: value, codeHash: codeHash })
71+ console.log('This is a contract deployment transaction')
72+
73+ let account = new Map()
74+ account.set('nonce', new U256(0))
75+ account.set('balance', value)
76+ account.set('code', data)
77+ account.set('storage', new Map())
78+
79+ // FIXME: calculate the contract address
80+ this.environment.state.set(address.toString(), account)
7481 }
7582 }
7683
7784 let account = this.environment.state.get(address.toString())
7885 if (!account) {
7986 throw new Error('Account not found')
8087 }
8188
82- const code = this.environment.state.get(account.codeHash)
83-
84- if (!code) {
89+ const code = Uint8Array.from(account.get('code'))
90+ if (code.length === 0) {
8591 throw new Error('Contract not found')
8692 }
8793
8894 if (!Utils.isWASMCode(code)) {
@@ -136,19 +142,19 @@
136142 throw new Error('Sender account not found')
137143 }
138144
139145 // deduct gasLimit * gasPrice from sender
140- if (fromAccount.balance.lt(tx.gasLimit.mul(tx.gasPrice))) {
141- throw new Error('Insufficient account balance')
146+ if (fromAccount.get('balance').lt(tx.gasLimit.mul(tx.gasPrice))) {
147+ throw new Error(`Insufficient account balance: ${fromAccount.get('balance').toString()} < ${tx.gasLimit.mul(tx.gasPrice).toString()}`)
142148 }
143149
144- fromAccount.balance = fromAccount.balance.sub(ts.gasLimit.mul(tx.gasPrice))
150+ fromAccount.set('balance', fromAccount.get('balance').sub(tx.gasLimit.mul(tx.gasPrice)))
145151
146152 let ret = this.callHandler(tx.to, tx.gasLimit, tx.gasPrice, tx.value, tx.data)
147153
148154 // refund gas
149155 if (ret.executionOutcome === 1) {
150- fromAccount.balance = fromAccount.balance.add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund)))
156+ fromAccount.set('balance', fromAccount.get('balance').add(tx.gasPrice.mul(ret.gasLeft.add(ret.gasRefund))))
151157 }
152158
153159 // save new state?
154160

Built with git-ssb-web