Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize meerpool and change the eth original behavior #74

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ replace (
golang.org/x/tools v0.0.0-20181006002542-f60d9635b16a => github.com/golang/tools v0.0.0-20181006002542-f60d9635b16a
)

replace github.com/ethereum/go-ethereum v1.10.9 => github.com/Qitmeer/go-ethereum v1.10.9-q.6
replace github.com/ethereum/go-ethereum v1.10.9 => github.com/Qitmeer/go-ethereum v1.10.9-q.7
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a h1:LUGOJG/lF0TlnlYT
github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:gbGKdXSJn71Mc2xcKJHqC/waPiX0byZae67zarj83m4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a h1:O2Erw/YvYAkIqkc2uvP/WwuWf0V8S0+pjU/FKHmjFU4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:KiA7g46zc6dkgf/3NbEpJirY75v656WYlmSQNR1wTVk=
github.com/Qitmeer/go-ethereum v1.10.9-q.6 h1:vDjqE5wJi7M9Ddb+BPirGDJ1RToPUOSJlltRPlxDL98=
github.com/Qitmeer/go-ethereum v1.10.9-q.6/go.mod h1:8bALkv9mq8Bvsw8TEUaxO2TtxAGRxoXebspLZ+CSJ9U=
github.com/Qitmeer/go-ethereum v1.10.9-q.7 h1:7gAqVxpj7Y3E2BDsXZk16xdjORMeS5+uhWWvmE6qcYU=
github.com/Qitmeer/go-ethereum v1.10.9-q.7/go.mod h1:8bALkv9mq8Bvsw8TEUaxO2TtxAGRxoXebspLZ+CSJ9U=
github.com/Shopify/sarama v1.29.1/go.mod h1:mdtqvCSg8JOxk8PmpTNGyo6wzd4BMm4QXSfDnTXmgkE=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
Expand Down
5 changes: 3 additions & 2 deletions meerevm/chain/ethchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package chain
import (
"crypto/ecdsa"
"fmt"
"github.com/Qitmeer/qng/meerevm/evm/engine"
"github.com/Qitmeer/qng/core/protocol"
"github.com/Qitmeer/qng/meerevm/evm/engine"
qparams "github.com/Qitmeer/qng/params"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/external"
Expand Down Expand Up @@ -441,6 +441,7 @@ func MakeMeerethConfig(datadir string) (*MeerethConfig, error) {

econfig.Miner.Etherbase = etherbase
econfig.Miner.ExtraData = []byte{byte(0)}
econfig.Miner.External = &MeerPool{}

econfig.TxPool.NoLocals = true

Expand Down Expand Up @@ -696,7 +697,7 @@ func InitEnv(env string) {
return
}
if e, err := strconv.Unquote(env); err == nil {
env = e;
env = e
}
args := strings.Split(env, " ")
if len(args) <= 0 {
Expand Down
17 changes: 9 additions & 8 deletions meerevm/chain/meerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ package chain
import (
"encoding/hex"
"fmt"
qcommon "github.com/Qitmeer/qng/meerevm/common"
qconsensus "github.com/Qitmeer/qng/vm/consensus"
qtypes "github.com/Qitmeer/qng/core/types"
qcommon "github.com/Qitmeer/qng/meerevm/common"
"github.com/Qitmeer/qng/rpc/api"
qconsensus "github.com/Qitmeer/qng/vm/consensus"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -249,13 +249,13 @@ func (b *MeerChain) addTx(tx *types.Transaction, header *types.Header, statedb *
config := b.chain.Config().Eth.Genesis.Config
statedb.Prepare(tx.Hash(), len(*txs))

bc:=b.chain.Ether().BlockChain()
bc := b.chain.Ether().BlockChain()
snap := statedb.Snapshot()

receipt, err := core.ApplyTransaction(config, bc, &header.Coinbase, gasPool, statedb, header, tx, &header.GasUsed, *bc.GetVMConfig())
if err != nil {
statedb.RevertToSnapshot(snap)
return err
return err
}

*txs = append(*txs, tx)
Expand All @@ -282,11 +282,11 @@ func (b *MeerChain) RegisterAPIs(apis []api.API) {
}

func (b *MeerChain) Start() {
b.meerpool.start()
b.meerpool.Start(b.chain.config.Eth.Miner.Etherbase)
}

func (b *MeerChain) Stop() {
b.meerpool.stop()
b.meerpool.Stop()
}

func (b *MeerChain) MeerPool() *MeerPool {
Expand All @@ -296,8 +296,9 @@ func (b *MeerChain) MeerPool() *MeerPool {
func NewMeerChain(chain *ETHChain, ctx qconsensus.Context) *MeerChain {
mc := &MeerChain{
chain: chain,
meerpool: newMeerPool(&chain.config.Eth.Miner, chain.config.Eth.Genesis.Config, chain.ether.Engine(), chain.ether, chain.ether.EventMux(), ctx),
meerpool: chain.config.Eth.Miner.External.(*MeerPool),
}
mc.meerpool.init(&chain.config.Eth.Miner, chain.config.Eth.Genesis.Config, chain.ether.Engine(), chain.ether, chain.ether.EventMux(), ctx)
return mc
}

Expand All @@ -311,7 +312,7 @@ func makeHeader(cfg *ethconfig.Config, parent *types.Block, state *state.StateDB
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
Difficulty: common.Big1,
GasLimit: core.CalcGasLimit(parent.GasLimit(), cfg.Miner.GasCeil),
GasLimit: 0x7fffffffffffffff,
Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: uint64(timestamp),
}
Expand Down
166 changes: 134 additions & 32 deletions meerevm/chain/meerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"github.com/ethereum/go-ethereum/miner"
"math/big"
"sync"
"sync/atomic"
"time"

qtypes "github.com/Qitmeer/qng/core/types"
qcommon "github.com/Qitmeer/qng/meerevm/common"
qconsensus "github.com/Qitmeer/qng/vm/consensus"
qtypes "github.com/Qitmeer/qng/core/types"
"github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
Expand Down Expand Up @@ -54,8 +55,9 @@ type environment struct {
}

type MeerPool struct {
wg sync.WaitGroup
quit chan struct{}
wg sync.WaitGroup
quit chan struct{}
running int32

ctx qconsensus.Context

Expand All @@ -79,41 +81,62 @@ type MeerPool struct {

mu sync.RWMutex // The lock used to protect the coinbase and extra fields

snapshotMu sync.RWMutex // The lock used to protect the snapshots below
snapshotBlock *types.Block
snapshotMu sync.RWMutex // The lock used to protect the snapshots below
snapshotBlock *types.Block
snapshotReceipts types.Receipts
snapshotState *state.StateDB

// Feeds
pendingLogsFeed event.Feed
}

func newMeerPool(config *miner.Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, ctx qconsensus.Context) *MeerPool {
func (m *MeerPool) init(config *miner.Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, ctx qconsensus.Context) error {
log.Info(fmt.Sprintf("Meer pool init..."))

mp := &MeerPool{
ctx: ctx,
config: config,
chainConfig: chainConfig,
engine: engine,
eth: eth,
mux: mux,
chain: eth.BlockChain(),
txsCh: make(chan core.NewTxsEvent, txChanSize),
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
quit: make(chan struct{}),
remoteTxsQM: map[string]*qtypes.Transaction{},
remoteTxsM: map[string]*qtypes.Transaction{},
}
mp.txsSub = eth.TxPool().SubscribeNewTxsEvent(mp.txsCh)
mp.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(mp.chainHeadCh)

mp.wg.Add(1)
go mp.handler()

return mp
m.ctx = ctx
m.config = config
m.chainConfig = chainConfig
m.engine = engine
m.eth = eth
m.mux = mux
m.chain = eth.BlockChain()
m.txsCh = make(chan core.NewTxsEvent, txChanSize)
m.chainHeadCh = make(chan core.ChainHeadEvent, chainHeadChanSize)
m.quit = make(chan struct{})
m.remoteTxsQM = map[string]*qtypes.Transaction{}
m.remoteTxsM = map[string]*qtypes.Transaction{}
m.txsSub = eth.TxPool().SubscribeNewTxsEvent(m.txsCh)
m.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(m.chainHeadCh)

return nil
}

func (m *MeerPool) start() {
func (m *MeerPool) Start(coinbase common.Address) {
if m.isRunning() {
log.Info("Meer pool was started")
return
}

atomic.StoreInt32(&m.running, 1)

m.quit = make(chan struct{})
m.wg.Add(1)
go m.handler()

m.updateTemplate(time.Now().Unix())
}

func (m *MeerPool) stop() {
func (m *MeerPool) Close() {

}

func (m *MeerPool) Stop() {
if !m.isRunning() {
log.Info("Meer pool was stopped")
return
}
atomic.StoreInt32(&m.running, 0)

log.Info(fmt.Sprintf("Meer pool stopping"))
if m.current != nil && m.current.state != nil {
m.current.state.StopPrefetcher()
Expand All @@ -124,6 +147,10 @@ func (m *MeerPool) stop() {
log.Info(fmt.Sprintf("Meer pool stopped"))
}

func (m *MeerPool) isRunning() bool {
return atomic.LoadInt32(&m.running) == 1
}

func (m *MeerPool) handler() {
defer m.txsSub.Unsubscribe()
defer m.chainHeadSub.Unsubscribe()
Expand Down Expand Up @@ -208,6 +235,9 @@ func (m *MeerPool) updateSnapshot() {
m.current.receipts,
trie.NewStackTrie(nil),
)

m.snapshotReceipts = qcommon.CopyReceipts(m.current.receipts)
m.snapshotState = m.current.state.Copy()
}

func (m *MeerPool) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) {
Expand All @@ -234,6 +264,8 @@ func (m *MeerPool) commitTransactions(txs *types.TransactionsByPriceAndNonce, co
m.current.gasPool = new(core.GasPool).AddGas(gasLimit)
}

var coalescedLogs []*types.Log

for {
if m.current.gasPool.Gas() < params.TxGas {
log.Trace("Not enough gas for further transactions", "have", m.current.gasPool, "want", params.TxGas)
Expand All @@ -252,7 +284,7 @@ func (m *MeerPool) commitTransactions(txs *types.TransactionsByPriceAndNonce, co
}
m.current.state.Prepare(tx.Hash(), m.current.tcount)

_, err := m.commitTransaction(tx, coinbase)
logs, err := m.commitTransaction(tx, coinbase)
switch {
case errors.Is(err, core.ErrGasLimitReached):
log.Trace("Gas limit exceeded for current block", "sender", from)
Expand All @@ -267,6 +299,7 @@ func (m *MeerPool) commitTransactions(txs *types.TransactionsByPriceAndNonce, co
txs.Pop()

case errors.Is(err, nil):
coalescedLogs = append(coalescedLogs, logs...)
m.current.tcount++
txs.Shift()

Expand All @@ -279,7 +312,14 @@ func (m *MeerPool) commitTransactions(txs *types.TransactionsByPriceAndNonce, co
txs.Shift()
}
}

if len(coalescedLogs) > 0 {
cpy := make([]*types.Log, len(coalescedLogs))
for i, l := range coalescedLogs {
cpy[i] = new(types.Log)
*cpy[i] = *l
}
m.pendingLogsFeed.Send(cpy)
}
return false
}

Expand All @@ -297,7 +337,7 @@ func (m *MeerPool) updateTemplate(timestamp int64) {
header := &types.Header{
ParentHash: parent.Hash(),
Number: num.Add(num, common.Big1),
GasLimit: core.CalcGasLimit(parent.GasLimit(), m.config.GasCeil),
GasLimit: 0x7fffffffffffffff,
Extra: m.config.ExtraData,
Time: uint64(timestamp),
Coinbase: m.config.Etherbase,
Expand Down Expand Up @@ -511,3 +551,65 @@ func (m *MeerPool) AnnounceNewTransactions(txs []*types.Transaction) error {

return nil
}

func (m *MeerPool) Mining() bool {
log.Debug("Temporarily not supported: Mining")
return false
}

func (m *MeerPool) Hashrate() uint64 {
log.Debug("Temporarily not supported: Hashrate")
return 0
}

func (m *MeerPool) SetExtra(extra []byte) error {
log.Debug("Temporarily not supported: SetExtra")
return nil
}

func (m *MeerPool) SetRecommitInterval(interval time.Duration) {
log.Debug("Temporarily not supported: SetRecommitInterval")
}

func (m *MeerPool) Pending() (*types.Block, *state.StateDB) {
m.snapshotMu.RLock()
defer m.snapshotMu.RUnlock()
if m.snapshotState == nil {
return nil, nil
}
return m.snapshotBlock, m.snapshotState.Copy()
}

func (m *MeerPool) PendingBlock() *types.Block {
m.snapshotMu.RLock()
defer m.snapshotMu.RUnlock()
return m.snapshotBlock
}

func (m *MeerPool) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
m.snapshotMu.RLock()
defer m.snapshotMu.RUnlock()
return m.snapshotBlock, m.snapshotReceipts
}

func (m *MeerPool) SetEtherbase(addr common.Address) {
log.Debug("Temporarily not supported: SetEtherbase")
}

func (m *MeerPool) SetGasCeil(ceil uint64) {
m.mu.Lock()
defer m.mu.Unlock()
m.config.GasCeil = ceil
}

func (m *MeerPool) EnablePreseal() {
log.Debug("Temporarily not supported: EnablePreseal")
}

func (m *MeerPool) DisablePreseal() {
log.Debug("Temporarily not supported: DisablePreseal")
}

func (m *MeerPool) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {
return m.pendingLogsFeed.Subscribe(ch)
}
6 changes: 0 additions & 6 deletions meerevm/evm/engine/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ func (me *MeerEngine) verifyHeader(chain consensus.ChainHeaderReader, header, pa
if header.BaseFee != nil {
return fmt.Errorf("invalid baseFee before fork: have %d, expected 'nil'", header.BaseFee)
}
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
return err
}
} else if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil {
// Verify the header's EIP-1559 attributes.
return err
}
// Verify that the block number is parent's +1
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
const (
Major uint = 1
Minor uint = 0
Patch uint = 7
Patch uint = 8
)

var (
Expand Down