Added new state object change echanism
This commit is contained in:
parent
f3818478e2
commit
0f93da400a
@ -50,6 +50,10 @@ type StateManager struct {
|
|||||||
// Comparative state it used for comparing and validating end
|
// Comparative state it used for comparing and validating end
|
||||||
// results
|
// results
|
||||||
compState *State
|
compState *State
|
||||||
|
|
||||||
|
// It's generally know that a map is faster for small lookups than arrays
|
||||||
|
// we'll eventually have to make a decision if the map grows too large
|
||||||
|
watchedAddresses map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStateManager(ethereum EthManager) *StateManager {
|
func NewStateManager(ethereum EthManager) *StateManager {
|
||||||
@ -60,6 +64,7 @@ func NewStateManager(ethereum EthManager) *StateManager {
|
|||||||
Ethereum: ethereum,
|
Ethereum: ethereum,
|
||||||
stateObjectCache: NewStateObjectCache(),
|
stateObjectCache: NewStateObjectCache(),
|
||||||
bc: ethereum.BlockChain(),
|
bc: ethereum.BlockChain(),
|
||||||
|
watchedAddresses: make(map[string]bool),
|
||||||
}
|
}
|
||||||
sm.procState = ethereum.BlockChain().CurrentBlock.State()
|
sm.procState = ethereum.BlockChain().CurrentBlock.State()
|
||||||
return sm
|
return sm
|
||||||
@ -309,18 +314,9 @@ func (sm *StateManager) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Transaction, block *Block) {
|
func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Transaction, block *Block) {
|
||||||
// Recovering function in case the VM had any errors
|
|
||||||
/*
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
fmt.Println("Recovered from VM execution with err =", r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
*/
|
|
||||||
|
|
||||||
caller := sm.procState.GetAccount(tx.Sender())
|
caller := sm.procState.GetAccount(tx.Sender())
|
||||||
closure := NewClosure(caller, object, script, sm.procState, tx.Gas, tx.GasPrice, tx.Value)
|
closure := NewClosure(caller, object, script, sm.procState, tx.Gas, tx.GasPrice, tx.Value)
|
||||||
vm := NewVm(sm.procState, RuntimeVars{
|
vm := NewVm(sm.procState, sm, RuntimeVars{
|
||||||
Origin: caller.Address(),
|
Origin: caller.Address(),
|
||||||
BlockNumber: block.BlockInfo().Number,
|
BlockNumber: block.BlockInfo().Number,
|
||||||
PrevHash: block.PrevHash,
|
PrevHash: block.PrevHash,
|
||||||
@ -333,5 +329,22 @@ func (sm *StateManager) EvalScript(script []byte, object *StateObject, tx *Trans
|
|||||||
|
|
||||||
// Update the account (refunds)
|
// Update the account (refunds)
|
||||||
sm.procState.UpdateStateObject(caller)
|
sm.procState.UpdateStateObject(caller)
|
||||||
|
sm.Changed(caller)
|
||||||
sm.procState.UpdateStateObject(object)
|
sm.procState.UpdateStateObject(object)
|
||||||
|
sm.Changed(object)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch a specific address
|
||||||
|
func (sm *StateManager) Watch(addr []byte) {
|
||||||
|
if !sm.watchedAddresses[string(addr)] {
|
||||||
|
sm.watchedAddresses[string(addr)] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The following objects are used when changing a value and using the "watched" attribute
|
||||||
|
// to determine whether the reactor should be used to notify any subscribers on the address
|
||||||
|
func (sm *StateManager) Changed(stateObject *StateObject) {
|
||||||
|
if sm.watchedAddresses[string(stateObject.Address())] {
|
||||||
|
sm.Ethereum.Reactor().Post("addressChanged", stateObject)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,28 @@ type StateObject struct {
|
|||||||
initScript []byte
|
initScript []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Converts an transaction in to a state object
|
||||||
|
func MakeContract(tx *Transaction, state *State) *StateObject {
|
||||||
|
// Create contract if there's no recipient
|
||||||
|
if tx.IsContract() {
|
||||||
|
// FIXME
|
||||||
|
addr := tx.Hash()[12:]
|
||||||
|
|
||||||
|
value := tx.Value
|
||||||
|
contract := NewContract(addr, value, []byte(""))
|
||||||
|
state.UpdateStateObject(contract)
|
||||||
|
|
||||||
|
contract.script = tx.Data
|
||||||
|
contract.initScript = tx.Init
|
||||||
|
|
||||||
|
state.UpdateStateObject(contract)
|
||||||
|
|
||||||
|
return contract
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject {
|
func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject {
|
||||||
contract := &StateObject{address: address, Amount: Amount, Nonce: 0}
|
contract := &StateObject{address: address, Amount: Amount, Nonce: 0}
|
||||||
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
|
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
|
||||||
@ -39,6 +61,10 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject {
|
|||||||
return object
|
return object
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *StateObject) State() *State {
|
||||||
|
return c.state
|
||||||
|
}
|
||||||
|
|
||||||
func (c *StateObject) Addr(addr []byte) *ethutil.Value {
|
func (c *StateObject) Addr(addr []byte) *ethutil.Value {
|
||||||
return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr))))
|
return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr))))
|
||||||
}
|
}
|
||||||
@ -47,8 +73,10 @@ func (c *StateObject) SetAddr(addr []byte, value interface{}) {
|
|||||||
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
|
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) State() *State {
|
func (c *StateObject) SetMem(num *big.Int, val *ethutil.Value) {
|
||||||
return c.state
|
addr := ethutil.BigToBytes(num, 256)
|
||||||
|
c.SetAddr(addr, val)
|
||||||
|
//c.state.trie.Update(string(addr), string(val.Encode()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) GetMem(num *big.Int) *ethutil.Value {
|
func (c *StateObject) GetMem(num *big.Int) *ethutil.Value {
|
||||||
@ -65,11 +93,6 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
|
|||||||
return ethutil.NewValueFromBytes([]byte{c.script[pc.Int64()]})
|
return ethutil.NewValueFromBytes([]byte{c.script[pc.Int64()]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) SetMem(num *big.Int, val *ethutil.Value) {
|
|
||||||
addr := ethutil.BigToBytes(num, 256)
|
|
||||||
c.state.trie.Update(string(addr), string(val.Encode()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
||||||
func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
|
func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
|
||||||
remainder := new(big.Int).Mul(gas, price)
|
remainder := new(big.Int).Mul(gas, price)
|
||||||
@ -77,11 +100,15 @@ func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) AddAmount(amount *big.Int) {
|
func (c *StateObject) AddAmount(amount *big.Int) {
|
||||||
c.Amount.Add(c.Amount, amount)
|
c.SetAmount(new(big.Int).Add(c.Amount, amount))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) SubAmount(amount *big.Int) {
|
func (c *StateObject) SubAmount(amount *big.Int) {
|
||||||
c.Amount.Sub(c.Amount, amount)
|
c.SetAmount(new(big.Int).Sub(c.Amount, amount))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *StateObject) SetAmount(amount *big.Int) {
|
||||||
|
c.Amount = amount
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *StateObject) ConvertGas(gas, price *big.Int) error {
|
func (c *StateObject) ConvertGas(gas, price *big.Int) error {
|
||||||
@ -130,28 +157,6 @@ func (c *StateObject) RlpDecode(data []byte) {
|
|||||||
c.script = decoder.Get(3).Bytes()
|
c.script = decoder.Get(3).Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts an transaction in to a state object
|
|
||||||
func MakeContract(tx *Transaction, state *State) *StateObject {
|
|
||||||
// Create contract if there's no recipient
|
|
||||||
if tx.IsContract() {
|
|
||||||
// FIXME
|
|
||||||
addr := tx.Hash()[12:]
|
|
||||||
|
|
||||||
value := tx.Value
|
|
||||||
contract := NewContract(addr, value, []byte(""))
|
|
||||||
state.UpdateStateObject(contract)
|
|
||||||
|
|
||||||
contract.script = tx.Data
|
|
||||||
contract.initScript = tx.Init
|
|
||||||
|
|
||||||
state.UpdateStateObject(contract)
|
|
||||||
|
|
||||||
return contract
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// The cached state and state object cache are helpers which will give you somewhat
|
// The cached state and state object cache are helpers which will give you somewhat
|
||||||
// control over the nonce. When creating new transactions you're interested in the 'next'
|
// control over the nonce. When creating new transactions you're interested in the 'next'
|
||||||
// nonce rather than the current nonce. This to avoid creating invalid-nonce transactions.
|
// nonce rather than the current nonce. This to avoid creating invalid-nonce transactions.
|
||||||
|
@ -30,6 +30,8 @@ type Vm struct {
|
|||||||
vars RuntimeVars
|
vars RuntimeVars
|
||||||
|
|
||||||
state *State
|
state *State
|
||||||
|
|
||||||
|
stateManager *StateManager
|
||||||
}
|
}
|
||||||
|
|
||||||
type RuntimeVars struct {
|
type RuntimeVars struct {
|
||||||
@ -42,8 +44,8 @@ type RuntimeVars struct {
|
|||||||
TxData []string
|
TxData []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVm(state *State, vars RuntimeVars) *Vm {
|
func NewVm(state *State, stateManager *StateManager, vars RuntimeVars) *Vm {
|
||||||
return &Vm{vars: vars, state: state}
|
return &Vm{vars: vars, state: state, stateManager: stateManager}
|
||||||
}
|
}
|
||||||
|
|
||||||
var Pow256 = ethutil.BigPow(2, 256)
|
var Pow256 = ethutil.BigPow(2, 256)
|
||||||
|
@ -20,7 +20,12 @@ func (val *Value) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewValue(val interface{}) *Value {
|
func NewValue(val interface{}) *Value {
|
||||||
return &Value{Val: val}
|
t := val
|
||||||
|
if v, ok := val.(*Value); ok {
|
||||||
|
t = v.Val
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Value{Val: t}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (val *Value) Type() reflect.Kind {
|
func (val *Value) Type() reflect.Kind {
|
||||||
|
Loading…
Reference in New Issue
Block a user