Skip to content

Commit

Permalink
rebuild consensus and add setWeight function
Browse files Browse the repository at this point in the history
  • Loading branch information
subowei committed Sep 13, 2023
1 parent feb0fdf commit 16bd566
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 42 deletions.
2 changes: 1 addition & 1 deletion precompiled/consensus/Consensus.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.6.0;
pragma solidity >=0.6.10 <0.8.20;

contract ConsensusPrecompiled {
function addSealer(string memory,uint256) public returns (int32){}
Expand Down
189 changes: 154 additions & 35 deletions precompiled/consensus/consensus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions precompiled/consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (service *Service) AddObserver(nodeID string) (int64, error) {
return precompiled.DefaultErrorCode, fmt.Errorf("the node is already in the observer list")
}
}
_, receipt, err := service.consensus.AddObserver(service.consensusAuth, nodeID)
_, _, receipt, err := service.consensus.AddObserver(service.consensusAuth, nodeID)
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("ConsensusService addObserver failed: %+v", err)
}
Expand Down Expand Up @@ -132,11 +132,10 @@ func (service *Service) AddSealer(nodeID string, weight int64) (int64, error) {
}
}

tx, receipt, err := service.consensus.AddSealer(service.consensusAuth, nodeID, big.NewInt(weight))
_, _, receipt, err := service.consensus.AddSealer(service.consensusAuth, nodeID, big.NewInt(weight))
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("ConsensusService addSealer failed: %+v", err)
}
_ = tx
return parseReturnValue(receipt, "addSealer")
}

Expand Down Expand Up @@ -164,7 +163,7 @@ func (service *Service) RemoveNode(nodeID string) (int64, error) {
return precompiled.DefaultErrorCode, fmt.Errorf("the node is not a group peer")
}

_, receipt, err := service.consensus.Remove(service.consensusAuth, nodeID)
_, _, receipt, err := service.consensus.Remove(service.consensusAuth, nodeID)
// maybe will occur something wrong
// when request the receipt from the SDK since the connected node of SDK is removed
//TODO: how to handle the problem that can't get the tx receipt when remove the connected node of SDK
Expand Down Expand Up @@ -194,6 +193,25 @@ func (service *Service) isValidNodeID(nodeID string) (bool, error) {
return flag, nil
}

func (service *Service) SetWeight(nodeID string, weight int64) (int64, error) {
sealerRaw, err := service.client.GetSealerList(context.Background())
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("get the sealer list failed: %v", err)
}

var nodeIDs []nodeIdList
err = json.Unmarshal(sealerRaw, &nodeIDs)
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("unmarshal the sealer list failed: %v", err)
}

_, _, receipt, err := service.consensus.SetWeight(service.consensusAuth, nodeID, big.NewInt(weight))
if err != nil {
return precompiled.DefaultErrorCode, fmt.Errorf("ConsensusService setWeight failed: %+v", err)
}
return parseReturnValue(receipt, "setWeight")
}

func parseReturnValue(receipt *types.Receipt, name string) (int64, error) {
errorMessage := receipt.GetErrorMessage()
if errorMessage != "" {
Expand Down
Loading

0 comments on commit 16bd566

Please sign in to comment.