mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
Merge pull request #211 from ngtuna/double-validation
adding double validation layer
This commit is contained in:
commit
f67c3a8c58
7 changed files with 106 additions and 24 deletions
|
|
@ -414,6 +414,17 @@ func (c *Posv) GetMasternodes(chain consensus.ChainReader, header *types.Header)
|
|||
|
||||
func (c *Posv) GetPeriod() uint64 { return c.config.Period }
|
||||
|
||||
func WhoIsCreator(snap *Snapshot, header *types.Header) (common.Address, error) {
|
||||
if header.Number.Uint64() == 0 {
|
||||
return common.Address{}, errors.New("Don't take block 0")
|
||||
}
|
||||
m, err := ecrecover(header, snap.sigcache)
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func YourTurn(masternodes []common.Address, snap *Snapshot, header *types.Header, cur common.Address) (int, int, bool, error) {
|
||||
if len(masternodes) == 0 {
|
||||
return -1, -1, true, nil
|
||||
|
|
@ -423,7 +434,7 @@ func YourTurn(masternodes []common.Address, snap *Snapshot, header *types.Header
|
|||
var err error
|
||||
preIndex := -1
|
||||
if header.Number.Uint64() != 0 {
|
||||
pre, err = ecrecover(header, snap.sigcache)
|
||||
pre, err = WhoIsCreator(snap, header)
|
||||
if err != nil {
|
||||
return 0, 0, false, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -272,6 +272,7 @@ func ExtractValidatorsFromBytes(byteValidators []byte) []int64 {
|
|||
intNumber, err := strconv.Atoi(string(trimByte))
|
||||
if err != nil {
|
||||
log.Error("Can not convert string to integer", "error", err)
|
||||
return []int64{}
|
||||
}
|
||||
validators = append(validators, int64(intNumber))
|
||||
}
|
||||
|
|
@ -568,26 +569,22 @@ func GetMasternodesFromCheckpointHeader(checkpointHeader *types.Header) []common
|
|||
}
|
||||
|
||||
// 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 {
|
||||
return nil, errors.New("This block is not checkpoint block epoc.")
|
||||
}
|
||||
|
||||
// Get singers from this block.
|
||||
m1m2 := map[common.Address]common.Address{}
|
||||
// Get signers from this block.
|
||||
masternodes := GetMasternodesFromCheckpointHeader(checkpointBlock.Header())
|
||||
validators := ExtractValidatorsFromBytes(checkpointBlock.Header().Validators)
|
||||
|
||||
var m2List []common.Address
|
||||
lenMasternodes := len(masternodes)
|
||||
var valAddr common.Address
|
||||
for validatorIndex := range validators {
|
||||
if validatorIndex < lenMasternodes {
|
||||
valAddr = masternodes[validatorIndex]
|
||||
} else {
|
||||
valAddr = masternodes[validatorIndex-lenMasternodes]
|
||||
}
|
||||
m2List = append(m2List, valAddr)
|
||||
if len(validators) < len(masternodes) {
|
||||
return nil, errors.New("len(m2) is less than len(m1)")
|
||||
}
|
||||
|
||||
return m2List, nil
|
||||
if len(masternodes) > 0 {
|
||||
for i, m1 := range masternodes {
|
||||
m1m2[m1] = masternodes[validators[i]%int64(len(masternodes))]
|
||||
}
|
||||
}
|
||||
return m1m2, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1635,16 +1635,16 @@ func (bc *BlockChain) UpdateM1() error {
|
|||
ms = append(ms, posv.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 {
|
||||
log.Info("No masternode candidates found. Keep the current masternodes set for the next epoch")
|
||||
} else {
|
||||
sort.Slice(ms, func(i, j int) bool {
|
||||
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
|
||||
log.Info("Updating new set of masternodes")
|
||||
if len(ms) > common.MaxMasternodes {
|
||||
|
|
|
|||
|
|
@ -553,6 +553,14 @@ func (pool *TxPool) local() map[common.Address]types.Transactions {
|
|||
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
|
||||
// rules and adheres to some heuristic limits of the local node (price and size).
|
||||
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/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"time"
|
||||
)
|
||||
|
||||
const NumOfMasternodes = 99
|
||||
|
|
@ -201,10 +202,56 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
return
|
||||
}
|
||||
if _, authorized := snap.Signers[eth.etherbase]; authorized {
|
||||
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
|
||||
// double validation
|
||||
m2, err := getM2(snap, eth, block)
|
||||
if err != nil {
|
||||
log.Error("Fail to validate M2 condition for imported block", "error", err)
|
||||
return
|
||||
}
|
||||
if eth.etherbase != m2 {
|
||||
// firstly, look into pending txPool
|
||||
pendingMap, err := eth.txPool.Pending()
|
||||
if err != nil {
|
||||
log.Error("Fail to get txPool pending", "err", err)
|
||||
//reset pendingMap
|
||||
pendingMap = map[common.Address]types.Transactions{}
|
||||
}
|
||||
txsSentFromM2 := pendingMap[m2]
|
||||
if len(txsSentFromM2) > 0 {
|
||||
for _, tx := range txsSentFromM2 {
|
||||
if tx.To().String() == common.BlockSigners {
|
||||
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
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
//then wait until signTx from m2 comes into txPool
|
||||
txCh := make(chan core.TxPreEvent, txChanSize)
|
||||
subEvent := 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
|
||||
}
|
||||
return
|
||||
}
|
||||
//timeout 10s
|
||||
case <-time.After(time.Duration(10) * time.Second):
|
||||
break G
|
||||
}
|
||||
subEvent.Unsubscribe()
|
||||
} 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)
|
||||
|
|
@ -295,6 +342,25 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
return eth, nil
|
||||
}
|
||||
|
||||
func getM2(snap *posv.Snapshot, eth *Ethereum, block *types.Block) (common.Address, error) {
|
||||
epoch := eth.chainConfig.Posv.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 := posv.WhoIsCreator(snap, block.Header())
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
return m[m1], nil
|
||||
}
|
||||
|
||||
func makeExtraData(extra []byte) []byte {
|
||||
if len(extra) == 0 {
|
||||
// create default extradata
|
||||
|
|
|
|||
|
|
@ -674,7 +674,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
|
|||
propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
|
||||
go f.broadcastBlock(block, false)
|
||||
|
||||
// Invoke the testing hook if needed
|
||||
// Invoke the imported hook if needed
|
||||
if f.importedHook != nil {
|
||||
f.importedHook(block)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,8 +63,8 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
|
|||
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
|
||||
},
|
||||
{
|
||||
code: StatusMsg, data: statusData{uint32(protocol), 89, td, head.Hash(), genesis.Hash()},
|
||||
wantError: errResp(ErrNetworkIdMismatch, "89 (!= 1)"),
|
||||
code: StatusMsg, data: statusData{uint32(protocol), 999, td, head.Hash(), genesis.Hash()},
|
||||
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 89)"),
|
||||
},
|
||||
{
|
||||
code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, td, head.Hash(), common.Hash{3}},
|
||||
|
|
|
|||
Loading…
Reference in a new issue