From db875f1fc13c508a53f01c8589e0b39df3aabff4 Mon Sep 17 00:00:00 2001 From: vsmk98 Date: Mon, 16 Jul 2018 10:34:20 +0000 Subject: [PATCH] added changes to write to permissioned-nodes.json --- permissions/node_permissions.go | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/permissions/node_permissions.go b/permissions/node_permissions.go index 8ef13f7b5..33ee5037e 100644 --- a/permissions/node_permissions.go +++ b/permissions/node_permissions.go @@ -2,9 +2,11 @@ package permissions import ( "fmt" + "encoding/json" "io/ioutil" "math/big" "path/filepath" + "os" "strings" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -17,6 +19,9 @@ import ( "github.com/ethereum/go-ethereum/p2p" "gopkg.in/urfave/cli.v1" ) +const ( + PERMISSIONED_CONFIG = "permissioned-nodes.json" +) //This function first adds the node list from permissioned-nodes.json to //the permissiones contract deployed as a precompile via genesis.json @@ -28,8 +33,10 @@ func QuorumPermissioning(ctx *cli.Context, stack *node.Node ){ //call populate nodes to populate the nodes into contract populateNodesToContract (ctx, stack, e, stateReader) + dataDir := stack.DataDir() + //monitor for new nodes addition via smart contract - go monitorNewNodeAdd(stateReader) + go monitorNewNodeAdd(stateReader, dataDir) } //populates the nodes list from permissioned-nodes.json into the permissions @@ -82,7 +89,7 @@ func populateNodesToContract(ctx *cli.Context, stack *node.Node, e *eth.Ethereum //This functions listens on the channel for new node approval via smart contract and // adds the same into permissioned-nodes.json -func monitorNewNodeAdd(stateReader *ethclient.Client){ +func monitorNewNodeAdd(stateReader *ethclient.Client, dataDir string ){ permissions, err := NewPermissionsFilterer(params.QuorumPermissionsContract, stateReader) if err != nil { @@ -107,6 +114,7 @@ func monitorNewNodeAdd(stateReader *ethclient.Client){ // newEvent = <-ch var newEvent *PermissionsNewNodeProposed = <-ch log.Info("Found Node add event", "enodeId", newEvent.EnodeId) + populatePermissionedNodes(newEvent.EnodeId, dataDir) } } @@ -152,3 +160,44 @@ func getKeyFromKeyStore(ctx *cli.Context) string { return string(keyBlob[:n]) } +//this function populates the new node information into the permissioned-nodes.json file +func populatePermissionedNodes(enodeId string, dataDir string){ + log.Debug("populatePermissionedNodes", "DataDir", dataDir, "file", PERMISSIONED_CONFIG) + + path := filepath.Join(dataDir, PERMISSIONED_CONFIG) + if _, err := os.Stat(path); err != nil { + log.Error("Read Error for permissioned-nodes.json file. This is because 'permissioned' flag is specified but no permissioned-nodes.json file is present.", "err", err) + return + } + // Load the nodes from the config file + blob, err := ioutil.ReadFile(path) + log.Info("blob is before append: ","blob", blob) + if err != nil { + log.Error("populatePermissionedNodes: Failed to access nodes", "err", err) + return + } + + nodelist := []string{} + if err := json.Unmarshal(blob, &nodelist); err != nil { + log.Error("parsePermissionedNodes: Failed to load nodes", "err", err) + return + } + + log.Info("node list is: ","nodelist", nodelist) + + newEnodeId := "enode://" + enodeId + "@127.0.0.1:21009?discport=0&raftport=50402" + // newEnodeId = append(newEnodeId, enodeId) + // newEnodeId = append(newEnodeId, "@127.0.0.1:21009?discport=0&raftport=50402") + + nodelist = append(nodelist, newEnodeId) + + log.Info("node list is after append: ","nodelist", nodelist) + + blob, _ = json.Marshal(nodelist) + log.Info("blob is after append: ","blob", blob) + + if err:= ioutil.WriteFile(path, blob, 0644); err!= nil{ + log.Error("populatePermissionedNodes: Error writing new node info to file", "err", err) + } + +}