Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix egoistic funding #414

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
66fee75
feat(wallet, channel): Change backend to map of backends
sophia1ch Aug 21, 2024
28685ea
fix(multi): Add AssetIDKey struct to fix the map lookups for funders …
sophia1ch Aug 28, 2024
ef11de9
fix(all): Use map[int]ID instead of id for each backend.
sophia1ch Sep 4, 2024
ab8e60b
fix(all): Add map of IDs
sophia1ch Sep 4, 2024
13eeddf
fix(channel/backend): Revert change in verify.
sophia1ch Sep 5, 2024
8181cb0
fix(Adjudicator): Change to singular channel ID in events.
sophia1ch Sep 11, 2024
794298a
fix(client/test): Generalize role test.
sophia1ch Sep 11, 2024
969c6a7
feat(channel): Add backend field check.
sophia1ch Sep 20, 2024
10c9b28
fix(subchannel_dispute): Generalize event watcher.
sophia1ch Sep 25, 2024
3c1e261
feat: Asset Address function to handle encoding in backends.
sophia1ch Oct 1, 2024
a695d14
feat(apps/payment, backend/sim, channel/test, client/test, log, walle…
sophia1ch Oct 15, 2024
8589eb8
fix(asset.go, allocation.go): Adjust Address() to return byte[] inste…
sophia1ch Oct 29, 2024
39095c1
chore: Remove unnecessary logs
sophia1ch Nov 6, 2024
98e11ea
fix: Remove repeated assetIDs in LedgerID array
sophia1ch Nov 27, 2024
46de2d9
chore: update go.sum, gofmt some files
sophia1ch Dec 18, 2024
dac83b9
Merge branch 'main' into feat-mult-backends
sophia1ch Dec 18, 2024
5e5bfb3
fix(wallet/backend.go): Return just one error instead of concatenatio…
sophia1ch Dec 19, 2024
70036df
fix(golangci.yml): Exclude generated file from linter
sophia1ch Dec 19, 2024
19a792f
fix(ci): Use go1.18 for the any type in proto
sophia1ch Dec 19, 2024
fd8bf8a
fix(ci): Use glinter 1.45 which supports go1.18
sophia1ch Dec 19, 2024
98b900b
chore(all): Add nolint flags for conversions
sophia1ch Dec 19, 2024
2f24516
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
8622a49
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
caa8d4c
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
b272564
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
2c079f1
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
ced8fb5
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
056e726
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
d73ca03
chore(all): Rename and add comments to exported functions
sophia1ch Dec 19, 2024
2d98975
chore: Rename variables
sophia1ch Dec 23, 2024
d463e82
fix(params, address): Fix index allocation
sophia1ch Dec 23, 2024
6823276
fix(funder, proposal): Make egoistic funding dependant on participant…
sophia1ch Jan 7, 2025
091445a
fix(funder): Add egoistic check, otherwise one participant is always …
sophia1ch Jan 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
release:

env:
go-version: 1.17
go-version: 1.18

jobs:
check-copyright:
Expand Down Expand Up @@ -39,7 +39,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.43
version: v1.45

- name: Lint proto files
uses: plexsystems/protolint-action@v0.6.0
Expand Down
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ linters-settings:
# Default values:
- ^print.*$
- 'fmt\.Print.*'

exclude-rules:
- path: ".*\\.pb\\.go$" # Exclude protobuf generated files.
issues:
# Re-enable default excludes.
include:
Expand Down
2 changes: 1 addition & 1 deletion apps/payment/app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestApp_Def(t *testing.T) {
rng := pkgtest.Prng(t)
def := test.NewRandomAppID(rng)
def := test.NewRandomAppID(rng, 0)
app := &App{def}
assert.True(t, app.Def().Equal(app.Def()))
}
Expand Down
6 changes: 4 additions & 2 deletions apps/payment/randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package payment
import (
"math/rand"

"perun.network/go-perun/wallet"

"perun.network/go-perun/channel"
"perun.network/go-perun/channel/test"
)
Expand All @@ -27,8 +29,8 @@ type Randomizer struct{}
var _ test.AppRandomizer = (*Randomizer)(nil)

// NewRandomApp always returns a payment app with a different address.
func (*Randomizer) NewRandomApp(rng *rand.Rand) channel.App {
return &App{test.NewRandomAppID(rng)}
func (*Randomizer) NewRandomApp(rng *rand.Rand, bID wallet.BackendID) channel.App {
return &App{test.NewRandomAppID(rng, bID)}
}

// NewRandomData returns NoData because a PaymentApp does not have data.
Expand Down
2 changes: 1 addition & 1 deletion apps/payment/randomizer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRandomizer(t *testing.T) {
rng := pkgtest.Prng(t)

r := new(Randomizer)
app := r.NewRandomApp(rng)
app := r.NewRandomApp(rng, 0)
channel.RegisterApp(app)
regApp, err := channel.Resolve(app.Def())
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion apps/payment/resolver_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestResolver(t *testing.T) {
rng := pkgtest.Prng(t)
assert, require := assert.New(t), require.New(t)

def := ctest.NewRandomAppID(rng)
def := ctest.NewRandomAppID(rng, 0)
channel.RegisterAppResolver(def.Equal, &Resolver{})

app, err := channel.Resolve(def)
Expand Down
2 changes: 1 addition & 1 deletion backend/sim/channel/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func (id AppID) Key() channel.AppIDKey {
// NewRandomAppID generates a new random app identifier.
func NewRandomAppID(rng *rand.Rand) AppID {
addr := wallet.NewRandomAddress(rng)
return AppID{addr}
return AppID{Address: addr}
}
6 changes: 6 additions & 0 deletions backend/sim/channel/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ func (a Asset) Equal(b channel.Asset) bool {
}
return a.ID == simAsset.ID
}

// Address returns the address of the asset.
func (a Asset) Address() []byte {
data, _ := a.MarshalBinary()
return data
}
19 changes: 9 additions & 10 deletions backend/sim/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ type backend struct{}
var _ channel.Backend = new(backend)

// CalcID calculates a channel's ID by hashing all fields of its parameters.
func (*backend) CalcID(p *channel.Params) (id channel.ID) {
func (*backend) CalcID(p *channel.Params) (id channel.ID, err error) {
w := sha256.New()

// Write Parts
for _, addr := range p.Parts {
if err := perunio.Encode(w, addr); err != nil {
log.Panic("Could not write to sha256 hasher")
}
if err := perunio.Encode(w, wallet.AddressMapArray{Addr: p.Parts}); err != nil {
log.Panic("Could not write to sha256 hasher")
}

err := perunio.Encode(w, p.Nonce, p.ChallengeDuration, channel.OptAppEnc{App: p.App}, p.LedgerChannel, p.VirtualChannel)
err = perunio.Encode(w, p.Nonce, p.ChallengeDuration, channel.OptAppEnc{App: p.App}, p.LedgerChannel, p.VirtualChannel)
if err != nil {
log.Panic("Could not write to sha256 hasher")
return
}

if copy(id[:], w.Sum(nil)) != channel.IDLen {
log.Panic("Could not copy id")
err = errors.New("Could not copy id")
return
}
return
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func (b *backend) NewAsset() channel.Asset {

// NewAppID returns an object of type AppID, which can be used for
// unmarshalling an app identifier from its binary representation.
func (b *backend) NewAppID() channel.AppID {
func (b *backend) NewAppID() (channel.AppID, error) {
addr := &simwallet.Address{}
return AppID{addr}
return AppID{addr}, nil
}
4 changes: 2 additions & 2 deletions backend/sim/channel/channel_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newChannelSetup(t *testing.T) *chtest.Setup {
Params2: params2,
State: state,
State2: state2,
Account: wtest.NewRandomAccount(rng),
RandomAddress: func() wallet.Address { return wtest.NewRandomAddress(rng) },
Account: wtest.NewRandomAccount(rng, 0),
RandomAddress: func() map[wallet.BackendID]wallet.Address { return wtest.NewRandomAddresses(rng, 0) },
}
}
6 changes: 3 additions & 3 deletions backend/sim/channel/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
)

func init() {
channel.SetBackend(new(backend))
test.SetRandomizer(new(randomizer))
channel.SetBackend(new(backend), 0)
test.SetRandomizer(new(randomizer), 0)
test.SetNewRandomAppID(func(r *rand.Rand) channel.AppID {
return NewRandomAppID(r)
})
}, 0)
}
2 changes: 1 addition & 1 deletion backend/sim/wallet/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewRandomAccount(rng io.Reader) *Account {

// Address returns the address of this account.
func (a *Account) Address() wallet.Address {
return wallet.Address((*Address)(&a.privKey.PublicKey))
return (*Address)(&a.privKey.PublicKey)
}

// SignData is used to sign data with this account. If the account is locked,
Expand Down
28 changes: 25 additions & 3 deletions backend/sim/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// Address represents a simulated address.
type Address ecdsa.PublicKey

// BackendID returns the backend id of the address.
func (a Address) BackendID() wallet.BackendID {
return 0
}

const (
// elemLen is the length of the binary representation of a single element
// of the address in bytes.
Expand Down Expand Up @@ -54,6 +59,21 @@ func NewRandomAddress(rng io.Reader) *Address {
}
}

// NewRandomAddresses creates a new address using the randomness
// provided by rng.
func NewRandomAddresses(rng io.Reader) map[int]wallet.Address {
privateKey, err := ecdsa.GenerateKey(curve, rng)
if err != nil {
log.Panicf("Creation of account failed with error", err)
}

return map[int]wallet.Address{0: &Address{
Curve: privateKey.Curve,
X: privateKey.X,
Y: privateKey.Y,
}}
}

// Bytes converts this address to bytes.
func (a *Address) Bytes() []byte {
data := a.byteArray()
Expand Down Expand Up @@ -90,9 +110,11 @@ func (a *Address) Equal(addr wallet.Address) bool {
}

// Cmp checks the ordering of two addresses according to following definition:
// -1 if (a.X < addr.X) || ((a.X == addr.X) && (a.Y < addr.Y))
// 0 if (a.X == addr.X) && (a.Y == addr.Y)
// +1 if (a.X > addr.X) || ((a.X == addr.X) && (a.Y > addr.Y))
//
// -1 if (a.X < addr.X) || ((a.X == addr.X) && (a.Y < addr.Y))
// 0 if (a.X == addr.X) && (a.Y == addr.Y)
// +1 if (a.X > addr.X) || ((a.X == addr.X) && (a.Y > addr.Y))
//
// So the X coordinate is weighted higher.
// Pancis if the passed address is of the wrong type.
func (a *Address) Cmp(addr wallet.Address) int {
Expand Down
4 changes: 2 additions & 2 deletions backend/sim/wallet/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import (
)

func init() {
wallet.SetBackend(new(Backend))
test.SetRandomizer(newRandomizer())
wallet.SetBackend(new(Backend), 0)
test.SetRandomizer(newRandomizer(), 0)
}
22 changes: 11 additions & 11 deletions backend/sim/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type Wallet struct {
func (w *Wallet) Unlock(a wallet.Address) (wallet.Account, error) {
w.accMutex.RLock()
defer w.accMutex.RUnlock()

acc, ok := w.accs[wallet.Key(a)]
key := wallet.Key(a)
acc, ok := w.accs[key]
if !ok {
return nil, errors.Errorf("unlock unknown address: %v", a)
}
Expand All @@ -90,8 +90,8 @@ func (w *Wallet) LockAll() {
func (w *Wallet) IncrementUsage(a wallet.Address) {
w.accMutex.RLock()
defer w.accMutex.RUnlock()

acc, ok := w.accs[wallet.Key(a)]
key := wallet.Key(a)
acc, ok := w.accs[key]
if !ok {
panic("invalid address")
}
Expand All @@ -106,8 +106,8 @@ func (w *Wallet) IncrementUsage(a wallet.Address) {
func (w *Wallet) DecrementUsage(a wallet.Address) {
w.accMutex.Lock()
defer w.accMutex.Unlock()

acc, ok := w.accs[wallet.Key(a)]
key := wallet.Key(a)
acc, ok := w.accs[key]
if !ok {
panic("invalid address")
}
Expand All @@ -119,7 +119,7 @@ func (w *Wallet) DecrementUsage(a wallet.Address) {

if newCount == 0 {
acc.locked.Set()
delete(w.accs, wallet.Key(a))
delete(w.accs, key)
}
}

Expand All @@ -129,8 +129,8 @@ func (w *Wallet) DecrementUsage(a wallet.Address) {
func (w *Wallet) UsageCount(a wallet.Address) int {
w.accMutex.RLock()
defer w.accMutex.RUnlock()

acc, ok := w.accs[wallet.Key(a)]
key := wallet.Key(a)
acc, ok := w.accs[key]
if !ok {
panic("invalid address")
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (w *Wallet) AddAccount(acc *Account) error {
func (w *Wallet) HasAccount(acc *Account) bool {
w.accMutex.RLock()
defer w.accMutex.RUnlock()

_, ok := w.accs[wallet.Key(acc.Address())]
key := wallet.Key(acc.Address())
_, ok := w.accs[key]
return ok
}
2 changes: 1 addition & 1 deletion channel/actionmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ActionMachine struct {
}

// NewActionMachine creates a new ActionMachine.
func NewActionMachine(acc wallet.Account, params Params) (*ActionMachine, error) {
func NewActionMachine(acc map[wallet.BackendID]wallet.Account, params Params) (*ActionMachine, error) {
app, ok := params.App.(ActionApp)
if !ok {
return nil, errors.New("app must be ActionApp")
Expand Down
11 changes: 6 additions & 5 deletions channel/adjudicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type (
// subscription should be closed by calling Close on the subscription after
// the channel is closed.
EventSubscriber interface {
Subscribe(context.Context, ID) (AdjudicatorSubscription, error)
Subscribe(context.Context, map[wallet.BackendID]ID) (AdjudicatorSubscription, error)
}

// An AdjudicatorReq collects all necessary information to make calls to the
Expand All @@ -110,7 +110,7 @@ type (
// protocol, possibly saving unnecessary double sending of transactions.
AdjudicatorReq struct {
Params *Params
Acc wallet.Account
Acc map[wallet.BackendID]wallet.Account
Tx Transaction
Idx Index // Always the own index
Secondary bool // Optimized secondary call protocol
Expand Down Expand Up @@ -204,7 +204,7 @@ type (
}

// StateMap represents a channel state tree.
StateMap map[ID]*State
StateMap map[string]*State
)

// NewProgressReq creates a new ProgressReq object.
Expand Down Expand Up @@ -302,12 +302,13 @@ func (t *TimeTimeout) String() string {

// MakeStateMap creates a new StateMap object.
func MakeStateMap() StateMap {
return make(map[ID]*State)
return make(map[string]*State)
}

// Add adds the given states to the state map.
func (m StateMap) Add(states ...*State) {
for _, s := range states {
m[s.ID] = s
key := IDKey(s.ID)
m[key] = s
}
}
Loading
Loading