-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make validators to produce consecutive blocks defined by BEP-341
- Loading branch information
Showing
21 changed files
with
509 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package parlia | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
mrand "math/rand" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/ethereum/go-ethereum/core/systemcontracts" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/internal/ethapi" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
func (p *Parlia) getTurnTerm(header *types.Header) (turnTerm *big.Int, err error) { | ||
if params.UseRandTurnTerm { | ||
return p.getRandTurnTerm(header) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
method := "getTurnTerm" | ||
toAddress := common.HexToAddress(systemcontracts.ValidatorContract) | ||
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) | ||
|
||
data, err := p.validatorSetABI.Pack(method) | ||
if err != nil { | ||
log.Error("Unable to pack tx for getTurnTerm", "error", err) | ||
return nil, err | ||
} | ||
msgData := (hexutil.Bytes)(data) | ||
|
||
blockNr := rpc.BlockNumberOrHashWithHash(header.Hash(), false) | ||
result, err := p.ethAPI.Call(ctx, ethapi.TransactionArgs{ | ||
Gas: &gas, | ||
To: &toAddress, | ||
Data: &msgData, | ||
}, &blockNr, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := p.validatorSetABI.UnpackIntoInterface(&turnTerm, method, result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return turnTerm, nil | ||
} | ||
|
||
// getRandTurnTerm returns a random valid value, used to test switching turn terms | ||
func (p *Parlia) getRandTurnTerm(header *types.Header) (turnTerm *big.Int, err error) { | ||
turnTerms := [8]uint8{1, 3, 4, 5, 6, 7, 8, 9} | ||
r := mrand.New(mrand.NewSource(int64(header.Time))) | ||
termIndex := int(r.Int31n(int32(len(turnTerms)))) | ||
return big.NewInt(int64(turnTerms[termIndex])), nil | ||
} |
Oops, something went wrong.