go-perun

module
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: Apache-2.0

README


Perun

Perun Blockchain-Agnostic State Channels Framework

TravisCI build status pkg.go.dev docs Read the Docs

go-perun is a Go implementation of the Perun state channel protocols (introduction paper). The perun protocols provide payment and general state channel functionality to all existing blockchains that feature smart contracts. As a blockchain scalability solution, payment and state channels reduce transaction costs and increase the system throughput by executing incremental transactions off-chain. The Perun protocols have been proven cryptographically secure in the UC-framework. They are blockchain-agnostic and only rely on a blockchain's capability to execute smart contracts.

Security Disclaimer

This software is still under development. The authors take no responsibility for any loss of digital assets or other damage caused by the use of it.

Getting Started

Running go-perun requires a working Go distribution, see go.mod for the required version.

# Clone the repository into a directory of your choice
git clone https://github.com/hyperledger-labs/go-perun.git
# Or directly download it with go
# go get -d perun.network/go-perun
cd go-perun
# Run the unit tests
go test ./...

You can import go-perun in your project like this:

import "perun.network/go-perun/client"

go-perun implements the core state channel protocol in a blockchain-agnostic fashion by following the dependency inversion principle. For this reason, a blockchain backend has to be chosen and blockchain-specific initializations need to be executed at program startup.

Documentation

In-depth documentation can be found at perun-doc and on go-perun's pkg.go.dev site.

Our walkthrough tutorial describes how go-perun is used to build a simple micro-payment application on top of Ethereum.

A bunch of examples can be found at perun-examples.

Features

go-perun currently supports all features needed for two-party generalized state channels. The following features are provided:

  • Generalized two-party state channels, including app/sub-channels
  • Cooperative settling
  • Channel disputes
  • Dispute watchtower
  • Data persistence
  • Virtual two-party payment channels (direct dispute)

The following features are planned for future releases:

  • Virtual two-party state channels (direct dispute)
  • Virtual two-party channels (indirect dispute)
  • Multi-party ledger channels
  • Virtual multi-party channels (direct dispute)
  • Cross-blockchain virtual channels (indirect dispute)

Backends

There are multiple blockchain backends available. A backend is automatically initialized when its top-level package backend/<name> is imported.

  • Ethereum. The Ethereum backend is available at perun-eth-backend.
  • Polkadot. The Polkadot backend is available at perun-polkadot-backend.
  • Cosmos. The Cosmos backend is available at perun-cosmwasm-backend.
  • SimBackend. The SimBackend represents an ideal blockchain backend (backend/sim) implementation that can be used for testing.

Logging and networking capabilities can also be injected by the user. A default logrus implementation of the log.Logger interface can be set using log/logrus.Set. The Perun framework relies on a user-injected wire.Bus for inter-peer communication. go-perun ships with the wire/net.Bus implementation for TCP and Unix sockets.

Data persistence can be enabled to continuously persist new states and signatures. There are currently three persistence backends provided, namely, a test backend for testing purposes, an in-memory key-value persister and a LevelDB backend.

API Primer

In essence, go-perun provides a state channel network client, akin to ethereum's ethclient package, to interact with a state channels network. Once the client has been set up, it can be used to propose channels to other network peers, accept channel proposals, send updates on those channels and eventually settle them.

A minimal, illustrative usage is as follows

package main

import (
	"context"
	"time"

	"perun.network/go-perun/channel"
	"perun.network/go-perun/client"
	"perun.network/go-perun/log"
	"perun.network/go-perun/peer"
	"perun.network/go-perun/wallet"
	"perun.network/go-perun/watcher"
	"perun.network/go-perun/wire"
	// other imports
)

func main() {
	// setup blockchain interaction
	var funder channel.Funder
	var adjudicator channel.Adjudicator
	// setup perun network identity
	var perunID wire.Address
	// setup communication bus
	var bus wire.Bus
	// setup wallet for channel accounts
	var wallet wallet.Wallet
	// setup channel watcher
	var watcher watcher.Watcher

	// create state channel network client
	c := client.New(perunID, bus, funder, adjudicator, wallet, watcher)

	// choose how to react to incoming channel proposals
	var proposalHandler client.ProposalHandler
	// choose how to react to incoming channel update requests
	var updateHandler client.UpdateHandler
	// start incoming request handler
	go c.Handle(proposalHandler, updateHandler)

	// propose a new channel
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
	defer cancel()
	ch, err := c.ProposeChannel(ctx, client.NewLedgerChannelProposal(
		// details of channel proposal, like peers, app, initial balances, challenge duration...
	))
	if err != nil { /* handle error */ }

	// start watchtower
	go func() {
		err := ch.Watch()
		log.Info("Watcher returned with error ", err)
	}()

	// send a channel update request to the other channel peer(s)
	err = ch.Update(ctx, func(s *channel.State) {
		// update state s, e.g., moving funds or changing app data
	})
	if err != nil { /* handle error */ }

	// send further updates and finally, settle/close the channel
	if err := ch.Settle(ctx); err != nil { /* handle error */ }
}

For a full-fledged example, have a look at our CLI Demo perun-eth-demo. Go mobile wrappers for Android and iOS App development can be found at perun-eth-mobile.

Funding

This project has been started at the Chair of Applied Cryptography at Technische Universität Darmstadt, Germany, and is currently developed and maintained by a group of dedicated hackers from PolyCrypt GmbH. We thank the German Federal Ministry of Education and Research (BMBF) for their funding through the StartUpSecure grants program as well as the German Science Foundation (DFG), the Foundation for Polish Science (FNP) and the Ethereum Foundation for their support in the research that preceded this implementation.

Copyright 2021 - See NOTICE file for copyright holders. Use of the source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

Contact us at info@perun.network.

Directories

Path Synopsis
Package apps contains Go implementations of apps that are distributed with go-perun.
Package apps contains Go implementations of apps that are distributed with go-perun.
payment
Package payment implements the payment channel app.
Package payment implements the payment channel app.
Package backend contains blockchain backend implementations.
Package backend contains blockchain backend implementations.
sim
Package sim contains the simulated blockchain backend.
Package sim contains the simulated blockchain backend.
sim/channel
Package channel contains the simulated channel backend.
Package channel contains the simulated channel backend.
sim/wallet
Package wallet contains the simulated wallet backend.
Package wallet contains the simulated wallet backend.
Package channel holds the core channel data structures.
Package channel holds the core channel data structures.
persistence
Package persistence specifies how the framework interacts with a persistence backend.
Package persistence specifies how the framework interacts with a persistence backend.
persistence/keyvalue
Package keyvalue contains an implementation of the channel persister interface using a keyvalue database interface.
Package keyvalue contains an implementation of the channel persister interface using a keyvalue database interface.
persistence/test
Package test provides a PersistRestorer implementation for testing purposes as well as a generic PersistRestorer implementation test.
Package test provides a PersistRestorer implementation for testing purposes as well as a generic PersistRestorer implementation test.
test
Package test contains generic tests for channel backend implementations and random generators of Params, States etc.
Package test contains generic tests for channel backend implementations and random generators of Params, States etc.
Package client contains the Perun State Channel network protocol implementation.
Package client contains the Perun State Channel network protocol implementation.
test
Package test contains testing setup types and functions for package client.
Package test contains testing setup types and functions for package client.
log
Package log implements the logger interface of go-perun.
Package log implements the logger interface of go-perun.
logrus
Package logrus contains an adaptation of the github.com/sirupsen/logrus project to our log interface.
Package logrus contains an adaptation of the github.com/sirupsen/logrus project to our log interface.
Package wallet contains the definition of the wallet backend interfaces, and manages a global wallet backend instance that is accessible from the rest of the project.
Package wallet contains the definition of the wallet backend interfaces, and manages a global wallet backend instance that is accessible from the rest of the project.
test
Package test contains generic tests and benchmarks for wallet backend implementation.
Package test contains generic tests and benchmarks for wallet backend implementation.
Package watcher contains interface definitions for the watcher.
Package watcher contains interface definitions for the watcher.
internal/mock
Package mock contains generated mocks for use in tests.
Package mock contains generated mocks for use in tests.
local
Package local implements a local watcher.
Package local implements a local watcher.
Package wire contains the basic wire communication infrastructure like wire message en- and decoding.
Package wire contains the basic wire communication infrastructure like wire message en- and decoding.
net
Package net contains the abstract communication logic between peers.
Package net contains the abstract communication logic between peers.
net/simple
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
net/test
Package test contains the testing types for wire/net.
Package test contains the testing types for wire/net.
perunio
Package perunio contains functionality for the serialization of standard Go types.
Package perunio contains functionality for the serialization of standard Go types.
perunio/serializer
Package serializer implements the wire serializer interface using the perunio encoding format.
Package serializer implements the wire serializer interface using the perunio encoding format.
perunio/test
Package test contains test helper functions for running generic tests for types implementing perunio serializer.
Package test contains test helper functions for running generic tests for types implementing perunio serializer.
protobuf
Package protobuf implements the wire serializer interface using the protocol buffers serialization protocol.
Package protobuf implements the wire serializer interface using the protocol buffers serialization protocol.
protobuf/test
Package test contains test helper functions for running generic tests for protobuf serialization.
Package test contains test helper functions for running generic tests for protobuf serialization.
test
Package test contains implementations of the peer interfaces that are useful for testing.
Package test contains implementations of the peer interfaces that are useful for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL