refactor chain_config and add decodeNetworkParams

allow custom network not only loaded from file but also
decoded from json string
This commit is contained in:
jangko 2022-02-15 10:26:03 +07:00
parent 73e28694b5
commit 2970fc4b02
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
1 changed files with 35 additions and 14 deletions

View File

@ -192,21 +192,8 @@ template to(a: string, b: type UInt256): UInt256 =
# json_serialization decode table stuff
UInt256.fromHex(a)
proc loadNetworkParams*(fileName: string, cg: var NetworkParams):
proc loadNetworkParams*(cc: CustomChain, cg: var NetworkParams):
bool {.raises: [Defect].} =
var cc: CustomChain
try:
cc = Json.loadFile(fileName, CustomChain, allowUnknownFields = true)
except IOError as e:
error "Network params I/O error", fileName, msg=e.msg
return false
except JsonReaderError as e:
error "Invalid network params file format", fileName, msg=e.formatMsg("")
return false
except:
var msg = getCurrentExceptionMsg()
error "Error loading network params file", fileName, msg
return false
cg.genesis = cc.genesis
cg.config.chainId = cc.config.chainId
@ -253,6 +240,40 @@ proc loadNetworkParams*(fileName: string, cg: var NetworkParams):
return true
proc loadNetworkParams*(fileName: string, cg: var NetworkParams):
bool {.raises: [Defect].} =
var cc: CustomChain
try:
cc = Json.loadFile(fileName, CustomChain, allowUnknownFields = true)
except IOError as e:
error "Network params I/O error", fileName, msg=e.msg
return false
except JsonReaderError as e:
error "Invalid network params file format", fileName, msg=e.formatMsg("")
return false
except:
var msg = getCurrentExceptionMsg()
error "Error loading network params file", fileName, msg
return false
loadNetworkParams(cc, cg)
proc decodeNetworkParams*(jsonString: string, cg: var NetworkParams):
bool {.raises: [Defect].} =
var cc: CustomChain
try:
cc = Json.decode(jsonString, CustomChain, allowUnknownFields = true)
except JsonReaderError as e:
error "Invalid network params format", msg=e.formatMsg("")
return false
except:
var msg = getCurrentExceptionMsg()
error "Error decoding network params", msg
return false
loadNetworkParams(cc, cg)
proc parseGenesisAlloc*(data: string, ga: var GenesisAlloc): bool =
try:
ga = Json.decode(data, GenesisAlloc, allowUnknownFields = true)