@@ -22,11 +22,13 @@ type State struct {
2222 stateObjects map [string ]* StateObject
2323
2424 manifest * Manifest
25+
26+ refund map [string ]* big.Int
2527}
2628
2729// Create a new state from a given trie
2830func New (trie * ethtrie.Trie ) * State {
29- return & State {Trie : trie , stateObjects : make (map [string ]* StateObject ), manifest : NewManifest ()}
31+ return & State {Trie : trie , stateObjects : make (map [string ]* StateObject ), manifest : NewManifest (), refund : make ( map [ string ] * big. Int ) }
3032}
3133
3234// Retrieve the balance from the given address or 0 if object not found
@@ -39,6 +41,16 @@ func (self *State) GetBalance(addr []byte) *big.Int {
3941 return ethutil .Big0
4042}
4143
44+ func (self * State ) Refund (addr []byte , gas , price * big.Int ) {
45+ amount := new (big.Int ).Mul (gas , price )
46+
47+ if self .refund [string (addr )] == nil {
48+ self .refund [string (addr )] = new (big.Int )
49+ }
50+
51+ self .refund [string (addr )] = new (big.Int ).Add (self .refund [string (addr )], amount )
52+ }
53+
4254func (self * State ) AddBalance (addr []byte , amount * big.Int ) {
4355 stateObject := self .GetStateObject (addr )
4456 if stateObject != nil {
@@ -186,6 +198,10 @@ func (self *State) Copy() *State {
186198 state .stateObjects [k ] = stateObject .Copy ()
187199 }
188200
201+ for addr , refund := range self .refund {
202+ state .refund [addr ] = refund
203+ }
204+
189205 return state
190206 }
191207
@@ -199,6 +215,7 @@ func (self *State) Set(state *State) {
199215
200216 self .Trie = state .Trie
201217 self .stateObjects = state .stateObjects
218+ self .refund = state .refund
202219}
203220
204221func (s * State ) Root () interface {} {
@@ -240,10 +257,17 @@ func (s *State) Sync() {
240257
241258func (self * State ) Empty () {
242259 self .stateObjects = make (map [string ]* StateObject )
260+ self .refund = make (map [string ]* big.Int )
243261}
244262
245263func (self * State ) Update () {
246264 var deleted bool
265+
266+ // Refund any gas that's left
267+ for addr , amount := range self .refund {
268+ self .GetStateObject ([]byte (addr )).AddBalance (amount )
269+ }
270+
247271 for _ , stateObject := range self .stateObjects {
248272 if stateObject .remove {
249273 self .DeleteStateObject (stateObject )
0 commit comments