diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index a0323a96f9..e6d39fd7a3 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -577,7 +577,7 @@ func (c *Bor) verifySeal(chain consensus.ChainReader, header *types.Header, pare } proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer) signerIndex, _ := snap.ValidatorSet.GetByAddress(signer) - limit := len(validators) - (len(validators)/2 + 1) + limit := len(validators)/2 + 1 // temp index tempIndex := signerIndex @@ -761,7 +761,7 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer) signerIndex, _ := snap.ValidatorSet.GetByAddress(signer) - limit := len(validators) - (len(validators)/2 + 1) + limit := len(validators)/2 + 1 // temp index tempIndex := signerIndex @@ -1038,7 +1038,7 @@ func (c *Bor) commitSpan( header *types.Header, chain core.ChainContext, ) error { - response, err := FetchFromHeimdall(c.httpClient, c.chainConfig.Bor.Heimdall, "bor", "span", strconv.FormatUint(span.ID+1, 10)) + response, err := FetchFromHeimdallWithRetry(c.httpClient, c.chainConfig.Bor.Heimdall, "bor", "span", strconv.FormatUint(span.ID+1, 10)) if err != nil { return err } @@ -1157,7 +1157,7 @@ func (c *Bor) CommitStates( // itereate through state ids for _, stateID := range stateIds { // fetch from heimdall - response, err := FetchFromHeimdall(c.httpClient, c.chainConfig.Bor.Heimdall, "clerk", "event-record", strconv.FormatUint(stateID.Uint64(), 10)) + response, err := FetchFromHeimdallWithRetry(c.httpClient, c.chainConfig.Bor.Heimdall, "clerk", "event-record", strconv.FormatUint(stateID.Uint64(), 10)) if err != nil { return err } diff --git a/consensus/bor/rest.go b/consensus/bor/rest.go index f855adc992..c46eb2dec5 100644 --- a/consensus/bor/rest.go +++ b/consensus/bor/rest.go @@ -7,6 +7,9 @@ import ( "net/http" "net/url" "path" + "time" + + "github.com/maticnetwork/bor/log" ) // ResponseWithHeight defines a response object type that wraps an original @@ -16,19 +19,8 @@ type ResponseWithHeight struct { Result json.RawMessage `json:"result"` } -// FetchFromHeimdall returns data from heimdall -func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { - u, err := url.Parse(urlString) - if err != nil { - return nil, err - } - - for _, e := range paths { - if e != "" { - u.Path = path.Join(u.Path, e) - } - } - +// internal fetch method +func internalFetch(client http.Client, u *url.URL) (*ResponseWithHeight, error) { res, err := client.Get(u.String()) if err != nil { return nil, err @@ -54,3 +46,42 @@ func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (* return &response, nil } + +// FetchFromHeimdallWithRetry returns data from heimdall with retry +func FetchFromHeimdallWithRetry(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { + u, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + for _, e := range paths { + if e != "" { + u.Path = path.Join(u.Path, e) + } + } + + for { + res, err := internalFetch(client, u) + if err == nil && res != nil { + return res, nil + } + log.Info("Retrying again in 5 seconds", u.String()) + time.Sleep(5 * time.Second) + } +} + +// FetchFromHeimdall returns data from heimdall +func FetchFromHeimdall(client http.Client, urlString string, paths ...string) (*ResponseWithHeight, error) { + u, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + for _, e := range paths { + if e != "" { + u.Path = path.Join(u.Path, e) + } + } + + return internalFetch(client, u) +} diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 74ef08358f..7928f0baea 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -150,6 +150,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { return nil, err } +<<<<<<< HEAD // change validator set and change proposer if number > 0 && (number+1)%s.config.Sprint == 0 { validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal] @@ -164,6 +165,8 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { log.Info("Current validator set", "number", snap.Number, "validatorSet", snap.ValidatorSet) } +======= +>>>>>>> origin/mat-494 // check if signer is in validator set if !snap.ValidatorSet.HasAddress(signer.Bytes()) { return nil, errUnauthorizedSigner @@ -178,7 +181,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { proposer := snap.ValidatorSet.GetProposer().Address proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer) signerIndex, _ := snap.ValidatorSet.GetByAddress(signer) - limit := len(validators) - (len(validators)/2 + 1) + limit := len(validators)/2 + 1 // temp index tempIndex := signerIndex @@ -188,12 +191,27 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { } if tempIndex-proposerIndex > limit { + fmt.Println("Invalid signer: error while applying headers", "proposerIndex", validators[proposerIndex].Address.Hex(), "signerIndex", validators[signerIndex].Address.Hex()) return nil, errRecentlySigned } } // add recents snap.Recents[number] = signer + + // change validator set and change proposer + if number > 0 && (number+1)%s.config.Sprint == 0 { + validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal] + + // get validators from headers and use that for new validator set + newVals, _ := ParseValidators(validatorBytes) + v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals) + v.IncrementProposerPriority(1) + snap.ValidatorSet = v + + // log new validator set + fmt.Println("New changed validator set", "number", snap.Number, "validatorSet", snap.ValidatorSet, "currentSigner", signer.Hex()) + } } snap.Number += uint64(len(headers)) snap.Hash = headers[len(headers)-1].Hash()