Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions consensus/parlia/stakehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,50 @@ import (

// GetValidators retrieves validators from the StakeHubContract
// It returns operator addresses, credit addresses, and total length of validators
func (p *Parlia) GetValidators(blockNumber uint64, offset, limit *big.Int) ([]common.Address, []common.Address, *big.Int, error) {
log.Debug("Getting validators", "block", blockNumber, "offset", offset, "limit", limit)
func (p *Parlia) GetValidators(offset, limit *big.Int) ([]common.Address, []common.Address, *big.Int, error) {
log.Debug("Getting validators from latest block", "offset", offset, "limit", limit)

// Create the call data for getValidators
data, err := p.stakeHubABI.Pack("getValidators", offset, limit)
if err != nil {
log.Error("Failed to pack getValidators", "error", err)
log.Error("Failed to pack stakehub getValidators", "error", err)
return nil, nil, nil, fmt.Errorf("failed to pack getValidators: %v", err)
}

// Make the call
blockNr := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockNumber))
blockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
msgData := (hexutil.Bytes)(data)
toAddress := common.HexToAddress(systemcontracts.StakeHubContract)
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))

log.Debug("Calling getValidators", "block", blockNumber, "to", toAddress)
log.Debug("Calling getValidators from latest block", "to", toAddress)
result, err := p.ethAPI.Call(context.Background(), ethapi.TransactionArgs{
Gas: &gas,
To: &toAddress,
Data: &msgData,
}, &blockNr, nil, nil)
if err != nil {
log.Error("Failed to call getValidators", "error", err)
return nil, nil, nil, fmt.Errorf("failed to call getValidators: %v", err)
log.Error("Failed to call stakehub getValidators", "error", err)
return nil, nil, nil, fmt.Errorf("failed to call stakehub getValidators: %v", err)
}

// Unpack the result
var operatorAddrs []common.Address
var creditAddrs []common.Address
var totalLength *big.Int
if err := p.stakeHubABI.UnpackIntoInterface(&[]interface{}{&operatorAddrs, &creditAddrs, &totalLength}, "getValidators", result); err != nil {
log.Error("Failed to unpack getValidators result", "error", err)
log.Error("Failed to unpack stakehub getValidators result", "error", err)
return nil, nil, nil, fmt.Errorf("failed to unpack getValidators result: %v", err)
}

log.Debug("Successfully retrieved validators", "operators", len(operatorAddrs), "credits", len(creditAddrs), "total", totalLength)
log.Debug("Successfully retrieved stakehub validators", "operators", len(operatorAddrs), "credits", len(creditAddrs), "total", totalLength)
return operatorAddrs, creditAddrs, totalLength, nil
}

// getNodeIDsForValidators retrieves node IDs for the given validators
// It returns a map of consensus addresses to their node IDs
func (p *Parlia) getNodeIDsForValidators(blockNumber uint64, validatorsToQuery []common.Address) (map[common.Address][]enode.ID, error) {
log.Debug("Listing node IDs for validators", "block", blockNumber, "validators", len(validatorsToQuery))
func (p *Parlia) getNodeIDsForValidators(validatorsToQuery []common.Address) (map[common.Address][]enode.ID, error) {
log.Debug("Listing node IDs for validators from latest block", "validators", len(validatorsToQuery))

// Create the call data for getNodeIDs
data, err := p.stakeHubABI.Pack("getNodeIDs", validatorsToQuery)
Expand All @@ -72,12 +72,12 @@ func (p *Parlia) getNodeIDsForValidators(blockNumber uint64, validatorsToQuery [
}

// Make the call
blockNr := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockNumber))
blockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
msgData := (hexutil.Bytes)(data)
toAddress := common.HexToAddress(systemcontracts.StakeHubContract)
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))

