mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-23 15:14:32 +00:00
verify penalty info in header check point
This commit is contained in:
parent
b41dcb301f
commit
eb9742ae27
5 changed files with 58 additions and 8 deletions
|
|
@ -11,4 +11,5 @@ const (
|
|||
EpocBlockOpening = 850
|
||||
EpocBlockRandomize = 900
|
||||
MaxMasternodes = 150
|
||||
LimitPenaltyEpoch = 4
|
||||
)
|
||||
|
|
@ -243,3 +243,38 @@ func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
|
|||
func (a UnprefixedAddress) MarshalText() ([]byte, error) {
|
||||
return []byte(hex.EncodeToString(a[:])), nil
|
||||
}
|
||||
|
||||
// Extract validators from byte array.
|
||||
func RemoveItemFromArray(array []Address, items []Address) []Address {
|
||||
if items == nil || len(items) == 0 {
|
||||
return array
|
||||
}
|
||||
for i, value := range array {
|
||||
for _, item := range items {
|
||||
if value == item {
|
||||
array = append(array[:i], array[i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
// Extract validators from byte array.
|
||||
func ExtractAddressToBytes(penalties []Address) []byte {
|
||||
data := []byte{}
|
||||
for _, signer := range penalties {
|
||||
data = append(data, signer[:]...)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func ExtractAddressFromBytes(bytePenalties []byte) []Address {
|
||||
if bytePenalties != nil && len(bytePenalties) < AddressLength {
|
||||
return []Address{}
|
||||
}
|
||||
penalties := make([]Address, len(bytePenalties)/AddressLength)
|
||||
for i := 0; i < len(penalties); i++ {
|
||||
copy(penalties[i][:], bytePenalties[i*AddressLength:])
|
||||
}
|
||||
return penalties
|
||||
}
|
||||
|
|
@ -149,3 +149,12 @@ func BenchmarkAddressHex(b *testing.B) {
|
|||
testAddr.Hex()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveItemInArray(t *testing.T) {
|
||||
array := []Address{HexToAddress("0x0000000"), HexToAddress("0x0000001"), HexToAddress("0x0000002")}
|
||||
remove := []Address{HexToAddress("0x0000000"), HexToAddress("0x0000004"), HexToAddress("0x0000003")}
|
||||
array = RemoveItemFromArray(array, remove)
|
||||
if len(array) != 2 {
|
||||
t.Error("fail remove item from array addres ")
|
||||
}
|
||||
}
|
||||
|
|
@ -238,6 +238,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
}
|
||||
if len(m2) > 0 {
|
||||
header.Validators = contracts.BuildValidatorFromM2(m2)
|
||||
log.Debug("New set Validators", "m2", m2, "number", header.Number.Uint64())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -247,10 +248,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
c.HookPenalty = func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error) {
|
||||
client, err := eth.blockchain.GetClient()
|
||||
if err != nil {
|
||||
log.Error("Fail to connect IPC client for blockSigner", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
prevEpoc := blockNumberEpoc - chain.Config().XDPoS.Epoch
|
||||
if prevEpoc > 0 {
|
||||
if prevEpoc >= 0 {
|
||||
prevHeader := chain.GetHeaderByNumber(prevEpoc)
|
||||
penSigners := c.GetMasternodes(chain, prevHeader)
|
||||
if len(penSigners) > 0 {
|
||||
|
|
@ -259,7 +260,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
for i := prevEpoc; i <= blockNumberEpoc; i++ {
|
||||
blockHeader := chain.GetHeaderByNumber(i)
|
||||
if len(penSigners) > 0 {
|
||||
signedMasternodes, _ := contracts.GetSignersFromContract(blockSignerAddr, client, blockHeader.Hash())
|
||||
signedMasternodes, err := contracts.GetSignersFromContract(blockSignerAddr, client, blockHeader.Hash())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(signedMasternodes) > 0 {
|
||||
// Check signer signed?
|
||||
for _, signed := range signedMasternodes {
|
||||
|
|
@ -271,8 +275,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return penSigners, nil
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
|
|||
},
|
||||
{
|
||||
code: StatusMsg, data: statusData{uint32(protocol), 999, td, head.Hash(), genesis.Hash()},
|
||||
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 1)"),
|
||||
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 89)"),
|
||||
},
|
||||
{
|
||||
code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, td, head.Hash(), common.Hash{3}},
|
||||
|
|
@ -85,7 +85,7 @@ func testStatusMsgErrors(t *testing.T, protocol int) {
|
|||
} else if err.Error() != test.wantError.Error() {
|
||||
t.Errorf("test %d: wrong error: got %q, want %q", i, err, test.wantError)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Errorf("protocol did not shut down within 2 seconds")
|
||||
}
|
||||
p.close()
|
||||
|
|
@ -220,4 +220,4 @@ func TestGetBlockHeadersDataEncodeDecode(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue