From 4b452d8a3ade0a65994a0020b22d11899a009503 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Tue, 3 Dec 2019 16:47:28 +0530 Subject: [PATCH 1/3] retry span and states in bor --- consensus/bor/bor.go | 6 ++--- consensus/bor/rest.go | 56 +++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index dd7cee90ea..a622f0d7d0 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -17,7 +17,7 @@ import ( "sync" "time" - "github.com/maticnetwork/bor" + ethereum "github.com/maticnetwork/bor" "github.com/maticnetwork/bor/accounts" "github.com/maticnetwork/bor/accounts/abi" "github.com/maticnetwork/bor/common" @@ -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 } @@ -1158,7 +1158,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..d68c70304e 100644 --- a/consensus/bor/rest.go +++ b/consensus/bor/rest.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "path" + "time" ) // ResponseWithHeight defines a response object type that wraps an original @@ -16,19 +17,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 +44,43 @@ 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 + } + fmt.Println("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) +} From 29e8a42df6a298d6740415ed71ab3268f3e3f053 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Tue, 3 Dec 2019 18:50:30 +0530 Subject: [PATCH 2/3] verify before validator set change at sprint end --- consensus/bor/rest.go | 1 - consensus/bor/snapshot.go | 31 ++++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/consensus/bor/rest.go b/consensus/bor/rest.go index d68c70304e..6e234c18ac 100644 --- a/consensus/bor/rest.go +++ b/consensus/bor/rest.go @@ -65,7 +65,6 @@ func FetchFromHeimdallWithRetry(client http.Client, urlString string, paths ...s } fmt.Println("Retrying again in 5 seconds", u.String()) time.Sleep(5 * time.Second) - } } diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 09f08af5ad..9d41d7f3fb 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -21,12 +21,12 @@ import ( "encoding/json" "fmt" + lru "github.com/hashicorp/golang-lru" "github.com/maticnetwork/bor/common" "github.com/maticnetwork/bor/core/types" "github.com/maticnetwork/bor/ethdb" "github.com/maticnetwork/bor/internal/ethapi" "github.com/maticnetwork/bor/params" - lru "github.com/hashicorp/golang-lru" ) // Snapshot is the state of the authorization voting at a given point in time. @@ -150,20 +150,6 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { return nil, err } - // 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("Current validator set", "number", snap.Number, "validatorSet", snap.ValidatorSet) - } - // check if signer is in validator set if !snap.ValidatorSet.HasAddress(signer.Bytes()) { return nil, errUnauthorizedSigner @@ -188,12 +174,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() From 2f0b5ce1434ba6028170b03ca6f4bd6114b94301 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Tue, 3 Dec 2019 22:55:30 +0530 Subject: [PATCH 3/3] increase backup limit --- consensus/bor/bor.go | 4 ++-- consensus/bor/snapshot.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index a622f0d7d0..8b9cfc7fb7 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 diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 9d41d7f3fb..dc78a4ca1e 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -164,7 +164,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