/
blockchain_network_config.go
145 lines (125 loc) · 4.72 KB
/
blockchain_network_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package config
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"io/ioutil"
)
type NetworkSelected struct {
NetworkName string
EthereumJSONRPCEndpoint string
NetworkId string
RegistryAddressKey string
}
const (
BlockChainNetworkFileName = "resources/blockchain_network_config.json"
EthereumJsonRpcEndpointKey = "ethereum_json_rpc_endpoint"
NetworkId = "network_id"
RegistryAddressKey = "registry_address_key"
)
var networkSelected = &NetworkSelected{}
var networkIdNameMapping string
var registryAddressJson string
func determineNetworkSelected(data []byte) (err error) {
dynamicBinding := map[string]interface{}{}
networkName := GetString(BlockChainNetworkSelected)
if err = json.Unmarshal(data, &dynamicBinding); err != nil {
return err
}
//Get the Network Name selected in config ( snetd.config.json) , Based on this retrieve the Registry address ,
//Ethereum End point and Network ID mapped to
networkSelected.NetworkName= networkName
networkSelected.RegistryAddressKey= getDetailsFromJsonOrConfig(dynamicBinding[networkName].(map[string]interface{})[RegistryAddressKey],RegistryAddressKey)
networkSelected.EthereumJSONRPCEndpoint= getDetailsFromJsonOrConfig(dynamicBinding[networkName].(map[string]interface{})[EthereumJsonRpcEndpointKey],EthereumJsonRpcEndpointKey)
networkSelected.NetworkId= fmt.Sprintf("%v", dynamicBinding[networkName].(map[string]interface{})[NetworkId])
return err
}
//Check if the value set in the config file, if yes, then we use it as is
//else we derive the value from the JSON parsed
func getDetailsFromJsonOrConfig(details interface{},configName string) string {
if len(GetString(configName))> 0 {
return GetString(configName)
}
if details == nil {
return ""
}
return fmt.Sprintf("%v", details)
}
//Get the Network ID associated with the network selected
func GetNetworkId() string {
return networkSelected.NetworkId
}
//Get the block chain end point associated with the Network selected
func GetBlockChainEndPoint() string {
return networkSelected.EthereumJSONRPCEndpoint
}
//Get the Registry address of the contract
func GetRegistryAddress() string {
return networkSelected.RegistryAddressKey
}
//Read the Registry address from JSON file passed
func setRegistryAddress(fileName string) (err error) {
var data []byte
//if address is already set in the config file and has been initialized , then skip the setting process
if len(networkSelected.RegistryAddressKey) > 0 {
return
}
//This value will be set when the binary across multiple platforms is built
if len(registryAddressJson) > 0 {
data = []byte (registryAddressJson)
} else {
//this is only for your local set up and testing
//This is only for local set up testing
if data, err = ReadFromFile(fileName); err != nil {
return fmt.Errorf("cannot parse the JSON file at %v to determine registry address of %v network, the error is :%v",
fileName, GetString(BlockChainNetworkSelected), err)
}
}
if err = deriveDatafromJSON(data); err != nil {
return err
}
return nil
}
func deriveDatafromJSON(data []byte) (err error) {
m := map[string]interface{}{}
err = json.Unmarshal(data, &m)
if err != nil {
return fmt.Errorf("cannot parse the Registry JSON file for the network %v , the error is : %v",
GetString(BlockChainNetworkSelected), err)
}
if m[GetNetworkId()] != nil {
networkSelected.RegistryAddressKey = fmt.Sprintf("%v", m[GetNetworkId()].(map[string]interface{})["address"])
}
log.Infof("The Network specified is :%v, and maps to the Network Id %v, the determined Registry address is %v and block chain end point is %v",
GetString(BlockChainNetworkSelected), GetNetworkId(), GetRegistryAddress(), GetBlockChainEndPoint())
return nil
}
//Read the file given file, if the file is not found ,then return back an error
func ReadFromFile(filename string) ([]byte, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
if file, err = ioutil.ReadFile("../" + filename); err != nil {
return nil, errors.Wrapf(err, "could not read file: %v", filename)
}
}
return file, nil
}
//Read from the block chain network config json
func setBlockChainNetworkDetails(fileName string) (err error) {
var data []byte
if (len(networkIdNameMapping) > 0 ){
data = []byte (networkIdNameMapping)
} else {
data, err = ReadFromFile(fileName)
if err != nil {
return fmt.Errorf("cannot read the file %v , error is %v",
fileName, err)
}
}
if err = determineNetworkSelected(data); err == nil {
//this file passed will be used only for local testing
err = setRegistryAddress("resources/blockchain/node_modules/singularitynet-platform-contracts/networks/Registry.json")
}
return err
}