Changed refund

This commit is contained in:
obscuren 2014-11-28 21:20:32 +01:00
parent 6ba8328077
commit c8d0f8adc5
3 changed files with 22 additions and 18 deletions

View File

@ -154,10 +154,11 @@ done:
} }
} }
// Update the state with pending changes
state.Update()
txGas.Sub(txGas, st.gas) txGas.Sub(txGas, st.gas)
// Update the state with pending changes
state.Update(txGas)
cumulative := new(big.Int).Set(totalUsedGas.Add(totalUsedGas, txGas)) cumulative := new(big.Int).Set(totalUsedGas.Add(totalUsedGas, txGas))
receipt := &Receipt{ethutil.CopyBytes(state.Root()), cumulative, nil /*bloom*/, state.Logs()} receipt := &Receipt{ethutil.CopyBytes(state.Root()), cumulative, nil /*bloom*/, state.Logs()}
receipt.Bloom = CreateBloom(Receipts{receipt}) receipt.Bloom = CreateBloom(Receipts{receipt})
@ -245,7 +246,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *Block) (td *big.Int, me
return return
} }
state.Update() state.Update(nil)
if !block.State().Cmp(state) { if !block.State().Cmp(state) {
err = fmt.Errorf("invalid merkle root. received=%x got=%x", block.Root(), state.Root()) err = fmt.Errorf("invalid merkle root. received=%x got=%x", block.Root(), state.Root())

View File

@ -203,7 +203,7 @@ func (self *Miner) mine() {
// Accumulate the rewards included for this block // Accumulate the rewards included for this block
blockManager.AccumelateRewards(block.State(), block, parent) blockManager.AccumelateRewards(block.State(), block, parent)
block.State().Update() block.State().Update(nil)
minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions))

View File

@ -23,14 +23,14 @@ type State struct {
manifest *Manifest manifest *Manifest
refund map[string]*big.Int refund map[string][]refund
logs Logs logs Logs
} }
// Create a new state from a given trie // Create a new state from a given trie
func New(trie *trie.Trie) *State { func New(trie *trie.Trie) *State {
return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)} return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string][]refund)}
} }
func (self *State) EmptyLogs() { func (self *State) EmptyLogs() {
@ -55,14 +55,12 @@ func (self *State) GetBalance(addr []byte) *big.Int {
return ethutil.Big0 return ethutil.Big0
} }
type refund struct {
gas, price *big.Int
}
func (self *State) Refund(addr []byte, gas, price *big.Int) { func (self *State) Refund(addr []byte, gas, price *big.Int) {
amount := new(big.Int).Mul(gas, price) self.refund[string(addr)] = append(self.refund[string(addr)], refund{gas, price})
if self.refund[string(addr)] == nil {
self.refund[string(addr)] = new(big.Int)
}
self.refund[string(addr)].Add(self.refund[string(addr)], amount)
} }
func (self *State) AddBalance(addr []byte, amount *big.Int) { func (self *State) AddBalance(addr []byte, amount *big.Int) {
@ -276,15 +274,20 @@ func (s *State) Sync() {
func (self *State) Empty() { func (self *State) Empty() {
self.stateObjects = make(map[string]*StateObject) self.stateObjects = make(map[string]*StateObject)
self.refund = make(map[string]*big.Int) self.refund = make(map[string][]refund)
} }
func (self *State) Update() { func (self *State) Update(gasUsed *big.Int) {
var deleted bool var deleted bool
// Refund any gas that's left // Refund any gas that's left
for addr, amount := range self.refund { uhalf := new(big.Int).Div(gasUsed, ethutil.Big2)
self.GetStateObject([]byte(addr)).AddBalance(amount) for addr, refs := range self.refund {
for _, ref := range refs {
refund := ethutil.BigMin(uhalf, ref.gas)
self.GetStateObject([]byte(addr)).AddBalance(refund.Mul(refund, ref.price))
}
} }
for _, stateObject := range self.stateObjects { for _, stateObject := range self.stateObjects {