Skip to content

Commit

Permalink
Merge pull request #65 from ClarkChenc/add_get_snapshot_proposer
Browse files Browse the repository at this point in the history
Add GetSnapshotProposer and GetSnapshotProposerSequence
  • Loading branch information
bladehan1 authored Mar 6, 2024
2 parents 18eb478 + d829fbe commit a2c6046
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
50 changes: 50 additions & 0 deletions consensus/bor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions internal/web3ext/bor_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit a2c6046

Please sign in to comment.