diff --git a/cmd/es-node/config.go b/cmd/es-node/config.go index 55068c65..223c5713 100644 --- a/cmd/es-node/config.go +++ b/cmd/es-node/config.go @@ -269,8 +269,6 @@ func NewL1EndpointConfig(ctx *cli.Context) (*eth.L1EndpointConfig, *ethclient.Cl L1NodeAddr: l1NodeAddr, L1BlockTime: ctx.GlobalUint64(flags.L1BlockTime.Name), L1BeaconURL: ctx.GlobalString(flags.L1BeaconAddr.Name), - L1BeaconBasedTime: ctx.GlobalUint64(flags.L1BeaconBasedTime.Name), - L1BeaconBasedSlot: ctx.GlobalUint64(flags.L1BeaconBasedSlot.Name), L1BeaconSlotTime: ctx.GlobalUint64(flags.L1BeaconSlotTime.Name), DAURL: ctx.GlobalString(flags.DAURL.Name), L1MinDurationForBlobsRequest: ctx.GlobalUint64(flags.L1MinDurationForBlobsRequest.Name), diff --git a/ethstorage/eth/beacon_client.go b/ethstorage/eth/beacon_client.go index 528d921a..db966d80 100644 --- a/ethstorage/eth/beacon_client.go +++ b/ethstorage/eth/beacon_client.go @@ -9,16 +9,16 @@ import ( "fmt" "net/http" "net/url" + "strconv" "github.com/crate-crypto/go-proto-danksharding-crypto/eth" "github.com/ethereum/go-ethereum/common" ) type BeaconClient struct { - beaconURL string - basedTime uint64 - basedSlot uint64 - slotTime uint64 + beaconURL string + genesisSlotTime uint64 + slotTime uint64 } type Blob struct { @@ -41,18 +41,48 @@ type beaconBlobData struct { KZGProof string `json:"kzg_proof"` } -func NewBeaconClient(url string, basedTime uint64, basedSlot uint64, slotTime uint64) *BeaconClient { +func NewBeaconClient(url string, slotTime uint64) (*BeaconClient, error) { + genesisSlotTime, err := queryGenesisTime(url) + if err != nil { + return nil, err + } res := &BeaconClient{ - beaconURL: url, - basedTime: basedTime, - basedSlot: basedSlot, - slotTime: slotTime, + beaconURL: url, + genesisSlotTime: genesisSlotTime, + slotTime: slotTime, + } + return res, nil +} + +func queryGenesisTime(beaconUrl string) (uint64, error) { + queryUrl, err := url.JoinPath(beaconUrl, "/eth/v1/beacon/genesis") + if err != nil { + return 0, err + } + resp, err := http.Get(queryUrl) + if err != nil { + return 0, err + } + defer resp.Body.Close() + + genesisResponse := &struct { + Data struct { + GenesisTime string `json:"genesis_time"` + } `json:"data"` + }{} + err = json.NewDecoder(resp.Body).Decode(&genesisResponse) + if err != nil { + return 0, err + } + gt, err := strconv.ParseUint(genesisResponse.Data.GenesisTime, 10, 64) + if err != nil { + return 0, err } - return res + return gt, nil } func (c *BeaconClient) Timestamp2Slot(time uint64) uint64 { - return (time-c.basedTime)/c.slotTime + c.basedSlot + return (time - c.genesisSlotTime) / c.slotTime } func (c *BeaconClient) DownloadBlobs(slot uint64) (map[common.Hash]Blob, error) { diff --git a/ethstorage/eth/config.go b/ethstorage/eth/config.go index 50826dd0..01d00cef 100644 --- a/ethstorage/eth/config.go +++ b/ethstorage/eth/config.go @@ -5,8 +5,6 @@ type L1EndpointConfig struct { L1NodeAddr string // Address of L1 User JSON-RPC endpoint to use (eth namespace required) L1BlockTime uint64 // Block time of L1 chain L1BeaconURL string // L1 beacon chain endpoint - L1BeaconBasedTime uint64 // A pair of timestamp and slot number in the past time - L1BeaconBasedSlot uint64 // A pair of timestamp and slot number in the past time L1BeaconSlotTime uint64 // Slot duration DAURL string // Custom DA URL L1MinDurationForBlobsRequest uint64 // Min duration for blobs sidecars request diff --git a/ethstorage/flags/flags.go b/ethstorage/flags/flags.go index fc752e2c..e3ec4b5e 100644 --- a/ethstorage/flags/flags.go +++ b/ethstorage/flags/flags.go @@ -68,18 +68,6 @@ var ( Usage: "URL of JSON-RPC endpoint to query randao", EnvVar: prefixEnvVar("RANDAO_URL"), } - // TODO: @Qiang everytime devnet changed, we may need to change it - L1BeaconBasedTime = cli.Uint64Flag{ - Name: "l1.beacon-based-time", - Usage: "Timestamp of a slot specified by l1.beacon-based-slot", - EnvVar: prefixEnvVar("L1_BEACON_BASED_TIME"), - } - // TODO: @Qiang everytime devnet changed, we may need to change it - L1BeaconBasedSlot = cli.Uint64Flag{ - Name: "l1.beacon-based-slot", - Usage: "A slot number in the past time specified by l1.beacon-based-time", - EnvVar: prefixEnvVar("L1_BEACON_BASED_SLOT"), - } // TODO: @Qiang everytime devnet changed, we may need to change it if the slot time changed L1BeaconSlotTime = cli.Uint64Flag{ Name: "l1.beacon-slot-time", @@ -236,8 +224,6 @@ var optionalFlags = []cli.Flag{ L1BlockTime, L1BeaconSlotTime, L1BeaconAddr, - L1BeaconBasedTime, - L1BeaconBasedSlot, DAURL, RandaoURL, L1MinDurationForBlobsRequest, diff --git a/ethstorage/node/node.go b/ethstorage/node/node.go index d59f21fb..2fc2102c 100644 --- a/ethstorage/node/node.go +++ b/ethstorage/node/node.go @@ -164,7 +164,10 @@ func (n *EsNode) initL1(ctx context.Context, cfg *Config) error { n.daClient = eth.NewDAClient(cfg.L1.DAURL) n.log.Info("Using DA URL", "url", cfg.L1.DAURL) } else if cfg.L1.L1BeaconURL != "" { - n.l1Beacon = eth.NewBeaconClient(cfg.L1.L1BeaconURL, cfg.L1.L1BeaconBasedTime, cfg.L1.L1BeaconBasedSlot, cfg.L1.L1BeaconSlotTime) + n.l1Beacon, err = eth.NewBeaconClient(cfg.L1.L1BeaconURL, cfg.L1.L1BeaconSlotTime) + if err != nil { + return fmt.Errorf("failed to create L1 beacon source: %w", err) + } n.log.Info("Using L1 Beacon URL", "url", cfg.L1.L1BeaconURL) } else { return fmt.Errorf("no L1 beacon or DA URL provided") diff --git a/run.sh b/run.sh index 5cab5994..cd336537 100755 --- a/run.sh +++ b/run.sh @@ -31,8 +31,6 @@ start_flags=" --network devnet \ --storage.l1contract 0x804C520d3c084C805E37A35E90057Ac32831F96f \ --l1.rpc http://88.99.30.186:8545 \ --l1.beacon http://88.99.30.186:3500 \ - --l1.beacon-based-time 1706684472 \ - --l1.beacon-based-slot 4245906 \ --miner.enabled \ --miner.zkey $zkey_file \ --download.thread 32 \