go-perun

module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2021 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.

Tutorial

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

Documentation

More in-depth documentation can be found here and on go-perun's pkg.go.dev site.

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 as part of the current release: Ethereum (backend/ethereum), and a simulated, ideal blockchain backend (backend/sim). A backend is automatically initialized when its top-level package backend/<name> is imported. The Ethereum smart contracts can be found in the contracts-eth repository.

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.UpdateBy(ctx, func(s *channel.State) error {
		// 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.
ethereum
Package ethereum contains the Ethereum blockchain backend.
Package ethereum contains the Ethereum blockchain backend.
ethereum/bindings
Package bindings contains all automatically generated code bindings to interact with the smart contracts of the Perun Ethereum blockchain backend.
Package bindings contains all automatically generated code bindings to interact with the smart contracts of the Perun Ethereum blockchain backend.
ethereum/channel
Package channel contains the Ethereum channel backend implementation.
Package channel contains the Ethereum channel backend implementation.
ethereum/channel/errors
Package errors contains error checking and wrapping functions.
Package errors contains error checking and wrapping functions.
ethereum/channel/test
Package test contains utilities for testing the Ethereum channel backend, such as a simulated blockchain backend and a custom Adjudicator.
Package test contains utilities for testing the Ethereum channel backend, such as a simulated blockchain backend and a custom Adjudicator.
ethereum/client/test
Package test contains utilities for running client tests for Ethereum.
Package test contains utilities for running client tests for Ethereum.
ethereum/subscription
Package subscription contains generic event subscriptions.
Package subscription contains generic event subscriptions.
ethereum/wallet
Package wallet implements go-perun's wallet interface for the ethereum backend.
Package wallet implements go-perun's wallet interface for the ethereum backend.
ethereum/wallet/hd
Package hd contains perun wallet and accounts implementation for accessing accounts stored in an ethereum hierarchial deterministic (HD) wallet.
Package hd contains perun wallet and accounts implementation for accessing accounts stored in an ethereum hierarchial deterministic (HD) wallet.
ethereum/wallet/keystore
Package keystore contains perun wallet and accounts implementation for accessing accounts stored in ethereum keystore.
Package keystore contains perun wallet and accounts implementation for accessing accounts stored in ethereum keystore.
ethereum/wallet/simple
Package simple contains a simplistic implementation of the perun wallet, account, and transactor interfaces.
Package simple contains a simplistic implementation of the perun wallet, account, and transactor interfaces.
ethereum/wallet/test
Package test contains utilities for generating random ethereum accounts and addresses.
Package test contains utilities for generating random ethereum accounts and addresses.
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.
pkg
Package pkg contains generic utility packages that are used in the go-perun framework but are generally reusable in other projects.
Package pkg contains generic utility packages that are used in the go-perun framework but are generally reusable in other projects.
context
Package context contains helper utilities regarding go contexts.
Package context contains helper utilities regarding go contexts.
context/test
Package test tests the helper utilities regarding go contexts.
Package test tests the helper utilities regarding go contexts.
errors
Package errors contains functionality for simplifying and improving error handling.
Package errors contains functionality for simplifying and improving error handling.
io
Package io contains functionality for the serialization of standard Go types.
Package io contains functionality for the serialization of standard Go types.
io/test
Package test contains the generic serializer tests.
Package test contains the generic serializer tests.
math/big
Package big contains generic functions that operate on big.Int and can be used independently of go-perun.
Package big contains generic functions that operate on big.Int and can be used independently of go-perun.
sortedkv
Package sortedkv defines a sorted key-value store abstraction.
Package sortedkv defines a sorted key-value store abstraction.
sortedkv/key
Package key of sortedkv provides helper functions to manipulate db keys
Package key of sortedkv provides helper functions to manipulate db keys
sortedkv/leveldb
Package leveldb implements the key-value database interface using LevelDB.
Package leveldb implements the key-value database interface using LevelDB.
sortedkv/memorydb
Package memorydb provides an implementation of the sortedkv interfaces.
Package memorydb provides an implementation of the sortedkv interfaces.
sortedkv/test
Package test of the go-perun/sortedkv package implements a generic test for all implementations of the Database interface.
Package test of the go-perun/sortedkv package implements a generic test for all implementations of the Database interface.
sync
Package sync contains a mutex that can be used in a select statement.
Package sync contains a mutex that can be used in a select statement.
sync/atomic
Package atomic contains extensions of "sync/atomic"
Package atomic contains extensions of "sync/atomic"
test
Package test contains Perun independent testing functionality.
Package test contains Perun independent testing functionality.
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.
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