From d829fbef03b2e4452a834f8a2d6164ecd545e149 Mon Sep 17 00:00:00 2001 From: clarkchenc <651044554@qq.com> Date: Thu, 24 Aug 2023 17:26:22 +0800 Subject: [PATCH] add GetSnapshotProposer and GetSnapshotProposerSequence --- consensus/bor/api.go | 50 +++++++++++++++++++++++++++++++++++++ internal/web3ext/bor_ext.go | 12 +++++++++ 2 files changed, 62 insertions(+) diff --git a/consensus/bor/api.go b/consensus/bor/api.go index 12841290a..f7f4de184 100644 --- a/consensus/bor/api.go +++ b/consensus/bor/api.go @@ -63,6 +63,56 @@ func (api *API) GetAuthor(number *rpc.BlockNumber) (*common.Address, error) { return &author, err } +type BlockSigners struct { + Signers map[common.Address]uint64 + Diff int + Author common.Address +} + +// GetSnapshotProposerSequence retrieves the in-turn signers of all sprints in a span +func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigners, error) { + + var difficulties = make(map[common.Address]uint64) + snap, err := api.GetSnapshot(number) + if err != nil { + return BlockSigners{}, err + } + proposer := snap.ValidatorSet.GetProposer().Address + proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer) + + signers := snap.signers() + for i := 0; i < len(signers); i++ { + tempIndex := i + if tempIndex < proposerIndex { + tempIndex = tempIndex + len(signers) + } + difficulties[signers[i]] = uint64(len(signers) - (tempIndex - proposerIndex)) + } + + author, err := api.GetAuthor(number) + if err != nil { + return BlockSigners{}, err + } + diff := int(difficulties[*author]) + blockSigners := &BlockSigners{ + Signers: difficulties, + Diff: diff, + Author: *author, + } + + return *blockSigners, nil +} + +// GetSnapshotProposer retrieves the in-turn signer at a given block. +func (api *API) GetSnapshotProposer(number *rpc.BlockNumber) (common.Address, error) { + *number -= 1 + snap, err := api.GetSnapshot(number) + if err != nil { + return common.Address{}, err + } + return snap.ValidatorSet.GetProposer().Address, nil +} + // GetSnapshotAtHash retrieves the state snapshot at a given block. func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) { header := api.chain.GetHeaderByHash(hash) diff --git a/internal/web3ext/bor_ext.go b/internal/web3ext/bor_ext.go index b7fe9b488..fe8b01da1 100644 --- a/internal/web3ext/bor_ext.go +++ b/internal/web3ext/bor_ext.go @@ -17,6 +17,18 @@ web3._extend({ params: 1, inputFormatter: [null] }), + new web3._extend.Method({ + name: 'getSnapshotProposer', + call: 'bor_getSnapshotProposer', + params: 1, + inputFormatter: [null] + }), + new web3._extend.Method({ + name: 'getSnapshotProposerSequence', + call: 'bor_getSnapshotProposerSequence', + params: 1, + inputFormatter: [null] + }), new web3._extend.Method({ name: 'getSnapshotAtHash', call: 'bor_getSnapshotAtHash',