mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-14 16:03:45 +00:00
span sync done right
This commit is contained in:
parent
e55e19da72
commit
a4d92f8e30
3 changed files with 60 additions and 38 deletions
|
|
@ -3,6 +3,7 @@ package bor
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -247,7 +248,6 @@ type Bor struct {
|
||||||
|
|
||||||
ethAPI *ethapi.PublicBlockChainAPI
|
ethAPI *ethapi.PublicBlockChainAPI
|
||||||
validatorSetABI abi.ABI
|
validatorSetABI abi.ABI
|
||||||
span *Span
|
|
||||||
httpClient http.Client
|
httpClient http.Client
|
||||||
|
|
||||||
// The fields below are for testing only
|
// The fields below are for testing only
|
||||||
|
|
@ -279,7 +279,6 @@ func New(
|
||||||
ethAPI: ethAPI,
|
ethAPI: ethAPI,
|
||||||
recents: recents,
|
recents: recents,
|
||||||
signatures: signatures,
|
signatures: signatures,
|
||||||
span: nil,
|
|
||||||
validatorSetABI: vABI,
|
validatorSetABI: vABI,
|
||||||
httpClient: http.Client{
|
httpClient: http.Client{
|
||||||
Timeout: time.Duration(5 * time.Second),
|
Timeout: time.Duration(5 * time.Second),
|
||||||
|
|
@ -662,12 +661,13 @@ func (c *Bor) Prepare(chain consensus.ChainReader, header *types.Header) error {
|
||||||
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
|
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
|
||||||
// rewards given.
|
// rewards given.
|
||||||
func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
|
func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
|
||||||
// // commit span
|
// commit span
|
||||||
// err := c.checkAndCommitSpan(state, header, chainContext{Chain: chain, Bor: c})
|
if header.Number.Uint64()%c.config.Sprint == 0 {
|
||||||
// if err != nil {
|
if err := c.checkAndCommitSpan(state, header, chainContext{Chain: chain, Bor: c}); err != nil {
|
||||||
// fmt.Println("Error while committing span", err)
|
fmt.Println("Error while committing span", err)
|
||||||
// // return nil, err
|
// return nil, err
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// No block rewards in PoA, so the state remains as is and uncles are dropped
|
// No block rewards in PoA, so the state remains as is and uncles are dropped
|
||||||
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
|
||||||
|
|
@ -678,10 +678,12 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
|
||||||
// nor block rewards given, and returns the final block.
|
// nor block rewards given, and returns the final block.
|
||||||
func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
|
func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
|
||||||
// commit span
|
// commit span
|
||||||
err := c.checkAndCommitSpan(state, header, chainContext{Chain: chain, Bor: c})
|
if header.Number.Uint64()%c.config.Sprint == 0 {
|
||||||
if err != nil {
|
err := c.checkAndCommitSpan(state, header, chainContext{Chain: chain, Bor: c})
|
||||||
fmt.Println("Error while committing span", err)
|
if err != nil {
|
||||||
// return nil, err
|
fmt.Println("Error while committing span", err)
|
||||||
|
// return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No block rewards in PoA, so the state remains as is and uncles are dropped
|
// No block rewards in PoA, so the state remains as is and uncles are dropped
|
||||||
|
|
@ -967,8 +969,7 @@ func (c *Bor) checkAndCommitSpan(
|
||||||
chain core.ChainContext,
|
chain core.ChainContext,
|
||||||
) error {
|
) error {
|
||||||
var pending bool = false
|
var pending bool = false
|
||||||
var span *Span = c.span
|
var span *Span = nil
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -976,28 +977,45 @@ func (c *Bor) checkAndCommitSpan(
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// fetch if span is nil
|
wg.Add(1)
|
||||||
if span == nil {
|
go func() {
|
||||||
wg.Add(1)
|
span, _ = c.GetCurrentSpan(header.Number.Uint64() - 1)
|
||||||
go func() {
|
wg.Done()
|
||||||
fmt.Println("Fetching current span")
|
}()
|
||||||
span, _ = c.GetCurrentSpan(header.Number.Uint64() - 1)
|
|
||||||
c.span = span // store in cache
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
fmt.Println("Span", span.ID, span.StartBlock, span.EndBlock, "number", header.Number, "needToCommitSpan", c.needToCommitSpan(span, header))
|
||||||
|
|
||||||
// commit span if there is new span pending or span is ending or end block is not set
|
// commit span if there is new span pending or span is ending or end block is not set
|
||||||
if pending || (span != nil && (span.EndBlock == 0 || span.EndBlock == header.Number.Uint64())) {
|
if pending || c.needToCommitSpan(span, header) {
|
||||||
err := c.commitSpan(span, state, header, chain)
|
err := c.commitSpan(span, state, header, chain)
|
||||||
c.span = nil // reset cache
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Bor) needToCommitSpan(span *Span, header *types.Header) bool {
|
||||||
|
// if span is nil
|
||||||
|
if span == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// check span is not set initially
|
||||||
|
if span.EndBlock == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// if current block is first block of last sprint in current span
|
||||||
|
h := header.Number.Uint64()
|
||||||
|
if span.EndBlock > c.config.Sprint && span.EndBlock-c.config.Sprint+1 == h {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Bor) commitSpan(
|
func (c *Bor) commitSpan(
|
||||||
span *Span,
|
span *Span,
|
||||||
state *state.StateDB,
|
state *state.StateDB,
|
||||||
|
|
@ -1019,7 +1037,7 @@ func (c *Bor) commitSpan(
|
||||||
for _, val := range heimdallSpan.ValidatorSet.Validators {
|
for _, val := range heimdallSpan.ValidatorSet.Validators {
|
||||||
validators = append(validators, val.MinimalVal())
|
validators = append(validators, val.MinimalVal())
|
||||||
}
|
}
|
||||||
validatorsBytes, err := rlp.EncodeToBytes(validators)
|
validatorBytes, err := rlp.EncodeToBytes(validators)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -1027,9 +1045,9 @@ func (c *Bor) commitSpan(
|
||||||
// get producers bytes
|
// get producers bytes
|
||||||
var producers []MinimalVal
|
var producers []MinimalVal
|
||||||
for _, val := range heimdallSpan.SelectedProducers {
|
for _, val := range heimdallSpan.SelectedProducers {
|
||||||
producers = append(validators, val.MinimalVal())
|
producers = append(producers, val.MinimalVal())
|
||||||
}
|
}
|
||||||
producersBytes, err := rlp.EncodeToBytes(producers)
|
producerBytes, err := rlp.EncodeToBytes(producers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -1037,13 +1055,21 @@ func (c *Bor) commitSpan(
|
||||||
// method
|
// method
|
||||||
method := "commitSpan"
|
method := "commitSpan"
|
||||||
|
|
||||||
|
fmt.Println(
|
||||||
|
"id", heimdallSpan.ID,
|
||||||
|
"startBlock", heimdallSpan.StartBlock,
|
||||||
|
"endBlock", heimdallSpan.EndBlock,
|
||||||
|
"validatorBytes", hex.EncodeToString(validatorBytes),
|
||||||
|
"producerBytes", hex.EncodeToString(producerBytes),
|
||||||
|
)
|
||||||
|
|
||||||
// get packed data
|
// get packed data
|
||||||
data, err := c.validatorSetABI.Pack(method,
|
data, err := c.validatorSetABI.Pack(method,
|
||||||
heimdallSpan.ID,
|
big.NewInt(0).SetUint64(heimdallSpan.ID),
|
||||||
heimdallSpan.StartBlock,
|
big.NewInt(0).SetUint64(heimdallSpan.StartBlock),
|
||||||
heimdallSpan.EndBlock,
|
big.NewInt(0).SetUint64(heimdallSpan.EndBlock),
|
||||||
validatorsBytes,
|
validatorBytes,
|
||||||
producersBytes,
|
producerBytes,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Unable to pack tx for commitSpan", "error", err)
|
fmt.Println("Unable to pack tx for commitSpan", "error", err)
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,9 @@ func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (*
|
||||||
|
|
||||||
// unmarshall data from buffer
|
// unmarshall data from buffer
|
||||||
var response ResponseWithHeight
|
var response ResponseWithHeight
|
||||||
fmt.Println("body", string(body))
|
|
||||||
if err := json.Unmarshal(body, &response); err != nil {
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("response", response.Result)
|
|
||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -194,8 +194,6 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(" validator set change ", number)
|
|
||||||
|
|
||||||
// change validator set and change proposer
|
// change validator set and change proposer
|
||||||
if number > 0 && (number+1)%s.config.Sprint == 0 {
|
if number > 0 && (number+1)%s.config.Sprint == 0 {
|
||||||
validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal]
|
validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue