updated to types

This commit is contained in:
obscuren 2014-12-03 14:05:19 +01:00
parent 709eff4ea7
commit 8240550187
7 changed files with 31 additions and 59 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/chain/types"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
)
@ -45,6 +46,7 @@ func CalcDifficulty(block, parent *types.Block) *big.Int {
type ChainManager struct {
//eth EthManager
processor types.BlockProcessor
eventMux *event.TypeMux
genesisBlock *types.Block
// Last known total difficulty
TD *big.Int
@ -55,10 +57,10 @@ type ChainManager struct {
LastBlockHash []byte
}
func NewChainManager() *ChainManager {
func NewChainManager(mux *event.TypeMux) *ChainManager {
bc := &ChainManager{}
bc.genesisBlock = types.NewBlockFromBytes(ethutil.Encode(Genesis))
//bc.eth = ethereum
bc.eventMux = mux
bc.setLastBlock()
@ -250,9 +252,9 @@ func (bc *ChainManager) Stop() {
}
}
func (self *ChainManager) InsertChain(chain Blocks) error {
func (self *ChainManager) InsertChain(chain types.Blocks) error {
for _, block := range chain {
td, messages, err := self.Ethereum.BlockManager().Process(block)
td, messages, err := self.processor.Process(block)
if err != nil {
if IsKnownBlockErr(err) {
continue
@ -266,8 +268,8 @@ func (self *ChainManager) InsertChain(chain Blocks) error {
self.add(block)
self.SetTotalDifficulty(td)
self.Ethereum.EventMux().Post(NewBlockEvent{block})
self.Ethereum.EventMux().Post(messages)
self.eventMux.Post(NewBlockEvent{block})
self.eventMux.Post(messages)
}
return nil

View File

@ -110,7 +110,8 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
return fmt.Errorf("Invalid recipient. len = %d", len(tx.Recipient))
}
if tx.v > 28 || tx.v < 27 {
v, _, _ := tx.Curve()
if v > 28 || v < 27 {
return fmt.Errorf("tx.v != (28 || 27)")
}
@ -142,7 +143,7 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
func (self *TxPool) Add(tx *types.Transaction) error {
hash := tx.Hash()
foundTx := FindTx(self.pool, func(tx *Transaction, e *list.Element) bool {
foundTx := FindTx(self.pool, func(tx *types.Transaction, e *list.Element) bool {
return bytes.Compare(tx.Hash(), hash) == 0
})
@ -168,42 +169,6 @@ func (self *TxPool) Add(tx *types.Transaction) error {
return nil
}
func (pool *TxPool) queueHandler() {
out:
for {
select {
case tx := <-pool.queueChan:
hash := tx.Hash()
foundTx := FindTx(pool.pool, func(tx *types.Transaction, e *list.Element) bool {
return bytes.Compare(tx.Hash(), hash) == 0
})
if foundTx != nil {
break
}
// Validate the transaction
err := pool.ValidateTransaction(tx)
if err != nil {
txplogger.Debugln("Validating Tx failed", err)
} else {
// Call blocking version.
pool.addTransaction(tx)
tmp := make([]byte, 4)
copy(tmp, tx.Recipient)
txplogger.Debugf("(t) %x => %x (%v) %x\n", tx.Sender()[:4], tmp, tx.Value, tx.Hash())
// Notify the subscribers
pool.Ethereum.EventMux().Post(TxPreEvent{tx})
}
case <-pool.quit:
break out
}
}
}
func (pool *TxPool) CurrentTransactions() []*types.Transaction {
pool.mutex.Lock()
defer pool.mutex.Unlock()
@ -261,12 +226,10 @@ func (pool *TxPool) Flush() []*types.Transaction {
}
func (pool *TxPool) Start() {
go pool.queueHandler()
//go pool.queueHandler()
}
func (pool *TxPool) Stop() {
close(pool.quit)
pool.Flush()
txplogger.Infoln("Stopped")

View File

@ -2,9 +2,10 @@ package types
import (
"math/big"
"github.com/ethereum/go-ethereum/state"
)
type BlockProcessor interface {
ProcessWithParent(*Block, *Block) (*big.Int, state.Messages, error)
Process(*Block) (*big.Int, state.Messages, error)
}

View File

@ -82,6 +82,14 @@ func (tx *Transaction) CreationAddress(state *state.State) []byte {
return crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
}
func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
v = tx.v
r = ethutil.LeftPadBytes(tx.r, 32)
s = ethutil.LeftPadBytes(tx.s, 32)
return
}
func (tx *Transaction) Signature(key []byte) []byte {
hash := tx.Hash()
@ -93,12 +101,10 @@ func (tx *Transaction) Signature(key []byte) []byte {
func (tx *Transaction) PublicKey() []byte {
hash := tx.Hash()
// TODO
r := ethutil.LeftPadBytes(tx.r, 32)
s := ethutil.LeftPadBytes(tx.s, 32)
v, r, s := tx.Curve()
sig := append(r, s...)
sig = append(sig, tx.v-27)
sig = append(sig, v-27)
pubkey := crypto.Ecrecover(append(hash, sig...))
//pubkey, _ := secp256k1.RecoverPubkey(hash, sig)

View File

@ -129,7 +129,7 @@ func New(db ethutil.Database, clientIdentity wire.ClientIdentity, keyManager *cr
ethereum.blockPool = NewBlockPool(ethereum)
ethereum.txPool = chain.NewTxPool(ethereum)
ethereum.blockChain = chain.NewChainManager()
ethereum.blockChain = chain.NewChainManager(ethereum.EventMux())
ethereum.blockManager = chain.NewBlockManager(ethereum)
ethereum.blockChain.SetProcessor(ethereum.blockManager)

View File

@ -215,7 +215,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr
if err != nil {
return "", err
}
if chain.IsContractAddr(to) {
if types.IsContractAddr(to) {
return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil
}

View File

@ -93,7 +93,7 @@ func (self *XEth) Exists(addr []byte) bool {
return self.World().Get(addr) != nil
}
func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) {
func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) {
// Check if an address is stored by this address
var hash []byte
addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes()
@ -108,10 +108,10 @@ func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, pr
return self.Transact(key, hash, value, gas, price, data)
}
func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) {
func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) {
var hash []byte
var contractCreation bool
if chain.IsContractAddr(to) {
if types.IsContractAddr(to) {
contractCreation = true
} else {
// Check if an address is stored by this address
@ -125,9 +125,9 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et
var tx *types.Transaction
if contractCreation {
tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data)
tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data)
} else {
tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data)
}
state := self.blockManager.TransState()