Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: implement milestones #961

Merged
merged 186 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
186 commits
Select commit Hold shift + click to select a range
ce0cd1f
Milestone Implementation
VAIBHAVJINDAL3012 Aug 5, 2022
66e685d
Merge branch 'develop' into vaibhav/POS-609
VAIBHAVJINDAL3012 Aug 8, 2022
9281c04
Merge branch 'POS-347' into reciept-e2e-test
VAIBHAVJINDAL3012 Aug 8, 2022
04d6b0f
Changes for testing, will be removed after testing
VAIBHAVJINDAL3012 Aug 9, 2022
a38ba6d
Debugged the error
VAIBHAVJINDAL3012 Aug 9, 2022
0e582ef
Just for testing purpose
VAIBHAVJINDAL3012 Aug 9, 2022
1fbccf3
merge develop, refactor
manav2401 Aug 22, 2022
59a75ee
refactor debug api methods, rename whitelist -> checkpoint
manav2401 Aug 23, 2022
a6085cb
remove first iteration based vars
manav2401 Aug 23, 2022
d77f6be
fix linters
manav2401 Aug 23, 2022
9894d28
Rewind Changes
VAIBHAVJINDAL3012 Aug 29, 2022
4e73ca1
Merge branch 'vaibhav/POS-609' of https://github.com/maticnetwork/bor…
VAIBHAVJINDAL3012 Aug 29, 2022
58823b1
Error changes
VAIBHAVJINDAL3012 Sep 5, 2022
7e02d62
RewindBack function in bor_checkpoint_verifier
VAIBHAVJINDAL3012 Sep 15, 2022
ac89fb3
Testcases
VAIBHAVJINDAL3012 Sep 19, 2022
4fc8921
Added the fetch test for milestone and checkpoint
VAIBHAVJINDAL3012 Sep 19, 2022
58784e9
Merge branch 'develop' of https://github.com/maticnetwork/bor into va…
VAIBHAVJINDAL3012 Sep 19, 2022
f620f55
Debugged the lint changes
VAIBHAVJINDAL3012 Sep 19, 2022
f791529
Debugged the lint changes
VAIBHAVJINDAL3012 Sep 19, 2022
19ad678
Debugged the lint changes
VAIBHAVJINDAL3012 Sep 19, 2022
c4b26fb
Debugged the lint changes
VAIBHAVJINDAL3012 Sep 20, 2022
75f37a2
Improved the error in Miner test file
VAIBHAVJINDAL3012 Sep 20, 2022
3f67083
Improved the error of pointing to the wrong function
VAIBHAVJINDAL3012 Sep 20, 2022
03f4d4a
Locking the sprint after the vote has been made on it.
VAIBHAVJINDAL3012 Sep 29, 2022
a808649
Adding more logs for testing
VAIBHAVJINDAL3012 Sep 29, 2022
c42eb74
Adding more logs for testing
VAIBHAVJINDAL3012 Sep 29, 2022
d7c3c68
Adding more logs for testing
VAIBHAVJINDAL3012 Sep 29, 2022
1ece67d
Implemented the NoAckMilestone fetching mechanism
VAIBHAVJINDAL3012 Oct 10, 2022
4fb409d
Merge branch 'develop' of https://github.com/maticnetwork/bor into Mi…
VAIBHAVJINDAL3012 Oct 13, 2022
b70723d
Testcases for milestone implementation
VAIBHAVJINDAL3012 Oct 13, 2022
2080d53
Testing code for fetchNoAckMilestone and fetchLastNoAckMilestone
VAIBHAVJINDAL3012 Oct 13, 2022
f5e0782
Testing changes
VAIBHAVJINDAL3012 Oct 14, 2022
352150b
refactor else-if
JekaMas Oct 17, 2022
34ee564
Corrected the number of params in bor_ext.go
VAIBHAVJINDAL3012 Oct 19, 2022
f5337eb
Dummy API for testing
VAIBHAVJINDAL3012 Oct 19, 2022
cb3d1b4
Defined the GetVoteOnRootHash in interface
VAIBHAVJINDAL3012 Oct 19, 2022
c7e99fc
Defined the GetVoteOnRootHash in interface
VAIBHAVJINDAL3012 Oct 19, 2022
2dad0ce
Made changes in the web3ext file
VAIBHAVJINDAL3012 Oct 19, 2022
3cfab8d
Added the GetVoteOnRootHash in PublicBlockChain API
VAIBHAVJINDAL3012 Oct 19, 2022
9a3e1f6
Added the GetVoteOnRootHash in filterBackend
VAIBHAVJINDAL3012 Oct 19, 2022
268cfb0
Added the log of Root and RooHash
VAIBHAVJINDAL3012 Oct 19, 2022
ed89d2c
Removed the 0x from rootHash
VAIBHAVJINDAL3012 Oct 19, 2022
d8a472e
Just for testing purpose
VAIBHAVJINDAL3012 Oct 19, 2022
98cdc6d
"GetVoteOnRootHash" mock implementation
VAIBHAVJINDAL3012 Oct 26, 2022
2de19d6
bor_test.go
VAIBHAVJINDAL3012 Oct 26, 2022
ee97a0e
Added the test for milestone implementation
VAIBHAVJINDAL3012 Nov 1, 2022
174408c
Added service for fetching milestone by ID
VAIBHAVJINDAL3012 Nov 1, 2022
2b492c5
Improved the comments
VAIBHAVJINDAL3012 Nov 2, 2022
1271f1c
Removed the duplicate code
VAIBHAVJINDAL3012 Nov 2, 2022
dfc05a3
use setter for borVerifier
JekaMas Nov 9, 2022
a998577
use setter for borVerifier
JekaMas Nov 9, 2022
412ad34
refactor handleNoAckMilestone
JekaMas Nov 9, 2022
8444aae
remove code repetition with retry function
JekaMas Nov 9, 2022
b931c9a
Converged the repetitive code
VAIBHAVJINDAL3012 Nov 9, 2022
9ccf910
Merge branch 'MilestoneNoReorg' of https://github.com/maticnetwork/bo…
VAIBHAVJINDAL3012 Nov 9, 2022
557cb74
after CR
JekaMas Nov 10, 2022
932dda1
persistence
JekaMas Nov 10, 2022
b5bf246
persistence implementation
VAIBHAVJINDAL3012 Nov 11, 2022
7bd871f
feature flag
JekaMas Nov 14, 2022
0a07d17
Persistence Changes
VAIBHAVJINDAL3012 Nov 15, 2022
a4909c2
Merge changes
VAIBHAVJINDAL3012 Nov 15, 2022
18e5c26
cr
JekaMas Nov 15, 2022
8ff51f8
initial
JekaMas Nov 15, 2022
146a4ab
fix
JekaMas Nov 15, 2022
4ae4c92
fix
JekaMas Nov 15, 2022
dbfa440
Whitelist Flag
VAIBHAVJINDAL3012 Nov 16, 2022
ec48715
1.Revert back the changes which included the milestone services as du…
VAIBHAVJINDAL3012 Dec 14, 2022
8889310
1 Add:Included the milestone flag 2.Add:Hardlimit the rewind to maxi…
VAIBHAVJINDAL3012 Dec 14, 2022
b8cbea0
Pull from the develop branch
VAIBHAVJINDAL3012 Dec 14, 2022
91f2572
Chg:Updated go.mod file
VAIBHAVJINDAL3012 Dec 14, 2022
e1c2f58
Remove:Dubai Hardfork code
VAIBHAVJINDAL3012 Dec 14, 2022
6782fc7
Add:checked errors for call functions to the Db, Rmv: Remote Header v…
VAIBHAVJINDAL3012 Dec 19, 2022
a8f58d6
Fix:Linting issues'
VAIBHAVJINDAL3012 Dec 20, 2022
22e7861
Add:MilestoneGRPC functions
VAIBHAVJINDAL3012 Dec 20, 2022
df7abad
Fix:Lint issues
VAIBHAVJINDAL3012 Dec 20, 2022
d67c4f8
Fix:Lint issues
VAIBHAVJINDAL3012 Dec 20, 2022
bbc7d33
Merge branch 'MilestoneNoReorg' of https://github.com/maticnetwork/bo…
VAIBHAVJINDAL3012 Dec 20, 2022
34d60e5
Fix: TestFetchMilestoneFromMockHeimdall
VAIBHAVJINDAL3012 Dec 20, 2022
d85e858
Fix:Integrations tests
VAIBHAVJINDAL3012 Dec 21, 2022
72d8b79
Add:Test for sprint length and milestone changes
VAIBHAVJINDAL3012 Jan 3, 2023
e404467
Add:Functionality to fetch the finalized block
VAIBHAVJINDAL3012 Jan 3, 2023
82d7554
Chg:Changed default val of TriesInmemory to 1024
VAIBHAVJINDAL3012 Jan 3, 2023
d1b1531
fix:Some functions of heimdallGRPC client
VAIBHAVJINDAL3012 Jan 3, 2023
587c6cd
Restored the GRPC functionality, was commented out for developing pu…
VAIBHAVJINDAL3012 Jan 3, 2023
09d1650
Fix:Bor_checkpoint_Verfier function
VAIBHAVJINDAL3012 Jan 5, 2023
d38fe80
Test:Added the chain Rewinding test
VAIBHAVJINDAL3012 Jan 5, 2023
871417c
Test:Added the Sprint Length + Milestone merge test
VAIBHAVJINDAL3012 Jan 5, 2023
ba7f99f
Pull merge from the develop branch
VAIBHAVJINDAL3012 Jan 5, 2023
7d59e63
Add:Implemented the future milestone
VAIBHAVJINDAL3012 Jan 5, 2023
9b58367
Add:Future milestone changes
VAIBHAVJINDAL3012 Jan 6, 2023
ea35b11
Add:Future milestone changes
VAIBHAVJINDAL3012 Jan 6, 2023
3953ce8
Chg: Voting on endBlockHash rather than rootHash
VAIBHAVJINDAL3012 Jan 6, 2023
7197813
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Jan 6, 2023
d3c3480
Chg: Changed the logic of future milestone from rootHash checking to …
VAIBHAVJINDAL3012 Jan 9, 2023
8a1fad4
Fix:Using endBockHash while verifying the incoming milestone
VAIBHAVJINDAL3012 Jan 9, 2023
7d7533f
Chg:Variable names for better readiblity
VAIBHAVJINDAL3012 Jan 9, 2023
1608cb2
Pull vaibhav/Milestone branch
VAIBHAVJINDAL3012 Jan 9, 2023
ade9aae
Fix:Testing changes
VAIBHAVJINDAL3012 Jan 11, 2023
0bd591a
Add:metrics for milestone implementation
VAIBHAVJINDAL3012 Jan 11, 2023
0f948c5
Pull merge from vaibhav/Milestone branch
VAIBHAVJINDAL3012 Jan 11, 2023
2ccde19
Add:Metrics for milestone implementatian
VAIBHAVJINDAL3012 Jan 11, 2023
35e4e6e
Fix:Order of statements in a function for better optimization
VAIBHAVJINDAL3012 Jan 11, 2023
9ebea27
Chg:Removed unrequired file
VAIBHAVJINDAL3012 Jan 11, 2023
76c0306
Fix:new variable intialization
VAIBHAVJINDAL3012 Jan 11, 2023
55502c3
Add:Comment to increase readiblity
VAIBHAVJINDAL3012 Jan 11, 2023
a6ed767
Fix:Logs
VAIBHAVJINDAL3012 Jan 11, 2023
fe86040
Chg:Name of GetVoteOnRootHash to GetVoteOnHash
VAIBHAVJINDAL3012 Jan 11, 2023
9e9abba
Fix:Linting issues
VAIBHAVJINDAL3012 Jan 11, 2023
c221da9
Fixed linting issues
VAIBHAVJINDAL3012 Jan 11, 2023
4269a69
Rmv: Unnecessary logs and Add:Skip test for long tests
VAIBHAVJINDAL3012 Jan 11, 2023
a888be9
Fix:Checking current chain with whitelisted milestone or checkpoint i…
VAIBHAVJINDAL3012 Jan 11, 2023
f174d10
Fix:Test
VAIBHAVJINDAL3012 Jan 11, 2023
b7e60ac
Fix:Whitelisting of Milestone and Checkpoint process
VAIBHAVJINDAL3012 Jan 11, 2023
67ad3d5
Fix: Milestone JSON structure
VAIBHAVJINDAL3012 Jan 12, 2023
c65d3e4
Chg:Testcases changes
VAIBHAVJINDAL3012 Jan 12, 2023
3d15384
Fix:Change from VoteOnRootHash to VoteOnHash
VAIBHAVJINDAL3012 Jan 12, 2023
eada98f
Fix:Variable name fix
VAIBHAVJINDAL3012 Jan 12, 2023
9a9d152
Fix:Finalized API
VAIBHAVJINDAL3012 Jan 13, 2023
9c94784
internal/jsre/deps: update web3.js bundle
manav2401 Jan 16, 2023
82f3f1a
Fix:milestone verifier
VAIBHAVJINDAL3012 Jan 16, 2023
873a8e0
Pull from develop branch
VAIBHAVJINDAL3012 Jan 16, 2023
70c657d
Chg:Handling the long future chain import issue
VAIBHAVJINDAL3012 Jan 17, 2023
46da940
Fix:Lint issues
VAIBHAVJINDAL3012 Jan 17, 2023
0860862
Fix:TestLowDiffLongChain and TestPrunedImportSide tests, used hardcod…
VAIBHAVJINDAL3012 Jan 17, 2023
eec608a
Chg:Testcode for producing metrics
VAIBHAVJINDAL3012 Jan 17, 2023
659a1fb
Chg:Milestong polling value to 32 secs
VAIBHAVJINDAL3012 Jan 19, 2023
7d0ba4c
Add:Testcases
VAIBHAVJINDAL3012 Jan 19, 2023
d9b6b11
Add:Implemented the check to fetch the milestoneId from heimdall befo…
VAIBHAVJINDAL3012 Feb 16, 2023
0c82971
Added GRPC method for FetchMilestoneID
VAIBHAVJINDAL3012 Feb 16, 2023
4688ccd
Fix:lint issue
VAIBHAVJINDAL3012 Feb 17, 2023
947550f
Fix:lint issue
VAIBHAVJINDAL3012 Feb 17, 2023
1e1131d
Skiped out the tests which were mainly used to produce the supporting…
VAIBHAVJINDAL3012 Feb 20, 2023
323f70c
remove vcs build when running snyk
VAIBHAVJINDAL3012 Feb 20, 2023
5863f82
Add:Improved the logs and comments
VAIBHAVJINDAL3012 Feb 24, 2023
608611e
fix linters
temaniarpit27 Feb 25, 2023
941157b
Skipped some test as they are panic due to timeout issue in github
VAIBHAVJINDAL3012 Feb 27, 2023
95307d0
Chg:Variable name LockerSprintNumber to LockedMilestoneNumber for bet…
VAIBHAVJINDAL3012 Feb 27, 2023
9474e1e
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Feb 27, 2023
45a65c8
Chg:Conflicting variable names in milestone test file
VAIBHAVJINDAL3012 Feb 27, 2023
84bfe67
Chg:Conflicting function names in milestone test file
VAIBHAVJINDAL3012 Feb 28, 2023
fa085af
fix : minor fix in TestInsertingSpanSizeBlocks
0xsharma Mar 1, 2023
2758c32
Fix:Mocking issue in TestInsertingSpanSizeBlocks
VAIBHAVJINDAL3012 Mar 13, 2023
4bfded4
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 13, 2023
b5192da
merge develop
temaniarpit27 Mar 15, 2023
575f58a
Fix:GRPC Polyproto Version
VAIBHAVJINDAL3012 Mar 20, 2023
41de99c
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 20, 2023
8514287
eth/downloader: skip peer drop due to whitelisting err
manav2401 Mar 20, 2023
b08b6a7
eth, tests/bor: bug fixes and minor refactor
manav2401 Mar 20, 2023
5f86479
Add:Implemented the milestone related functions in the HeimdallApp
VAIBHAVJINDAL3012 Mar 21, 2023
271c2fb
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
0952e78
Merge branch 'develop' into vaibhav/Milestone
VAIBHAVJINDAL3012 Mar 21, 2023
451e0b8
Fix:Lint Errors & Remove:Redundant Code
VAIBHAVJINDAL3012 Mar 21, 2023
6093d99
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
75d370d
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
c770435
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
36b1025
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
70c51d3
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Mar 21, 2023
0fb3e05
Fix:Testing Errors
VAIBHAVJINDAL3012 Mar 21, 2023
4bcfbfe
Fix:Bor integeration tests
VAIBHAVJINDAL3012 Mar 21, 2023
b7e896d
Fix:Test errors
VAIBHAVJINDAL3012 Mar 21, 2023
a767fb9
update heimdall client mock files
anshalshukla Mar 24, 2023
d828191
remove unused arguments
anshalshukla Mar 29, 2023
c26ae6c
remove redundant code
anshalshukla Mar 29, 2023
a752c10
Chg:Changed the milestone polling intervals
VAIBHAVJINDAL3012 Apr 4, 2023
95aa1a7
Merge branch 'vaibhav/Milestone' of https://github.com/maticnetwork/b…
VAIBHAVJINDAL3012 Apr 4, 2023
0904159
Add: added block finality from whitelisted checkpoint
VAIBHAVJINDAL3012 Apr 10, 2023
e3582ca
skip future chain validation
VAIBHAVJINDAL3012 Apr 10, 2023
a9e8f6c
Add:confirmation check of 16 blocks over the end block while voting f…
VAIBHAVJINDAL3012 Jul 28, 2023
c28f981
Chg:Included endBlockNum in UnlockMutex function
VAIBHAVJINDAL3012 Aug 7, 2023
9874b9e
Add:Property based test for milestone
VAIBHAVJINDAL3012 Aug 7, 2023
138152d
Fix:Opening the lock while processing future milestone
VAIBHAVJINDAL3012 Aug 9, 2023
3755c32
Add:Property based test for futureMilestone
VAIBHAVJINDAL3012 Aug 9, 2023
2ede58f
merge develop
temaniarpit27 Aug 14, 2023
9f350cc
Defined the value of TempTriesInMemory
VAIBHAVJINDAL3012 Aug 16, 2023
896df73
Fixed the finalized api
VAIBHAVJINDAL3012 Aug 16, 2023
5e442f4
Merge pull request #966 from maticnetwork/vaibhav/MilestoneIteration1
VAIBHAVJINDAL3012 Aug 16, 2023
0237b09
Merge branch 'vaibhav/Milestone-2' of https://github.com/maticnetwork…
VAIBHAVJINDAL3012 Aug 16, 2023
05c6c7f
Fixed lint issues
VAIBHAVJINDAL3012 Aug 16, 2023
074d28b
eth: add logs while fetching and rewinding
manav2401 Aug 22, 2023
713c704
fix linters: use default returns instead of recursive calls
manav2401 Aug 22, 2023
26c22a6
Merge branch 'develop' into vaibhav/Milestone-2
VAIBHAVJINDAL3012 Aug 23, 2023
6a18768
Fix:Milestone intergration test
VAIBHAVJINDAL3012 Aug 23, 2023
597c810
Add:GetVoteHash fn in mock backend
0xsharma Jul 17, 2023
892ee07
tests/bor: fix mock span
manav2401 Aug 24, 2023
25f8a2b
tests/bor: remove t.Parallel()
manav2401 Aug 25, 2023
d6e9bd4
use bor namespace in ethclient, fix mock function
manav2401 Aug 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions accounts/abi/bind/backends/bor_simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (fb *filterBackend) GetBorBlockReceipt(ctx context.Context, hash common.Has
return receipt, nil
}

func (fb *filterBackend) GetVoteOnHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64, hash string, milestoneId string) (bool, error) {
return false, nil
}

func (fb *filterBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error) {
receipt, err := fb.GetBorBlockReceipt(ctx, hash)
if err != nil || receipt == nil {
Expand Down
3 changes: 3 additions & 0 deletions common/flags/milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package flags

const Milestone = true
5 changes: 5 additions & 0 deletions common/gererics/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gererics

func Empty[T any]() (t T) {
return
}
4 changes: 4 additions & 0 deletions consensus/bor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func (api *API) GetRootHash(start uint64, end uint64) (string, error) {
return root, nil
}

func (api *API) GetVoteOnHash(starBlockNr uint64, endBlockNr uint64, hash string, milestoneId string) (bool, error) {
return false, nil
}

func (api *API) initializeRootHashCache() error {
var err error
if api.rootHashCache == nil {
Expand Down
6 changes: 6 additions & 0 deletions consensus/bor/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ethereum/go-ethereum/consensus/bor/clerk"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/milestone"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/span"
)

Expand All @@ -14,5 +15,10 @@ type IHeimdallClient interface {
Span(ctx context.Context, spanID uint64) (*span.HeimdallSpan, error)
FetchCheckpoint(ctx context.Context, number int64) (*checkpoint.Checkpoint, error)
FetchCheckpointCount(ctx context.Context) (int64, error)
FetchMilestone(ctx context.Context) (*milestone.Milestone, error)
FetchMilestoneCount(ctx context.Context) (int64, error)
FetchNoAckMilestone(ctx context.Context, milestoneID string) error //Fetch the bool value whether milestone corresponding to the given id failed in the Heimdall
FetchLastNoAckMilestone(ctx context.Context) (string, error) //Fetch latest failed milestone id
FetchMilestoneID(ctx context.Context, milestoneID string) error //Fetch the bool value whether milestone corresponding to the given id is in process in Heimdall
Close()
}
134 changes: 132 additions & 2 deletions consensus/bor/heimdall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ethereum/go-ethereum/consensus/bor/clerk"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/milestone"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/span"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
Expand All @@ -23,6 +24,8 @@ var (
ErrShutdownDetected = errors.New("shutdown detected")
ErrNoResponse = errors.New("got a nil response")
ErrNotSuccessfulResponse = errors.New("error while fetching data from Heimdall")
ErrNotInRejectedList = errors.New("milestoneID doesn't exist in rejected list")
ErrNotInMilestoneList = errors.New("milestoneID doesn't exist in Heimdall")
)

const (
Expand Down Expand Up @@ -66,8 +69,16 @@ func NewHeimdallClient(urlString string) *HeimdallClient {
const (
fetchStateSyncEventsFormat = "from-id=%d&to-time=%d&limit=%d"
fetchStateSyncEventsPath = "clerk/event-record/list"
fetchCheckpoint = "/checkpoints/%s"
fetchCheckpointCount = "/checkpoints/count"

fetchCheckpoint = "/checkpoints/%s"
fetchCheckpointCount = "/checkpoints/count"

fetchMilestone = "/milestone/latest"
fetchMilestoneCount = "/milestone/count"

fetchLastNoAckMilestone = "/milestone/lastNoAck"
fetchNoAckMilestone = "/milestone/noAck/%s"
fetchMilestoneID = "/milestone/ID/%s"

fetchSpanFormat = "bor/span/%d"
)
Expand Down Expand Up @@ -144,6 +155,23 @@ func (h *HeimdallClient) FetchCheckpoint(ctx context.Context, number int64) (*ch
return &response.Result, nil
}

// FetchMilestone fetches the checkpoint from heimdall
func (h *HeimdallClient) FetchMilestone(ctx context.Context) (*milestone.Milestone, error) {
url, err := milestoneURL(h.urlString)
if err != nil {
return nil, err
}

ctx = withRequestType(ctx, milestoneRequest)

response, err := FetchWithRetry[milestone.MilestoneResponse](ctx, h.client, url, h.closeCh)
if err != nil {
return nil, err
}

return &response.Result, nil
}

// FetchCheckpointCount fetches the checkpoint count from heimdall
func (h *HeimdallClient) FetchCheckpointCount(ctx context.Context) (int64, error) {
url, err := checkpointCountURL(h.urlString)
Expand All @@ -161,6 +189,84 @@ func (h *HeimdallClient) FetchCheckpointCount(ctx context.Context) (int64, error
return response.Result.Result, nil
}

// FetchMilestoneCount fetches the milestone count from heimdall
func (h *HeimdallClient) FetchMilestoneCount(ctx context.Context) (int64, error) {
url, err := milestoneCountURL(h.urlString)
if err != nil {
return 0, err
}

ctx = withRequestType(ctx, milestoneCountRequest)

response, err := FetchWithRetry[milestone.MilestoneCountResponse](ctx, h.client, url, h.closeCh)
if err != nil {
return 0, err
}

return response.Result.Count, nil
}

// FetchLastNoAckMilestone fetches the last no-ack-milestone from heimdall
func (h *HeimdallClient) FetchLastNoAckMilestone(ctx context.Context) (string, error) {
url, err := lastNoAckMilestoneURL(h.urlString)
if err != nil {
return "", err
}

ctx = withRequestType(ctx, milestoneLastNoAckRequest)

response, err := FetchWithRetry[milestone.MilestoneLastNoAckResponse](ctx, h.client, url, h.closeCh)
if err != nil {
return "", err
}

return response.Result.Result, nil
}

// FetchNoAckMilestone fetches the last no-ack-milestone from heimdall
func (h *HeimdallClient) FetchNoAckMilestone(ctx context.Context, milestoneID string) error {
url, err := noAckMilestoneURL(h.urlString, milestoneID)
if err != nil {
return err
}

ctx = withRequestType(ctx, milestoneNoAckRequest)

response, err := FetchWithRetry[milestone.MilestoneNoAckResponse](ctx, h.client, url, h.closeCh)
if err != nil {
return err
}

if !response.Result.Result {
return fmt.Errorf("%w: milestoneID %q", ErrNotInRejectedList, milestoneID)
}

return nil
}

// FetchMilestoneID fetches the bool result from Heimdal whether the ID corresponding
// to the given milestone is in process in Heimdall
func (h *HeimdallClient) FetchMilestoneID(ctx context.Context, milestoneID string) error {
url, err := milestoneIDURL(h.urlString, milestoneID)
if err != nil {
return err
}

ctx = withRequestType(ctx, milestoneIDRequest)

response, err := FetchWithRetry[milestone.MilestoneIDResponse](ctx, h.client, url, h.closeCh)

if err != nil {
return err
}

if !response.Result.Result {
return fmt.Errorf("%w: milestoneID %q", ErrNotInMilestoneList, milestoneID)
}

return nil
}

// FetchWithRetry returns data from heimdall with retry
func FetchWithRetry[T any](ctx context.Context, client http.Client, url *url.URL, closeCh chan struct{}) (*T, error) {
// request data once
Expand Down Expand Up @@ -266,10 +372,34 @@ func checkpointURL(urlString string, number int64) (*url.URL, error) {
return makeURL(urlString, url, "")
}

func milestoneURL(urlString string) (*url.URL, error) {
url := fetchMilestone

return makeURL(urlString, url, "")
}

func checkpointCountURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchCheckpointCount, "")
}

func milestoneCountURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchMilestoneCount, "")
}

func lastNoAckMilestoneURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchLastNoAckMilestone, "")
}

func noAckMilestoneURL(urlString string, id string) (*url.URL, error) {
url := fmt.Sprintf(fetchNoAckMilestone, id)
return makeURL(urlString, url, "")
}

func milestoneIDURL(urlString string, id string) (*url.URL, error) {
url := fmt.Sprintf(fetchMilestoneID, id)
return makeURL(urlString, url, "")
}

func makeURL(urlString, rawPath, rawQuery string) (*url.URL, error) {
u, err := url.Parse(urlString)
if err != nil {
Expand Down
99 changes: 93 additions & 6 deletions consensus/bor/heimdall/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/network"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/milestone"

"github.com/stretchr/testify/require"
)
Expand All @@ -23,7 +24,10 @@ import (
// requests to the mock heimdal server for specific functions. Add more handlers
// according to requirements.
type HttpHandlerFake struct {
handleFetchCheckpoint http.HandlerFunc
handleFetchCheckpoint http.HandlerFunc
handleFetchMilestone http.HandlerFunc
handleFetchNoAckMilestone http.HandlerFunc
handleFetchLastNoAckMilestone http.HandlerFunc
}

func (h *HttpHandlerFake) GetCheckpointHandler() http.HandlerFunc {
Expand All @@ -32,6 +36,24 @@ func (h *HttpHandlerFake) GetCheckpointHandler() http.HandlerFunc {
}
}

func (h *HttpHandlerFake) GetMilestoneHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.handleFetchMilestone.ServeHTTP(w, r)
}
}

func (h *HttpHandlerFake) GetNoAckMilestoneHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.handleFetchNoAckMilestone.ServeHTTP(w, r)
}
}

func (h *HttpHandlerFake) GetLastNoAckMilestoneHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.handleFetchLastNoAckMilestone.ServeHTTP(w, r)
}
}

func CreateMockHeimdallServer(wg *sync.WaitGroup, port int, listener net.Listener, handler *HttpHandlerFake) (*http.Server, error) {
// Create a new server mux
mux := http.NewServeMux()
Expand All @@ -41,6 +63,21 @@ func CreateMockHeimdallServer(wg *sync.WaitGroup, port int, listener net.Listene
handler.GetCheckpointHandler()(w, r)
})

// Create a route for fetching milestone
mux.HandleFunc("/milestone/latest", func(w http.ResponseWriter, r *http.Request) {
handler.GetMilestoneHandler()(w, r)
})

// Create a route for fetching milestone
mux.HandleFunc("/milestone/noAck/{id}", func(w http.ResponseWriter, r *http.Request) {
handler.GetNoAckMilestoneHandler()(w, r)
})

// Create a route for fetching milestone
mux.HandleFunc("/milestone/lastNoAck", func(w http.ResponseWriter, r *http.Request) {
handler.GetLastNoAckMilestoneHandler()(w, r)
})

// Add other routes as per requirement

// Create the server with given port and mux
Expand Down Expand Up @@ -118,9 +155,59 @@ func TestFetchCheckpointFromMockHeimdall(t *testing.T) {
wg.Wait()
}

