This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move broadcast code to new file and use RespendTimeout instead of a t…
…ries counter
- Loading branch information
1 parent
97dc84c
commit 29c5100
Showing
11 changed files
with
159 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package modules | ||
|
||
import "github.com/NebulousLabs/Sia/types" | ||
|
||
// Consts that are required by multiple modules | ||
const ( | ||
// maxTxnAge determines the maximum age of a transaction (in block height) | ||
// allowed before the transaction is pruned from the transaction pool. | ||
MaxTxnAge = types.BlockHeight(24) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package wallet | ||
|
||
import "github.com/NebulousLabs/Sia/types" | ||
|
||
// broadcastedTSet is a helper struct to keep track of transaction sets and to | ||
// help rebroadcast them. | ||
type broadcastedTSet struct { | ||
firstTry types.BlockHeight // first time the tSet was broadcasted | ||
lastTry types.BlockHeight // last time the tSet was broadcasted | ||
confirmedTxn map[types.TransactionID]bool // tracks confirmed txns of set | ||
transactions []types.Transaction // the tSet | ||
} | ||
|
||
// newBroadcastedTSet creates a broadcastedTSet from a normal tSet | ||
func (w *Wallet) newBroadcastedTSet(tSet []types.Transaction) (bts *broadcastedTSet, err error) { | ||
bts = &broadcastedTSet{} | ||
// Set the height of the first and last try | ||
bts.firstTry, err = dbGetConsensusHeight(w.dbTx) | ||
if err != nil { | ||
return | ||
} | ||
bts.lastTry = bts.firstTry | ||
|
||
// Initialize confirmedTxn and transactions | ||
bts.confirmedTxn = make(map[types.TransactionID]bool) | ||
for _, txn := range tSet { | ||
bts.confirmedTxn[txn.ID()] = false | ||
bts.transactions = append(bts.transactions, txn) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package wallet | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NebulousLabs/Sia/crypto" | ||
"github.com/NebulousLabs/Sia/modules" | ||
"github.com/NebulousLabs/Sia/types" | ||
) | ||
|
||
// TestRebroadcastTransactions checks if transactions are correctly | ||
// rebroadcasted after some time if they haven't been confirmed | ||
func TestRebroadcastTransactions(t *testing.T) { | ||
if testing.Short() { | ||
t.SkipNow() | ||
} | ||
wt, err := createWalletTester(t.Name(), &ProductionDependencies{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer wt.closeWt() | ||
|
||
// Get an address to send money to | ||
uc, err := wt.wallet.NextAddress() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Send money to the address | ||
_, err = wt.wallet.SendSiacoins(types.SiacoinPrecision, uc.UnlockHash()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// The wallet should track the new tSet | ||
if len(wt.wallet.broadcastedTSets) != 1 { | ||
t.Fatalf("len(broadcastedTSets) should be %v but was %v", | ||
1, len(wt.wallet.broadcastedTSets)) | ||
} | ||
// Mine enough blocks for the wallet to stop tracking the tSet | ||
for i := 0; i < rebroadcastInterval+1; i++ { | ||
if _, err := wt.miner.AddBlock(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
if len(wt.wallet.broadcastedTSets) > 0 { | ||
t.Fatalf("len(broadcastedTSets) should be 0 but was %v", | ||
len(wt.wallet.broadcastedTSets)) | ||
} | ||
|
||
// Send some more money to the address | ||
tSet, err := wt.wallet.SendSiacoins(types.SiacoinPrecision, uc.UnlockHash()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// The wallet should track the new tSet | ||
if len(wt.wallet.broadcastedTSets) != 1 { | ||
t.Fatalf("len(broadcastedTSets) should be %v but was %v", | ||
1, len(wt.wallet.broadcastedTSets)) | ||
} | ||
// Mine a block to get the tSet confirmed | ||
if _, err := wt.miner.AddBlock(); err != nil { | ||
t.Fatal(err) | ||
} | ||
// Corrupt the new tSet to make sure the wallet believes it is not confirmed | ||
tSetID := modules.TransactionSetID(crypto.HashAll(tSet)) | ||
bts := wt.wallet.broadcastedTSets[tSetID] | ||
for tid := range bts.confirmedTxn { | ||
bts.confirmedTxn[tid] = false | ||
} | ||
// Mine the same number of blocks. This time the wallet should still track | ||
// the tSet afterwards. | ||
for i := 0; i < rebroadcastInterval+1; i++ { | ||
if _, err := wt.miner.AddBlock(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
if len(wt.wallet.broadcastedTSets) != 1 { | ||
t.Fatalf("The wallet should still track the tSet") | ||
} | ||
// Continue mining to make sure that the wallet stops tracking the tSet | ||
// once the max number of retries is reached | ||
for i := types.BlockHeight(0); i < rebroadcastTimeout; i++ { | ||
if _, err := wt.miner.AddBlock(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
if _, exists := wt.wallet.broadcastedTSets[tSetID]; exists { | ||
t.Fatalf("Wallet should drop txnSet after %v blocks", rebroadcastTimeout) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.