func (bc *blockchain) resolveConflict() bool {
	maxLength := len(bc.chain)
	newChain := []*block{}

	type chain struct {
		Length int      `json:"length"`
		Chain  []*block `json:"chain"`
	}

	for _, node := range bc.nodes {
		req, err := requestChainFrom(fmt.Sprintf("%s/chain", node))

		if err != nil {
			log.Println("Error fetching chain from node: ", node, err)
			continue
		}

		c := chain{}
		err = json.NewDecoder(req.Body).Decode(&c)
		if err != nil {
			log.Println("Error decoding chain info from node: ", node, err)
			continue
		}

		// ordering throws off the hashing
		if c.Length > maxLength /*&& validChain(c.Chain) */ {
			maxLength = c.Length
			newChain = c.Chain
			log.Printf("This is now c: %+v", c)
		}
	}

	if newChain != nil {
		bc.chain = newChain
		return true
	}
	return false
}
// This is our Consensus Algorithm, it resolves conflicts
// by replacing our chain with the longest one in the network.
This is our Consensus Algorithm, it resolves conflicts
by replacing our chain with the longest one in the network.
// This is our Consensus Algorithm it resolves conflicts // by replacing our chain with the longest one in the network .
func (bc *blockchain) resolveConflict() bool {
	maxLength := len(bc.chain)
	newChain := []*block{}
	type chain struct {
		Length int      `json:"length"`
		Chain  []*block `json:"chain"`
	}
	for _, node := range bc.nodes {
		req, err := requestChainFrom(fmt.Sprintf("%s/chain", node))
		if err != nil {
			log.Println("Error fetching chain from node: ", node, err)
			continue
		}
		c := chain{}
		err = json.NewDecoder(req.Body).Decode(&c)
		if err != nil {
			log.Println("Error decoding chain info from node: ", node, err)
			continue
		}
		if c.Length > maxLength  {
			maxLength = c.Length
			newChain = c.Chain
			log.Printf("This is now c: %+v", c)
		}
	}
	if newChain != nil {
		bc.chain = newChain
		return true
	}
	return false
}
func ( bc * blockchain ) resolveConflict ( ) bool { maxLength := len ( bc . chain ) newChain := [ ] * block { } type chain struct { Length int `json:"length"` Chain [ ] * block `json:"chain"` } for _ , node := range bc . nodes { req , err := requestChainFrom ( fmt . Sprintf ( " " , node ) ) if err != nil { log . Println ( " " , node , err ) continue } c := chain { } err = json . NewDecoder ( req . Body ) . Decode ( & c ) if err != nil { log . Println ( " " , node , err ) continue } if c . Length > maxLength { maxLength = c . Length newChain = c . Chain log . Printf ( " " , c ) } } if newChain != nil { bc . chain = newChain return true } return false }
This is our Consensus Algorithm, it resolves conflicts
by replacing our chain with the longest one in the network.
This is our Consensus Algorithm it resolves conflicts by replacing our chain with the longest one in the network .
// ordering throws off the hashing /*&& validChain(c.Chain) */