mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
move unit-test to posv
This commit is contained in:
parent
8a28238008
commit
3e47d28cce
2 changed files with 61 additions and 10 deletions
|
|
@ -225,8 +225,8 @@ type Posv struct {
|
||||||
signFn clique.SignerFn // Signer function to authorize hashes with
|
signFn clique.SignerFn // Signer function to authorize hashes with
|
||||||
lock sync.RWMutex // Protects the signer fields
|
lock sync.RWMutex // Protects the signer fields
|
||||||
|
|
||||||
BlockSigners *lru.Cache
|
BlockSigners *lru.Cache
|
||||||
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
|
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
|
||||||
HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error)
|
HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error)
|
||||||
HookPenaltyTIPSigning func(chain consensus.ChainReader, header *types.Header, candidate []common.Address) ([]common.Address, error)
|
HookPenaltyTIPSigning func(chain consensus.ChainReader, header *types.Header, candidate []common.Address) ([]common.Address, error)
|
||||||
HookValidator func(header *types.Header, signers []common.Address) ([]byte, error)
|
HookValidator func(header *types.Header, signers []common.Address) ([]byte, error)
|
||||||
|
|
@ -1129,31 +1129,35 @@ func GetM1M2FromCheckpointHeader(checkpointHeader *types.Header, currentHeader *
|
||||||
if checkpointHeader.Number.Uint64()%common.EpocBlockRandomize != 0 {
|
if checkpointHeader.Number.Uint64()%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 signers from this block.
|
// Get signers from this block.
|
||||||
masternodes := GetMasternodesFromCheckpointHeader(checkpointHeader)
|
masternodes := GetMasternodesFromCheckpointHeader(checkpointHeader)
|
||||||
validators := ExtractValidatorsFromBytes(checkpointHeader.Validators)
|
validators := ExtractValidatorsFromBytes(checkpointHeader.Validators)
|
||||||
|
m1m2, _, err := getM1M2(masternodes, validators, currentHeader, config)
|
||||||
|
if err != nil {
|
||||||
|
return map[common.Address]common.Address{}, err
|
||||||
|
}
|
||||||
|
return m1m2, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getM1M2(masternodes []common.Address, validators []int64, currentHeader *types.Header, config *params.ChainConfig) (map[common.Address]common.Address, uint64, error) {
|
||||||
|
m1m2 := map[common.Address]common.Address{}
|
||||||
maxMNs := len(masternodes)
|
maxMNs := len(masternodes)
|
||||||
|
moveM2 := uint64(0)
|
||||||
if len(validators) < maxMNs {
|
if len(validators) < maxMNs {
|
||||||
return nil, errors.New("len(m2) is less than len(m1)")
|
return nil, moveM2, errors.New("len(m2) is less than len(m1)")
|
||||||
}
|
}
|
||||||
if maxMNs > 0 {
|
if maxMNs > 0 {
|
||||||
isForked := config.IsTIPRandomize(currentHeader.Number)
|
isForked := config.IsTIPRandomize(currentHeader.Number)
|
||||||
moveM2 := uint64(0)
|
|
||||||
if isForked {
|
if isForked {
|
||||||
moveM2 = (currentHeader.Number.Uint64() % config.Posv.Epoch) / uint64(maxMNs)
|
moveM2 = (currentHeader.Number.Uint64() % config.Posv.Epoch) / uint64(maxMNs)
|
||||||
}
|
}
|
||||||
for i, m1 := range masternodes {
|
for i, m1 := range masternodes {
|
||||||
m2Index := uint64(validators[i] % int64(maxMNs))
|
m2Index := uint64(validators[i] % int64(maxMNs))
|
||||||
m2Index = m2Index + moveM2
|
m2Index = (m2Index + moveM2) % uint64(maxMNs)
|
||||||
if m2Index >= common.MaxMasternodes {
|
|
||||||
m2Index = m2Index - common.MaxMasternodes
|
|
||||||
}
|
|
||||||
m1m2[m1] = masternodes[m2Index]
|
m1m2[m1] = masternodes[m2Index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m1m2, nil
|
return m1m2, moveM2, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract validators from byte array.
|
// Extract validators from byte array.
|
||||||
|
|
|
||||||
47
consensus/posv/posv_test.go
Normal file
47
consensus/posv/posv_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package posv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"math/big"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetM1M2FromCheckpointHeader(t *testing.T) {
|
||||||
|
masternodes := []common.Address{
|
||||||
|
common.StringToAddress("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
|
||||||
|
common.StringToAddress("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
|
||||||
|
common.StringToAddress("cccccccccccccccccccccccccccccccccccccccc"),
|
||||||
|
}
|
||||||
|
validators := []int64{
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
epoch := int64(900)
|
||||||
|
config := ¶ms.ChainConfig{
|
||||||
|
Posv: ¶ms.PosvConfig{
|
||||||
|
Epoch: uint64(epoch),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
//try from block 900 to 909
|
||||||
|
for i:=int64(0); i<9; i++ {
|
||||||
|
currentHeader := &types.Header{
|
||||||
|
Number: big.NewInt(epoch+i),
|
||||||
|
}
|
||||||
|
m1m2, moveM2, err := getM1M2(masternodes, validators, currentHeader, config)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("can't get m1m2", "err", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("block: %v, moveM2: %v\n", currentHeader.Number.Int64(), moveM2)
|
||||||
|
for _,k := range masternodes {
|
||||||
|
fmt.Printf("m1: %v - m2: %v\n", k.Str(), m1m2[k].Str())
|
||||||
|
}
|
||||||
|
if moveM2 != uint64(i/3) { //3 = len(masternodes)
|
||||||
|
t.Error("wrong moveM2", "want", uint64(i/3), "have", moveM2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue