From abb0dcc48b7bac513ee1640090257848bd4785f2 Mon Sep 17 00:00:00 2001 From: span14 Date: Tue, 29 Nov 2022 17:39:16 -0500 Subject: [PATCH 1/4] add confirmed loopup --- eth/api_backend.go | 37 +++++++++++++++++++++++++++++++++++-- eth/backend.go | 6 +++++- rpc/types.go | 11 +++++++---- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 7255c826bc..86fff24c1c 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -56,8 +56,9 @@ import ( // EthApiBackend implements ethapi.Backend for full nodes type EthApiBackend struct { - eth *Ethereum - gpo *gasprice.Oracle + eth *Ethereum + gpo *gasprice.Oracle + XDPoS *XDPoS.XDPoS } func (b *EthApiBackend) ChainConfig() *params.ChainConfig { @@ -81,6 +82,22 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum // Otherwise resolve and return the block if blockNr == rpc.LatestBlockNumber { return b.eth.blockchain.CurrentBlock().Header(), nil + } else if blockNr == rpc.ConfirmedBlockNumber { + if b.eth.chainConfig.XDPoS == nil { + return nil, errors.New("PoW does not support confirmed block loopup") + } + current := b.eth.blockchain.CurrentBlock().Header() + if b.eth.blockchain.Config().XDPoS.BlockConsensusVersion( + current.Number, + current.Extra, + XDPoS.ExtraFieldCheck, + ) == params.ConsensusEngineVersion2 { + // TO CHECK: why calling config in XDPoS is blocked (not field and method) + confirmedHash := b.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash + return b.eth.blockchain.GetHeaderByHash(confirmedHash), nil + } else { + return nil, errors.New("PoS V1 does not support confirmed block loopup") + } } return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil } @@ -93,6 +110,22 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb // Otherwise resolve and return the block if blockNr == rpc.LatestBlockNumber { return b.eth.blockchain.CurrentBlock(), nil + } else if blockNr == rpc.ConfirmedBlockNumber { + if b.eth.chainConfig.XDPoS == nil { + return nil, errors.New("PoW does not support confirmed block loopup") + } + current := b.eth.blockchain.CurrentBlock().Header() + if b.eth.blockchain.Config().XDPoS.BlockConsensusVersion( + current.Number, + current.Extra, + XDPoS.ExtraFieldCheck, + ) == params.ConsensusEngineVersion2 { + // TO CHECK: why calling config in XDPoS is blocked (not field and method) + confirmedHash := b.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash + return b.eth.blockchain.GetBlockByHash(confirmedHash), nil + } else { + return nil, errors.New("PoS V1 does not support confirmed block loopup") + } } return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil } diff --git a/eth/backend.go b/eth/backend.go index b08219cf3e..a3f09ccac0 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -216,7 +216,11 @@ func New(ctx *node.ServiceContext, config *Config, XDCXServ *XDCx.XDCX, lendingS eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, ctx.GetConfig().AnnounceTxs) eth.miner.SetExtra(makeExtraData(config.ExtraData)) - eth.ApiBackend = &EthApiBackend{eth, nil} + if eth.chainConfig.XDPoS != nil { + eth.ApiBackend = &EthApiBackend{eth, nil, eth.engine.(*XDPoS.XDPoS)} + } else { + eth.ApiBackend = &EthApiBackend{eth, nil, nil} + } gpoParams := config.GPO if gpoParams.Default == nil { gpoParams.Default = config.GasPrice diff --git a/rpc/types.go b/rpc/types.go index e3cd435314..6aa7d239b9 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -120,10 +120,11 @@ type BlockNumber int64 type EpochNumber int64 const ( - PendingBlockNumber = BlockNumber(-2) - LatestBlockNumber = BlockNumber(-1) - EarliestBlockNumber = BlockNumber(0) - LatestEpochNumber = EpochNumber(-1) + ConfirmedBlockNumber = BlockNumber(-3) + PendingBlockNumber = BlockNumber(-2) + LatestBlockNumber = BlockNumber(-1) + EarliestBlockNumber = BlockNumber(0) + LatestEpochNumber = EpochNumber(-1) ) // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: @@ -144,6 +145,8 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { case "pending": *bn = PendingBlockNumber return nil + case "confirmed": + *bn = ConfirmedBlockNumber } blckNum, err := hexutil.DecodeUint64(input) From c637c7b915932b451845fee46e3ecf1b9053f678 Mon Sep 17 00:00:00 2001 From: span14 Date: Wed, 30 Nov 2022 23:21:35 -0500 Subject: [PATCH 2/4] fix switch case issue --- rpc/types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rpc/types.go b/rpc/types.go index 6aa7d239b9..57f13e0354 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -128,7 +128,7 @@ const ( ) // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: -// - "latest", "earliest" or "pending" as string arguments +// - "latest", "earliest", "pending" and "confirmed" as string arguments // - the block number // Returned errors: // - an invalid block number error when the given argument isn't a known strings @@ -147,6 +147,7 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error { return nil case "confirmed": *bn = ConfirmedBlockNumber + return nil } blckNum, err := hexutil.DecodeUint64(input) From 5aff0d35db6e3e16ef58ca300195214b186cb0cb Mon Sep 17 00:00:00 2001 From: span14 Date: Wed, 7 Dec 2022 16:32:42 -0500 Subject: [PATCH 3/4] fix typo --- eth/api_backend.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 86fff24c1c..cf0da7e036 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -84,7 +84,7 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum return b.eth.blockchain.CurrentBlock().Header(), nil } else if blockNr == rpc.ConfirmedBlockNumber { if b.eth.chainConfig.XDPoS == nil { - return nil, errors.New("PoW does not support confirmed block loopup") + return nil, errors.New("PoW does not support confirmed block lookup") } current := b.eth.blockchain.CurrentBlock().Header() if b.eth.blockchain.Config().XDPoS.BlockConsensusVersion( @@ -96,7 +96,7 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum confirmedHash := b.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash return b.eth.blockchain.GetHeaderByHash(confirmedHash), nil } else { - return nil, errors.New("PoS V1 does not support confirmed block loopup") + return nil, errors.New("PoS V1 does not support confirmed block lookup") } } return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil From eac0e4258405bbb000b79d4bdcce5bb757b647e3 Mon Sep 17 00:00:00 2001 From: span14 Date: Thu, 2 Feb 2023 12:13:16 -0500 Subject: [PATCH 4/4] fix typo --- eth/api_backend.go | 4 ++-- light/txpool.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index cf0da7e036..7669755277 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -112,7 +112,7 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb return b.eth.blockchain.CurrentBlock(), nil } else if blockNr == rpc.ConfirmedBlockNumber { if b.eth.chainConfig.XDPoS == nil { - return nil, errors.New("PoW does not support confirmed block loopup") + return nil, errors.New("PoW does not support confirmed block lookup") } current := b.eth.blockchain.CurrentBlock().Header() if b.eth.blockchain.Config().XDPoS.BlockConsensusVersion( @@ -124,7 +124,7 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb confirmedHash := b.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash return b.eth.blockchain.GetBlockByHash(confirmedHash), nil } else { - return nil, errors.New("PoS V1 does not support confirmed block loopup") + return nil, errors.New("PoS V1 does not support confirmed block lookup") } } return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil diff --git a/light/txpool.go b/light/txpool.go index 5fabb78989..27b749bf4d 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -179,7 +179,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number } // If some transactions have been mined, write the needed data to disk and update if list != nil { - // Retrieve all the receipts belonging to this block and write the loopup table + // Retrieve all the receipts belonging to this block and write the lookup table if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results return err }