mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
adding 2 new apis: eth.getBlockSigners and eth.getBlockFinality
This commit is contained in:
parent
ee026fc506
commit
1cf653e158
3 changed files with 149 additions and 33 deletions
|
|
@ -608,6 +608,80 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A
|
||||||
return res[:], state.Error()
|
return res[:], state.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *PublicBlockChainAPI) GetBlockSignersByHash(ctx context.Context, blockHash common.Hash) ([]common.Address, error) {
|
||||||
|
block, err := s.b.GetBlock(ctx, blockHash)
|
||||||
|
if err != nil || block == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
masternodes, err := s.GetMasternodes(block, ctx)
|
||||||
|
if err != nil || len(masternodes) == 0 {
|
||||||
|
log.Error("Failed to get masternodes")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s.rpcOutputBlockSigners(block, ctx, masternodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PublicBlockChainAPI) GetBlockSignersByNumber(ctx context.Context, blockNumber rpc.BlockNumber) ([]common.Address, error) {
|
||||||
|
block, err := s.b.BlockByNumber(ctx, blockNumber)
|
||||||
|
if err != nil || block == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
masternodes, err := s.GetMasternodes(block, ctx)
|
||||||
|
if err != nil || len(masternodes) == 0 {
|
||||||
|
log.Error("Failed to get masternodes")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s.rpcOutputBlockSigners(block, ctx, masternodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PublicBlockChainAPI) GetBlockFinalityByHash(ctx context.Context, blockHash common.Hash) (int32, error) {
|
||||||
|
block, err := s.b.GetBlock(ctx, blockHash)
|
||||||
|
if err != nil || block == nil {
|
||||||
|
return int32(0), err
|
||||||
|
}
|
||||||
|
masternodes, err := s.GetMasternodes(block, ctx)
|
||||||
|
if err != nil || len(masternodes) == 0 {
|
||||||
|
log.Error("Failed to get masternodes")
|
||||||
|
return int32(0), err
|
||||||
|
}
|
||||||
|
blockSigners, err := s.rpcOutputBlockSigners(block, ctx, masternodes)
|
||||||
|
return int32(100 * len(blockSigners) / len(masternodes)), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PublicBlockChainAPI) GetBlockFinalityByNumber(ctx context.Context, blockNumber rpc.BlockNumber) (int32, error) {
|
||||||
|
block, err := s.b.BlockByNumber(ctx, blockNumber)
|
||||||
|
if err != nil || block == nil {
|
||||||
|
return int32(0), err
|
||||||
|
}
|
||||||
|
masternodes, err := s.GetMasternodes(block, ctx)
|
||||||
|
if err != nil || len(masternodes) == 0 {
|
||||||
|
log.Error("Failed to get masternodes")
|
||||||
|
return int32(0), err
|
||||||
|
}
|
||||||
|
blockSigners, err := s.rpcOutputBlockSigners(block, ctx, masternodes)
|
||||||
|
return int32(100 * len(blockSigners) / len(masternodes)), err
|
||||||
|
}
|
||||||
|
func (s *PublicBlockChainAPI) GetMasternodes(b *types.Block, ctx context.Context) ([]common.Address, error) {
|
||||||
|
var masternodes []common.Address
|
||||||
|
if b.Number().Int64() > 0 {
|
||||||
|
curBlockNumber := b.Number().Uint64()
|
||||||
|
prevBlockNumber := curBlockNumber + (common.MergeSignRange - (curBlockNumber % common.MergeSignRange))
|
||||||
|
latestBlockNumber := s.b.CurrentBlock().Number().Uint64()
|
||||||
|
if prevBlockNumber >= latestBlockNumber || !s.b.ChainConfig().IsTIP2019(b.Number()) {
|
||||||
|
prevBlockNumber = curBlockNumber
|
||||||
|
}
|
||||||
|
if engine, ok := s.b.GetEngine().(*posv.Posv); ok {
|
||||||
|
// Get block epoc latest.
|
||||||
|
lastCheckpointNumber := prevBlockNumber - (prevBlockNumber % s.b.ChainConfig().Posv.Epoch)
|
||||||
|
prevCheckpointBlock, _ := s.b.BlockByNumber(ctx, rpc.BlockNumber(lastCheckpointNumber))
|
||||||
|
if prevCheckpointBlock != nil {
|
||||||
|
masternodes = engine.GetMasternodesFromCheckpointHeader(prevCheckpointBlock.Header(), curBlockNumber, s.b.ChainConfig().Posv.Epoch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return masternodes, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CallArgs represents the arguments for a call.
|
// CallArgs represents the arguments for a call.
|
||||||
type CallArgs struct {
|
type CallArgs struct {
|
||||||
From common.Address `json:"from"`
|
From common.Address `json:"from"`
|
||||||
|
|
@ -854,16 +928,19 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
|
||||||
uncleHashes[i] = uncle.Hash()
|
uncleHashes[i] = uncle.Hash()
|
||||||
}
|
}
|
||||||
fields["uncles"] = uncleHashes
|
fields["uncles"] = uncleHashes
|
||||||
|
return fields, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *PublicBlockChainAPI) rpcOutputBlockSigners(b *types.Block, ctx context.Context, masternodes []common.Address) ([]common.Address, error) {
|
||||||
// Get signers for block.
|
// Get signers for block.
|
||||||
client, err := s.b.GetIPCClient()
|
client, err := s.b.GetIPCClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to connect IPC client for block status", "error", err)
|
log.Error("Fail to connect IPC client for block status", "error", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var signers []common.Address
|
var signers []common.Address
|
||||||
var filterSigners []common.Address
|
var filterSigners []common.Address
|
||||||
finality := int32(0)
|
|
||||||
if b.Number().Int64() > 0 {
|
if b.Number().Int64() > 0 {
|
||||||
curBlockNumber := b.Number().Uint64()
|
curBlockNumber := b.Number().Uint64()
|
||||||
prevBlockNumber := curBlockNumber + (common.MergeSignRange - (curBlockNumber % common.MergeSignRange))
|
prevBlockNumber := curBlockNumber + (common.MergeSignRange - (curBlockNumber % common.MergeSignRange))
|
||||||
|
|
@ -872,7 +949,11 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
|
||||||
prevBlockNumber = curBlockNumber
|
prevBlockNumber = curBlockNumber
|
||||||
}
|
}
|
||||||
if engine, ok := s.b.GetEngine().(*posv.Posv); ok {
|
if engine, ok := s.b.GetEngine().(*posv.Posv); ok {
|
||||||
prevBlock, _ := s.b.BlockByNumber(ctx, rpc.BlockNumber(prevBlockNumber))
|
prevBlock, err := s.b.BlockByNumber(ctx, rpc.BlockNumber(prevBlockNumber))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to get previous block", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
addrBlockSigner := common.HexToAddress(common.BlockSigners)
|
addrBlockSigner := common.HexToAddress(common.BlockSigners)
|
||||||
signers, err = contracts.GetSignersByExecutingEVM(addrBlockSigner, client, prevBlock.Hash())
|
signers, err = contracts.GetSignersByExecutingEVM(addrBlockSigner, client, prevBlock.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -883,29 +964,17 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
|
||||||
creator, _ := engine.RecoverSigner(b.Header())
|
creator, _ := engine.RecoverSigner(b.Header())
|
||||||
signers = append(signers, validator)
|
signers = append(signers, validator)
|
||||||
signers = append(signers, creator)
|
signers = append(signers, creator)
|
||||||
// Get block epoc latest.
|
for _, masternode := range masternodes {
|
||||||
lastCheckpointNumber := prevBlockNumber - (prevBlockNumber % s.b.ChainConfig().Posv.Epoch)
|
for _, signer := range signers {
|
||||||
prevCheckpointBlock, _ := s.b.BlockByNumber(ctx, rpc.BlockNumber(lastCheckpointNumber))
|
if signer == masternode {
|
||||||
if prevCheckpointBlock != nil {
|
filterSigners = append(filterSigners, masternode)
|
||||||
masternodes := engine.GetMasternodesFromCheckpointHeader(prevCheckpointBlock.Header(), curBlockNumber, s.b.ChainConfig().Posv.Epoch)
|
break
|
||||||
countFinality := 0
|
|
||||||
for _, masternode := range masternodes {
|
|
||||||
for _, signer := range signers {
|
|
||||||
if signer == masternode {
|
|
||||||
countFinality++
|
|
||||||
filterSigners = append(filterSigners, masternode)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finality = int32(countFinality * 100 / len(masternodes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fields["signers"] = filterSigners
|
return filterSigners, nil
|
||||||
fields["finality"] = finality
|
|
||||||
|
|
||||||
return fields, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
|
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -3840,6 +3840,22 @@ var outputBlockFormatter = function(block) {
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* Formats the output of a blockSigner list
|
||||||
|
*
|
||||||
|
* @method outputBlockFormatter
|
||||||
|
* @param {Object} blockSigners
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
var outputBlockSignersFormatter = function(blockSigners) {
|
||||||
|
if (utils.isArray(blockSigners)) {
|
||||||
|
blockSigners.forEach(function(item){
|
||||||
|
if(!utils.isString(item))
|
||||||
|
return formatOutputAddress(item);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return blockSigners;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the output of a log
|
* Formats the output of a log
|
||||||
|
|
@ -3958,6 +3974,7 @@ module.exports = {
|
||||||
outputTransactionFormatter: outputTransactionFormatter,
|
outputTransactionFormatter: outputTransactionFormatter,
|
||||||
outputTransactionReceiptFormatter: outputTransactionReceiptFormatter,
|
outputTransactionReceiptFormatter: outputTransactionReceiptFormatter,
|
||||||
outputBlockFormatter: outputBlockFormatter,
|
outputBlockFormatter: outputBlockFormatter,
|
||||||
|
outputBlockSignersFormatter: outputBlockSignersFormatter,
|
||||||
outputLogFormatter: outputLogFormatter,
|
outputLogFormatter: outputLogFormatter,
|
||||||
outputPostFormatter: outputPostFormatter,
|
outputPostFormatter: outputPostFormatter,
|
||||||
outputSyncingFormatter: outputSyncingFormatter
|
outputSyncingFormatter: outputSyncingFormatter
|
||||||
|
|
@ -5210,6 +5227,14 @@ var blockCall = function (args) {
|
||||||
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber";
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var blockSignersCall = function (args) {
|
||||||
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockSignersByHash" : "eth_getBlockSignersByNumber";
|
||||||
|
};
|
||||||
|
|
||||||
|
var blockFinalityCall = function (args) {
|
||||||
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockFinalityByHash" : "eth_getBlockFinalityByNumber";
|
||||||
|
};
|
||||||
|
|
||||||
var transactionFromBlockCall = function (args) {
|
var transactionFromBlockCall = function (args) {
|
||||||
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex';
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex';
|
||||||
};
|
};
|
||||||
|
|
@ -5297,6 +5322,22 @@ var methods = function () {
|
||||||
outputFormatter: formatters.outputBlockFormatter
|
outputFormatter: formatters.outputBlockFormatter
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var getBlockSigners = new Method({
|
||||||
|
name: 'getBlockSigners',
|
||||||
|
call: blockSignersCall,
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [formatters.inputBlockNumberFormatter],
|
||||||
|
outputFormatter: formatters.outputBlockSignersFormatter
|
||||||
|
});
|
||||||
|
|
||||||
|
var getBlockFinality = new Method({
|
||||||
|
name: 'getBlockFinality',
|
||||||
|
call: blockFinalityCall,
|
||||||
|
params: 1,
|
||||||
|
inputFormatter: [formatters.inputBlockNumberFormatter],
|
||||||
|
outputFormatter: formatters.formatOutputInt
|
||||||
|
});
|
||||||
|
|
||||||
var getUncle = new Method({
|
var getUncle = new Method({
|
||||||
name: 'getUncle',
|
name: 'getUncle',
|
||||||
call: uncleCall,
|
call: uncleCall,
|
||||||
|
|
@ -5436,6 +5477,8 @@ var methods = function () {
|
||||||
getStorageAt,
|
getStorageAt,
|
||||||
getCode,
|
getCode,
|
||||||
getBlock,
|
getBlock,
|
||||||
|
getBlockSigners,
|
||||||
|
getBlockFinality,
|
||||||
getUncle,
|
getUncle,
|
||||||
getCompilers,
|
getCompilers,
|
||||||
getBlockTransactionCount,
|
getBlockTransactionCount,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue