core, miner: miner header validation, transaction & receipt writing
* Miners do now verify their own header, not their state. * Changed old putTx and putReceipts to be exported * Moved writing of transactions and receipts out of the block processer in to the chain manager. Closes #1386 * Miner post ChainHeadEvent & ChainEvent. Closes #1388
This commit is contained in:
parent
03129e7c93
commit
29e2fb38f8
@ -412,7 +412,7 @@ func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, ex
|
|||||||
eventMux := new(event.TypeMux)
|
eventMux := new(event.TypeMux)
|
||||||
pow := ethash.New()
|
pow := ethash.New()
|
||||||
genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
|
genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
|
||||||
chain, err = core.NewChainManager(genesis, blockDB, stateDB, pow, eventMux)
|
chain, err = core.NewChainManager(genesis, blockDB, stateDB, extraDB, pow, eventMux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Could not start chainmanager: %v", err)
|
Fatalf("Could not start chainmanager: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -152,7 +152,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
|
|||||||
// Time the insertion of the new chain.
|
// Time the insertion of the new chain.
|
||||||
// State and blocks are stored in the same DB.
|
// State and blocks are stored in the same DB.
|
||||||
evmux := new(event.TypeMux)
|
evmux := new(event.TypeMux)
|
||||||
chainman, _ := NewChainManager(genesis, db, db, FakePow{}, evmux)
|
chainman, _ := NewChainManager(genesis, db, db, db, FakePow{}, evmux)
|
||||||
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
|
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
|
||||||
defer chainman.Stop()
|
defer chainman.Stop()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
@ -151,7 +151,7 @@ func (sm *BlockProcessor) RetryProcess(block *types.Block) (logs state.Logs, err
|
|||||||
errch := make(chan bool)
|
errch := make(chan bool)
|
||||||
go func() { errch <- sm.Pow.Verify(block) }()
|
go func() { errch <- sm.Pow.Verify(block) }()
|
||||||
|
|
||||||
logs, err = sm.processWithParent(block, parent)
|
logs, _, err = sm.processWithParent(block, parent)
|
||||||
if !<-errch {
|
if !<-errch {
|
||||||
return nil, ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
|
return nil, ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
|
||||||
}
|
}
|
||||||
@ -162,23 +162,23 @@ func (sm *BlockProcessor) RetryProcess(block *types.Block) (logs state.Logs, err
|
|||||||
// Process block will attempt to process the given block's transactions and applies them
|
// Process block will attempt to process the given block's transactions and applies them
|
||||||
// on top of the block's parent state (given it exists) and will return wether it was
|
// on top of the block's parent state (given it exists) and will return wether it was
|
||||||
// successful or not.
|
// successful or not.
|
||||||
func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, err error) {
|
func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, receipts types.Receipts, err error) {
|
||||||
// Processing a blocks may never happen simultaneously
|
// Processing a blocks may never happen simultaneously
|
||||||
sm.mutex.Lock()
|
sm.mutex.Lock()
|
||||||
defer sm.mutex.Unlock()
|
defer sm.mutex.Unlock()
|
||||||
|
|
||||||
if sm.bc.HasBlock(block.Hash()) {
|
if sm.bc.HasBlock(block.Hash()) {
|
||||||
return nil, &KnownBlockError{block.Number(), block.Hash()}
|
return nil, nil, &KnownBlockError{block.Number(), block.Hash()}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sm.bc.HasBlock(block.ParentHash()) {
|
if !sm.bc.HasBlock(block.ParentHash()) {
|
||||||
return nil, ParentError(block.ParentHash())
|
return nil, nil, ParentError(block.ParentHash())
|
||||||
}
|
}
|
||||||
parent := sm.bc.GetBlock(block.ParentHash())
|
parent := sm.bc.GetBlock(block.ParentHash())
|
||||||
return sm.processWithParent(block, parent)
|
return sm.processWithParent(block, parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, err error) {
|
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, receipts types.Receipts, err error) {
|
||||||
// Create a new state based on the parent's root (e.g., create copy)
|
// Create a new state based on the parent's root (e.g., create copy)
|
||||||
state := state.New(parent.Root(), sm.db)
|
state := state.New(parent.Root(), sm.db)
|
||||||
header := block.Header()
|
header := block.Header()
|
||||||
@ -192,10 +192,10 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
|
|||||||
|
|
||||||
// There can be at most two uncles
|
// There can be at most two uncles
|
||||||
if len(uncles) > 2 {
|
if len(uncles) > 2 {
|
||||||
return nil, ValidationError("Block can only contain maximum 2 uncles (contained %v)", len(uncles))
|
return nil, nil, ValidationError("Block can only contain maximum 2 uncles (contained %v)", len(uncles))
|
||||||
}
|
}
|
||||||
|
|
||||||
receipts, err := sm.TransitionState(state, parent, block, false)
|
receipts, err = sm.TransitionState(state, parent, block, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -248,15 +248,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
|
|||||||
// Sync the current block's state to the database
|
// Sync the current block's state to the database
|
||||||
state.Sync()
|
state.Sync()
|
||||||
|
|
||||||
// This puts transactions in a extra db for rpc
|
return state.Logs(), receipts, nil
|
||||||
for i, tx := range block.Transactions() {
|
|
||||||
putTx(sm.extraDb, tx, block, uint64(i))
|
|
||||||
}
|
|
||||||
|
|
||||||
// store the receipts
|
|
||||||
putReceipts(sm.extraDb, block.Hash(), receipts)
|
|
||||||
|
|
||||||
return state.Logs(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -411,43 +403,3 @@ func getBlockReceipts(db common.Database, bhash common.Hash) (receipts types.Rec
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func putTx(db common.Database, tx *types.Transaction, block *types.Block, i uint64) {
|
|
||||||
rlpEnc, err := rlp.EncodeToBytes(tx)
|
|
||||||
if err != nil {
|
|
||||||
glog.V(logger.Debug).Infoln("Failed encoding tx", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
db.Put(tx.Hash().Bytes(), rlpEnc)
|
|
||||||
|
|
||||||
var txExtra struct {
|
|
||||||
BlockHash common.Hash
|
|
||||||
BlockIndex uint64
|
|
||||||
Index uint64
|
|
||||||
}
|
|
||||||
txExtra.BlockHash = block.Hash()
|
|
||||||
txExtra.BlockIndex = block.NumberU64()
|
|
||||||
txExtra.Index = i
|
|
||||||
rlpMeta, err := rlp.EncodeToBytes(txExtra)
|
|
||||||
if err != nil {
|
|
||||||
glog.V(logger.Debug).Infoln("Failed encoding tx meta data", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
|
|
||||||
}
|
|
||||||
|
|
||||||
func putReceipts(db common.Database, hash common.Hash, receipts types.Receipts) error {
|
|
||||||
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
|
|
||||||
for i, receipt := range receipts {
|
|
||||||
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes, err := rlp.EncodeToBytes(storageReceipts)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Put(append(receiptsPre, hash[:]...), bytes)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -18,7 +18,7 @@ func proc() (*BlockProcessor, *ChainManager) {
|
|||||||
var mux event.TypeMux
|
var mux event.TypeMux
|
||||||
|
|
||||||
genesis := GenesisBlock(0, db)
|
genesis := GenesisBlock(0, db)
|
||||||
chainMan, err := NewChainManager(genesis, db, db, thePow(), &mux)
|
chainMan, err := NewChainManager(genesis, db, db, db, thePow(), &mux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ func TestPutReceipt(t *testing.T) {
|
|||||||
Index: 0,
|
Index: 0,
|
||||||
}})
|
}})
|
||||||
|
|
||||||
putReceipts(db, hash, types.Receipts{receipt})
|
PutReceipts(db, hash, types.Receipts{receipt})
|
||||||
receipts, err := getBlockReceipts(db, hash)
|
receipts, err := getBlockReceipts(db, hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("got err:", err)
|
t.Error("got err:", err)
|
||||||
|
@ -167,7 +167,7 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
|
|||||||
// InsertChain on the result of makeChain.
|
// InsertChain on the result of makeChain.
|
||||||
func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
|
func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
|
||||||
evmux := &event.TypeMux{}
|
evmux := &event.TypeMux{}
|
||||||
chainman, _ := NewChainManager(GenesisBlock(0, db), db, db, FakePow{}, evmux)
|
chainman, _ := NewChainManager(GenesisBlock(0, db), db, db, db, FakePow{}, evmux)
|
||||||
bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux)
|
bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux)
|
||||||
bman.bc.SetProcessor(bman)
|
bman.bc.SetProcessor(bman)
|
||||||
parent := bman.bc.CurrentBlock()
|
parent := bman.bc.CurrentBlock()
|
||||||
|
@ -58,7 +58,7 @@ func ExampleGenerateChain() {
|
|||||||
|
|
||||||
// Import the chain. This runs all block validation rules.
|
// Import the chain. This runs all block validation rules.
|
||||||
evmux := &event.TypeMux{}
|
evmux := &event.TypeMux{}
|
||||||
chainman, _ := NewChainManager(genesis, db, db, FakePow{}, evmux)
|
chainman, _ := NewChainManager(genesis, db, db, db, FakePow{}, evmux)
|
||||||
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
|
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
|
||||||
if i, err := chainman.InsertChain(chain); err != nil {
|
if i, err := chainman.InsertChain(chain); err != nil {
|
||||||
fmt.Printf("insert error (block %d): %v\n", i, err)
|
fmt.Printf("insert error (block %d): %v\n", i, err)
|
||||||
|
@ -42,6 +42,7 @@ type ChainManager struct {
|
|||||||
//eth EthManager
|
//eth EthManager
|
||||||
blockDb common.Database
|
blockDb common.Database
|
||||||
stateDb common.Database
|
stateDb common.Database
|
||||||
|
extraDb common.Database
|
||||||
processor types.BlockProcessor
|
processor types.BlockProcessor
|
||||||
eventMux *event.TypeMux
|
eventMux *event.TypeMux
|
||||||
genesisBlock *types.Block
|
genesisBlock *types.Block
|
||||||
@ -70,11 +71,12 @@ type ChainManager struct {
|
|||||||
pow pow.PoW
|
pow pow.PoW
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) {
|
func NewChainManager(genesis *types.Block, blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) {
|
||||||
cache, _ := lru.New(blockCacheLimit)
|
cache, _ := lru.New(blockCacheLimit)
|
||||||
bc := &ChainManager{
|
bc := &ChainManager{
|
||||||
blockDb: blockDb,
|
blockDb: blockDb,
|
||||||
stateDb: stateDb,
|
stateDb: stateDb,
|
||||||
|
extraDb: extraDb,
|
||||||
genesisBlock: GenesisBlock(42, stateDb),
|
genesisBlock: GenesisBlock(42, stateDb),
|
||||||
eventMux: mux,
|
eventMux: mux,
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
@ -477,10 +479,10 @@ func (self *ChainManager) procFutureBlocks() {
|
|||||||
type writeStatus byte
|
type writeStatus byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
nonStatTy writeStatus = iota
|
NonStatTy writeStatus = iota
|
||||||
canonStatTy
|
CanonStatTy
|
||||||
splitStatTy
|
SplitStatTy
|
||||||
sideStatTy
|
SideStatTy
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteBlock writes the block to the chain (or pending queue)
|
// WriteBlock writes the block to the chain (or pending queue)
|
||||||
@ -497,10 +499,10 @@ func (self *ChainManager) WriteBlock(block *types.Block, queued bool) (status wr
|
|||||||
// during split we merge two different chains and create the new canonical chain
|
// during split we merge two different chains and create the new canonical chain
|
||||||
err := self.merge(cblock, block)
|
err := self.merge(cblock, block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nonStatTy, err
|
return NonStatTy, err
|
||||||
}
|
}
|
||||||
|
|
||||||
status = splitStatTy
|
status = SplitStatTy
|
||||||
}
|
}
|
||||||
|
|
||||||
self.mu.Lock()
|
self.mu.Lock()
|
||||||
@ -511,9 +513,9 @@ func (self *ChainManager) WriteBlock(block *types.Block, queued bool) (status wr
|
|||||||
self.setTransState(state.New(block.Root(), self.stateDb))
|
self.setTransState(state.New(block.Root(), self.stateDb))
|
||||||
self.txState.SetState(state.New(block.Root(), self.stateDb))
|
self.txState.SetState(state.New(block.Root(), self.stateDb))
|
||||||
|
|
||||||
status = canonStatTy
|
status = CanonStatTy
|
||||||
} else {
|
} else {
|
||||||
status = sideStatTy
|
status = SideStatTy
|
||||||
}
|
}
|
||||||
|
|
||||||
self.write(block)
|
self.write(block)
|
||||||
@ -581,7 +583,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
|
|||||||
|
|
||||||
// Call in to the block processor and check for errors. It's likely that if one block fails
|
// Call in to the block processor and check for errors. It's likely that if one block fails
|
||||||
// all others will fail too (unless a known block is returned).
|
// all others will fail too (unless a known block is returned).
|
||||||
logs, err := self.processor.Process(block)
|
logs, receipts, err := self.processor.Process(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsKnownBlockErr(err) {
|
if IsKnownBlockErr(err) {
|
||||||
stats.ignored++
|
stats.ignored++
|
||||||
@ -620,19 +622,24 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
switch status {
|
switch status {
|
||||||
case canonStatTy:
|
case CanonStatTy:
|
||||||
if glog.V(logger.Debug) {
|
if glog.V(logger.Debug) {
|
||||||
glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
|
glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
|
||||||
}
|
}
|
||||||
queue[i] = ChainEvent{block, block.Hash(), logs}
|
queue[i] = ChainEvent{block, block.Hash(), logs}
|
||||||
queueEvent.canonicalCount++
|
queueEvent.canonicalCount++
|
||||||
case sideStatTy:
|
|
||||||
|
// This puts transactions in a extra db for rpc
|
||||||
|
PutTransactions(self.extraDb, block, block.Transactions())
|
||||||
|
// store the receipts
|
||||||
|
PutReceipts(self.extraDb, block.Hash(), receipts)
|
||||||
|
case SideStatTy:
|
||||||
if glog.V(logger.Detail) {
|
if glog.V(logger.Detail) {
|
||||||
glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
|
glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
|
||||||
}
|
}
|
||||||
queue[i] = ChainSideEvent{block, logs}
|
queue[i] = ChainSideEvent{block, logs}
|
||||||
queueEvent.sideCount++
|
queueEvent.sideCount++
|
||||||
case splitStatTy:
|
case SplitStatTy:
|
||||||
queue[i] = ChainSplitEvent{block, logs}
|
queue[i] = ChainSplitEvent{block, logs}
|
||||||
queueEvent.splitCount++
|
queueEvent.splitCount++
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func thePow() pow.PoW {
|
|||||||
func theChainManager(db common.Database, t *testing.T) *ChainManager {
|
func theChainManager(db common.Database, t *testing.T) *ChainManager {
|
||||||
var eventMux event.TypeMux
|
var eventMux event.TypeMux
|
||||||
genesis := GenesisBlock(0, db)
|
genesis := GenesisBlock(0, db)
|
||||||
chainMan, err := NewChainManager(genesis, db, db, thePow(), &eventMux)
|
chainMan, err := NewChainManager(genesis, db, db, db, thePow(), &eventMux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("failed creating chainmanager:", err)
|
t.Error("failed creating chainmanager:", err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -96,7 +96,7 @@ func printChain(bc *ChainManager) {
|
|||||||
func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) {
|
func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) {
|
||||||
td := new(big.Int)
|
td := new(big.Int)
|
||||||
for _, block := range chainB {
|
for _, block := range chainB {
|
||||||
_, err := bman.bc.processor.Process(block)
|
_, _, err := bman.bc.processor.Process(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsKnownBlockErr(err) {
|
if IsKnownBlockErr(err) {
|
||||||
continue
|
continue
|
||||||
@ -367,7 +367,7 @@ func TestGetBlocksFromHash(t *testing.T) {
|
|||||||
|
|
||||||
type bproc struct{}
|
type bproc struct{}
|
||||||
|
|
||||||
func (bproc) Process(*types.Block) (state.Logs, error) { return nil, nil }
|
func (bproc) Process(*types.Block) (state.Logs, types.Receipts, error) { return nil, nil, nil }
|
||||||
|
|
||||||
func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block {
|
func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block {
|
||||||
var chain []*types.Block
|
var chain []*types.Block
|
||||||
@ -390,7 +390,7 @@ func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block
|
|||||||
|
|
||||||
func chm(genesis *types.Block, db common.Database) *ChainManager {
|
func chm(genesis *types.Block, db common.Database) *ChainManager {
|
||||||
var eventMux event.TypeMux
|
var eventMux event.TypeMux
|
||||||
bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}}
|
bc := &ChainManager{extraDb: db, blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}}
|
||||||
bc.cache, _ = lru.New(100)
|
bc.cache, _ = lru.New(100)
|
||||||
bc.futureBlocks, _ = lru.New(100)
|
bc.futureBlocks, _ = lru.New(100)
|
||||||
bc.processor = bproc{}
|
bc.processor = bproc{}
|
||||||
@ -479,12 +479,12 @@ func TestGenesisMismatch(t *testing.T) {
|
|||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
var mux event.TypeMux
|
var mux event.TypeMux
|
||||||
genesis := GenesisBlock(0, db)
|
genesis := GenesisBlock(0, db)
|
||||||
_, err := NewChainManager(genesis, db, db, thePow(), &mux)
|
_, err := NewChainManager(genesis, db, db, db, thePow(), &mux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
genesis = GenesisBlock(1, db)
|
genesis = GenesisBlock(1, db)
|
||||||
_, err = NewChainManager(genesis, db, db, thePow(), &mux)
|
_, err = NewChainManager(genesis, db, db, db, thePow(), &mux)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected genesis mismatch error")
|
t.Error("expected genesis mismatch error")
|
||||||
}
|
}
|
||||||
|
@ -14,5 +14,6 @@ type Backend interface {
|
|||||||
TxPool() *TxPool
|
TxPool() *TxPool
|
||||||
BlockDb() common.Database
|
BlockDb() common.Database
|
||||||
StateDb() common.Database
|
StateDb() common.Database
|
||||||
|
ExtraDb() common.Database
|
||||||
EventMux() *event.TypeMux
|
EventMux() *event.TypeMux
|
||||||
}
|
}
|
||||||
|
51
core/transaction_util.go
Normal file
51
core/transaction_util.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PutTransactions(db common.Database, block *types.Block, txs types.Transactions) {
|
||||||
|
for i, tx := range block.Transactions() {
|
||||||
|
rlpEnc, err := rlp.EncodeToBytes(tx)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(logger.Debug).Infoln("Failed encoding tx", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.Put(tx.Hash().Bytes(), rlpEnc)
|
||||||
|
|
||||||
|
var txExtra struct {
|
||||||
|
BlockHash common.Hash
|
||||||
|
BlockIndex uint64
|
||||||
|
Index uint64
|
||||||
|
}
|
||||||
|
txExtra.BlockHash = block.Hash()
|
||||||
|
txExtra.BlockIndex = block.NumberU64()
|
||||||
|
txExtra.Index = uint64(i)
|
||||||
|
rlpMeta, err := rlp.EncodeToBytes(txExtra)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(logger.Debug).Infoln("Failed encoding tx meta data", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PutReceipts(db common.Database, hash common.Hash, receipts types.Receipts) error {
|
||||||
|
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
|
||||||
|
for i, receipt := range receipts {
|
||||||
|
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := rlp.EncodeToBytes(storageReceipts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Put(append(receiptsPre, hash[:]...), bytes)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BlockProcessor interface {
|
type BlockProcessor interface {
|
||||||
Process(*Block) (state.Logs, error)
|
Process(*Block) (state.Logs, Receipts, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const bloomLength = 256
|
const bloomLength = 256
|
||||||
|
@ -339,7 +339,7 @@ func New(config *Config) (*Ethereum, error) {
|
|||||||
|
|
||||||
eth.pow = ethash.New()
|
eth.pow = ethash.New()
|
||||||
genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb)
|
genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb)
|
||||||
eth.chainManager, err = core.NewChainManager(genesis, blockDb, stateDb, eth.pow, eth.EventMux())
|
eth.chainManager, err = core.NewChainManager(genesis, blockDb, stateDb, extraDb, eth.pow, eth.EventMux())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,7 @@ func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *Protocol
|
|||||||
var (
|
var (
|
||||||
em = new(event.TypeMux)
|
em = new(event.TypeMux)
|
||||||
db, _ = ethdb.NewMemDatabase()
|
db, _ = ethdb.NewMemDatabase()
|
||||||
chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, core.FakePow{}, em)
|
chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, db, core.FakePow{}, em)
|
||||||
txpool = &fakeTxPool{added: txAdded}
|
txpool = &fakeTxPool{added: txAdded}
|
||||||
pm = NewProtocolManager(0, em, txpool, core.FakePow{}, chain)
|
pm = NewProtocolManager(0, em, txpool, core.FakePow{}, chain)
|
||||||
)
|
)
|
||||||
|
@ -79,9 +79,10 @@ type worker struct {
|
|||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
pow pow.PoW
|
pow pow.PoW
|
||||||
|
|
||||||
eth core.Backend
|
eth core.Backend
|
||||||
chain *core.ChainManager
|
chain *core.ChainManager
|
||||||
proc *core.BlockProcessor
|
proc *core.BlockProcessor
|
||||||
|
extraDb common.Database
|
||||||
|
|
||||||
coinbase common.Address
|
coinbase common.Address
|
||||||
gasPrice *big.Int
|
gasPrice *big.Int
|
||||||
@ -105,6 +106,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker {
|
|||||||
worker := &worker{
|
worker := &worker{
|
||||||
eth: eth,
|
eth: eth,
|
||||||
mux: eth.EventMux(),
|
mux: eth.EventMux(),
|
||||||
|
extraDb: eth.ExtraDb(),
|
||||||
recv: make(chan *types.Block),
|
recv: make(chan *types.Block),
|
||||||
gasPrice: new(big.Int),
|
gasPrice: new(big.Int),
|
||||||
chain: eth.ChainManager(),
|
chain: eth.ChainManager(),
|
||||||
@ -233,11 +235,28 @@ func (self *worker) wait() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := self.chain.WriteBlock(block, false)
|
parent := self.chain.GetBlock(block.ParentHash())
|
||||||
|
if parent == nil {
|
||||||
|
glog.V(logger.Error).Infoln("Invalid block found during mining")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true); err != nil {
|
||||||
|
glog.V(logger.Error).Infoln("Invalid header on mined block:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
stat, err := self.chain.WriteBlock(block, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.V(logger.Error).Infoln("error writing block to chain", err)
|
glog.V(logger.Error).Infoln("error writing block to chain", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// check if canon block and write transactions
|
||||||
|
if stat == core.CanonStatTy {
|
||||||
|
// This puts transactions in a extra db for rpc
|
||||||
|
core.PutTransactions(self.extraDb, block, block.Transactions())
|
||||||
|
// store the receipts
|
||||||
|
core.PutReceipts(self.extraDb, block.Hash(), self.current.receipts)
|
||||||
|
}
|
||||||
|
|
||||||
// check staleness and display confirmation
|
// check staleness and display confirmation
|
||||||
var stale, confirm string
|
var stale, confirm string
|
||||||
@ -252,7 +271,13 @@ func (self *worker) wait() {
|
|||||||
glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm)
|
glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm)
|
||||||
|
|
||||||
// broadcast before waiting for validation
|
// broadcast before waiting for validation
|
||||||
go self.mux.Post(core.NewMinedBlockEvent{block})
|
go func(block *types.Block, logs state.Logs) {
|
||||||
|
self.mux.Post(core.NewMinedBlockEvent{block})
|
||||||
|
self.mux.Post(core.ChainEvent{block, block.Hash(), logs})
|
||||||
|
if stat == core.CanonStatTy {
|
||||||
|
self.mux.Post(core.ChainHeadEvent{block})
|
||||||
|
}
|
||||||
|
}(block, self.current.state.Logs())
|
||||||
|
|
||||||
self.commitNewWork()
|
self.commitNewWork()
|
||||||
}
|
}
|
||||||
|
@ -980,6 +980,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
|
|||||||
} else {
|
} else {
|
||||||
glog.V(logger.Info).Infof("Tx(%x) to: %x\n", tx.Hash(), tx.To())
|
glog.V(logger.Info).Infof("Tx(%x) to: %x\n", tx.Hash(), tx.To())
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.Hash().Hex(), nil
|
return tx.Hash().Hex(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user