Skip to content

Commit

Permalink
Merge branch 'master' into dkg-pedersen
Browse files Browse the repository at this point in the history
  • Loading branch information
nkcr committed May 13, 2020
2 parents 1746558 + 70a54cf commit 5c12442
Show file tree
Hide file tree
Showing 46 changed files with 1,977 additions and 657 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/go_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Go lint

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

lint:
runs-on: ubuntu-latest
steps:
- name: Set up Go ^1.14
uses: actions/setup-go@v2
with:
go-version: ^1.14

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Tidy
run: go mod tidy && [ -z "$(git status -s)" ]

- name: Lint
run: make lint

- name: Vet
run: make vet
39 changes: 39 additions & 0 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Go test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

test:
runs-on: ubuntu-latest
steps:
- name: Set up Go ^1.14
uses: actions/setup-go@v2
with:
go-version: ^1.14
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Test
run: go test -covermode=count -coverprofile=profile.cov ./...

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
parallel: true

# notifies that all test jobs are finished.
finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

This framework aims to deliver a modular approach to a public ledger implementation. This project is under heavy development at this stage. Don't consider using it for the moment.

[![Build Status](https://travis-ci.org/dedis/fabric.svg?branch=master)](https://travis-ci.org/dedis/fabric)
[![Go lint](https://github.com/dedis/fabric/workflows/Go%20lint/badge.svg)](https://github.com/dedis/fabric/actions?query=workflow%3A%22Go+lint%22)
[![Go test](https://github.com/dedis/fabric/workflows/Go%20test/badge.svg)](https://github.com/dedis/fabric/actions?query=workflow%3A%22Go+test%22)
[![Coverage Status](https://coveralls.io/repos/github/dedis/fabric/badge.svg?branch=master)](https://coveralls.io/github/dedis/fabric?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dedis/fabric)](https://goreportcard.com/report/github.com/dedis/fabric)

Expand Down
2 changes: 1 addition & 1 deletion blockchain/skipchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (b SkipBlock) Pack(encoder encoding.ProtoMarshaler) (proto.Message, error)
// String implements fmt.Stringer. It returns a string representation of the
// block.
func (b SkipBlock) String() string {
return fmt.Sprintf("Block[%v]", b.hash)
return fmt.Sprintf("Block[%d:%v]", b.Index, b.hash)
}

func (b SkipBlock) computeHash(factory crypto.HashFactory,
Expand Down
9 changes: 7 additions & 2 deletions blockchain/skipchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func TestSkipBlock_HashUniqueness(t *testing.T) {
}

func TestSkipBlock_String(t *testing.T) {
block := SkipBlock{hash: Digest{1}}
require.Equal(t, block.String(), "Block[0100000000000000]")
block := SkipBlock{Index: 5, hash: Digest{1}}
require.Equal(t, block.String(), "Block[5:0100000000000000]")
}

func TestVerifiableBlock_Pack(t *testing.T) {
Expand Down Expand Up @@ -345,6 +345,7 @@ type fakeConsensus struct {
err error
errChain error
errFactory error
errStore error
}

func (c fakeConsensus) GetChainFactory() (consensus.ChainFactory, error) {
Expand All @@ -362,3 +363,7 @@ func (c fakeConsensus) GetChain(id []byte) (consensus.Chain, error) {
func (c fakeConsensus) Listen(consensus.Validator) (consensus.Actor, error) {
return nil, c.err
}

func (c fakeConsensus) Store(consensus.Chain) error {
return c.errStore
}
7 changes: 7 additions & 0 deletions blockchain/skipchain/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// blocks.
type Queries interface {
Write(block SkipBlock) error
Contains(index uint64) bool
Read(index int64) (SkipBlock, error)
ReadLast() (SkipBlock, error)
}
Expand Down Expand Up @@ -78,6 +79,12 @@ func (db *InMemoryDatabase) Write(block SkipBlock) error {
return nil
}

// Contains implements skipchain.Database. It returns true if the block is
// stored in the database, otherwise false.
func (db *InMemoryDatabase) Contains(index uint64) bool {
return index < uint64(len(db.blocks))
}

// Read implements skipchain.Database. It returns the block at the given index
// if it exists, otherwise an error.
func (db *InMemoryDatabase) Read(index int64) (SkipBlock, error) {
Expand Down
86 changes: 60 additions & 26 deletions blockchain/skipchain/handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package skipchain

import (
"bytes"
"context"

proto "github.com/golang/protobuf/proto"
"go.dedis.ch/fabric"
"go.dedis.ch/fabric/blockchain"
"go.dedis.ch/fabric/mino"
"golang.org/x/xerrors"
)
Expand All @@ -14,15 +15,12 @@ import (
// - implements mino.Handler
type handler struct {
mino.UnsupportedHandler
*Skipchain

proc blockchain.PayloadProcessor
*operations
}

func newHandler(sc *Skipchain, proc blockchain.PayloadProcessor) handler {
func newHandler(ops *operations) handler {
return handler{
Skipchain: sc,
proc: proc,
operations: ops,
}
}

Expand All @@ -33,36 +31,72 @@ func (h handler) Process(req mino.Request) (proto.Message, error) {
case *PropagateGenesis:
genesis, err := h.blockFactory.decodeBlock(in.GetGenesis())
if err != nil {
return nil, xerrors.Errorf("couldn't decode the block: %v", err)
return nil, xerrors.Errorf("couldn't decode block: %v", err)
}

err = h.insertBlock(genesis)
if err != nil {
return nil, xerrors.Errorf("couldn't store genesis: %v", err)
}

err = h.proc.Validate(0, genesis.GetPayload())
return nil, nil
default:
return nil, xerrors.Errorf("unknown message type '%T'", in)
}
}

// Stream implements mino.Handler. It handles block requests to help another
// participant to catch up the latest chain.
func (h handler) Stream(out mino.Sender, in mino.Receiver) error {
addr, msg, err := in.Recv(context.Background())
if err != nil {
return xerrors.Errorf("couldn't receive message: %v", err)
}

req, ok := msg.(*BlockRequest)
if !ok {
return xerrors.Errorf("invalid message type '%T' != '%T'", msg, req)
}

var block SkipBlock
for i := int64(0); !bytes.Equal(block.hash[:], req.To); i++ {
block, err = h.db.Read(i)
if err != nil {
return nil, xerrors.Errorf("couldn't validate genesis payload: %v", err)
return xerrors.Errorf("couldn't read block at index %d: %v", i, err)
}

err = h.db.Atomic(func(ops Queries) error {
err = ops.Write(genesis)
blockpb, err := h.encoder.Pack(block)
if err != nil {
return xerrors.Errorf("couldn't pack block: %v", err)
}

resp := &BlockResponse{
Block: blockpb.(*BlockProto),
}

if block.GetIndex() > 0 {
// In the case the genesis block needs to be sent, there is no chain
// to send alongside.

chain, err := h.consensus.GetChain(block.GetHash())
if err != nil {
return xerrors.Errorf("couldn't write the block: %v", err)
return xerrors.Errorf("couldn't get chain to block %d: %v",
block.GetIndex(), err)
}

err = h.proc.Commit(genesis.GetPayload())
chainpb, err := h.encoder.PackAny(chain)
if err != nil {
return xerrors.Errorf("couldn't commit genesis payload: %v", err)
return xerrors.Errorf("couldn't pack chain: %v", err)
}

return nil
})
if err != nil {
return nil, xerrors.Errorf("tx aborted: %v", err)
resp.Chain = chainpb
}

fabric.Logger.Trace().Msgf("new genesis block written: %v", genesis.hash)
h.watcher.Notify(genesis)

return nil, nil
default:
return nil, xerrors.Errorf("unknown message type '%T'", in)
err = <-out.Send(resp, addr)
if err != nil {
return xerrors.Errorf("couldn't send block: %v", err)
}
}

return nil
}
Loading

0 comments on commit 5c12442

Please sign in to comment.