// TestFetchMilestoneFromMockHeimdall tests the heimdall client side logic
// to fetch milestone from a mock heimdall server.
// It can be used for debugging purpose (like response fields, marshalling/unmarshalling, etc).
func TestFetchMilestoneFromMockHeimdall(t *testing.T) {
t.Parallel()

// Create a wait group for sending across the mock server
wg := &sync.WaitGroup{}
wg.Add(1)

// Initialize the fake handler and add a fake milestone handler function
handler := &HttpHandlerFake{}
handler.handleFetchMilestone = func(w http.ResponseWriter, _ *http.Request) {
err := json.NewEncoder(w).Encode(milestone.MilestoneResponse{
Height: "0",
Result: milestone.Milestone{
Proposer: common.Address{},
StartBlock: big.NewInt(0),
EndBlock: big.NewInt(512),
Hash: common.Hash{},
BorChainID: "15001",
Timestamp: 0,
},
})

if err != nil {
w.WriteHeader(500) // Return 500 Internal Server Error.
}
}

// Fetch available port
port, listener, err := network.FindAvailablePort()
require.NoError(t, err, "expect no error in finding available port")

// Create mock heimdall server and pass handler instance for setting up the routes
srv, err := CreateMockHeimdallServer(wg, port, listener, handler)
require.NoError(t, err, "expect no error in starting mock heimdall server")

// Create a new heimdall client and use same port for connection
client := NewHeimdallClient(fmt.Sprintf("http://localhost:%d", port))
_, err = client.FetchMilestone(context.Background())
require.NoError(t, err, "expect no error in fetching milestone")

// Shutdown the server
err = srv.Shutdown(context.TODO())
require.NoError(t, err, "expect no error in shutting down mock heimdall server")

// Wait for `wg.Done()` to be called in the mock server's routine.
wg.Wait()
}

