Merge pull request #218 from jpmorganchase/getDualState

Get dual state
This commit is contained in:
Joel Burget 2017-11-09 20:18:37 -05:00 committed by GitHub
commit a6f117d138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View File

@ -119,8 +119,9 @@ func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *
func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var (
db = getDualState(evm, contract.Address())
y, x = stack.Back(1), stack.Back(0)
val = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
val = db.GetState(contract.Address(), common.BigToHash(x))
)
// This checks for 3 scenario's and calculates gas accordingly
// 1. From a zero-value address to a non-zero value (NEW VALUE)
@ -130,7 +131,7 @@ func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
// 0 => non 0
return params.SstoreSetGas, nil
} else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) {
evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SstoreRefundGas))
db.AddRefund(new(big.Int).SetUint64(params.SstoreRefundGas))
return params.SstoreClearGas, nil
} else {
@ -324,10 +325,10 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
)
if eip158 {
if transfersValue && evm.StateDB.Empty(address) {
if transfersValue && getDualState(evm, address).Empty(address) {
gas += params.CallNewAccountGas
}
} else if !evm.StateDB.Exist(address) {
} else if !getDualState(evm, address).Exist(address) {
gas += params.CallNewAccountGas
}
if transfersValue {
@ -401,6 +402,7 @@ func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
}
func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var db StateDB
var gas uint64
// EIP150 homestead gas reprice fork:
if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
@ -409,19 +411,20 @@ func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
address = common.BigToAddress(stack.Back(0))
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
)
db = getDualState(evm, address)
if eip158 {
// if empty and transfers value
if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
if db.Empty(address) && db.GetBalance(contract.Address()).Sign() != 0 {
gas += gt.CreateBySuicide
}
} else if !evm.StateDB.Exist(address) {
} else if !db.Exist(address) {
gas += gt.CreateBySuicide
}
}
if !evm.StateDB.HasSuicided(contract.Address()) {
evm.StateDB.AddRefund(new(big.Int).SetUint64(params.SuicideRefundGas))
if !db.HasSuicided(contract.Address()) {
db.AddRefund(new(big.Int).SetUint64(params.SuicideRefundGas))
}
return gas, nil
}

View File

@ -324,7 +324,7 @@ func opAddress(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *
func opBalance(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
addr := common.BigToAddress(stack.pop())
balance := evm.StateDB.GetBalance(addr)
balance := getDualState(evm, addr).GetBalance(addr)
stack.push(new(big.Int).Set(balance))
return nil, nil
@ -393,7 +393,7 @@ func opExtCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, sta
a := stack.pop()
addr := common.BigToAddress(a)
a.SetInt64(int64(evm.StateDB.GetCodeSize(addr)))
a.SetInt64(int64(getDualState(evm, addr).GetCodeSize(addr)))
stack.push(a)
return nil, nil
@ -425,7 +425,7 @@ func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, sta
codeOffset = stack.pop()
length = stack.pop()
)
codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length)
codeCopy := getDataBig(getDualState(evm, addr).GetCode(addr), codeOffset, length)
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
evm.interpreter.intPool.put(memOffset, codeOffset, length)
@ -508,7 +508,7 @@ func opMstore8(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *
func opSload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
loc := common.BigToHash(stack.pop())
val := evm.StateDB.GetState(contract.Address(), loc).Big()
val := getDualState(evm, contract.Address()).GetState(contract.Address(), loc).Big()
stack.push(val)
return nil, nil
}
@ -516,7 +516,7 @@ func opSload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
func opSstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
loc := common.BigToHash(stack.pop())
val := stack.pop()
evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
getDualState(evm, contract.Address()).SetState(contract.Address(), loc, common.BigToHash(val))
evm.interpreter.intPool.put(val)
return nil, nil
@ -741,10 +741,11 @@ func opStop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
}
func opSuicide(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
balance := evm.StateDB.GetBalance(contract.Address())
evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
db := getDualState(evm, contract.Address())
balance := db.GetBalance(contract.Address())
db.AddBalance(common.BigToAddress(stack.pop()), balance)
evm.StateDB.Suicide(contract.Address())
db.Suicide(contract.Address())
return nil, nil
}