From f7d03e35acfd40f133abdb920a7ef01723a33893 Mon Sep 17 00:00:00 2001 From: syntrust Date: Tue, 30 Dec 2025 15:00:33 +0800 Subject: [PATCH 1/2] use latest block --- ethstorage/eth/polling_client.go | 10 ++-------- ethstorage/eth/randao_client.go | 3 +-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/ethstorage/eth/polling_client.go b/ethstorage/eth/polling_client.go index 95169cb7..0d1591b0 100644 --- a/ethstorage/eth/polling_client.go +++ b/ethstorage/eth/polling_client.go @@ -17,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/rpc" "golang.org/x/mod/semver" ) @@ -195,14 +196,7 @@ func (w *PollingClient) pollHeads() { func (w *PollingClient) getLatestHeader() (*types.Header, error) { ctx, cancel := context.WithTimeout(w.ctx, 5*time.Second) defer cancel() - latest, err := w.BlockNumber(ctx) - if err != nil { - w.lg.Error("Failed to get latest block number", "err", err) - return nil, err - } - // The latest blockhash could be empty - number := new(big.Int).SetUint64(latest - 1) - return w.HeaderByNumber(ctx, number) + return w.HeaderByNumber(ctx, big.NewInt(rpc.LatestBlockNumber.Int64())) } func (w *PollingClient) reqPoll() { diff --git a/ethstorage/eth/randao_client.go b/ethstorage/eth/randao_client.go index b429d5e4..c8bfd2c1 100644 --- a/ethstorage/eth/randao_client.go +++ b/ethstorage/eth/randao_client.go @@ -80,8 +80,7 @@ func (q *RandaoBlockQuerier) getLatestNumber(ctx context.Context) (*big.Int, err if err != nil { return nil, fmt.Errorf("failed to call number() %v", err) } - // The latest blockhash could be empty - curBlock := new(big.Int).Sub(new(big.Int).SetBytes(ret), common.Big1) + curBlock := new(big.Int).SetBytes(ret) q.lg.Debug("Got latest block number by Randao querier", "number", curBlock) return curBlock, nil } From 60bfff6edcbcea0e5f3d4f2464b17daae978ec88 Mon Sep 17 00:00:00 2001 From: syntrust Date: Wed, 31 Dec 2025 14:34:27 +0800 Subject: [PATCH 2/2] hold on submit tx --- cmd/es-node/config.go | 1 + ethstorage/miner/config.go | 1 + ethstorage/miner/worker.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/es-node/config.go b/cmd/es-node/config.go index 9c778939..2c5607e8 100644 --- a/cmd/es-node/config.go +++ b/cmd/es-node/config.go @@ -63,6 +63,7 @@ func NewConfig(ctx *cli.Context, lg log.Logger) (*node.Config, error) { lg.Info("Read chain ID of EthStorage network", "chainID", chainId) if minerConfig != nil { minerConfig.ChainID = chainId + minerConfig.Slot = l1Endpoint.L1BeaconSlotTime } archiverConfig := archiver.NewConfig(ctx) // l2Endpoint, err := NewL2EndpointConfig(ctx, lg) diff --git a/ethstorage/miner/config.go b/ethstorage/miner/config.go index 3a14bf10..8025d0ca 100644 --- a/ethstorage/miner/config.go +++ b/ethstorage/miner/config.go @@ -17,6 +17,7 @@ import ( type Config struct { // es network chain id ChainID *big.Int + Slot uint64 // contract RandomChecks uint64 diff --git a/ethstorage/miner/worker.go b/ethstorage/miner/worker.go index 47b9257e..f3ae5fc0 100644 --- a/ethstorage/miner/worker.go +++ b/ethstorage/miner/worker.go @@ -26,8 +26,6 @@ import ( const ( chainHeadChanSize = 1 taskQueueSize = 1 - resultQueueSize = 10 - slot = 12 // seconds miningTransactionTimeout = 50 // seconds // tx status codes @@ -428,6 +426,13 @@ func (w *worker) resultLoop() { continue } w.lg.Info("Mining result loop get result", "shard", result.startShardId, "block", result.blockNumber, "nonce", result.nonce) + + // Mining result comes within the same block time window + if tillNextSlot := int64(result.timestamp) + int64(w.config.Slot) - time.Now().Unix(); tillNextSlot > 0 { + // Wait until next block comes to avoid empty blockhash on gas estimation + w.lg.Info("Hold on submitting mining result till block+1", "block", result.blockNumber, "secondsToWait", tillNextSlot) + time.Sleep(time.Duration(tillNextSlot) * time.Second) + } txHash, err := w.l1API.SubmitMinedResult( context.Background(), w.storageMgr.ContractAddress(), @@ -633,7 +638,7 @@ func (w *worker) mineTask(t *taskItem) (bool, error) { w.lg.Debug("Mining task started", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "nonces", fmt.Sprintf("%d~%d", t.nonceStart, t.nonceEnd)) for w.isRunning() { // always use new randao to mine for each slot - if time.Since(startTime).Seconds() > slot { + if time.Since(startTime).Seconds() > float64(w.config.Slot) { if t.thread == 0 { nonceTriedTotal := (nonce - t.nonceStart) * w.config.ThreadsPerShard w.lg.Warn("Mining tasks timed out", "shard", t.shardIdx, "block", t.blockNumber,