log.Debug("Calling getNodeIDs", "block", blockNumber, "to", toAddress)
log.Debug("Calling getNodeIDs from latest block", "to", toAddress)
result, err := p.ethAPI.Call(context.Background(), ethapi.TransactionArgs{
Gas: &gas,
To: &toAddress,
Expand Down Expand Up @@ -110,20 +110,16 @@ func (p *Parlia) getNodeIDsForValidators(blockNumber uint64, validatorsToQuery [

// GetNodeIDs returns a flattened array of all node IDs for current validators
func (p *Parlia) GetNodeIDs() ([]enode.ID, error) {
// Get latest block number
block := p.ethAPI.BlockNumber()
log.Debug("Getting all node IDs", "block", block)

// Call GetValidators with latest block number
operatorAddrs, _, _, err := p.GetValidators(uint64(block), big.NewInt(0), big.NewInt(1000))
operatorAddrs, _, _, err := p.GetValidators(big.NewInt(0), big.NewInt(1000))
if err != nil {
log.Error("Failed to get validators", "error", err)
return nil, fmt.Errorf("failed to get validators: %v", err)
}
log.Debug("Retrieved validators", "count", len(operatorAddrs))

// Get node IDs for validators
nodeIDs, err := p.getNodeIDsForValidators(uint64(block), operatorAddrs)
nodeIDs, err := p.getNodeIDsForValidators(operatorAddrs)
if err != nil {
log.Error("Failed to get node IDs", "error", err)
return nil, fmt.Errorf("failed to get node IDs: %v", err)
Expand Down Expand Up @@ -200,20 +196,16 @@ func (p *Parlia) AddNodeIDs(nodeIDs []enode.ID, nonce uint64) (*types.Transactio

// GetNodeIDsMap returns a map of consensus addresses to their node IDs for all current validators
func (p *Parlia) GetNodeIDsMap() (map[common.Address][]enode.ID, error) {
// Get latest block number
block := p.ethAPI.BlockNumber()
log.Debug("Getting node IDs map", "block", block)

// Call GetValidators with latest block number
operatorAddrs, _, _, err := p.GetValidators(uint64(block), big.NewInt(0), big.NewInt(1000))
operatorAddrs, _, _, err := p.GetValidators(big.NewInt(0), big.NewInt(1000))
if err != nil {
log.Error("Failed to get validators", "error", err)
return nil, fmt.Errorf("failed to get validators: %v", err)
}
log.Debug("Retrieved validators", "count", len(operatorAddrs))

// Get node IDs for validators
nodeIDsMap, err := p.getNodeIDsForValidators(uint64(block), operatorAddrs)
nodeIDsMap, err := p.getNodeIDsForValidators(operatorAddrs)
if err != nil {
log.Error("Failed to get node IDs", "error", err)
return nil, fmt.Errorf("failed to get node IDs: %v", err)
Expand Down
12 changes: 8 additions & 4 deletions eth/protocols/bsc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
)

Expand Down Expand Up @@ -48,17 +49,19 @@ func (d *Dispatcher) GenRequestID() uint64 {

// DispatchRequest send the request, and block until the later response
func (d *Dispatcher) DispatchRequest(req *Request) (interface{}, error) {
// record the request before sending
d.mu.Lock()
d.requests[req.requestID] = req
d.mu.Unlock()

log.Debug("send BlocksByRange request", "code", req.code, "requestId", req.requestID)
err := p2p.Send(d.peer.rw, req.code, req.data)
if err != nil {
return nil, err
}
req.resCh = make(chan interface{}, 1)
req.cancelCh = make(chan string, 1)

d.mu.Lock()
d.requests[req.requestID] = req
d.mu.Unlock()

// clean the requests when the request is done
defer func() {
d.mu.Lock()
Expand Down Expand Up @@ -90,6 +93,7 @@ func (d *Dispatcher) getRequestByResp(res *Response) (*Request, error) {
if req.want != res.code {
return nil, fmt.Errorf("response mismatch: %d != %d", res.code, req.want)
}
log.Debug("get the request, then clean it", "requestId", req.requestID)
delete(d.requests, req.requestID)
return req, nil
}
Expand Down
4 changes: 2 additions & 2 deletions eth/protocols/bsc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func handleGetBlocksByRange(backend Backend, msg Decoder, peer *Peer) error {
blocks = append(blocks, NewBlockData(block))
}

log.Trace("reply GetBlocksByRange msg", "from", peer.id, "req", req.Count, "blocks", len(blocks))
log.Debug("reply GetBlocksByRange msg", "from", peer.id, "req", req.Count, "blocks", len(blocks))
return p2p.Send(peer.rw, BlocksByRangeMsg, &BlocksByRangePacket{
RequestId: req.RequestId,
Blocks: blocks,
Expand All @@ -192,7 +192,7 @@ func handleBlocksByRange(backend Backend, msg Decoder, peer *Peer) error {
data: res,
code: BlocksByRangeMsg,
})
log.Debug("receive BlocksByRange response", "from", peer.id, "blocks", len(res.Blocks), "err", err)
log.Debug("receive BlocksByRange response", "from", peer.id, "requestId", res.RequestId, "blocks", len(res.Blocks), "err", err)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions eth/protocols/bsc/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (p *Peer) RequestBlocksByRange(startHeight uint64, startHash common.Hash, c
},
timeout: 400 * time.Millisecond,
})
log.Debug("RequestBlocksByRange result", "requestID", requestID, "ret", res == nil, "err", err)
if err != nil {
return nil, err
}
Expand Down