mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
init double validation layer
This commit is contained in:
parent
fa02ac70cb
commit
b955291388
5 changed files with 73 additions and 22 deletions
|
|
@ -257,6 +257,7 @@ func ExtractValidatorsFromBytes(byteValidators []byte) []int64 {
|
||||||
intNumber, err := strconv.Atoi(string(trimByte))
|
intNumber, err := strconv.Atoi(string(trimByte))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Can not convert string to integer", "error", err)
|
log.Error("Can not convert string to integer", "error", err)
|
||||||
|
return []int64{}
|
||||||
}
|
}
|
||||||
validators = append(validators, int64(intNumber))
|
validators = append(validators, int64(intNumber))
|
||||||
}
|
}
|
||||||
|
|
@ -553,26 +554,20 @@ func GetMasternodesFromCheckpointHeader(checkpointHeader *types.Header) []common
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get m2 list from checkpoint block.
|
// Get m2 list from checkpoint block.
|
||||||
func GetM2FromCheckpointBlock(checkpointBlock types.Block) ([]common.Address, error) {
|
func GetM1M2FromCheckpointBlock(checkpointBlock *types.Block) (map[common.Address]common.Address, error) {
|
||||||
if checkpointBlock.Number().Int64()%common.EpocBlockRandomize != 0 {
|
if checkpointBlock.Number().Int64()%common.EpocBlockRandomize != 0 {
|
||||||
return nil, errors.New("This block is not checkpoint block epoc.")
|
return nil, errors.New("This block is not checkpoint block epoc.")
|
||||||
}
|
}
|
||||||
|
m1m2 := map[common.Address]common.Address{}
|
||||||
// Get singers from this block.
|
// Get signers from this block.
|
||||||
masternodes := GetMasternodesFromCheckpointHeader(checkpointBlock.Header())
|
masternodes := GetMasternodesFromCheckpointHeader(checkpointBlock.Header())
|
||||||
validators := ExtractValidatorsFromBytes(checkpointBlock.Header().Validators)
|
validators := ExtractValidatorsFromBytes(checkpointBlock.Header().Validators)
|
||||||
|
|
||||||
var m2List []common.Address
|
if len(validators) < len(masternodes) {
|
||||||
lenMasternodes := len(masternodes)
|
return nil, errors.New("Len(m2) is less than len(m1)")
|
||||||
var valAddr common.Address
|
|
||||||
for validatorIndex := range validators {
|
|
||||||
if validatorIndex < lenMasternodes {
|
|
||||||
valAddr = masternodes[validatorIndex]
|
|
||||||
} else {
|
|
||||||
valAddr = masternodes[validatorIndex-lenMasternodes]
|
|
||||||
}
|
|
||||||
m2List = append(m2List, valAddr)
|
|
||||||
}
|
}
|
||||||
|
for i, m1 := range masternodes {
|
||||||
return m2List, nil
|
m1m2[m1] = masternodes[validators[i]]
|
||||||
|
}
|
||||||
|
return m1m2, nil
|
||||||
}
|
}
|
||||||
|
|
@ -1633,16 +1633,17 @@ func (bc *BlockChain) UpdateM1() error {
|
||||||
ms = append(ms, XDPoS.Masternode{Address: candidate, Stake: v.Uint64()})
|
ms = append(ms, XDPoS.Masternode{Address: candidate, Stake: v.Uint64()})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Info("Ordered list of masternode candidates")
|
|
||||||
for _, m := range ms {
|
|
||||||
log.Info("", "address", m.Address.String(), "stake", m.Stake)
|
|
||||||
}
|
|
||||||
if len(ms) == 0 {
|
if len(ms) == 0 {
|
||||||
log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch")
|
log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch")
|
||||||
} else {
|
} else {
|
||||||
sort.Slice(ms, func(i, j int) bool {
|
sort.Slice(ms, func(i, j int) bool {
|
||||||
return ms[i].Stake >= ms[j].Stake
|
return ms[i].Stake >= ms[j].Stake
|
||||||
})
|
})
|
||||||
|
log.Info("Ordered list of masternode candidates")
|
||||||
|
for _, m := range ms {
|
||||||
|
log.Info("", "address", m.Address.String(), "stake", m.Stake)
|
||||||
|
}
|
||||||
// update masternodes
|
// update masternodes
|
||||||
log.Info("Updating new set of masternodes")
|
log.Info("Updating new set of masternodes")
|
||||||
if len(ms) > common.MaxMasternodes {
|
if len(ms) > common.MaxMasternodes {
|
||||||
|
|
|
||||||
|
|
@ -553,6 +553,14 @@ func (pool *TxPool) local() map[common.Address]types.Transactions {
|
||||||
return txs
|
return txs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pool *TxPool) GetSender(tx *types.Transaction) (common.Address, error) {
|
||||||
|
from, err := types.Sender(pool.signer, tx)
|
||||||
|
if err != nil {
|
||||||
|
return common.Address{}, ErrInvalidSender
|
||||||
|
}
|
||||||
|
return from, nil
|
||||||
|
}
|
||||||
|
|
||||||
// validateTx checks whether a transaction is valid according to the consensus
|
// validateTx checks whether a transaction is valid according to the consensus
|
||||||
// rules and adheres to some heuristic limits of the local node (price and size).
|
// rules and adheres to some heuristic limits of the local node (price and size).
|
||||||
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const NumOfMasternodes = 99
|
const NumOfMasternodes = 99
|
||||||
|
|
@ -201,10 +202,37 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
||||||
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
|
// double validation
|
||||||
log.Error("Fail to create tx sign for imported block", "error", err)
|
m2, err := getM2(snap, eth, block)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to validate M2 condition for imported block", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if eth.etherbase != m2 {
|
||||||
|
//wait until signTx from m2 comes into txPool
|
||||||
|
txCh := make(chan core.TxPreEvent, txChanSize)
|
||||||
|
eth.txPool.SubscribeTxPreEvent(txCh)
|
||||||
|
G:
|
||||||
|
select {
|
||||||
|
case event := <-txCh:
|
||||||
|
from, err := eth.txPool.GetSender(event.Tx)
|
||||||
|
if (err == nil) && (event.Tx.To().String() == common.BlockSigners) && (from == m2) {
|
||||||
|
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
|
||||||
|
log.Error("Fail to create tx sign for imported block", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//timeout 10s
|
||||||
|
case <-time.After(time.Duration(10) * time.Second):
|
||||||
|
break G
|
||||||
|
}
|
||||||
|
close(txCh)
|
||||||
|
} else if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
|
||||||
|
log.Error("Fail to create tx sign for imported block", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// end of double validation
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eth.protocolManager.fetcher.SetImportedHook(importedHook)
|
eth.protocolManager.fetcher.SetImportedHook(importedHook)
|
||||||
|
|
@ -292,6 +320,25 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
return eth, nil
|
return eth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getM2(snap *XDPoS.Snapshot, eth *Ethereum, block *types.Block) (common.Address, error) {
|
||||||
|
epoch := eth.chainConfig.XDPoS.Epoch
|
||||||
|
no := block.NumberU64()
|
||||||
|
cpNo := no
|
||||||
|
if no%epoch != 0 {
|
||||||
|
cpNo = no - (no % epoch)
|
||||||
|
}
|
||||||
|
cpBlk := eth.blockchain.GetBlockByNumber(cpNo)
|
||||||
|
m, err := contracts.GetM1M2FromCheckpointBlock(cpBlk)
|
||||||
|
if err != nil {
|
||||||
|
return common.Address{}, err
|
||||||
|
}
|
||||||
|
m1, err := XDPoS.WhoIsCreator(snap, block.Header())
|
||||||
|
if err != nil {
|
||||||
|
return common.Address{}, err
|
||||||
|
}
|
||||||
|
return m[m1], nil
|
||||||
|
}
|
||||||
|
|
||||||
func makeExtraData(extra []byte) []byte {
|
func makeExtraData(extra []byte) []byte {
|
||||||
if len(extra) == 0 {
|
if len(extra) == 0 {
|
||||||
// create default extradata
|
// create default extradata
|
||||||
|
|
|
||||||
|
|
@ -674,7 +674,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
|
||||||
propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
|
propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
|
||||||
go f.broadcastBlock(block, false)
|
go f.broadcastBlock(block, false)
|
||||||
|
|
||||||
// Invoke the testing hook if needed
|
// Invoke the imported hook if needed
|
||||||
if f.importedHook != nil {
|
if f.importedHook != nil {
|
||||||
f.importedHook(block)
|
f.importedHook(block)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue