state_object.go 6.87 KB
Newer Older
obscuren's avatar
obscuren committed
1
package state
2 3 4

import (
	"fmt"
5 6
	"math/big"

obscuren's avatar
obscuren committed
7
	"github.com/ethereum/go-ethereum/crypto"
8
	"github.com/ethereum/go-ethereum/ethutil"
9
	"github.com/ethereum/go-ethereum/trie"
10 11 12 13 14
)

type Code []byte

func (self Code) String() string {
15
	return string(self) //strings.Join(Disassemble(self), " ")
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
}

type Storage map[string]*ethutil.Value

func (self Storage) Copy() Storage {
	cpy := make(Storage)
	for key, value := range self {
		// XXX Do we need a 'value' copy or is this sufficient?
		cpy[key] = value
	}

	return cpy
}

type StateObject struct {
31
	db ethutil.Database
32 33 34
	// Address of the object
	address []byte
	// Shared attributes
35
	balance  *big.Int
36
	codeHash []byte
37 38
	Nonce    uint64
	// Contract related attributes
obscuren's avatar
obscuren committed
39
	State    *StateDB
40
	Code     Code
41
	InitCode Code
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

	storage Storage

	// Total gas pool is the total amount of gas currently
	// left if this object is the coinbase. Gas is directly
	// purchased of the coinbase.
	gasPool *big.Int

	// Mark for deletion
	// When an object is marked for deletion it will be delete from the trie
	// during the "update" phase of the state transition
	remove bool
}

func (self *StateObject) Reset() {
	self.storage = make(Storage)
58
	self.State.Reset()
59 60
}

61
func NewStateObject(addr []byte, db ethutil.Database) *StateObject {
62 63 64
	// This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
	address := ethutil.Address(addr)

65 66
	object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int)}
	object.State = New(nil, db) //New(trie.New(ethutil.Config.Db, ""))
67 68 69 70 71 72
	object.storage = make(Storage)
	object.gasPool = new(big.Int)

	return object
}

73 74
func NewStateObjectFromBytes(address, data []byte, db ethutil.Database) *StateObject {
	object := &StateObject{address: address, db: db}
75 76 77 78 79 80 81
	object.RlpDecode(data)

	return object
}

func (self *StateObject) MarkForDeletion() {
	self.remove = true
82
	statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance)
83 84
}

obscuren's avatar
obscuren committed
85 86
func (c *StateObject) getAddr(addr []byte) *ethutil.Value {
	return ethutil.NewValueFromBytes([]byte(c.State.trie.Get(addr)))
87 88
}

obscuren's avatar
obscuren committed
89 90
func (c *StateObject) setAddr(addr []byte, value interface{}) {
	c.State.trie.Update(addr, ethutil.Encode(value))
91 92 93
}

func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value {
obscuren's avatar
obscuren committed
94
	return self.GetState(key.Bytes())
95 96
}
func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) {
obscuren's avatar
obscuren committed
97
	self.SetState(key.Bytes(), value)
98 99
}

100 101 102 103
func (self *StateObject) Storage() map[string]*ethutil.Value {
	return self.storage
}

obscuren's avatar
obscuren committed
104
func (self *StateObject) GetState(k []byte) *ethutil.Value {
105 106 107 108
	key := ethutil.LeftPadBytes(k, 32)

	value := self.storage[string(key)]
	if value == nil {
obscuren's avatar
obscuren committed
109
		value = self.getAddr(key)
110 111 112 113 114 115 116 117 118

		if !value.IsNil() {
			self.storage[string(key)] = value
		}
	}

	return value
}

obscuren's avatar
obscuren committed
119
func (self *StateObject) SetState(k []byte, value *ethutil.Value) {
120 121 122 123 124 125
	key := ethutil.LeftPadBytes(k, 32)
	self.storage[string(key)] = value.Copy()
}

func (self *StateObject) Sync() {
	for key, value := range self.storage {
obscuren's avatar
obscuren committed
126
		if value.Len() == 0 {
obscuren's avatar
obscuren committed
127
			self.State.trie.Delete([]byte(key))
128 129 130
			continue
		}

obscuren's avatar
obscuren committed
131
		self.setAddr([]byte(key), value)
132 133 134 135 136 137 138 139 140 141 142
	}
}

func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
	if int64(len(c.Code)-1) < pc.Int64() {
		return ethutil.NewValue(0)
	}

	return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]})
}

143 144
func (c *StateObject) AddBalance(amount *big.Int) {
	c.SetBalance(new(big.Int).Add(c.balance, amount))
145

146
	statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount)
147
}
148
func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) }
149

