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

Backend/Utxo: support native segwit #211

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,53 @@ jobs:
run: |
sudo apt update
./scripts/snap_release.sh

snap_pkg_beta:

needs:
- conventions

runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v1
- name: Install snap tools
run: |
sudo apt update
./scripts/install_snapcraft.sh

# hack to disable msbuild detection
# NOTE: you might think an easier way to do this would be use container:\nimage: ubuntu22.04 and then not install msbuild,
# but that doesn't work because we get the following error when trying to install snapcraft via `snap install --classic`:
# > error: cannot communicate with server: Post "http://localhost/v2/snaps/snapcraft": dial unix /run/snapd.socket: connect: no such file or directory
- name: HACK to emulate msbuild uninstall
run: sudo rm `which msbuild`

- name: Bump snap version
run: |
git submodule foreach git fetch --all && git submodule sync --recursive && git submodule update --init --recursive
dotnet fsi ./scripts/snap_bump.fsx

- name: Generate snap package
run: |
# retry 3 times because of flakey nuget; TODO: remove retry when we migrate to .NET6 (removing LEGACY_FRAMEWORK support)
./scripts/snap_build.sh --native-segwit || ./scripts/snap_build.sh --native-segwit || ./scripts/snap_build.sh --native-segwit || ./scripts/snap_build.sh --native-segwit

- name: Install snap
# dangerous because it's a local snap (not one from the SnapStore)
run: sudo snap install --dangerous *.snap

- name: Test snap
run: geewallet --version