// TestFetchShutdown tests the heimdall client side logic for context timeout and
// interrupt handling while fetching checkpoints (latest for the scope of test)
// from a mock heimdall server.
// interrupt handling while fetching data from a mock heimdall server.
func TestFetchShutdown(t *testing.T) {
t.Parallel()

Expand All @@ -135,7 +222,7 @@ func TestFetchShutdown(t *testing.T) {
// greater than `retryDelay`. This should cause the request to timeout and trigger shutdown
// due to `ctx.Done()`. Expect context timeout error.
handler.handleFetchCheckpoint = func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(6 * time.Second)
time.Sleep(100 * time.Millisecond)

err := json.NewEncoder(w).Encode(checkpoint.CheckpointResponse{
Height: "0",
Expand Down Expand Up @@ -165,7 +252,7 @@ func TestFetchShutdown(t *testing.T) {
// Create a new heimdall client and use same port for connection
client := NewHeimdallClient(fmt.Sprintf("http://localhost:%d", port))

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)

// Expect this to fail due to timeout
_, err = client.FetchCheckpoint(ctx, -1)
Expand All @@ -182,7 +269,7 @@ func TestFetchShutdown(t *testing.T) {
w.WriteHeader(500) // Return 500 Internal Server Error.
}

ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second) // Use some high value for timeout
ctx, cancel = context.WithTimeout(context.Background(), 50*time.Millisecond) // Use some high value for timeout

// Cancel the context after a delay until we make request
go func(cancel context.CancelFunc) {
Expand Down
Loading