mirror of
https://github.com/arnaucube/blockchainIDsystem.git
synced 2026-02-07 02:56:43 +01:00
store blockchain into data file, read blockchain.data on peer poweron. frontend: cssMaterialColors added as a bower external package
This commit is contained in:
1
peer/blockchain.data
Normal file
1
peer/blockchain.data
Normal file
@@ -0,0 +1 @@
|
||||
{"genesisblock":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","lastupdate":"0001-01-01T00:00:00Z","blocks":[{"hash":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","height":0,"date":"2017-11-29T10:42:55.112675187+01:00","previoushash":"","nexthash":"","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="}]}
|
||||
@@ -75,6 +75,8 @@ func (bc *Blockchain) addBlock(block Block) error {
|
||||
}
|
||||
bc.Blocks = append(bc.Blocks, block)
|
||||
|
||||
bc.saveToDisk()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -129,3 +131,25 @@ func (bc *Blockchain) print() {
|
||||
color.Green("---")
|
||||
}
|
||||
}
|
||||
|
||||
func (bc *Blockchain) readFromDisk() error {
|
||||
file, err := ioutil.ReadFile("blockchain.data")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content := string(file)
|
||||
json.Unmarshal([]byte(content), &bc)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bc *Blockchain) saveToDisk() error {
|
||||
bytesBlockchain, err := json.Marshal(blockchain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile("blockchain.data", bytesBlockchain, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ func main() {
|
||||
//read configuration file
|
||||
readConfig("config.json")
|
||||
|
||||
//read the stored blockchain
|
||||
err := blockchain.readFromDisk()
|
||||
check(err)
|
||||
blockchain.print()
|
||||
|
||||
//runningPeer.ID = strconv.Itoa(randInt(1, 1000)) //0 is reserved for server
|
||||
runningPeer.IP = config.IP
|
||||
runningPeer.Port = config.Port
|
||||
|
||||
Reference in New Issue
Block a user