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

Add support for Qtum #65

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
eec814f
Make initial hacks to get Dockers to run
dezuman Nov 21, 2020
b31da55
Make some changes to test script so it runs "on my machine"
dezuman Nov 21, 2020
533eb4e
Change some generated files I can't be arsed to gitignore
dezuman Nov 21, 2020
8048055
Start (weak) attempt to integrate Qtum
dezuman Nov 21, 2020
d5c4d4c
Fix for Ubuntu shell not recognizing "source" command
dezuman Nov 22, 2020
02ddf2f
Define Qtum private key and address
dezuman Nov 22, 2020
5ee09f2
Add working docker for Qtum
dezuman Nov 22, 2020
bfdb384
Move Qtum testing to execute first for debugging convenience
dezuman Nov 22, 2020
5bffcc6
Tweak test files. Very messy, will clean later
dezuman Nov 23, 2020
e6c9e6a
Add tweaked copy of BTC API that passes unit test
dezuman Nov 23, 2020
bae4692
I really need to learn gitignore
dezuman Nov 23, 2020
faaf79a
Add comment on weirdly high tx fee
dezuman Nov 23, 2020
4e4c1ba
Clean up the docker-side script a bit
dezuman Nov 23, 2020
fc54cfb
Prettify docker stuff
dezuman Nov 25, 2020
79d1c06
Prettify some files, remove comments etc
dezuman Nov 25, 2020
bec793c
Refactor functions to use qtumsuite, add some stuff from btc's version
dezuman Nov 25, 2020
ded126c
Remove some debug prints
dezuman Nov 25, 2020
05d1610
Restore test files to mint condition
dezuman Nov 25, 2020
3b30609
Revert "Make initial hacks to get Dockers to run"
dezuman Nov 21, 2020
3dd24b2
Restore some generated files
dezuman Nov 25, 2020
657e3a7
Add tweaked variants of btcsuite's chaincfg netparams just in case
dezuman Nov 25, 2020
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
82 changes: 82 additions & 0 deletions chain/qtum/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package qtum

// This is a copy of Bitcoin's version with btcsuite swapped to qtumsuite

import (
"fmt"

"github.com/qtumproject/qtumsuite/chaincfg"
"github.com/qtumproject/qtumsuite"
"github.com/qtumproject/qtumsuite/base58"
"github.com/renproject/multichain/api/address"
)

// AddressEncodeDecoder implements the address.EncodeDecoder interface
type AddressEncodeDecoder struct {
AddressEncoder
AddressDecoder
}

// NewAddressEncodeDecoder constructs a new AddressEncodeDecoder with the
// chain specific configurations
func NewAddressEncodeDecoder(params *chaincfg.Params) AddressEncodeDecoder {
return AddressEncodeDecoder{
AddressEncoder: NewAddressEncoder(params),
AddressDecoder: NewAddressDecoder(params),
}
}

// AddressEncoder encapsulates the chain specific configurations and implements
// the address.Encoder interface
type AddressEncoder struct {
params *chaincfg.Params
}

// NewAddressEncoder constructs a new AddressEncoder with the chain specific
// configurations
func NewAddressEncoder(params *chaincfg.Params) AddressEncoder {
return AddressEncoder{params: params}
}

// EncodeAddress implements the address.Encoder interface
func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address.Address, error) {
// Validate that the base58 address is in fact in correct format.
encodedAddr := base58.Encode([]byte(rawAddr))
if _, err := qtumsuite.DecodeAddress(encodedAddr, encoder.params); err != nil {
return address.Address(""), err
}

return address.Address(encodedAddr), nil
}

// AddressDecoder encapsulates the chain specific configurations and implements
// the address.Decoder interface
type AddressDecoder struct {
params *chaincfg.Params
}

// NewAddressDecoder constructs a new AddressDecoder with the chain specific
// configurations
func NewAddressDecoder(params *chaincfg.Params) AddressDecoder {
return AddressDecoder{params: params}
}

// DecodeAddress implements the address.Decoder interface
func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error) {
// Decode the checksummed base58 format address.
decoded, ver, err := base58.CheckDecode(string(addr))
if err != nil {
return nil, fmt.Errorf("checking: %v", err)
}
if len(decoded) != 20 {
return nil, fmt.Errorf("expected len 20, got len %v", len(decoded))
}

// Validate the address format.
switch ver {
case decoder.params.PubKeyHashAddrID, decoder.params.ScriptHashAddrID:
return address.RawAddress(base58.Decode(string(addr))), nil
default:
return nil, fmt.Errorf("unexpected address prefix")
}
}
1 change: 1 addition & 0 deletions chain/qtum/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package qtum_test
11 changes: 11 additions & 0 deletions chain/qtum/gas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package qtum

// I *Think* it is fine to simply use Bitcoin's estimator here

import "github.com/renproject/multichain/chain/bitcoin"

// GasEstimator re-exports bitcoin.GasEstimator.
type GasEstimator = bitcoin.GasEstimator

// NewGasEstimator re-exports bitcoin.NewGasEstimator.
var NewGasEstimator = bitcoin.NewGasEstimator
54 changes: 54 additions & 0 deletions chain/qtum/gas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package qtum_test

// Shamelessly stolen from Dogecoin's re-export of bitcoin's implementation. Steal-ception!

import (
"context"

"github.com/renproject/multichain/chain/qtum"
"github.com/renproject/pack"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Gas", func() {
Context("when estimating qtum network fee", func() {
It("should work", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

client := qtum.NewClient(qtum.DefaultClientOptions())

// estimate fee to include tx within 1 block.
fallback1 := uint64(123)
gasEstimator1 := qtum.NewGasEstimator(client, 1, pack.NewU256FromUint64(fallback1))
gasPrice1, _, err := gasEstimator1.EstimateGas(ctx)
if err != nil {
Expect(gasPrice1).To(Equal(pack.NewU256FromUint64(fallback1)))
}

// estimate fee to include tx within 10 blocks.
fallback2 := uint64(234)
gasEstimator2 := qtum.NewGasEstimator(client, 10, pack.NewU256FromUint64(fallback2))
gasPrice2, _, err := gasEstimator2.EstimateGas(ctx)
if err != nil {
Expect(gasPrice2).To(Equal(pack.NewU256FromUint64(fallback2)))
}

// estimate fee to include tx within 100 blocks.
fallback3 := uint64(345)
gasEstimator3 := qtum.NewGasEstimator(client, 100, pack.NewU256FromUint64(fallback3))
gasPrice3, _, err := gasEstimator3.EstimateGas(ctx)
if err != nil {
Expect(gasPrice3).To(Equal(pack.NewU256FromUint64(fallback3)))
}

// expect fees in this order at the very least.
if err == nil {
Expect(gasPrice1.GreaterThanEqual(gasPrice2)).To(BeTrue())
Expect(gasPrice2.GreaterThanEqual(gasPrice3)).To(BeTrue())
}
})
})
})
Loading