-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ailidani
committed
Feb 16, 2018
1 parent
c43c6a9
commit 99f28de
Showing
41 changed files
with
614 additions
and
680 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
language: go | ||
|
||
sudo: false | ||
|
||
go: | ||
- 1.x | ||
install: go get github.com/ailidani/paxi | ||
script: cd bin/; ./build.sh | ||
|
||
install: | ||
- go get ./... | ||
|
||
script: | ||
- go install ./server/ | ||
- go install ./client/ | ||
- go install ./cmd/ | ||
|
||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env bash | ||
go build ../master/ | ||
#go build ../master/ | ||
go build ../server/ | ||
go build ../client/ | ||
go build ../cmd/ | ||
go build ../cmd/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package blockchain | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"encoding/gob" | ||
"math" | ||
|
||
"github.com/ailidani/paxi/log" | ||
) | ||
|
||
// PREFIX is the leading zeros in hash value as proof of work | ||
var PREFIX = []byte("0000") | ||
|
||
func init() { | ||
gob.Register(Block{}) | ||
} | ||
|
||
// Block contains some amount of data in blockchain | ||
type Block struct { | ||
Index uint64 | ||
Nonce uint64 | ||
Data []byte | ||
Prev []byte // previous block hash | ||
Hash []byte // current block hash | ||
|
||
next *Block | ||
} | ||
|
||
// Next generates the next block given some data | ||
func (b *Block) Next(data []byte) *Block { | ||
next := &Block{ | ||
Index: b.Index + 1, | ||
Data: data, | ||
Prev: b.Hash, | ||
} | ||
b.next = next | ||
h := sha256.New() | ||
h.Write(next.bytes()) | ||
for i := uint64(0); i <= math.MaxUint64; i++ { | ||
t := h | ||
err := binary.Write(t, binary.LittleEndian, i) | ||
if err != nil { | ||
log.Error("binary write failed: ", err) | ||
return nil | ||
} | ||
thash := t.Sum(nil) | ||
if bytes.HasPrefix(thash, PREFIX) { | ||
next.Nonce = i | ||
next.Hash = thash | ||
log.Debugf("Nonce found %d", i) | ||
return next | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *Block) bytes() []byte { | ||
buf := new(bytes.Buffer) | ||
err := binary.Write(buf, binary.LittleEndian, b.Index) | ||
if err != nil { | ||
log.Error("binary write failed: ", err) | ||
return nil | ||
} | ||
buf.Write(b.Data) | ||
buf.Write(b.Prev) | ||
return buf.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package blockchain | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
|
||
"github.com/ailidani/paxi" | ||
"github.com/ailidani/paxi/log" | ||
) | ||
|
||
// Miner is the maintainer of blockchain | ||
type Miner struct { | ||
paxi.Node | ||
|
||
blockchain map[uint64]*Block | ||
chain *Block | ||
requests map[uint64]*paxi.Request | ||
index uint64 | ||
} | ||
|
||
// NewMiner creates new Miner as paxi node | ||
func NewMiner(id paxi.ID) *Miner { | ||
miner := &Miner{ | ||
Node: paxi.NewNode(id), | ||
blockchain: make(map[uint64]*Block), | ||
} | ||
miner.Node.Register(paxi.Request{}, miner.handleRequest) | ||
miner.Node.Register(paxi.Request{}, miner.handleBlock) | ||
return miner | ||
} | ||
|
||
func (m *Miner) handleRequest(r paxi.Request) { | ||
buf := new(bytes.Buffer) | ||
encoder := gob.NewEncoder(buf) | ||
err := encoder.Encode(r.Command) | ||
if err != nil { | ||
log.Error("gob encode error: ", err) | ||
return | ||
} | ||
block := m.blockchain[m.index].Next(buf.Bytes()) | ||
m.blockchain[block.Index] = block | ||
m.requests[block.Index] = &r | ||
m.index = block.Index | ||
r.Reply(paxi.Reply{ | ||
Command: r.Command, | ||
}) | ||
} | ||
|
||
func (m *Miner) handleBlock(b Block) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.