- uses: actions/upload-artifact@v3
name: Upload snap package as artifact
with:
name: snap_beta
path: ./*.snap

- name: Upload snap package to Snap Store
env:
SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }}
run: |
sudo apt update
./scripts/snap_release.sh beta
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ In the development side of things, we advocate for simplicity:
This list is the (intended) order of preference for new features:

- Migration from Xamarin.Forms to MAUI (in progress, see the PR#199 or its successor).
- Switch from SegWit to native-SegWit (Bech32): PR#211.
- Make native-SegWit (Bech32) be default for bitcoin (right now this is the case for the beta version of our snap package).
- Support for payment-channels & state-channels (in BTC/LTC via lightning, see 'lightning' branch; and in ETH/ETC/DAI via Connext?).
- Explore better stablecoin approach than L1-DAI because DAI is not 100% decentralized (e.g. LUSD better?) and because L1 is too high fees (e.g. sDAI better? see https://twitter.com/koeppelmann/status/1736766570825654630 ).
- Automatic reminders for Seed/password checks to make sure user has not forgotten them (see https://twitter.com/takinbo/status/1201529679519330305 ).
Expand Down
22 changes: 15 additions & 7 deletions scripts/bump.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20230812-0646.git-2268d50"
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
open System.Configuration
Expand All @@ -21,15 +21,18 @@ let IsStable miniVersion =
(int miniVersion % 2) = 0

let args = Misc.FsxOnlyArguments()

let isAuto = List.contains "--auto" args

let suppliedVersion =
if args.Length > 0 then
if args.Length > 1 then
Console.Error.WriteLine "Only one argument supported, not more"
if args.Length > 2 then
Console.Error.WriteLine "Only two argument supported, not more"
Environment.Exit 1
failwith "Unreachable"
else
let full = Version(args.Head)
if not (IsStable full.Build) then
if (not isAuto) && not (IsStable full.Build) then
Console.Error.WriteLine "Mini-version (previous-to-last number, e.g. 2 in 0.1.2.3) should be an even (stable) number"
Environment.Exit 2
failwith "Unreachable"
Expand Down Expand Up @@ -263,14 +266,19 @@ if not replaceScript.Exists then
GitDiff()

Console.WriteLine "Bumping..."
RunUpdateServers()
if not isAuto then
RunUpdateServers()
let fullUnstableVersion,newFullStableVersion = Bump true
GitCommit fullUnstableVersion newFullStableVersion
GitTag newFullStableVersion
if not isAuto then
GitCommit fullUnstableVersion newFullStableVersion
GitTag newFullStableVersion

Console.WriteLine (sprintf "Version bumped to %s."
(newFullStableVersion.ToString()))

if isAuto then
Environment.Exit 0

if isReleaseManual then
Console.WriteLine "Release binaries now and press any key when you finish."
Console.ReadKey true |> ignore
Expand Down
26 changes: 24 additions & 2 deletions scripts/configure.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20230812-0646.git-2268d50"
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
open System.Configuration
Expand Down Expand Up @@ -187,6 +187,18 @@ let fsxRunner =
fsxRunnerBinText
buildConfigFile.Name

let AddToDefinedConstants (constant: string) (configMap: Map<string, string>) =
let configKey = "DefineConstants"

match configMap.TryFind configKey with
| None ->
configMap
|> Map.add configKey constant
| Some previousConstants ->
configMap
|> Map.add configKey (sprintf "%s;%s" previousConstants constant)


let configFileToBeWritten =
let initialConfigFile = Map.empty.Add("Prefix", prefix.FullName)

Expand All @@ -195,11 +207,21 @@ let configFileToBeWritten =
| Some theTool -> initialConfigFile.Add("LegacyBuildTool", theTool)
| None -> initialConfigFile

let finalConfigFile =
let configFileStageThree =
match buildTool with
| Some theTool -> configFileStageTwo.Add("BuildTool", theTool)
| None -> configFileStageTwo

let finalConfigFile =
let nativeSegwitEnabled =
Misc.FsxOnlyArguments()
|> List.contains "--native-segwit"
if nativeSegwitEnabled then
configFileStageThree
|> AddToDefinedConstants "NATIVE_SEGWIT"
else
configFileStageThree

finalConfigFile

let lines =
Expand Down
2 changes: 1 addition & 1 deletion scripts/find.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open System.IO
open System.Linq

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20230812-0646.git-2268d50"
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
#load "fsx/Fsdk/Misc.fs"
Expand Down
2 changes: 1 addition & 1 deletion scripts/fsx
2 changes: 1 addition & 1 deletion scripts/sanitycheck.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open System.Xml.Linq
open System.Xml.XPath

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20230812-0646.git-2268d50"
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
open System.Configuration
Expand Down
2 changes: 1 addition & 1 deletion scripts/snap_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DEBIAN_FRONTEND=noninteractive sudo apt install -y fsharp build-essential pkg-co
# just in case this is a retry-run, we want to clean artifacts from previous try
rm -rf ./staging

./configure.sh --prefix=./staging
./configure.sh --prefix=./staging "$@"
make
make install

Expand Down
33 changes: 33 additions & 0 deletions scripts/snap_bump.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env fsharpi

open System
open System.IO

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
open System.Configuration

#load "fsx/Fsdk/Misc.fs"
#load "fsx/Fsdk/Process.fs"
#load "fsx/Fsdk/Git.fs"
#endif
open Fsdk
open Fsdk.Process

#load "fsxHelper.fs"
open GWallet.Scripting

let currentVersion = Misc.GetCurrentVersion(FsxHelper.RootDir)

let newVersion =
// e.g. to bump from 0.7.x.y to 0.9.x.y
Version(currentVersion.Major, currentVersion.Minor + 2, currentVersion.Build, currentVersion.Revision).ToString()

Process.Execute(
{
Command = "dotnet"
Arguments = sprintf "fsi %s %s --auto" (Path.Combine(FsxHelper.ScriptsDir.FullName, "bump.fsx")) newVersion
}, Echo.All
).UnwrapDefault() |> ignore<string>
13 changes: 10 additions & 3 deletions scripts/snap_release.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ open System.Xml.Linq
open System.Xml.XPath

#if !LEGACY_FRAMEWORK
#r "nuget: Fsdk, Version=0.6.0--date20230812-0646.git-2268d50"
#r "nuget: Fsdk, Version=0.6.0--date20231031-0834.git-2737eea"
#else
#r "System.Configuration"
open System.Configuration
Expand Down Expand Up @@ -149,8 +149,15 @@ Process.Execute({ Command = "snapcraft"; Arguments = "login --with snapcraft.log
Console.WriteLine "Login successfull. Upload starting..."

let snapPush =
// the 'stable' and 'candidate' channels require 'stable' grade in the yaml
let channel = "stable"
let channel =
match Misc.FsxOnlyArguments() with
| [ channel ] ->
channel
| [] ->
// the 'stable' and 'candidate' channels require 'stable' grade in the yaml
"stable"
| _ ->
failwith "Invalid arguments"

Process.Execute(
{
Expand Down
4 changes: 2 additions & 2 deletions src/GWallet.Backend.Tests/ElectrumIntegrationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ type ElectrumIntegrationTests() =
let currency = Currency.BTC
let argument = GetScriptHash currency
CheckElectrumServersConnection ElectrumServerSeedList.DefaultBtcList currency
(ElectrumClient.GetBalance argument) BalanceAssertion
(ElectrumClient.GetBalances (List.singleton argument)) BalanceAssertion

[<Test>]
member __.``can connect (just check balance) to some electrum LTC servers``() =
let currency = Currency.LTC
let argument = GetScriptHash currency
CheckElectrumServersConnection ElectrumServerSeedList.DefaultLtcList currency
(ElectrumClient.GetBalance argument) BalanceAssertion
(ElectrumClient.GetBalances (List.singleton argument)) BalanceAssertion

[<Test>]
member __.``can get list UTXOs of an address from some electrum BTC servers``() =
Expand Down
7 changes: 7 additions & 0 deletions src/GWallet.Backend/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ module Config =
// balances, so you might find discrepancies (e.g. the donut-chart-view)
let internal NoNetworkBalanceForDebuggingPurposes = false

let internal UseNativeSegwit =
#if NATIVE_SEGWIT
true
#else
false
#endif

let IsWindowsPlatform() =
RuntimeInformation.IsOSPlatform OSPlatform.Windows

Expand Down
2 changes: 1 addition & 1 deletion src/GWallet.Backend/ServerManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module ServerManager =
failwith <| SPrintF1 "Currency %A not UTXO?" currency
let utxoFunc electrumServer =
async {
let! bal = UtxoCoin.ElectrumClient.GetBalance scriptHash electrumServer
let! bal = UtxoCoin.ElectrumClient.GetBalances (List.singleton scriptHash) electrumServer
return bal.Confirmed |> decimal
}
UtxoCoin.Server.GetServerFuncs utxoFunc servers |> Some
Expand Down
29 changes: 26 additions & 3 deletions src/GWallet.Backend/UtxoCoin/ElectrumClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module ElectrumClient =
| { Encrypted = false; Protocol = Tcp port } ->
Init electrumServer.ServerInfo.NetworkPath port

let GetBalance (scriptHash: string) (stratumServer: Async<StratumClient>) = async {
let GetBalances (scriptHashes: List<string>) (stratumServer: Async<StratumClient>) = async {
// FIXME: we should rather implement this method in terms of:
// - querying all unspent transaction outputs (X) -> block heights included
// - querying transaction history (Y) -> block heights included
Expand All @@ -67,8 +67,31 @@ module ElectrumClient =
// [ see https://www.youtube.com/watch?v=hjYCXOyDy7Y&feature=youtu.be&t=1171 for more information ]
// * -> although that would be fixing only half of the problem, we also need proof of completeness
aarani marked this conversation as resolved.
Show resolved Hide resolved
let! stratumClient = stratumServer
let! balanceResult = stratumClient.BlockchainScriptHashGetBalance scriptHash
return balanceResult.Result
let rec innerGetBalances (scriptHashes: List<string>) (result: BlockchainScriptHashGetBalanceInnerResult) =
async {
match scriptHashes with
| scriptHash::otherScriptHashes ->
let! balanceHash = stratumClient.BlockchainScriptHashGetBalance scriptHash

return!
innerGetBalances
otherScriptHashes
{
result with
Unconfirmed = result.Unconfirmed + balanceHash.Result.Unconfirmed
Confirmed = result.Confirmed + balanceHash.Result.Confirmed
}
| [] ->
return result
}

return!
innerGetBalances
scriptHashes
{
Unconfirmed = 0L
Confirmed = 0L
}
}

let GetUnspentTransactionOutputs scriptHash (stratumServer: Async<StratumClient>) = async {
Expand Down
Loading
Loading