Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
only copy list contents if a transaction was added or removed from th…
Browse files Browse the repository at this point in the history
…e unsolved block
  • Loading branch information
ChrisSchinnerl committed Jan 26, 2018
1 parent 6d0910f commit 8e73f27
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
44 changes: 34 additions & 10 deletions modules/miner/transactionlist.go
@@ -1,6 +1,9 @@
package miner

import "github.com/NebulousLabs/Sia/types"
import (
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/types"
)

type (
// txnListElemenet is a single element in a txnList
Expand All @@ -13,61 +16,82 @@ type (
// txnList is a helper structure to allow for quicker lookups, inserts and
// removals of transactions that should go into a block
txnList struct {
first *txnListElement
last *txnListElement
idToTxn map[types.TransactionID]*txnListElement
first *txnListElement // first element in list
last *txnListElement // last element in list
idToTxn map[types.TransactionID]*txnListElement // map that maps ids to elements
txns []types.Transaction // preallocated slice for faster copying
flagModified bool // indicates if the list has been modified
}
)

// newTxnList creates a new instance of the txnList
func newTxnList() *txnList {
return &txnList{
txns: make([]types.Transaction, 1000),
idToTxn: make(map[types.TransactionID]*txnListElement),
}
}

// appendTxn appends a transaction to the list
func (tl *txnList) appendTxn(txn *types.Transaction) {
// Ths operation modifies the data structure
tl.flagModified = true

// Create the element and store it in idToTxn for easier lookup by id
listElement := &txnListElement{
txn: txn,
}
tl.idToTxn[txn.ID()] = listElement

// check if it is the first element
// Check if it is the first element
if tl.first == nil {
tl.first = listElement
tl.last = listElement
return
}
// if not append it
// If not, append it
tl.last.next = listElement
listElement.prev = tl.last
tl.last = listElement
}

// changed indicates if the underlying data structure of the list has been modified since the last call to resetModified.
func (tl *txnList) modified() bool {
return tl.modified()
}

// resetModified resets the modified flag to false
func (tl *txnList) resetModified() {
tl.flagModified = false
}

// transactions returns the transactions contained in the list as a slice
func (tl *txnList) transactions() []types.Transaction {
if tl.first == nil {
return []types.Transaction{}
}
element := tl.first
txns := []types.Transaction{}
tl.txns = tl.txns[:0]
for element != nil {
txns = append(txns, *element.txn)
tl.txns = append(tl.txns, *element.txn)
element = element.next
}
return txns
return tl.txns
}

// removeTxn removes a transaction by id
func (tl *txnList) removeTxn(id types.TransactionID) {
// Ths operation modifies the data structure
tl.flagModified = true

// Get the corresponding list element and remove it from the map
listElement, exists := tl.idToTxn[id]
if !exists {
panic("transaction is not in the list")
build.Critical("transaction is not in the list")
return
}

// Get the removed element's previous and next element
pe := listElement.prev
ne := listElement.next
if pe == nil {
Expand Down
11 changes: 10 additions & 1 deletion modules/miner/update.go
Expand Up @@ -269,9 +269,18 @@ func (m *Miner) ReceiveUpdatedUnconfirmedTransactions(diff *modules.TransactionP
m.mu.Lock()
defer m.mu.Unlock()

// reset the list modified flag
m.blockTxns.resetModified()

// Remove reverted transactions and add new transactions
m.deleteReverts(diff)
m.addNewTxns(diff)
m.persist.UnsolvedBlock.Transactions = m.blockTxns.transactions()

// Only copy transactions to block if any transactions were added to the
// list or removed from the list
if m.blockTxns.modified() {
m.persist.UnsolvedBlock.Transactions = m.blockTxns.transactions()
}
}

// removeSplitSetFromUnsolvedBlock removes a split set from the miner's unsolved
Expand Down

0 comments on commit 8e73f27

Please sign in to comment.