Skip to content

Commit

Permalink
Allow for chain to be encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler King committed Mar 24, 2019
1 parent 4c5d0bf commit d7ef541
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: go
sudo: false
go:
- 1.11
- tip
before_install:
- go get github.com/mattn/goveralls
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

Port of my Blockchain-PHP library to a Golang. The speed is roughly 90% faster.

*Note: Not yet completed.*

## Usage

```go
Expand All @@ -17,6 +15,8 @@ import (
"fmt"
)

// See tests for more examples...

// New chain.
c := new(gc.Chain)

Expand All @@ -30,7 +30,23 @@ blk1.GenerateHash(true)
blk2.Mine()
blk2.GenerateHash(true)

fmt.Println("Chain is valid?", c.IsValid()); // See tests for more examples
fmt.Println("Block valid?", blk.IsValid())
fmt.Println("Block valid?", blk2.IsValid())
fmt.Println("Same block?", c.IsSameBlock(blk, blk))
fmt.Println("Chain is valid?", c.IsValid())

// Block to JSON
j := blk1.Encode()
fmt.Println(string(j)) // example: {"previous_hash": ..., "hash": ..., "index": ..., "nonce": ..., "timestamp": ..., "difficulty": ..., "data": ...}

// Chain to JSON
cj := c.Encode()
fmt.Println(string(j)) // example: {"blocks":[{"previous_hash": ..., "hash": ..., "index": ..., "nonce": ..., "timestamp": ..., "difficulty": ..., "data": ...}, {...}]}

// Get first, last, previous blocks
fb, _ := c.FirstBlock() // equals blk1, if no first block, error will be second return
lb, _ := c.LastBlock() // equals blk2, if no last block, error will be second return
pb, _ := c.PreviousBlock(1) // by index, 1 - 1 = 0, so this will equal blk1, if no previous block, error will be second return
```

## Testing
Expand Down
10 changes: 9 additions & 1 deletion chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package gochain

import (
"bytes"
"encoding/json"
"errors"
"time"
)

// Reprecents a block chain.
type Chain struct {
Blocks []*Block
Blocks []*Block `json:"blocks"`
}

// Encodes the struct to JSON format.
func (c Chain) Encode() (j []byte) {
j, _ = json.Marshal(c)

return
}

// Return the chain length.
Expand Down
19 changes: 19 additions & 0 deletions chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gochain
import (
"reflect"
"testing"
"time"
)

// Test chain has a length.
Expand Down Expand Up @@ -195,3 +196,21 @@ func TestInValidChain(t *testing.T) {
t.Errorf("Expected chain to invalid but result was valid")
}
}

// Test chain ability to encode its struct to JSON data.
func TestChainEncode(t *testing.T) {
// New chain.
c := new(Chain)

// Add a block manually
blk := createBlock()
c.AddBlock(blk)

// Actual and expected.
a := string(c.Encode())
e := "{\"blocks\":[{\"previous_hash\":null,\"hash\":null,\"index\":1,\"nonce\":0,\"difficulty\":1,\"data\":\"Hello World\",\"timestamp\":\"" + blk.Timestamp.Format(time.RFC3339Nano) + "\"}]}"

if a != e {
t.Errorf("Expected encode of %s but got %s", a, e)
}
}

0 comments on commit d7ef541

Please sign in to comment.