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

Fix state sync fetch #1443

Merged
merged 9 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 30 additions & 11 deletions consensus/bor/heimdall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
"time"

"github.com/0xPolygon/heimdall-v2/x/bor/types"
clerkTypes "github.com/0xPolygon/heimdall-v2/x/clerk/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/gogoproto/proto"
"github.com/ethereum/go-ethereum/common"
"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"
Expand Down Expand Up @@ -76,8 +78,9 @@ func NewHeimdallClient(urlString string) *HeimdallClient {
}

const (
fetchStateSyncEventsFormat = "from-id=%d&to-time=%d&limit=%d"
fetchStateSyncEventsPath = "clerk/event-record/list"
fetchStateSyncEventsFormat = "from-id=%d&to-time=%d"
fetchStateSyncEventsPath = "clerk/time"
fetchStateSyncList = "clerk/event-record/list"

fetchCheckpoint = "/checkpoints/%s"
fetchCheckpointCount = "/checkpoints/count"
Expand All @@ -95,7 +98,7 @@ func (h *HeimdallClient) StateSyncEvents(ctx context.Context, fromID uint64, to
eventRecords := make([]*clerk.EventRecordWithTime, 0)

for {
url, err := stateSyncURL(h.urlString, fromID, to)
url, err := stateSyncListURL(h.urlString)
if err != nil {
return nil, err
}
Expand All @@ -104,19 +107,31 @@ func (h *HeimdallClient) StateSyncEvents(ctx context.Context, fromID uint64, to

ctx = withRequestType(ctx, stateSyncRequest)

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

if response == nil || response.Result == nil {
// status 204
break
var record *clerk.EventRecordWithTime

for _, e := range response.EventRecords {
if e.Id >= fromID && e.RecordTime.Before(time.Unix(to, 0)) {
record = &clerk.EventRecordWithTime{
EventRecord: clerk.EventRecord{
ID: e.Id,
ChainID: e.BorChainId,
Contract: common.HexToAddress(e.Contract),
Data: e.Data,
LogIndex: e.LogIndex,
TxHash: common.HexToHash(e.TxHash),
},
Time: e.RecordTime,
}
eventRecords = append(eventRecords, record)
}
}

eventRecords = append(eventRecords, response.Result...)

if len(response.Result) < stateFetchLimit {
if len(response.EventRecords) < stateFetchLimit {
break
}

Expand Down Expand Up @@ -387,11 +402,15 @@ func spanURL(urlString string, spanID uint64) (*url.URL, error) {
}

func stateSyncURL(urlString string, fromID uint64, to int64) (*url.URL, error) {
queryParams := fmt.Sprintf(fetchStateSyncEventsFormat, fromID, to, stateFetchLimit)
queryParams := fmt.Sprintf(fetchStateSyncEventsFormat, fromID, to)

return makeURL(urlString, fetchStateSyncEventsPath, queryParams)
}

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

func checkpointURL(urlString string, number int64) (*url.URL, error) {
url := ""
if number == -1 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.2
toolchain go1.23.4

require (
github.com/0xPolygon/heimdall-v2 v0.0.0-20250124090128-bc0afd1d5e6d
github.com/0xPolygon/heimdall-v2 v0.0.0-20250206060755-5ff54cfa4cdc
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.3.2
github.com/BurntSushi/toml v1.4.0
github.com/JekaMas/go-grpc-net-conn v0.0.0-20220708155319-6aff21f2d13d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ github.com/0xPolygon/heimdall-v2 v0.0.0-20241223112457-d6d02498fc6b h1:TdBGUM30O
github.com/0xPolygon/heimdall-v2 v0.0.0-20241223112457-d6d02498fc6b/go.mod h1:LdqHmJXcPaELo1IfP0RwL8M5UsNmvwpG4+4jlF70/os=
github.com/0xPolygon/heimdall-v2 v0.0.0-20250124090128-bc0afd1d5e6d h1:UJA175jWMXBKRP67wsQEDQEvSqZZ9WgYtLyqZY8ayCw=
github.com/0xPolygon/heimdall-v2 v0.0.0-20250124090128-bc0afd1d5e6d/go.mod h1:LdqHmJXcPaELo1IfP0RwL8M5UsNmvwpG4+4jlF70/os=
github.com/0xPolygon/heimdall-v2 v0.0.0-20250206060755-5ff54cfa4cdc h1:tNuqe892hNl/4r178Sxj6WZs2jsSCwlp/YAltqgg7j0=
github.com/0xPolygon/heimdall-v2 v0.0.0-20250206060755-5ff54cfa4cdc/go.mod h1:LdqHmJXcPaELo1IfP0RwL8M5UsNmvwpG4+4jlF70/os=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
github.com/99designs/keyring v1.2.1 h1:tYLp1ULvO7i3fI5vE21ReQuj99QFSs7lGm0xWyJo87o=
Expand Down
Loading