Skip to content

Commit

Permalink
add isAuthorisedAddress function for v2 consensus (ethereum#34)
Browse files Browse the repository at this point in the history
* add isAuthorisedAddress function for v2 consensus

* update isAuthorisedAddress log messages
  • Loading branch information
wjrjerome authored Jan 3, 2022
1 parent d43940a commit ebbbf26
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion consensus/XDPoS/XDPoS.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (x *XDPoS) GetPeriod() uint64 {
func (x *XDPoS) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return true
return x.EngineV2.IsAuthorisedAddress(header, chain, address)
default: // Default "v1"
return x.EngineV1.IsAuthorisedAddress(header, chain, address)
}
Expand Down
23 changes: 23 additions & 0 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ func (x *XDPoS_v2) YourTurn(chain consensus.ChainReader, parent *types.Header, s
return len(masternodes), preIndex, curIndex, false, nil
}

func (x *XDPoS_v2) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool {
var extraField utils.ExtraFields_v2
err := utils.DecodeBytesExtraFields(header.Extra, &extraField)
if err != nil {
log.Error("[IsAuthorisedAddress] Fail to decode v2 extra data", "Hash", header.Hash(), "Extra", header.Extra, "Error", err)
return false
}
blockRound := extraField.Round

masterNodes := x.GetMasternodes(chain, header)

if len(masterNodes) == 0 {
log.Error("[IsAuthorisedAddress] Fail to find any master nodes from current block round epoch", "Hash", header.Hash(), "Round", blockRound, "Number", header.Number)
return false
}
leaderIndex := uint64(blockRound) % x.config.Epoch % uint64(len(masterNodes))
if masterNodes[leaderIndex] == address {
return true
}
log.Warn("Not authorised address", "Address", address, "MN", masterNodes, "Hash", header.Hash(), "masterNodes[leaderIndex]", masterNodes[leaderIndex], "Address", address)
return false
}

// Copy from v1
func whoIsCreator(snap *SnapshotV2, header *types.Header) (common.Address, error) {
if header.Number.Uint64() == 0 {
Expand Down
70 changes: 70 additions & 0 deletions consensus/tests/authorised_masternode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tests

import (
"math/big"
"testing"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/stretchr/testify/assert"
)

func TestIsAuthorisedMNForConsensusV1(t *testing.T) {
/*
V1 consensus engine
*/
blockchain, _, parentBlock, _ := PrepareXDCTestBlockChain(t, GAP-2, params.TestXDPoSMockChainConfig)
// Insert first Block 449
t.Logf("Inserting block with propose at 449...")
blockCoinbaseA := "0xaaa0000000000000000000000000000000000449"
tx, err := voteTX(37117, 0, acc1Addr.String())
if err != nil {
t.Fatal(err)
}

//Get from block validator error message
merkleRoot := "46234e9cd7e85a267f7f0435b15256a794a2f6d65cc98cdbd21dcd10a01d9772"
header := &types.Header{
Root: common.HexToHash(merkleRoot),
Number: big.NewInt(int64(449)),
ParentHash: parentBlock.Hash(),
Coinbase: common.HexToAddress(blockCoinbaseA),
}
block449, err := insertBlockTxs(blockchain, header, []*types.Transaction{tx})
if err != nil {
t.Fatal(err)
}
parentBlock = block449

// At block 449, we should not update signerList. we need to update it till block 450 gap block.
// Acc3 is the default account that is on the signerList

engine := blockchain.Engine().(*XDPoS.XDPoS)
isAuthorisedMN := engine.IsAuthorisedAddress(block449.Header(), blockchain, acc3Addr)
assert.True(t, isAuthorisedMN)

isAuthorisedMN = engine.IsAuthorisedAddress(block449.Header(), blockchain, acc1Addr)
assert.False(t, isAuthorisedMN)

// Now, let's mine another block to trigger the GAP block signerList update
block450CoinbaseAddress := "0xaaa0000000000000000000000000000000000450"
merkleRoot = "46234e9cd7e85a267f7f0435b15256a794a2f6d65cc98cdbd21dcd10a01d9772"
header = &types.Header{
Root: common.HexToHash(merkleRoot),
Number: big.NewInt(int64(450)),
ParentHash: parentBlock.Hash(),
Coinbase: common.HexToAddress(block450CoinbaseAddress),
}
block450, err := insertBlock(blockchain, header)
if err != nil {
t.Fatal(err)
}

isAuthorisedMN = engine.IsAuthorisedAddress(block450.Header(), blockchain, acc3Addr)
assert.False(t, isAuthorisedMN)

isAuthorisedMN = engine.IsAuthorisedAddress(block450.Header(), blockchain, acc1Addr)
assert.True(t, isAuthorisedMN)
}

0 comments on commit ebbbf26

Please sign in to comment.