Skip to content

Commit cba73e4

Browse files
committed
Return pending block instead of latest in eth_ namespace and attach pending block hash to receipts
1 parent 9d48c73 commit cba73e4

File tree

7 files changed

+56
-16
lines changed

7 files changed

+56
-16
lines changed

core/core.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,10 @@ func (c *Core) PendingBlock() *types.WorkObject {
11711171
return c.sl.miner.PendingBlock()
11721172
}
11731173

1174+
func (c *Core) PendingBlockByHash(blockHash common.Hash) *types.WorkObject {
1175+
return c.sl.miner.worker.PendingBlockByHash(blockHash)
1176+
}
1177+
11741178
// PendingBlockAndReceipts returns the currently pending block and corresponding receipts.
11751179
func (c *Core) PendingBlockAndReceipts() (*types.WorkObject, types.Receipts) {
11761180
return c.sl.miner.PendingBlockAndReceipts()

core/types/wo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ func (wo *WorkObject) RegionStateRoot() common.Hash {
407407
return wo.Header().RegionStateRoot()
408408
}
409409

410+
func (wo *WorkObject) QuaiTransactions() []*Transaction {
411+
quaiTxs := make([]*Transaction, 0)
412+
for _, t := range wo.Transactions() {
413+
if t.Type() == QuaiTxType {
414+
quaiTxs = append(quaiTxs, t)
415+
}
416+
}
417+
return quaiTxs
418+
}
419+
410420
func (wo *WorkObject) QiTransactions() []*Transaction {
411421
qiTxs := make([]*Transaction, 0)
412422
for _, t := range wo.Transactions() {

core/worker.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ type worker struct {
193193

194194
logger *log.Logger
195195

196-
newPendingCh chan *PendingState // newPendingCh is used to send new pending state, block and receipts to the worker
197-
pendingMu sync.RWMutex
198-
pendingState *PendingState
196+
newPendingCh chan *PendingState // newPendingCh is used to send new pending state, block and receipts to the worker
197+
pendingBlockCache *lru.Cache[common.Hash, *types.WorkObject]
198+
pendingMu sync.RWMutex
199+
pendingState *PendingState
199200
}
200201

201202
type RollingAverage struct {
@@ -282,6 +283,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, db ethdb.Databas
282283
if headerchain.ProcessingState() && nodeCtx == common.ZONE_CTX {
283284
worker.chainSideSub = worker.hc.SubscribeChainSideEvent(worker.chainSideCh)
284285
worker.wg.Add(1)
286+
worker.pendingBlockCache, _ = lru.New[common.Hash, *types.WorkObject](20)
285287
go worker.asyncStateLoop()
286288
go worker.PendingStateCache()
287289
}
@@ -378,6 +380,11 @@ func (w *worker) enablePreseal() {
378380
atomic.StoreUint32(&w.noempty, 0)
379381
}
380382

383+
func (w *worker) PendingBlockByHash(blockHash common.Hash) *types.WorkObject {
384+
pendingBlock, _ := w.pendingBlockCache.Get(blockHash)
385+
return pendingBlock
386+
}
387+
381388
// pending returns the pending state and corresponding block.
382389
func (w *worker) pending() *types.WorkObject {
383390
w.pendingMu.RLock()
@@ -452,6 +459,16 @@ func (w *worker) PendingStateCache() {
452459
w.pendingMu.Lock()
453460
w.pendingState = newPendingState
454461
w.pendingMu.Unlock()
462+
w.pendingBlockCache.Add(newPendingState.pendingBlock.Hash(), newPendingState.pendingBlock)
463+
for _, r := range newPendingState.pendingReceipts {
464+
if r.Type == types.QuaiTxType {
465+
w.logger.WithFields(log.Fields{
466+
"txHash": r.TxHash,
467+
"blockHash": newPendingState.pendingBlock.Hash(),
468+
"blockNumber": newPendingState.pendingBlock.NumberU64(common.ZONE_CTX),
469+
}).Info("New receipt in pending block")
470+
}
471+
}
455472
case <-w.exitCh:
456473
return
457474
}
@@ -528,7 +545,7 @@ func (w *worker) asyncStateLoop() {
528545
}()
529546
defer w.wg.Done() // decrement the wait group after the close of the loop
530547

531-
ticker := time.NewTicker(2 * time.Second)
548+
ticker := time.NewTicker(minRecommitInterval)
532549
var prevHeader *types.WorkObject = nil
533550
defer ticker.Stop()
534551
for {

internal/quaiapi/api.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,18 @@ func (s *PublicBlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.H
365365
// - When fullTx is true all transactions in the block are returned, otherwise
366366
// only the transaction hash is returned.
367367
func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
368+
if number == rpc.LatestBlockNumber && s.b.UsePendingState() {
369+
number = rpc.PendingBlockNumber // Use pending state for API queries if latest is requested and flag is set
370+
}
368371
block, err := s.b.BlockByNumber(ctx, number)
369372
if block != nil && err == nil {
370373
response, err := RPCMarshalETHBlock(block, true, fullTx, s.b.NodeLocation())
371-
if err == nil && number == rpc.PendingBlockNumber {
374+
/*if err == nil && number == rpc.PendingBlockNumber {
372375
// Pending blocks need to nil out a few fields
373376
for _, field := range []string{"hash", "nonce", "miner"} {
374377
response[field] = nil
375378
}
376-
}
379+
}*/
377380
return response, err
378381
}
379382
return nil, err
@@ -383,6 +386,9 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.B
383386
// detail, otherwise only the transaction hash is returned.
384387
func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) {
385388
block, err := s.b.BlockByHash(ctx, hash)
389+
if block == nil && s.b.UsePendingState() {
390+
block = s.b.PendingBlockByHash(hash)
391+
}
386392
if block != nil {
387393
return RPCMarshalETHBlock(block, true, fullTx, s.b.NodeLocation())
388394
}
@@ -963,7 +969,7 @@ func RPCMarshalETHBlock(block *types.WorkObject, inclTx bool, fullTx bool, nodeL
963969
return newRPCTransactionFromBlockHash(block, tx.Hash(), false, nodeLocation), nil
964970
}
965971
}
966-
txs := block.Transactions()
972+
txs := block.QuaiTransactions()
967973
transactions := make([]interface{}, len(txs))
968974
var err error
969975
for i, tx := range txs {
@@ -1303,14 +1309,13 @@ func (s *PublicBlockChainAPI) GetTransactionReceipt(ctx context.Context, hash co
13031309
}
13041310
usePending = true
13051311
blockNumber = s.b.CurrentHeader().NumberU64(s.b.NodeCtx()) + 1
1306-
blockHash = common.Hash{}
13071312
} else {
13081313
return nil, nil
13091314
}
13101315
}
13111316
receipt := &types.Receipt{}
13121317
if usePending {
1313-
receipt = s.b.GetPendingReceipt(hash)
1318+
receipt, blockHash = s.b.GetPendingReceipt(hash)
13141319
if receipt == nil {
13151320
return nil, nil
13161321
}

internal/quaiapi/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ type Backend interface {
6969
GetOutpointsByAddressAndRange(ctx context.Context, address common.Address, start, end uint32) ([]*types.OutpointAndDenomination, error)
7070
UTXOsByAddress(ctx context.Context, address common.Address) ([]*types.UtxoEntry, error)
7171
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
72-
GetPendingReceipt(txHash common.Hash) *types.Receipt
72+
GetPendingReceipt(txHash common.Hash) (*types.Receipt, common.Hash)
73+
PendingBlockByHash(blockHash common.Hash) *types.WorkObject
7374
GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.WorkObject, parent *types.WorkObject, vmConfig *vm.Config) (*vm.EVM, func() error, error)
7475
SetCurrentExpansionNumber(expansionNumber uint8)
7576
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription

internal/quaiapi/quai_api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,14 +1507,13 @@ func (s *PublicBlockChainQuaiAPI) GetTransactionReceipt(ctx context.Context, has
15071507
}
15081508
usePending = true
15091509
blockNumber = s.b.CurrentHeader().NumberU64(s.b.NodeCtx()) + 1
1510-
blockHash = common.Hash{}
15111510
} else {
15121511
return nil, nil
15131512
}
15141513
}
15151514
receipt := &types.Receipt{}
15161515
if usePending {
1517-
receipt = s.b.GetPendingReceipt(hash)
1516+
receipt, blockHash = s.b.GetPendingReceipt(hash)
15181517
if receipt == nil {
15191518
return nil, nil
15201519
}

quai/api_backend.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,18 @@ func (b *QuaiAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (typ
238238
return b.quai.core.GetReceiptsByHash(hash), nil
239239
}
240240

241-
func (b *QuaiAPIBackend) GetPendingReceipt(txHash common.Hash) *types.Receipt {
242-
pendingReceipts := b.quai.core.PendingReceipts()
241+
func (b *QuaiAPIBackend) GetPendingReceipt(txHash common.Hash) (*types.Receipt, common.Hash) {
242+
pendingBlock, pendingReceipts := b.quai.core.PendingBlockAndReceipts()
243243
for _, receipt := range pendingReceipts {
244244
if receipt.TxHash == txHash {
245-
return receipt
245+
return receipt, pendingBlock.Hash()
246246
}
247247
}
248-
return nil
248+
return nil, common.Hash{}
249+
}
250+
251+
func (b *QuaiAPIBackend) PendingBlockByHash(blockHash common.Hash) *types.WorkObject {
252+
return b.quai.core.PendingBlockByHash(blockHash)
249253
}
250254

251255
func (b *QuaiAPIBackend) GetPrimeBlock(blockHash common.Hash) *types.WorkObject {

0 commit comments

Comments
 (0)