Skip to content

Commit

Permalink
Merge pull request ethereum#132 from olumuyiwadad/Fix_wrongList_AtSna…
Browse files Browse the repository at this point in the history
…pShot

Work around for the issue "return wrong list signers from snapshot"
  • Loading branch information
AnilChinchawale authored Sep 21, 2021
2 parents f9ac777 + 8daf35f commit fae50b1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
MergeSignRange = 15
RangeReturnSigner = 150
MinimunMinerBlockPerEpoch = 1
IgnoreSignerCheckBlock = uint64(27307800)
)

var TIP2019Block = big.NewInt(1)
Expand Down
7 changes: 5 additions & 2 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
// IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not.
func IsHexAddress(s string) bool {
if hasXDCPrefix(s) {
s = s[3:]
}
if hasHexPrefix(s) {
s = s[2:]
}
Expand Down Expand Up @@ -196,7 +199,7 @@ func (a Address) Hex() string {
result[i] -= 32
}
}
return "0x" + string(result)
return "xdc" + string(result)
}

// String implements the stringer interface and is used also by the logger.
Expand Down Expand Up @@ -230,7 +233,7 @@ func (a *Address) Set(other Address) {

// MarshalText returns the hex representation of a.
func (a Address) MarshalText() ([]byte, error) {
return hexutil.Bytes(a[:]).MarshalText()
return hexutil.Bytes(a[:]).MarshalXDCText()
}

// UnmarshalText parses a hash in hex syntax.
Expand Down
28 changes: 18 additions & 10 deletions consensus/XDPoS/XDPoS.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,11 @@ func (c *XDPoS) verifyCascadingFields(chain consensus.ChainReader, header *types
if err == nil {
return c.verifySeal(chain, header, parents, fullVerify)
}

// try again the progress with signers querying from smart contract
// for example the checkpoint is 886500 -> the start gap block is 886495
startGapBlockHeader := header
for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ {
startGapBlockHeader = chain.GetHeader(startGapBlockHeader.ParentHash, number-step)
}
signers, err = c.HookGetSignersFromContract(startGapBlockHeader.Hash())
signers, err = c.GetSignersFromContract(chain, header)
if err != nil {
log.Debug("Can't get signers from Smart Contract ", err)
return err
}

err = c.checkSignersOnCheckpoint(chain, header, signers)
if err == nil {
return c.verifySeal(chain, header, parents, fullVerify)
}
Expand All @@ -440,6 +431,10 @@ func (c *XDPoS) verifyCascadingFields(chain consensus.ChainReader, header *types

func (c *XDPoS) checkSignersOnCheckpoint(chain consensus.ChainReader, header *types.Header, signers []common.Address) error {
number := header.Number.Uint64()
// ignore signerCheck at checkpoint block 27307800 due to wrong snapshot at gap 27307799
if number == common.IgnoreSignerCheckBlock {
return nil
}
penPenalties := []common.Address{}
if c.HookPenalty != nil || c.HookPenaltyTIPSigning != nil {
var err error
Expand Down Expand Up @@ -1281,3 +1276,16 @@ func (c *XDPoS) CheckMNTurn(chain consensus.ChainReader, parent *types.Header, s
}
return false
}

func (c *XDPoS) GetSignersFromContract(chain consensus.ChainReader, checkpointHeader *types.Header) ([]common.Address, error) {
startGapBlockHeader := checkpointHeader
number := checkpointHeader.Number.Uint64()
for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ {
startGapBlockHeader = chain.GetHeader(startGapBlockHeader.ParentHash, number-step)
}
signers, err := c.HookGetSignersFromContract(startGapBlockHeader.Hash())
if err != nil {
return []common.Address{}, fmt.Errorf("Can't get signers from Smart Contract . Err: %v", err)
}
return signers, nil
}
17 changes: 13 additions & 4 deletions contracts/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,26 @@ type rewardLog struct {
var TxSignMu sync.RWMutex

// Send tx sign for block number to smart contract blockSigner.
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block, chainDb ethdb.Database) error {
func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, manager *accounts.Manager, block *types.Block, chainDb ethdb.Database, eb common.Address) error {
TxSignMu.Lock()
defer TxSignMu.Unlock()
if chainConfig.XDPoS != nil {
// Find active account.
account := accounts.Account{}
var wallet accounts.Wallet
etherbaseAccount := accounts.Account{
Address: eb,
URL: accounts.URL{},
}
if wallets := manager.Wallets(); len(wallets) > 0 {
wallet = wallets[0]
if accts := wallets[0].Accounts(); len(accts) > 0 {
account = accts[0]
if w, err := manager.Find(etherbaseAccount); err == nil && w != nil {
wallet = w
account = etherbaseAccount
} else {
wallet = wallets[0]
if accts := wallets[0].Accounts(); len(accts) > 0 {
account = accts[0]
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
stats.processed++
stats.usedGas += usedGas
stats.report(chain, i, bc.stateCache.TrieDB().Size())
if status == CanonStatTy && bc.chainConfig.XDPoS != nil {
if bc.chainConfig.XDPoS != nil {
// epoch block
if (chain[i].NumberU64() % bc.chainConfig.XDPoS.Epoch) == 0 {
CheckpointCh <- 1
Expand Down
8 changes: 5 additions & 3 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
return nil
}
if block.NumberU64()%common.MergeSignRange == 0 || !eth.chainConfig.IsTIP2019(block.Number()) {
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb); err != nil {
if err := contracts.CreateTransactionSign(chainConfig, eth.txPool, eth.accountManager, block, chainDb, eb); err != nil {
return fmt.Errorf("Fail to create tx sign for importing block: %v", err)
}
}
Expand Down Expand Up @@ -472,12 +472,14 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].Stake.Cmp(candidates[j].Stake) >= 0
})
candidates = candidates[:150]
if len(candidates) > 150 {
candidates = candidates[:150]
}
result := []common.Address{}
for _, candidate := range candidates {
result = append(result, candidate.Address)
}
return result[:150], nil
return result, nil
}

// Hook calculates reward for masternodes
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (self *worker) wait() {
}
// Send tx sign to smart contract blockSigners.
if block.NumberU64()%common.MergeSignRange == 0 || !self.config.IsTIP2019(block.Number()) {
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block, self.chainDb); err != nil {
if err := contracts.CreateTransactionSign(self.config, self.eth.TxPool(), self.eth.AccountManager(), block, self.chainDb, self.coinbase); err != nil {
log.Error("Fail to create tx sign for signer", "error", "err")
}
}
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
VersionMajor = 1 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release
VersionMinor = 1 // Minor version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string
)
Expand Down

0 comments on commit fae50b1

Please sign in to comment.