2020-10-12 14:17:34 +00:00
package commands
2020-10-14 15:59:42 +00:00
import (
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/eth"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
)
// BlockNumber returns the latest block number of the chain
2020-10-16 12:15:10 +00:00
func ( api * APIImpl ) BlockNumber ( _ context . Context ) ( hexutil . Uint64 , error ) {
2020-10-14 15:59:42 +00:00
execution , _ , err := stages . GetStageProgress ( api . dbReader , stages . Finish )
if err != nil {
return 0 , err
}
return hexutil . Uint64 ( execution ) , nil
}
// Syncing - we can return the progress of the very first stage as the highest block, and then the progress of the very last stage as the current block
2020-10-16 12:15:10 +00:00
func ( api * APIImpl ) Syncing ( _ context . Context ) ( interface { } , error ) {
2020-10-14 15:59:42 +00:00
highestBlock , _ , err := stages . GetStageProgress ( api . dbReader , stages . Headers )
if err != nil {
return false , err
}
currentBlock , _ , err := stages . GetStageProgress ( api . dbReader , stages . Finish )
if err != nil {
return false , err
}
// Return not syncing if the synchronisation already completed
if currentBlock >= highestBlock {
return false , nil
}
// Otherwise gather the block sync stats
return map [ string ] hexutil . Uint64 {
"currentBlock" : hexutil . Uint64 ( currentBlock ) ,
"highestBlock" : hexutil . Uint64 ( highestBlock ) ,
} , nil
}
// ChainId returns the chain id from the config
func ( api * APIImpl ) ChainId ( ctx context . Context ) ( hexutil . Uint64 , error ) {
tx , err := api . dbReader . Begin ( ctx )
if err != nil {
return 0 , err
}
defer tx . Rollback ( )
chainConfig := getChainConfig ( tx )
return hexutil . Uint64 ( chainConfig . ChainID . Uint64 ( ) ) , nil
}
// ProtocolVersion returns the chain id from the config
func ( api * APIImpl ) ProtocolVersion ( _ context . Context ) ( hexutil . Uint , error ) {
return hexutil . Uint ( eth . ProtocolVersions [ 0 ] ) , nil
}
// GasPrice returns a suggestion for a gas price.
2020-10-16 12:15:10 +00:00
func ( api * APIImpl ) GasPrice ( _ context . Context ) ( * hexutil . Big , error ) {
2020-10-14 15:59:42 +00:00
return nil , fmt . Errorf ( NotImplemented , "eth_getPrice" )
// price, err := eth.SuggestPrice(ctx)
// return (*hexutil.Big)(price), err
}