Skip to content

Commit

Permalink
Merge pull request #306 from uprendis/feature/opera-rpc-calls
Browse files Browse the repository at this point in the history
Extend Opera-specific API calls
  • Loading branch information
uprendis authored May 12, 2022
2 parents 8ade66a + 5df0764 commit a7fbcde
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
23 changes: 23 additions & 0 deletions ethapi/abft_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)

// PublicAbftAPI provides an API to access consensus related information.
Expand All @@ -18,6 +19,28 @@ func NewPublicAbftAPI(b Backend) *PublicAbftAPI {
return &PublicAbftAPI{b}
}

func (s *PublicAbftAPI) GetValidators(ctx context.Context, epoch rpc.BlockNumber) (map[hexutil.Uint64]interface{}, error) {
bs, es, err := s.b.GetEpochBlockState(ctx, epoch)
if err != nil {
return nil, err
}
if es == nil {
return nil, nil
}
res := map[hexutil.Uint64]interface{}{}
for _, vid := range es.Validators.IDs() {
profiles := es.ValidatorProfiles
if epoch == rpc.PendingBlockNumber {
profiles = bs.NextValidatorProfiles
}
res[hexutil.Uint64(vid)] = map[string]interface{}{
"weight": (*hexutil.Big)(profiles[vid].Weight),
"pubkey": profiles[vid].PubKey.String(),
}
}
return res, nil
}

// GetDowntime returns validator's downtime.
func (s *PublicAbftAPI) GetDowntime(ctx context.Context, validatorID hexutil.Uint) (map[string]interface{}, error) {
blocks, period, err := s.b.GetDowntime(ctx, idx.ValidatorID(validatorID))
Expand Down
26 changes: 26 additions & 0 deletions ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -680,6 +681,30 @@ func (s *PublicBlockChainAPI) CurrentEpoch(ctx context.Context) hexutil.Uint64 {
return hexutil.Uint64(s.b.CurrentEpoch(ctx))
}

// GetRules returns network rules for an epoch
func (s *PublicBlockChainAPI) GetRules(ctx context.Context, epoch rpc.BlockNumber) (*opera.Rules, error) {
_, es, err := s.b.GetEpochBlockState(ctx, epoch)
if err != nil {
return nil, err
}
if es == nil {
return nil, nil
}
return &es.Rules, nil
}

// GetEpochBlock returns block height in a beginning of an epoch
func (s *PublicBlockChainAPI) GetEpochBlock(ctx context.Context, epoch rpc.BlockNumber) (hexutil.Uint64, error) {
bs, _, err := s.b.GetEpochBlockState(ctx, epoch)
if err != nil {
return 0, err
}
if bs == nil {
return 0, nil
}
return hexutil.Uint64(bs.LastBlock.Idx), nil
}

// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config.
func (api *PublicBlockChainAPI) ChainId() (*hexutil.Big, error) {
// if current block is at or past the EIP-155 replay-protection fork block, return chainID from config
Expand Down Expand Up @@ -1260,6 +1285,7 @@ type extBlockApi struct {
func RPCMarshalHeader(head *evmcore.EvmHeader, ext extBlockApi) map[string]interface{} {
result := map[string]interface{}{
"number": (*hexutil.Big)(head.Number),
"epoch": hexutil.Uint64(hash.Event(head.Hash).Epoch()),
"hash": head.Hash, // store EvmBlock's hash in extra, because extra is always empty
"parentHash": head.ParentHash,
"nonce": types.BlockNonce{},
Expand Down
2 changes: 2 additions & 0 deletions ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/Fantom-foundation/go-opera/evmcore"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/inter/iblockproc"
)

// PeerProgress is synchronization status of a peer
Expand Down Expand Up @@ -96,6 +97,7 @@ type Backend interface {
SealedEpochTiming(ctx context.Context) (start inter.Timestamp, end inter.Timestamp)

// Lachesis aBFT API
GetEpochBlockState(ctx context.Context, epoch rpc.BlockNumber) (*iblockproc.BlockState, *iblockproc.EpochState, error)
GetDowntime(ctx context.Context, vid idx.ValidatorID) (idx.Block, inter.Timestamp, error)
GetUptime(ctx context.Context, vid idx.ValidatorID) (*big.Int, error)
GetOriginatedFee(ctx context.Context, vid idx.ValidatorID) (*big.Int, error)
Expand Down
13 changes: 13 additions & 0 deletions gossip/ethapi_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/Fantom-foundation/go-opera/evmcore"
"github.com/Fantom-foundation/go-opera/gossip/evmstore"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/inter/iblockproc"
"github.com/Fantom-foundation/go-opera/opera"
"github.com/Fantom-foundation/go-opera/topicsdb"
"github.com/Fantom-foundation/go-opera/tracing"
Expand Down Expand Up @@ -506,6 +507,18 @@ func (b *EthAPIBackend) GetDowntime(ctx context.Context, vid idx.ValidatorID) (i
return missedBlocks, missedTime, nil
}

func (b *EthAPIBackend) GetEpochBlockState(ctx context.Context, epoch rpc.BlockNumber) (*iblockproc.BlockState, *iblockproc.EpochState, error) {
if epoch == rpc.PendingBlockNumber {
bs, es := b.svc.store.GetBlockState(), b.svc.store.GetEpochState()
return &bs, &es, nil
}
if epoch == rpc.LatestBlockNumber {
epoch = rpc.BlockNumber(b.svc.store.GetEpoch())
}
bs, es := b.svc.store.GetHistoryBlockEpochState(idx.Epoch(epoch))
return bs, es, nil
}

func (b *EthAPIBackend) CalcBlockExtApi() bool {
return b.svc.config.RPCBlockExt
}
Expand Down
2 changes: 1 addition & 1 deletion gossip/evm_state_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *EvmStateReader) getBlock(h hash.Event, n idx.Block, readTxs bool) *evmc
}

// find block rules
epoch := r.store.FindBlockEpoch(n)
epoch := block.Atropos.Epoch()
es := r.store.GetHistoryEpochState(epoch)
var rules opera.Rules
if es != nil {
Expand Down
6 changes: 3 additions & 3 deletions inter/validatorpk/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ var Types = struct {
Secp256k1: 0xc0,
}

func (pk *PubKey) Empty() bool {
func (pk PubKey) Empty() bool {
return len(pk.Raw) == 0 && pk.Type == 0
}

func (pk *PubKey) String() string {
func (pk PubKey) String() string {
return "0x" + common.Bytes2Hex(pk.Bytes())
}

func (pk *PubKey) Bytes() []byte {
func (pk PubKey) Bytes() []byte {
return append([]byte{pk.Type}, pk.Raw...)
}

Expand Down

0 comments on commit a7fbcde

Please sign in to comment.