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

Improve nops broadcasting. #150

Merged
merged 19 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
7 changes: 4 additions & 3 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ package wavelet

import (
"context"
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/store"
"github.com/pkg/errors"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/store"
"github.com/pkg/errors"
)

type Accounts struct {
Expand Down
92 changes: 75 additions & 17 deletions ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/rand"
"strings"
"sync"
"time"

"github.com/perlin-network/noise"
"github.com/perlin-network/noise/skademlia"
"github.com/perlin-network/wavelet/avl"
Expand All @@ -36,10 +41,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/peer"
"math/rand"
"strings"
"sync"
"time"
)

type Ledger struct {
Expand All @@ -57,9 +58,10 @@ type Ledger struct {

consensus sync.WaitGroup

broadcastNops bool
broadcastNopsDelay time.Time
broadcastNopsLock sync.Mutex
broadcastNops bool
broadcastNopsMaxDepth uint64
broadcastNopsDelay time.Time
broadcastNopsLock sync.Mutex

sync chan struct{}
syncVotes chan vote
Expand All @@ -68,6 +70,9 @@ type Ledger struct {
cacheChunks *LRU

sendQuota chan struct{}

finalizeCh chan struct{}
finalizeChLock sync.RWMutex
}

func NewLedger(kv store.KV, client *skademlia.Client, genesis *string) *Ledger {
Expand All @@ -77,7 +82,9 @@ func NewLedger(kv store.KV, client *skademlia.Client, genesis *string) *Ledger {
indexer := NewIndexer()

accounts := NewAccounts(kv)
go accounts.GC(context.Background())

// TODO: disable GC only for test because it's causing test to panic
// go accounts.GC(context.Background())

rounds, err := NewRounds(kv, sys.PruningLimit)

Expand Down Expand Up @@ -151,7 +158,8 @@ func (l *Ledger) AddTransaction(tx Transaction) error {

if err != nil && errors.Cause(err) != ErrAlreadyExists {
if !strings.Contains(errors.Cause(err).Error(), "transaction has no parents") {
fmt.Println(err)
logger := log.Node()
logger.Err(err).Msg("failed to add transaction")
}
return err
}
Expand All @@ -162,12 +170,13 @@ func (l *Ledger) AddTransaction(tx Transaction) error {
l.gossiper.Push(tx)

l.broadcastNopsLock.Lock()
if tx.Tag != sys.TagNop {
if tx.Tag != sys.TagNop && tx.Sender == l.client.Keys().PublicKey() {
l.broadcastNops = true
l.broadcastNopsDelay = time.Now()
}

if tx.Sender == l.client.Keys().PublicKey() && l.finalizer.Preferred() == nil {
l.broadcastNops = true
if tx.Depth > l.broadcastNopsMaxDepth {
l.broadcastNopsMaxDepth = tx.Depth
}
}
l.broadcastNopsLock.Unlock()
}
Expand Down Expand Up @@ -271,6 +280,37 @@ func (l *Ledger) Snapshot() *avl.Tree {
return l.accounts.Snapshot()
}

func (l *Ledger) BroadcastingNop() bool {
l.broadcastNopsLock.Lock()
broadcastNops := l.broadcastNops
defer l.broadcastNopsLock.Unlock()

return broadcastNops
}

// WaitForConsensus blocks until the ledger reaches consensus.
Copy link
Contributor Author

@hasyimibhar hasyimibhar Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iwasaki-kenta Need your thought on this. WaitForConsensus is basically added purely for testing purposes. What it does is it waits for a round to be finalized, or timeout if it takes too long. I'm not sure if it will impact the ledger in production, as it involves using a mutex. And it doesn't feel right to me at the moment.

Is there a better way to do this? Basically I just need a way to listen for a finalized round when testing. Otherwise either I'll just use time.Sleep (which makes the test run longer), or somehow poll the HTTP API to see if the round has increased.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, I just polled ledger.Round().Latest().Index to see if the round increased, and it works quite well.

// It returns false if it took longer than the timeout duration.
func (l *Ledger) WaitForConsensus(timeout time.Duration) bool {
l.finalizeChLock.Lock()
ch := make(chan struct{})
l.finalizeCh = ch
l.finalizeChLock.Unlock()

timer := time.NewTimer(timeout)
defer timer.Stop()

select {
case <-ch:
return true

case <-timer.C:
l.finalizeChLock.Lock()
l.finalizeCh = nil
l.finalizeChLock.Unlock()
return false
}
}

// BroadcastNop has the node send a nop transaction should they have sufficient
// balance available. They are broadcasted if no other transaction that is not a nop transaction
// is not broadcasted by the node after 500 milliseconds. These conditions only apply so long as
Expand Down Expand Up @@ -376,7 +416,9 @@ func (l *Ledger) PullMissingTransactions() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
batch, err := client.DownloadTx(ctx, req)
if err != nil {
fmt.Println("failed to download missing transactions:", err)
logger := log.Node()
logger.Err(err).Msg("failed to download missing transactions")

cancel()
continue
}
Expand All @@ -387,12 +429,18 @@ func (l *Ledger) PullMissingTransactions() {
for _, buf := range batch.Transactions {
tx, err := UnmarshalTransaction(bytes.NewReader(buf))
if err != nil {
fmt.Printf("error unmarshaling downloaded tx [%v]: %+v", err, tx)
logger := log.Node()
logger.Err(err).
Hex("tx_id", tx.ID[:]).
Msg("error unmarshaling downloaded tx")
continue
}

if err := l.AddTransaction(tx); err != nil && errors.Cause(err) != ErrMissingParents {
fmt.Printf("error adding downloaded tx to graph [%v]: %+v\n", err, tx)
logger := log.Node()
logger.Err(err).
Hex("tx_id", tx.ID[:]).
Msg("error adding downloaded tx to graph")
continue
}

Expand Down Expand Up @@ -474,8 +522,12 @@ FINALIZE_ROUNDS:
continue FINALIZE_ROUNDS
}

// Only stop broadcasting nops if the most recently added transaction
// has been applied
l.broadcastNopsLock.Lock()
l.broadcastNops = false
if l.broadcastNops && l.broadcastNopsMaxDepth <= l.graph.RootDepth() {
l.broadcastNops = false
}
l.broadcastNopsLock.Unlock()

workerChan := make(chan *grpc.ClientConn, 16)
Expand Down Expand Up @@ -688,6 +740,12 @@ FINALIZE_ROUNDS:
Msg("Finalized consensus round, and initialized a new round.")

//go ExportGraphDOT(finalized, l.graph)

l.finalizeChLock.RLock()
if l.finalizeCh != nil {
l.finalizeCh <- struct{}{}
}
l.finalizeChLock.RUnlock()
}
}

Expand Down
66 changes: 66 additions & 0 deletions nops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package wavelet

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestLedger_TransactionThroughput(t *testing.T) {
testnet := NewTestNetwork(t)
defer testnet.Cleanup()

for i := 0; i < 20; i++ {
testnet.AddNode(t, 0)
}

alice := testnet.AddNode(t, 1000000)
bob := testnet.AddNode(t, 0)

// Wait for alice to receive her PERL from the faucet
for <-alice.WaitForConsensus() {
if alice.Balance() > 0 {
break
}
}

txs := make([]Transaction, 1000)
var err error

fmt.Println("adding transactions...")
for i := 0; i < len(txs); i++ {
txs[i], err = alice.Pay(bob, 1)
assert.NoError(t, err)
}

timeout := time.NewTimer(time.Second * 60)
for {
select {
case <-timeout.C:
t.Fatal("timed out before all transactions are applied")

case <-alice.WaitForConsensus():
var appliedCount int
for _, tx := range txs {
if alice.Applied(tx) {
appliedCount++
}
}

fmt.Printf("%d/%d tx applied\n", appliedCount, len(txs))

if appliedCount < len(txs) {
assert.True(t, alice.ledger.BroadcastingNop(),
"node should not stop broadcasting nop while there are unapplied tx")
}

// The test is successful if all tx are applied,
// and nop broadcasting is stopped once all tx are applied
if appliedCount == len(txs) && !alice.ledger.BroadcastingNop() {
return
}
}
}
}
3 changes: 1 addition & 2 deletions snowball.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package wavelet

import (
"fmt"
"sync"
)

Expand Down Expand Up @@ -112,7 +111,7 @@ func (s *Snowball) Tick(round *Round) {

if s.lastID != round.ID { // Handle termination case.
if s.lastID != ZeroRoundID {
fmt.Printf("Snowball (%s) liveness fault: Last ID is %x with count %d, and new ID is %x.\n", s.name, s.lastID, s.count, round.ID)
//fmt.Printf("Snowball (%s) liveness fault: Last ID is %x with count %d, and new ID is %x.\n", s.name, s.lastID, s.count, round.ID)
}

s.lastID = round.ID
Expand Down
Loading