150 151
func (c *StateObject) SubBalance(amount *big.Int) {
	c.SetBalance(new(big.Int).Sub(c.balance, amount))
152

153
	statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.balance, amount)
154
}
155
func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) }
156

157
func (c *StateObject) SetBalance(amount *big.Int) {
158
	c.balance = amount
159 160
}

161 162
func (self *StateObject) Balance() *big.Int { return self.balance }

163 164 165 166 167 168 169 170
//
// Gas setters and getters
//

// Return the gas back to the origin. Used by the Virtual machine or Closures
func (c *StateObject) ReturnGas(gas, price *big.Int) {}
func (c *StateObject) ConvertGas(gas, price *big.Int) error {
	total := new(big.Int).Mul(gas, price)
171 172
	if total.Cmp(c.balance) > 0 {
		return fmt.Errorf("insufficient amount: %v, %v", c.balance, total)
173 174 175 176 177 178 179 180 181 182
	}

	c.SubAmount(total)

	return nil
}

func (self *StateObject) SetGasPool(gasLimit *big.Int) {
	self.gasPool = new(big.Int).Set(gasLimit)

183
	statelogger.Debugf("%x: gas (+ %v)", self.Address(), self.gasPool)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

func (self *StateObject) BuyGas(gas, price *big.Int) error {
	if self.gasPool.Cmp(gas) < 0 {
		return GasLimitError(self.gasPool, gas)
	}

	rGas := new(big.Int).Set(gas)
	rGas.Mul(rGas, price)

	self.AddAmount(rGas)

	return nil
}

func (self *StateObject) RefundGas(gas, price *big.Int) {
	self.gasPool.Add(self.gasPool, gas)

	rGas := new(big.Int).Set(gas)
	rGas.Mul(rGas, price)

205
	self.balance.Sub(self.balance, rGas)
206 207 208
}

func (self *StateObject) Copy() *StateObject {
209
	stateObject := NewStateObject(self.Address(), self.db)
210
	stateObject.balance.Set(self.balance)
211
	stateObject.codeHash = ethutil.CopyBytes(self.codeHash)
212
	stateObject.Nonce = self.Nonce
213 214
	if self.State != nil {
		stateObject.State = self.State.Copy()
215 216
	}
	stateObject.Code = ethutil.CopyBytes(self.Code)
217
	stateObject.InitCode = ethutil.CopyBytes(self.InitCode)
218 219
	stateObject.storage = self.storage.Copy()
	stateObject.gasPool.Set(self.gasPool)
obscuren's avatar
obscuren committed
220
	stateObject.remove = self.remove
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

	return stateObject
}

func (self *StateObject) Set(stateObject *StateObject) {
	*self = *stateObject
}

//
// Attribute accessors
//

func (c *StateObject) N() *big.Int {
	return big.NewInt(int64(c.Nonce))
}

// Returns the address of the contract/account
func (c *StateObject) Address() []byte {
	return c.address
}

// Returns the initialization Code
func (c *StateObject) Init() Code {
244
	return c.InitCode
245 246
}

247
func (self *StateObject) Trie() *trie.Trie {
obscuren's avatar
obscuren committed
248 249 250
	return self.State.trie
}

obscuren's avatar
obscuren committed
251
func (self *StateObject) Root() []byte {
obscuren's avatar
obscuren committed
252
	return self.Trie().Root()
obscuren's avatar
obscuren committed
253 254
}

255 256 257 258
func (self *StateObject) SetCode(code []byte) {
	self.Code = code
}

259 260 261 262 263 264
//
// Encoding
//

// State object encoding methods
func (c *StateObject) RlpEncode() []byte {
obscuren's avatar
obscuren committed
265
	return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.Root(), c.CodeHash()})
266 267 268
}

func (c *StateObject) CodeHash() ethutil.Bytes {
269
	return crypto.Sha3(c.Code)
270 271 272 273 274 275
}

func (c *StateObject) RlpDecode(data []byte) {
	decoder := ethutil.NewValueFromBytes(data)

	c.Nonce = decoder.Get(0).Uint()
276
	c.balance = decoder.Get(1).BigInt()
277
	c.State = New(decoder.Get(2).Bytes(), c.db) //New(trie.New(ethutil.Config.Db, decoder.Get(2).Interface()))
278 279 280
	c.storage = make(map[string]*ethutil.Value)
	c.gasPool = new(big.Int)

281
	c.codeHash = decoder.Get(3).Bytes()
282

283
	c.Code, _ = c.db.Get(c.codeHash)
284 285 286 287 288 289 290 291 292
}

// Storage change object. Used by the manifest for notifying changes to
// the sub channels.
type StorageState struct {
	StateAddress []byte
	Address      []byte
	Value        *big.Int
}