pull/2/head
Minh Doan 7 years ago
parent 4cd5370f9c
commit 32d1f71ac1
  1. 4
      .vscode/settings.json
  2. 4
      benchmark_node.go
  3. 3
      block.go
  4. 13
      blockchain.go
  5. 2
      consensus/consensus.go
  6. 2
      consensus/consensus_leader.go
  7. 3
      consensus/consensus_validator.go
  8. 1
      transaction.go
  9. 38
      utils.go

@ -2,4 +2,8 @@
"workbench.colorTheme": "Solarized Light",
"npm.enableScriptExplorer": true,
"window.zoomLevel": 1,
"editor.tabCompletion": true,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false,
}

@ -9,8 +9,8 @@ import (
"strconv"
"strings"
"./consensus"
"./p2p"
"harmony-benchmark/consensus"
"harmony-benchmark/p2p"
)
// Consts

@ -3,6 +3,8 @@ package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"log"
"time"
)
@ -59,4 +61,3 @@ func DeserializeBlock(d []byte) *Block {
}
return &block
}

@ -9,17 +9,8 @@ type Blockchain struct {
blocks []*Block
}
// AddBlock saves provided data as a block in the blockchain
func (bc *Blockchain) AddBlock(data string) {
prevBlock := bc.blocks[len(bc.blocks)-1]
// TODO(minhdoan): Parse data.
newBlock := NewBlock({}, prevBlock.Hash)
bc.blocks = append(bc.blocks, newBlock)
}
// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain() *Blockchain {
func NewBlockchain(address string) *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}
@ -29,7 +20,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
spentTXOs := make(map[string][]int)
for index := len(bc.blocks) - 1; index >= 0; index-- {
block := bc.blocks[index];
block := bc.blocks[index]
BreakTransaction:
for _, tx := range block.Transactions {

@ -2,7 +2,7 @@
package consensus // consensus
import (
"../p2p"
"harmony-benchmark/p2p"
)
// Consensus data containing all info related to one consensus process

@ -4,7 +4,7 @@ import (
"log"
"sync"
"../p2p"
"harmony-benchmark/p2p"
)
var mutex = &sync.Mutex{}

@ -1,8 +1,8 @@
package consensus
import (
"harmony-benchmark/p2p"
"log"
"../p2p"
)
// Validator's consensus message dispatcher
@ -61,4 +61,3 @@ func (consensus *Consensus) processChallengeMessage(msg string) {
// Set state to RESPONSE_DONE
consensus.state = RESPONSE_DONE
}

@ -38,4 +38,3 @@ func (tx *Transaction) SetId() {
hash = sha256.Sum256(encoded.Bytes())
tx.ID = hash[:]
}

@ -3,7 +3,6 @@ package main
import (
"bytes"
"encoding/binary"
"encoding/gob"
"log"
"strconv"
"strings"
@ -20,26 +19,6 @@ func IntToHex(num int64) []byte {
return buff.Bytes()
}
// Serialize is to serialize a block into []byte.
func (b *Block) Serialize() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(b)
return result.Bytes()
}
// DeserializeBlock is to deserialize []byte into a Block.
func DeserializeBlock(d []byte) *Block {
var block Block
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&block)
return &block
}
// Helper library to convert '1,2,3,4' into []int{1,2,3,4}.
func ConvertIntoInts(data string) []int {
var res = []int{}
@ -51,20 +30,3 @@ func ConvertIntoInts(data string) []int {
}
return res
}
// Helper library to convert '1,2,3,4' into []int{1,2,3,4}.
func ConvertIntoMap(data string) map[string]int {
var res = map[string]int
items := strings.Split(data, ",")
for _, value := range items {
pair := strings.Split(value, " ")
if len(pair) == 3 {
intValue, err := strconv.Atoi(pair[2])
if err != nil {
pair[0] = strings.Trim(pair[0])
res[pair[0]] = intValue
}
}
}
return res
}

Loading…
Cancel
Save