Add more functionalities to worker

pull/91/head^2
Rongjian Lan 6 years ago
parent d35f12b85a
commit 5a3755419f
  1. 2
      node/node.go
  2. 55
      node/worker/worker.go

@ -8,6 +8,7 @@ import (
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/node/worker"
"net" "net"
"sync" "sync"
"time" "time"
@ -58,6 +59,7 @@ type Node struct {
Chain *core.BlockChain Chain *core.BlockChain
TxPool *core.TxPool TxPool *core.TxPool
BlockChannelAccount chan *types.Block // The channel to receive new blocks from Node BlockChannelAccount chan *types.Block // The channel to receive new blocks from Node
worker *worker.Worker
} }
// Add new crossTx and proofs to the list of crossTx that needs to be sent back to client // Add new crossTx and proofs to the list of crossTx that needs to be sent back to client

@ -7,14 +7,13 @@ import (
"github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm" "github.com/harmony-one/harmony/core/vm"
"math/big"
"time"
) )
// environment is the worker's current environment and holds all of the current state information. // environment is the worker's current environment and holds all of the current state information.
type environment struct { type environment struct {
signer types.Signer
state *state.StateDB // apply state changes here state *state.StateDB // apply state changes here
tcount int // tx count in cycle
gasPool *core.GasPool // available gas used to pack transactions gasPool *core.GasPool // available gas used to pack transactions
header *types.Header header *types.Header
@ -24,13 +23,16 @@ type environment struct {
// worker is the main object which takes care of submitting new work to consensus engine // worker is the main object which takes care of submitting new work to consensus engine
// and gathering the sealing result. // and gathering the sealing result.
type worker struct { type Worker struct {
config *params.ChainConfig config *params.ChainConfig
chain *core.BlockChain chain *core.BlockChain
current *environment // An environment for current running cycle. current *environment // An environment for current running cycle.
gasFloor uint64
gasCeil uint64
} }
func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) { func (w *Worker) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) {
snap := w.current.state.Snapshot() snap := w.current.state.Snapshot()
receipt, _, err := core.ApplyTransaction(w.config, w.chain, &coinbase, w.current.gasPool, w.current.state, w.current.header, tx, &w.current.header.GasUsed, vm.Config{}) receipt, _, err := core.ApplyTransaction(w.config, w.chain, &coinbase, w.current.gasPool, w.current.state, w.current.header, tx, &w.current.header.GasUsed, vm.Config{})
@ -43,3 +45,46 @@ func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Addres
return receipt.Logs, nil return receipt.Logs, nil
} }
func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Address) {
for _, tx := range txs {
w.commitTransaction(tx, coinbase)
}
}
// makeCurrent creates a new environment for the current cycle.
func (w *Worker) makeCurrent(parent *types.Block, header *types.Header) error {
state, err := w.chain.StateAt(parent.Root())
if err != nil {
return err
}
env := &environment{
state: state,
header: header,
}
w.current = env
return nil
}
func New(config *params.ChainConfig, chain *core.BlockChain) *Worker {
worker := &Worker{
config: config,
chain: chain,
}
worker.gasFloor = 0
worker.gasCeil = 10000000
parent := worker.chain.CurrentBlock()
num := parent.Number()
timestamp := time.Now().Unix()
header := &types.Header{
ParentHash: parent.Hash(),
Number: num.Add(num, common.Big1),
GasLimit: core.CalcGasLimit(parent, worker.gasFloor, worker.gasCeil),
Time: big.NewInt(timestamp),
}
worker.makeCurrent(parent, header)
return worker
}
Loading…
Cancel
Save