Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
5c11d1033d | |||
d2112556ee | |||
f0c7af0477 | |||
34c85d58a4 | |||
969eb61886 | |||
7fbf990cc9 | |||
d051274691 |
@ -50,7 +50,7 @@ import (
|
||||
|
||||
const (
|
||||
ClientIdentifier = "Geth"
|
||||
Version = "1.0.1"
|
||||
Version = "1.0.3"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -57,6 +57,18 @@ type BlockProcessor struct {
|
||||
eventMux *event.TypeMux
|
||||
}
|
||||
|
||||
// TODO: type GasPool big.Int
|
||||
//
|
||||
// GasPool is implemented by state.StateObject. This is a historical
|
||||
// coincidence. Gas tracking should move out of StateObject.
|
||||
|
||||
// GasPool tracks the amount of gas available during
|
||||
// execution of the transactions in a block.
|
||||
type GasPool interface {
|
||||
AddGas(gas, price *big.Int)
|
||||
SubGas(gas, price *big.Int) error
|
||||
}
|
||||
|
||||
func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
|
||||
sm := &BlockProcessor{
|
||||
db: db,
|
||||
@ -66,16 +78,15 @@ func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *Cha
|
||||
bc: chainManager,
|
||||
eventMux: eventMux,
|
||||
}
|
||||
|
||||
return sm
|
||||
}
|
||||
|
||||
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
|
||||
coinbase := statedb.GetOrNewStateObject(block.Coinbase())
|
||||
coinbase.SetGasLimit(block.GasLimit())
|
||||
gp := statedb.GetOrNewStateObject(block.Coinbase())
|
||||
gp.SetGasLimit(block.GasLimit())
|
||||
|
||||
// Process the transactions on to parent state
|
||||
receipts, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), transientProcess)
|
||||
receipts, err = sm.ApplyTransactions(gp, statedb, block, block.Transactions(), transientProcess)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -83,11 +94,8 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
|
||||
return receipts, nil
|
||||
}
|
||||
|
||||
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
|
||||
// If we are mining this block and validating we want to set the logs back to 0
|
||||
|
||||
cb := statedb.GetStateObject(coinbase.Address())
|
||||
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
|
||||
func (self *BlockProcessor) ApplyTransaction(gp GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
|
||||
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -122,7 +130,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
|
||||
return self.bc
|
||||
}
|
||||
|
||||
func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
|
||||
func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
|
||||
var (
|
||||
receipts types.Receipts
|
||||
totalUsedGas = big.NewInt(0)
|
||||
@ -134,7 +142,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
|
||||
for i, tx := range txs {
|
||||
statedb.StartRecord(tx.Hash(), block.Hash(), i)
|
||||
|
||||
receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess)
|
||||
receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -20,8 +20,5 @@ import "github.com/ethereum/go-ethereum/common"
|
||||
|
||||
// Set of manually tracked bad hashes (usually hard forks)
|
||||
var BadHashes = map[common.Hash]bool{
|
||||
common.HexToHash("f269c503aed286caaa0d114d6a5320e70abbc2febe37953207e76a2873f2ba79"): true,
|
||||
common.HexToHash("38f5bbbffd74804820ffa4bab0cd540e9de229725afb98c1a7e57936f4a714bc"): true,
|
||||
common.HexToHash("7064455b364775a16afbdecd75370e912c6e2879f202eda85b9beae547fff3ac"): true,
|
||||
common.HexToHash("5b7c80070a6eff35f3eb3181edb023465c776d40af2885571e1bc4689f3a44d8"): true,
|
||||
common.HexToHash("05bef30ef572270f654746da22639a7a0c97dd97a7050b9e252391996aaeb689"): true,
|
||||
}
|
||||
|
@ -24,10 +24,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
jeff = common.HexToAddress("a8edb1ac2c86d3d9d78f96cd18001f60df29e52c")
|
||||
vitalik = common.HexToAddress("1baf27b88c48dd02b744999cf3522766929d2b2a")
|
||||
christoph = common.HexToAddress("60d11b58744784dc97f878f7e3749c0f1381a004")
|
||||
gav = common.HexToAddress("4bb7e8ae99b645c2b7860b8f3a2328aae28bd80a")
|
||||
jeff = common.HexToAddress("959c33de5961820567930eccce51ea715c496f85")
|
||||
vitalik = common.HexToAddress("c8158da0b567a8cc898991c2c2a073af67dc03a9")
|
||||
christoph = common.HexToAddress("7a19a893f91d5b6e2cdf941b6acbba2cbcf431ee")
|
||||
gav = common.HexToAddress("539dd9aaf45c3feb03f9c004f4098bd3268fef6b")
|
||||
)
|
||||
|
||||
// Canary will check the 0'd address of the 4 contracts above.
|
||||
|
@ -82,8 +82,9 @@ type StateObject struct {
|
||||
// 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
|
||||
dirty bool
|
||||
remove bool
|
||||
deleted bool
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (self *StateObject) Reset() {
|
||||
|
@ -203,18 +203,20 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
|
||||
|
||||
// Delete the given state object and delete it from the state trie
|
||||
func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
|
||||
stateObject.deleted = true
|
||||
|
||||
addr := stateObject.Address()
|
||||
self.trie.Delete(addr[:])
|
||||
|
||||
//delete(self.stateObjects, addr.Str())
|
||||
}
|
||||
|
||||
// Retrieve a state object given my the address. Nil if not found
|
||||
func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
|
||||
//addr = common.Address(addr)
|
||||
|
||||
stateObject := self.stateObjects[addr.Str()]
|
||||
func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObject) {
|
||||
stateObject = self.stateObjects[addr.Str()]
|
||||
if stateObject != nil {
|
||||
if stateObject.deleted {
|
||||
stateObject = nil
|
||||
}
|
||||
|
||||
return stateObject
|
||||
}
|
||||
|
||||
@ -236,7 +238,7 @@ func (self *StateDB) SetStateObject(object *StateObject) {
|
||||
// Retrieve a state object or create a new state object if nil
|
||||
func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject == nil {
|
||||
if stateObject == nil || stateObject.deleted {
|
||||
stateObject = self.CreateAccount(addr)
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ import (
|
||||
* 6) Derive new state root
|
||||
*/
|
||||
type StateTransition struct {
|
||||
coinbase common.Address
|
||||
gp GasPool
|
||||
msg Message
|
||||
gas, gasPrice *big.Int
|
||||
initialGas *big.Int
|
||||
@ -53,8 +53,6 @@ type StateTransition struct {
|
||||
data []byte
|
||||
state *state.StateDB
|
||||
|
||||
cb, rec, sen *state.StateObject
|
||||
|
||||
env vm.Environment
|
||||
}
|
||||
|
||||
@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
|
||||
return igas
|
||||
}
|
||||
|
||||
func ApplyMessage(env vm.Environment, msg Message, coinbase *state.StateObject) ([]byte, *big.Int, error) {
|
||||
return NewStateTransition(env, msg, coinbase).transitionState()
|
||||
func ApplyMessage(env vm.Environment, msg Message, gp GasPool) ([]byte, *big.Int, error) {
|
||||
return NewStateTransition(env, msg, gp).transitionState()
|
||||
}
|
||||
|
||||
func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition {
|
||||
func NewStateTransition(env vm.Environment, msg Message, gp GasPool) *StateTransition {
|
||||
return &StateTransition{
|
||||
coinbase: coinbase.Address(),
|
||||
gp: gp,
|
||||
env: env,
|
||||
msg: msg,
|
||||
gas: new(big.Int),
|
||||
@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
|
||||
value: msg.Value(),
|
||||
data: msg.Data(),
|
||||
state: env.State(),
|
||||
cb: coinbase,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *StateTransition) Coinbase() *state.StateObject {
|
||||
return self.state.GetOrNewStateObject(self.coinbase)
|
||||
}
|
||||
func (self *StateTransition) From() (*state.StateObject, error) {
|
||||
f, err := self.msg.From()
|
||||
if err != nil {
|
||||
@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
|
||||
if sender.Balance().Cmp(mgval) < 0 {
|
||||
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
|
||||
}
|
||||
if err = self.Coinbase().SubGas(mgas, self.gasPrice); err != nil {
|
||||
if err = self.gp.SubGas(mgas, self.gasPrice); err != nil {
|
||||
return err
|
||||
}
|
||||
self.AddGas(mgas)
|
||||
@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
|
||||
}
|
||||
|
||||
self.refundGas()
|
||||
self.state.AddBalance(self.coinbase, new(big.Int).Mul(self.gasUsed(), self.gasPrice))
|
||||
self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))
|
||||
|
||||
return ret, self.gasUsed(), err
|
||||
}
|
||||
|
||||
func (self *StateTransition) refundGas() {
|
||||
coinbase := self.Coinbase()
|
||||
sender, _ := self.From() // err already checked
|
||||
// Return remaining gas
|
||||
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
|
||||
@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
|
||||
self.gas.Add(self.gas, refund)
|
||||
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
|
||||
|
||||
coinbase.AddGas(self.gas, self.gasPrice)
|
||||
self.gp.AddGas(self.gas, self.gasPrice)
|
||||
}
|
||||
|
||||
func (self *StateTransition) gasUsed() *big.Int {
|
||||
|
@ -150,7 +150,7 @@ func runBlockTests(bt map[string]*BlockTest, skipTests []string) error {
|
||||
|
||||
// test the block
|
||||
if err := runBlockTest(test); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("%s: %v", name, err)
|
||||
}
|
||||
glog.Infoln("Block test passed: ", name)
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
11
xeth/xeth.go
11
xeth/xeth.go
@ -20,8 +20,10 @@ package xeth
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -45,6 +47,7 @@ var (
|
||||
defaultGasPrice = big.NewInt(10000000000000) //150000000000
|
||||
defaultGas = big.NewInt(90000) //500000
|
||||
dappStorePre = []byte("dapp-")
|
||||
addrReg = regexp.MustCompile(`^(0x)?[a-fA-F0-9]{40}$`)
|
||||
)
|
||||
|
||||
// byte will be inferred
|
||||
@ -878,6 +881,10 @@ func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error)
|
||||
return common.ToHex(sig), nil
|
||||
}
|
||||
|
||||
func isAddress(addr string) bool {
|
||||
return addrReg.MatchString(addr)
|
||||
}
|
||||
|
||||
func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
|
||||
|
||||
// this minimalistic recoding is enough (works for natspec.js)
|
||||
@ -887,6 +894,10 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(toStr) > 0 && toStr != "0x" && !isAddress(toStr) {
|
||||
return "", errors.New("Invalid address")
|
||||
}
|
||||
|
||||
var (
|
||||
from = common.HexToAddress(fromStr)
|
||||
to = common.HexToAddress(toStr)
|
||||
|
26
xeth/xeth_test.go
Normal file
26
xeth/xeth_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package xeth
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsAddress(t *testing.T) {
|
||||
for _, invalid := range []string{
|
||||
"0x00",
|
||||
"0xNN",
|
||||
"0x00000000000000000000000000000000000000NN",
|
||||
"0xAAar000000000000000000000000000000000000",
|
||||
} {
|
||||
if isAddress(invalid) {
|
||||
t.Error("Expected", invalid, "to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
for _, valid := range []string{
|
||||
"0x0000000000000000000000000000000000000000",
|
||||
"0xAABBbbCCccff9900000000000000000000000000",
|
||||
"AABBbbCCccff9900000000000000000000000000",
|
||||
} {
|
||||
if !isAddress(valid) {
|
||||
t.Error("Expected", valid, "to be valid")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user