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

Fixed fakebeacon #104

Merged
merged 2 commits into from
Nov 19, 2024
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
23 changes: 21 additions & 2 deletions beacon/fakebeacon/api_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fakebeacon
import (
"context"
"sort"
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
Expand All @@ -11,9 +12,10 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)

// spec: https://ethereum.github.io/beacon-APIs/#/Beacon/getBlobSidecars
type BlobSidecar struct {
Blob kzg4844.Blob `json:"blob"`
Index int `json:"index"`
Index Uint64String `json:"index"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KZGCommitment kzg4844.Commitment `json:"kzg_commitment"`
KZGProof kzg4844.Proof `json:"kzg_proof"`
}
Expand All @@ -39,6 +41,23 @@ type IndexedBlobHash struct {
Hash common.Hash // hash of the blob, used for consistency checks
}

// Uint64String is a decimal string representation of an uint64, for usage in the Beacon API JSON encoding
type Uint64String uint64

func (v Uint64String) MarshalText() (out []byte, err error) {
out = strconv.AppendUint(out, uint64(v), 10)
return
}

func (v *Uint64String) UnmarshalText(b []byte) error {
n, err := strconv.ParseUint(string(b), 0, 64)
if err != nil {
return err
}
*v = Uint64String(n)
return nil
}

func configSpec() ReducedConfigData {
return ReducedConfigData{SecondsPerSlot: "1"}
}
Expand Down Expand Up @@ -72,7 +91,7 @@ func beaconBlobSidecars(ctx context.Context, backend ethapi.Backend, slot uint64
}
if fullBlob || idx == indices[curIdx] {
res.Data = append(res.Data, &BlobSidecar{
Index: idx,
Index: Uint64String(idx),
Blob: sideCar.Blobs[i],
KZGCommitment: sideCar.Commitments[i],
KZGProof: sideCar.Proofs[i],
Expand Down
24 changes: 20 additions & 4 deletions beacon/fakebeacon/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)

Expand All @@ -19,8 +20,10 @@ func fetchBlockNumberByTime(ctx context.Context, ts int64, backend ethapi.Backen
if ts > blockTime {
return nil, errors.New("time too large")
}
blockPeriod := getBlockPeriod(backend.ChainConfig())
adjustedBlockPeriod := getBlockPeriod(backend.ChainConfig())*2 - 1
blockNum := currentHeader.Number.Uint64()
estimateEndNumber := int64(blockNum) - (blockTime-ts)/5
estimateEndNumber := int64(blockNum) - (blockTime-ts)/adjustedBlockPeriod
// find the end number
for {
header, err := backend.HeaderByNumber(ctx, rpc.BlockNumber(estimateEndNumber))
Expand All @@ -39,10 +42,10 @@ func fetchBlockNumberByTime(ctx context.Context, ts int64, backend ethapi.Backen
}

// let the estimateEndNumber a little bigger than real value
if headerTime > ts+12 {
estimateEndNumber -= (headerTime - ts) / 5
if headerTime > ts+(blockPeriod*4) {
estimateEndNumber -= (headerTime - ts) / adjustedBlockPeriod
} else if headerTime < ts {
estimateEndNumber += (ts-headerTime)/5 + 1
estimateEndNumber += (ts-headerTime)/adjustedBlockPeriod + 1
} else {
// search one by one
for headerTime >= ts {
Expand All @@ -63,3 +66,16 @@ func fetchBlockNumberByTime(ctx context.Context, ts int64, backend ethapi.Backen
}
}
}

func getBlockPeriod(cfg *params.ChainConfig) int64 {
switch {
case cfg.ChainID.Cmp(params.OasysMainnetChainConfig.ChainID) == 0:
return params.SHORT_BLOCK_TIME_SECONDS
case cfg.ChainID.Cmp(params.OasysTestnetChainConfig.ChainID) == 0:
return params.SHORT_BLOCK_TIME_SECONDS
case cfg.Oasys != nil:
return int64(cfg.Oasys.Period) // for local chain
default:
return 1
}
}