mirror of https://github.com/poanetwork/quorum.git
Added monitoring for node deletion via smart contract
This commit is contained in:
parent
ad2c2c8964
commit
d29dc2e6b8
|
@ -34,6 +34,9 @@ func QuorumPermissioning(ctx *cli.Context, stack *node.Node ){
|
||||||
|
|
||||||
//monitor for new nodes addition via smart contract
|
//monitor for new nodes addition via smart contract
|
||||||
go monitorNewNodeAdd(stack, stateReader)
|
go monitorNewNodeAdd(stack, stateReader)
|
||||||
|
|
||||||
|
//monitor for nodes deletiin via smart contract
|
||||||
|
go monitorNodeDelete(stack, stateReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
//populates the nodes list from permissioned-nodes.json into the permissions
|
//populates the nodes list from permissioned-nodes.json into the permissions
|
||||||
|
@ -101,16 +104,46 @@ func monitorNewNodeAdd(stack *node.Node, stateReader *ethclient.Client){
|
||||||
var blockNumber uint64 = 1
|
var blockNumber uint64 = 1
|
||||||
opts.Start = &blockNumber
|
opts.Start = &blockNumber
|
||||||
|
|
||||||
|
var operation string = "ADD"
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, err = permissions.WatchNewNodeProposed(opts, ch)
|
_, err = permissions.WatchNewNodeProposed(opts, ch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Failed NewNodeProposed: %v", err)
|
log.Info("Failed NewNodeProposed: %v", err)
|
||||||
}
|
}
|
||||||
var newEvent *PermissionsNewNodeProposed = <-ch
|
var newEvent *PermissionsNewNodeProposed = <-ch
|
||||||
populatePermissionedNodes(newEvent.EnodeId, datadir)
|
updatePermissionedNodes(newEvent.EnodeId, datadir, operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This functions listens on the channel for new node approval via smart contract and
|
||||||
|
// adds the same into permissioned-nodes.json
|
||||||
|
func monitorNodeDelete(stack *node.Node, stateReader *ethclient.Client){
|
||||||
|
|
||||||
|
permissions, err := NewPermissionsFilterer(params.QuorumPermissionsContract, stateReader)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to instantiate a Permissions Filterer: %v", err)
|
||||||
|
}
|
||||||
|
datadir := stack.DataDir()
|
||||||
|
|
||||||
|
ch := make(chan *PermissionsNodeDeactivated)
|
||||||
|
|
||||||
|
opts := &bind.WatchOpts{}
|
||||||
|
var blockNumber uint64 = 1
|
||||||
|
opts.Start = &blockNumber
|
||||||
|
|
||||||
|
var operation string = "DEL"
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, err = permissions.WatchNodeDeactivated(opts, ch)
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Failed NodeDeactivated: %v", err)
|
||||||
|
}
|
||||||
|
var newEvent *PermissionsNodeDeactivated = <-ch
|
||||||
|
|
||||||
|
updatePermissionedNodes(newEvent.EnodeId, datadir, operation)
|
||||||
|
}
|
||||||
|
}
|
||||||
//Create an RPC client for the contract interface
|
//Create an RPC client for the contract interface
|
||||||
func createEthClient(stack *node.Node ) (*eth.Ethereum, *ethclient.Client){
|
func createEthClient(stack *node.Node ) (*eth.Ethereum, *ethclient.Client){
|
||||||
var e *eth.Ethereum
|
var e *eth.Ethereum
|
||||||
|
@ -154,8 +187,8 @@ func getKeyFromKeyStore(ctx *cli.Context) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
//this function populates the new node information into the permissioned-nodes.json file
|
//this function populates the new node information into the permissioned-nodes.json file
|
||||||
func populatePermissionedNodes(enodeId string, dataDir string){
|
func updatePermissionedNodes(enodeId string, dataDir string, operation string){
|
||||||
log.Debug("populatePermissionedNodes", "DataDir", dataDir, "file", PERMISSIONED_CONFIG)
|
log.Debug("updatePermissionedNodes", "DataDir", dataDir, "file", PERMISSIONED_CONFIG)
|
||||||
|
|
||||||
path := filepath.Join(dataDir, PERMISSIONED_CONFIG)
|
path := filepath.Join(dataDir, PERMISSIONED_CONFIG)
|
||||||
if _, err := os.Stat(path); err != nil {
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
@ -164,26 +197,37 @@ func populatePermissionedNodes(enodeId string, dataDir string){
|
||||||
}
|
}
|
||||||
// Load the nodes from the config file
|
// Load the nodes from the config file
|
||||||
blob, err := ioutil.ReadFile(path)
|
blob, err := ioutil.ReadFile(path)
|
||||||
log.Info("blob is before append: ","blob", blob)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("populatePermissionedNodes: Failed to access nodes", "err", err)
|
log.Error("updatePermissionedNodes: Failed to access permissioned-nodes.json", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nodelist := []string{}
|
nodelist := []string{}
|
||||||
if err := json.Unmarshal(blob, &nodelist); err != nil {
|
if err := json.Unmarshal(blob, &nodelist); err != nil {
|
||||||
log.Error("parsePermissionedNodes: Failed to load nodes", "err", err)
|
log.Error("parsePermissionedNodes: Failed to load nodes list", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: currently the ip, discpot and raft port are hard coded. Need to enhance the
|
// HACK: currently the ip, discpot and raft port are hard coded. Need to enhance the
|
||||||
//contract to pass these variables as part of the event and change this
|
//contract to pass these variables as part of the event and change this
|
||||||
newEnodeId := "enode://" + enodeId + "@127.0.0.1:21005?discport=0&raftport=50406"
|
newEnodeId := "enode://" + enodeId + "@127.0.0.1:21005?discport=0&raftport=50406"
|
||||||
|
|
||||||
|
if (operation == "ADD"){
|
||||||
nodelist = append(nodelist, newEnodeId)
|
nodelist = append(nodelist, newEnodeId)
|
||||||
|
} else {
|
||||||
|
index := 0
|
||||||
|
for i, enodeId := range nodelist {
|
||||||
|
if (enodeId == newEnodeId){
|
||||||
|
index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nodelist = append(nodelist[:index], nodelist[index+1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
blob, _ = json.Marshal(nodelist)
|
blob, _ = json.Marshal(nodelist)
|
||||||
if err:= ioutil.WriteFile(path, blob, 0644); err!= nil{
|
if err:= ioutil.WriteFile(path, blob, 0644); err!= nil{
|
||||||
log.Error("populatePermissionedNodes: Error writing new node info to file", "err", err)
|
log.Error("updatePermissionedNodes: Error writing new node info to file", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue