Skip to content

Commit

Permalink
Merge pull request #2011 from PlatONnetwork/feature/adapt-to-mapprotocol
Browse files Browse the repository at this point in the history
Feature/adapt to mapprotocol
  • Loading branch information
benbaley authored Mar 17, 2023
2 parents a3fe094 + 66eb12d commit e11fe5c
Show file tree
Hide file tree
Showing 32 changed files with 661 additions and 168 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (b *SimulatedBackend) callContract(_ context.Context, call ethereum.CallMsg
func (b *SimulatedBackend) SendTransaction(_ context.Context, tx *types.Transaction) error {
b.mu.Lock()
defer b.mu.Unlock()
sender, err := types.Sender(types.NewEIP155Signer(b.config.ChainID), tx)
sender, err := types.Sender(types.NewPIP11Signer(b.config.ChainID, b.config.PIP7ChainID), tx)
if err != nil {
panic(fmt.Errorf("invalid transaction: %v", err))
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/platon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ var (
utils.InsecureUnlockAllowedFlag,
utils.RPCGlobalGasCapFlag,
utils.RPCGlobalTxFeeCapFlag,
utils.AllowUnprotectedTxs,
}

metricsFlags = []cli.Flag{
Expand Down Expand Up @@ -176,6 +177,7 @@ var (
utils.DBGCTimeoutFlag,
utils.DBGCMptFlag,
utils.DBGCBlockFlag,
utils.DBValidatorsHistoryFlag,
}

vmFlags = []cli.Flag{
Expand Down
2 changes: 2 additions & 0 deletions cmd/platon/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.JSpathFlag,
utils.ExecFlag,
utils.PreloadJSFlag,
utils.AllowUnprotectedTxs,
},
},
{
Expand Down Expand Up @@ -211,6 +212,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.DBGCTimeoutFlag,
utils.DBGCMptFlag,
utils.DBGCBlockFlag,
utils.DBValidatorsHistoryFlag,
},
},
{
Expand Down
14 changes: 14 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ var (
Name: "preload",
Usage: "Comma separated list of JavaScript files to preload into the console",
}
AllowUnprotectedTxs = &cli.BoolFlag{
Name: "rpc.allow-unprotected-txs",
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC",
}

// Network Settings
MaxPeersFlag = cli.IntFlag{
Expand Down Expand Up @@ -621,6 +625,10 @@ var (
Usage: "Number of cache block states, default 10",
Value: eth.DefaultConfig.DBGCBlock,
}
DBValidatorsHistoryFlag = cli.BoolFlag{
Name: "db.validators_history",
Usage: "Store the list of validators for each consensus round",
}

VMWasmType = cli.StringFlag{
Name: "vm.wasm_type",
Expand Down Expand Up @@ -826,6 +834,9 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
if ctx.GlobalIsSet(HTTPVirtualHostsFlag.Name) {
cfg.HTTPVirtualHosts = SplitAndTrim(ctx.GlobalString(HTTPVirtualHostsFlag.Name))
}
if ctx.IsSet(AllowUnprotectedTxs.Name) {
cfg.AllowUnprotectedTxs = ctx.Bool(AllowUnprotectedTxs.Name)
}
}

// setGraphQL creates the GraphQL listener interface string from the set
Expand Down Expand Up @@ -1185,6 +1196,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
cfg.DBGCBlock = b
}
}
if ctx.GlobalIsSet(DBValidatorsHistoryFlag.Name) {
cfg.DBValidatorsHistory = ctx.GlobalBool(DBValidatorsHistoryFlag.Name)
}

// Read the value from the flag no matter if it's set or not.
cfg.Preimages = ctx.GlobalBool(CachePreimagesFlag.Name)
Expand Down
10 changes: 3 additions & 7 deletions consensus/cbft/consensus_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package cbft

import (
Expand Down Expand Up @@ -137,7 +136,7 @@ func (cbft *Cbft) OnPrepareVote(id string, msg *protocols.PrepareVote) error {
return err
}

cbft.state.AddPrepareVote(uint32(node.Index), msg)
cbft.state.AddPrepareVote(node.Index, msg)
cbft.log.Debug("Receive new prepareVote", "msgHash", msg.MsgHash(), "vote", msg.String(), "votes", cbft.state.PrepareVoteLenByIndex(msg.BlockIndex))

cbft.insertPrepareQC(msg.ParentQC)
Expand Down Expand Up @@ -362,9 +361,6 @@ func (cbft *Cbft) onAsyncExecuteStatus(s *executor.BlockExecuteStatus) {
// Sign the block that has been executed
// Every time try to trigger a send PrepareVote
func (cbft *Cbft) signBlock(hash common.Hash, number uint64, index uint32) error {
// todo sign vote
// parentQC added when sending
// Determine if the current consensus node is
node, err := cbft.validatorPool.GetValidatorByNodeID(cbft.state.Epoch(), cbft.config.Option.NodeID)
if err != nil {
return err
Expand All @@ -375,7 +371,7 @@ func (cbft *Cbft) signBlock(hash common.Hash, number uint64, index uint32) error
BlockHash: hash,
BlockNumber: number,
BlockIndex: index,
ValidatorIndex: uint32(node.Index),
ValidatorIndex: node.Index,
}

if err := cbft.signMsgByBls(prepareVote); err != nil {
Expand Down Expand Up @@ -423,7 +419,7 @@ func (cbft *Cbft) trySendPrepareVote() {
//Determine if the current consensus node is
node, _ := cbft.validatorPool.GetValidatorByNodeID(cbft.state.Epoch(), cbft.config.Option.NodeID)
cbft.log.Info("Add local prepareVote", "vote", p.String())
cbft.state.AddPrepareVote(uint32(node.Index), p)
cbft.state.AddPrepareVote(node.Index, p)
pending.Pop()

// write sendPrepareVote info to wal
Expand Down
2 changes: 0 additions & 2 deletions consensus/cbft/utils/bit_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the PlatON-Go library. If not, see <http://www.gnu.org/licenses/>.


package utils

import (
Expand Down Expand Up @@ -318,7 +317,6 @@ func (bA *BitArray) stringIndented(indent string) string {

// Bytes returns the byte representation of the bits within the bitarray.
func (bA *BitArray) Bytes() []byte {

numBytes := (bA.Bits + 7) / 8
bytes := make([]byte, numBytes)
for i := 0; i < len(bA.Elems); i++ {
Expand Down
2 changes: 1 addition & 1 deletion consensus/cbft/wal_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (cbft *Cbft) recoveryMsg(msg interface{}) error {

cbft.state.HadSendPrepareVote().Push(m.Vote)
node, _ := cbft.validatorPool.GetValidatorByNodeID(m.Vote.Epoch, cbft.config.Option.NodeID)
cbft.state.AddPrepareVote(uint32(node.Index), m.Vote)
cbft.state.AddPrepareVote(node.Index, m.Vote)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (bcc *BlockChainCache) executeBlock(block *types.Block, parent *types.Block
log.Error("BlockChainCache MakeStateDB failed", "err", err)
return err
}
SenderCacher.RecoverFromBlock(types.MakeSigner(bcc.chainConfig, gov.Gte120VersionState(state)), block)
SenderCacher.RecoverFromBlock(types.MakeSigner(bcc.chainConfig, gov.Gte120VersionState(state), gov.Gte140VersionState(state)), block)
if err != nil {
return errors.New("execute block error")
}
Expand Down
12 changes: 8 additions & 4 deletions core/parallel_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
executor Executor
EIP155Signer types.Signer
PIP7Signer types.Signer
PIP11Signer types.Signer
)

type Executor struct {
Expand Down Expand Up @@ -61,8 +62,9 @@ func NewExecutor(chainConfig *params.ChainConfig, chainContext ChainContext, vmC
})
executor.chainConfig = chainConfig
executor.chainContext = chainContext
EIP155Signer = types.NewEIP155Signer(chainConfig.ChainID)
PIP7Signer = types.MakeSigner(chainConfig, true)
EIP155Signer = types.MakeSigner(chainConfig, false, false)
PIP7Signer = types.MakeSigner(chainConfig, true, false)
PIP11Signer = types.MakeSigner(chainConfig, true, true)
executor.signer = EIP155Signer
executor.vmCfg = vmCfg
executor.txpool = txpool
Expand All @@ -74,8 +76,10 @@ func GetExecutor() *Executor {
}

func (exe *Executor) MakeSigner(stateDB *state.StateDB) types.Signer {
pip7 := gov.Gte120VersionState(stateDB)
if pip7 {
gte140 := gov.Gte140VersionState(stateDB)
if gte140 {
exe.signer = PIP11Signer
} else if gov.Gte120VersionState(stateDB) {
exe.signer = PIP7Signer
} else {
exe.signer = EIP155Signer
Expand Down
4 changes: 2 additions & 2 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg

// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
msg, err := tx.AsMessage(types.MakeSigner(p.config, gov.Gte120VersionState(statedb)))
msg, err := tx.AsMessage(types.MakeSigner(p.config, gov.Gte120VersionState(statedb), gov.Gte140VersionState(statedb)))
if err != nil {
return nil, nil, 0, err
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
msg, err := tx.AsMessage(types.MakeSigner(config, gov.Gte120VersionState(statedb)))
msg, err := tx.AsMessage(types.MakeSigner(config, gov.Gte120VersionState(statedb), gov.Gte140VersionState(statedb)))
if err != nil {
return nil, err
}
Expand Down
32 changes: 20 additions & 12 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,12 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain txPoo
// Sanitize the input to ensure no vulnerable gas prices are set
config = (&config).sanitize()

pip7 := false
pip7, gte140 := false, false
if currentBlock := chain.CurrentBlock(); currentBlock != nil {
stateDB, err := chain.GetState(currentBlock.Header())
if err == nil && stateDB != nil {
pip7 = gov.Gte120VersionState(stateDB)
gte140 = gov.Gte140VersionState(stateDB)
}
}

Expand All @@ -334,7 +335,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain txPoo
config: config,
chainconfig: chainconfig,
chain: chain,
signer: types.MakeSigner(chainconfig, pip7),
signer: types.MakeSigner(chainconfig, pip7, gte140),
pending: make(map[common.Address]*txList),
queue: make(map[common.Address]*txList),
beats: make(map[common.Address]time.Time),
Expand Down Expand Up @@ -508,11 +509,7 @@ func (pool *TxPool) ForkedReset(newHeader *types.Header, rollback []*types.Block
pool.currentMaxGas = newHeader.GasLimit

// reset signer
if gov.Gte120VersionState(statedb) {
pool.signer = types.MakeSigner(pool.chainconfig, true)
pool.locals.signer = pool.signer
pool.cacheAccountNeedPromoted.signer = pool.signer
}
pool.resetSigner(statedb)

// Inject any transactions discarded due to reorgs
t := time.Now()
Expand Down Expand Up @@ -1421,17 +1418,28 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
pool.pendingNonces = newTxNoncer(statedb)
pool.currentMaxGas = newHead.GasLimit
// reset signer
if gov.Gte120VersionState(statedb) {
pool.signer = types.MakeSigner(pool.chainconfig, true)
pool.locals.signer = pool.signer
pool.cacheAccountNeedPromoted.signer = pool.signer
}
pool.resetSigner(statedb)
// Inject any transactions discarded due to reorgs
t := time.Now()
SenderCacher.recover(pool.signer, reinject)
pool.addTxsLocked(reinject, false)
log.Debug("Reinjecting stale transactions", "oldNumber", oldNumber, "oldHash", oldHash, "newNumber", newHead.Number.Uint64(), "newHash", newHead.Hash(), "count", len(reinject), "elapsed", time.Since(t))
}

func (pool *TxPool) resetSigner(statedb *state.StateDB) {
var signer types.Signer
gte140 := gov.Gte140VersionState(statedb)
if gte140 {
signer = types.MakeSigner(pool.chainconfig, true, true)
} else if gov.Gte120VersionState(statedb) {
signer = types.MakeSigner(pool.chainconfig, true, false)
} else {
signer = types.MakeSigner(pool.chainconfig, false, false)
}

pool.signer = signer
pool.locals.signer = pool.signer
pool.cacheAccountNeedPromoted.signer = pool.signer
}

// promoteExecutables moves transactions that have become processable from the
Expand Down
2 changes: 1 addition & 1 deletion core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (r Receipts) GetRlp(i int) []byte {
// DeriveFields fills the receipts with their computed fields based on consensus
// data and contextual infos like containing block and transactions.
func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error {
signer := NewPIP7Signer(config.ChainID, config.PIP7ChainID)
signer := NewPIP11Signer(config.ChainID, config.PIP7ChainID)

logIndex := uint(0)
if len(txs) != len(r) {
Expand Down
17 changes: 16 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,26 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit
}
}

// ChainId returns which chain id this transaction was signed for (if at all)
// ChainId returns the EIP155 chain ID of the transaction. The return value will always be
// non-nil. For transactions which are not replay-protected, the return value is zero.
func (tx *Transaction) ChainId() *big.Int {
return deriveChainId(tx.data.V)
}

func isProtectedV(V *big.Int) bool {
if V.BitLen() <= 8 {
v := V.Uint64()
return v != 27 && v != 28 && v != 1 && v != 0
}
// anything not 27 or 28 is considered protected
return true
}

// Protected says whether the transaction is replay-protected.
func (tx *Transaction) Protected() bool {
return tx.data.V != nil && isProtectedV(tx.data.V)
}

// EncodeRLP implements rlp.Encoder
func (tx *Transaction) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &tx.data)
Expand Down
Loading

0 comments on commit e11fe5c

Please sign in to comment.