This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from BuxOrg/siggi/set-change-address-optimisa…
…tion Added some tests and benchmarks, changed setAddress on draft transaction to only derive what is needed
- Loading branch information
Showing
7 changed files
with
305 additions
and
12 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,124 @@ | ||
package bux | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/BuxOrg/bux/utils" | ||
"github.com/libsv/go-bk/bip32" | ||
) | ||
|
||
// BenchmarkAction_Transaction_recordTransaction will benchmark the method RecordTransaction() | ||
func BenchmarkAction_Transaction_recordTransaction(b *testing.B) { | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
b.StopTimer() | ||
ctx, client, xPub, config, err := initBenchmarkData(b) | ||
if err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
|
||
var draftTransaction *DraftTransaction | ||
if draftTransaction, err = client.NewTransaction(ctx, xPub.rawXpubKey, config, client.DefaultModelOptions()...); err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
|
||
var xPriv *bip32.ExtendedKey | ||
if xPriv, err = bip32.NewKeyFromString(testXPriv); err != nil { | ||
return | ||
} | ||
|
||
var hexString string | ||
if hexString, err = draftTransaction.SignInputs(xPriv); err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
|
||
b.StartTimer() | ||
if _, err = client.RecordTransaction(ctx, xPub.rawXpubKey, hexString, draftTransaction.ID, client.DefaultModelOptions()...); err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
} | ||
} | ||
|
||
// BenchmarkTransaction_newTransaction will benchmark the method newTransaction() | ||
func BenchmarkAction_Transaction_newTransaction(b *testing.B) { | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
b.StopTimer() | ||
ctx, client, xPub, config, err := initBenchmarkData(b) | ||
if err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
|
||
b.StartTimer() | ||
if _, err = client.NewTransaction(ctx, xPub.rawXpubKey, config, client.DefaultModelOptions()...); err != nil { | ||
fmt.Printf("ERROR: %s\n", err.Error()) | ||
b.Fail() | ||
} | ||
} | ||
} | ||
|
||
func initBenchmarkData(b *testing.B) (context.Context, ClientInterface, *Xpub, *TransactionConfig, error) { | ||
ctx, client, _ := CreateBenchmarkSQLiteClient(b, false, true, | ||
WithCustomTaskManager(&taskManagerMockBase{}), | ||
WithFreeCache(), | ||
WithIUCDisabled(), | ||
) | ||
|
||
opts := append(client.DefaultModelOptions(), New()) | ||
xPub, err := client.NewXpub(ctx, testXPub, opts...) | ||
if err != nil { | ||
b.Fail() | ||
} | ||
destination := newDestination(xPub.GetID(), testLockingScript, opts...) | ||
if err = destination.Save(ctx); err != nil { | ||
b.Fail() | ||
} | ||
|
||
utxo := newUtxo(xPub.GetID(), testTxID, testLockingScript, 1, 122500, opts...) | ||
if err = utxo.Save(ctx); err != nil { | ||
b.Fail() | ||
} | ||
utxo = newUtxo(xPub.GetID(), testTxID, testLockingScript, 2, 122500, opts...) | ||
if err = utxo.Save(ctx); err != nil { | ||
b.Fail() | ||
} | ||
utxo = newUtxo(xPub.GetID(), testTxID, testLockingScript, 3, 122500, opts...) | ||
if err = utxo.Save(ctx); err != nil { | ||
b.Fail() | ||
} | ||
utxo = newUtxo(xPub.GetID(), testTxID, testLockingScript, 4, 122500, opts...) | ||
if err = utxo.Save(ctx); err != nil { | ||
b.Fail() | ||
} | ||
|
||
config := &TransactionConfig{ | ||
FeeUnit: &utils.FeeUnit{ | ||
Satoshis: 5, | ||
Bytes: 100, | ||
}, | ||
Outputs: []*TransactionOutput{{ | ||
OpReturn: &OpReturn{ | ||
Map: &MapProtocol{ | ||
App: "getbux.io", | ||
Type: "blast", | ||
Keys: map[string]interface{}{ | ||
"bux": "blasting", | ||
}, | ||
}, | ||
}, | ||
}}, | ||
ChangeDestinationsStrategy: ChangeStrategyRandom, | ||
ChangeNumberOfDestinations: 2, | ||
} | ||
|
||
return ctx, client, xPub, config, err | ||
} |
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
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,89 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/libsv/go-bk/bip32" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
testXPub = "xpub661MyMwAqRbcFrBJbKwBGCB7d3fr2SaAuXGM95BA62X41m6eW2ehRQGW4xLi9wkEXUGnQZYxVVj4PxXnyrLk7jdqvBAs1Qq9gf6ykMvjR7J" | ||
) | ||
|
||
// Test_DeriveAddresses will test the method DeriveAddresses() | ||
func Test_DeriveAddresses(t *testing.T) { | ||
|
||
xPub, errX := bip32.NewKeyFromString(testXPub) | ||
require.NoError(t, errX) | ||
|
||
t.Run("DeriveAddresses 1", func(t *testing.T) { | ||
external, internal, err := DeriveAddresses(xPub, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, "1PQW54xMn5KA6uK7wgfzN4y7ZXMi6o7Qtm", internal) | ||
assert.Equal(t, "16fq7PmmXXbFUG5maT5Xvr2zDBUgN1xdMF", external) | ||
}) | ||
|
||
t.Run("DeriveAddresses 2", func(t *testing.T) { | ||
external, internal, err := DeriveAddresses(xPub, 2) | ||
require.NoError(t, err) | ||
assert.Equal(t, "13St2SHkw1b8ZuaExyMf6ZMEzNjYbWRqL4", internal) | ||
assert.Equal(t, "19jswATg9vBFta1aRnEjPHa2KMwafkmANj", external) | ||
}) | ||
} | ||
|
||
// Test_DeriveAddress will test the method DeriveAddress() | ||
func Test_DeriveAddress(t *testing.T) { | ||
|
||
xPub, errX := bip32.NewKeyFromString(testXPub) | ||
require.NoError(t, errX) | ||
|
||
t.Run("DeriveAddresses 1", func(t *testing.T) { | ||
internal, err := DeriveAddress(xPub, ChainInternal, 1) | ||
require.NoError(t, err) | ||
var external string | ||
external, err = DeriveAddress(xPub, ChainExternal, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, "1PQW54xMn5KA6uK7wgfzN4y7ZXMi6o7Qtm", internal) | ||
assert.Equal(t, "16fq7PmmXXbFUG5maT5Xvr2zDBUgN1xdMF", external) | ||
}) | ||
|
||
t.Run("DeriveAddresses 2", func(t *testing.T) { | ||
internal, err := DeriveAddress(xPub, ChainInternal, 2) | ||
require.NoError(t, err) | ||
var external string | ||
external, err = DeriveAddress(xPub, ChainExternal, 2) | ||
require.NoError(t, err) | ||
assert.Equal(t, "13St2SHkw1b8ZuaExyMf6ZMEzNjYbWRqL4", internal) | ||
assert.Equal(t, "19jswATg9vBFta1aRnEjPHa2KMwafkmANj", external) | ||
}) | ||
} | ||
|
||
// Benchmark_DeriveAddresses will benchmark the method DeriveAddresses() | ||
func Benchmark_DeriveAddresses(b *testing.B) { | ||
|
||
xPub, errX := bip32.NewKeyFromString(testXPub) | ||
if errX != nil { | ||
b.Fail() | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _, _ = DeriveAddresses(xPub, uint32(i)) | ||
} | ||
} | ||
|
||
// Benchmark_DeriveAddress will benchmark the method DeriveAddress() | ||
func Benchmark_DeriveAddress(b *testing.B) { | ||
|
||
xPub, errX := bip32.NewKeyFromString(testXPub) | ||
if errX != nil { | ||
b.Fail() | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = DeriveAddress(xPub, ChainInternal, uint32(i)) | ||
} | ||
} |