2020-10-16 12:15:10 +00:00
package commands
import (
"context"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/core/forkid"
2020-10-25 08:38:55 +00:00
"github.com/ledgerwatch/turbo-geth/ethdb"
2020-10-16 12:15:10 +00:00
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/ledgerwatch/turbo-geth/turbo/rpchelper"
)
2020-10-24 17:03:52 +00:00
// Forks is a data type to record a list of forks passed by this node
2020-10-16 12:15:10 +00:00
type Forks struct {
GenesisHash common . Hash ` json:"genesis" `
Passed [ ] uint64 ` json:"passed" `
Next * uint64 ` json:"next,omitempty" `
}
2020-10-24 17:03:52 +00:00
// Forks implements tg_forks. Returns the genesis block hash and a sorted list of already passed fork block numbers as well as the next fork block (if applicable)
2020-10-18 19:44:28 +00:00
func ( api * TgImpl ) Forks ( ctx context . Context , blockNrOrHash rpc . BlockNumberOrHash ) ( Forks , error ) {
2020-10-16 12:15:10 +00:00
blockNumber , _ , err := rpchelper . GetBlockNumber ( blockNrOrHash , api . dbReader )
if err != nil {
return Forks { } , err
}
2020-10-25 08:38:55 +00:00
tx , err := api . dbReader . Begin ( ctx , ethdb . RO )
2020-10-16 12:15:10 +00:00
if err != nil {
return Forks { } , err
}
defer tx . Rollback ( )
2020-10-24 06:57:09 +00:00
chainConfig , genesisHash , err := getChainConfigWithGenesis ( tx )
if err != nil {
return Forks { } , err
}
2020-10-16 12:15:10 +00:00
forksBlocks := forkid . GatherForks ( chainConfig )
lastAddedIdx := - 1
passedForks := make ( [ ] uint64 , 0 , len ( forksBlocks ) )
for i , num := range forksBlocks {
if num <= blockNumber {
passedForks = append ( passedForks , num )
lastAddedIdx = i
}
}
var nextFork * uint64
if len ( forksBlocks ) > lastAddedIdx + 1 {
nextFork = new ( uint64 )
* nextFork = forksBlocks [ lastAddedIdx + 1 ]
}
return Forks { genesisHash , passedForks , nextFork } , nil
}