Skip to content

Commit b193711

Browse files
committed
Add LBRY Support
Been chatting with the Ren team on Telegram and we are very eager to add support for renLBC! We're a fork of Bitcoin Core, so integration should be fairly straightforward. Note: Rebased and made updates to LBRY params on 1/20/2021. Happy New Year Ren Team, looking forward to working together.
1 parent cd69006 commit b193711

18 files changed

Lines changed: 590 additions & 2 deletions

chain/lbry/address.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package lbry
2+
3+
import "github.com/renproject/multichain/chain/bitcoin"
4+
5+
type (
6+
// AddressEncoder re-exports bitcoin.AddressEncoder.
7+
AddressEncoder = bitcoin.AddressEncoder
8+
9+
// AddressDecoder re-exports bitcoin.AddressDecoder.
10+
AddressDecoder = bitcoin.AddressDecoder
11+
12+
// AddressEncodeDecoder re-exports bitcoin.AddressEncodeDecoder.
13+
AddressEncodeDecoder = bitcoin.AddressEncodeDecoder
14+
)

chain/lbry/address_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package lbry_test

chain/lbry/gas.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package lbry
2+
3+
import "github.com/renproject/multichain/chain/bitcoin"
4+
5+
// GasEstimator re-exports bitcoin.GasEstimator.
6+
type GasEstimator = bitcoin.GasEstimator
7+
8+
// NewGasEstimator re-exports bitcoin.NewGasEstimator.
9+
var NewGasEstimator = bitcoin.NewGasEstimator

chain/lbry/gas_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package lbry_test
2+
3+
import (
4+
"context"
5+
6+
"github.com/renproject/multichain/chain/lbry"
7+
"github.com/renproject/pack"
8+
9+
. "github.com/onsi/ginkgo"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
var _ = Describe("Gas", func() {
14+
Context("when estimating LBRY network fee", func() {
15+
It("should work", func() {
16+
ctx, cancel := context.WithCancel(context.Background())
17+
defer cancel()
18+
19+
client := lbry.NewClient(lbry.DefaultClientOptions())
20+
21+
// estimate fee to include tx within 1 block.
22+
fallback1 := uint64(123)
23+
gasEstimator1 := lbry.NewGasEstimator(client, 1, pack.NewU256FromUint64(fallback1))
24+
gasPrice1, _, err := gasEstimator1.EstimateGas(ctx)
25+
if err != nil {
26+
Expect(gasPrice1).To(Equal(pack.NewU256FromUint64(fallback1)))
27+
}
28+
29+
// estimate fee to include tx within 10 blocks.
30+
fallback2 := uint64(234)
31+
gasEstimator2 := lbry.NewGasEstimator(client, 10, pack.NewU256FromUint64(fallback2))
32+
gasPrice2, _, err := gasEstimator2.EstimateGas(ctx)
33+
if err != nil {
34+
Expect(gasPrice2).To(Equal(pack.NewU256FromUint64(fallback2)))
35+
}
36+
37+
// estimate fee to include tx within 100 blocks.
38+
fallback3 := uint64(345)
39+
gasEstimator3 := lbry.NewGasEstimator(client, 100, pack.NewU256FromUint64(fallback3))
40+
gasPrice3, _, err := gasEstimator3.EstimateGas(ctx)
41+
if err != nil {
42+
Expect(gasPrice3).To(Equal(pack.NewU256FromUint64(fallback3)))
43+
}
44+
45+
// expect fees in this order at the very least.
46+
if err == nil {
47+
Expect(gasPrice1.GreaterThanEqual(gasPrice2)).To(BeTrue())
48+
Expect(gasPrice2.GreaterThanEqual(gasPrice3)).To(BeTrue())
49+
}
50+
})
51+
})
52+
})

chain/lbry/lbry.go

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package lbry
2+
3+
import (
4+
"time"
5+
6+
"github.com/btcsuite/btcd/chaincfg"
7+
"github.com/btcsuite/btcd/chaincfg/chainhash"
8+
"github.com/btcsuite/btcd/wire"
9+
)
10+
11+
func init() {
12+
if err := chaincfg.Register(&MainNetParams); err != nil {
13+
panic(err)
14+
}
15+
if err := chaincfg.Register(&TestNetParams); err != nil {
16+
panic(err)
17+
}
18+
if err := chaincfg.Register(&RegressionNetParams); err != nil {
19+
panic(err)
20+
}
21+
}
22+
23+
// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
24+
// the main network, regression test network, and test network (version 3).
25+
var genesisCoinbaseTx = wire.MsgTx{
26+
Version: 1,
27+
TxIn: []*wire.TxIn{
28+
{
29+
PreviousOutPoint: wire.OutPoint{
30+
Hash: chainhash.Hash{},
31+
Index: 0xffffffff,
32+
},
33+
SignatureScript: []byte{
34+
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x17,
35+
0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x74,
36+
0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70,
37+
0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
38+
},
39+
Sequence: 0xffffffff,
40+
},
41+
},
42+
TxOut: []*wire.TxOut{
43+
{
44+
Value: 0x12a05f200,
45+
PkScript: []byte{
46+
0x76, 0xa9, 0x14, 0x34, 0x59, 0x91, 0xdb, 0xf5,
47+
0x7b, 0xfb, 0x01, 0x4b, 0x87, 0x00, 0x6a, 0xcd,
48+
0xfa, 0xfb, 0xfc, 0x5f, 0xe8, 0x29, 0x2f, 0x88,
49+
0xac,
50+
},
51+
},
52+
},
53+
LockTime: 0,
54+
}
55+
56+
// https://github.com/lbryio/lbrycrd/blob/master/src/chainparams.cpp#L19
57+
var genesisMerkleRoot = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
58+
0xcc, 0x59, 0xe5, 0x9f, 0xf9, 0x7a, 0xc0, 0x92,
59+
0xb5, 0x5e, 0x42, 0x3a, 0xa5, 0x49, 0x51, 0x51,
60+
0xed, 0x6f, 0xb8, 0x05, 0x70, 0xa5, 0xbb, 0x78,
61+
0xcd, 0x5b, 0xd1, 0xc3, 0x82, 0x1c, 0x21, 0xb8,
62+
})
63+
64+
var genesisBlock = wire.MsgBlock{
65+
Header: wire.BlockHeader{
66+
Version: 1,
67+
PrevBlock: chainhash.Hash{}, // 0000000000000000000000000000000000000000000000000000000000000000
68+
MerkleRoot: genesisMerkleRoot, // b8211c82c3d15bcd78bba57005b86fed515149a53a425eb592c07af99fe559cc
69+
Timestamp: time.Unix(1446058291, 0), // Wednesday, October 28, 2015 6:51:31 PM GMT
70+
Bits: 0x1f00ffff,
71+
Nonce: 1287,
72+
},
73+
Transactions: []*wire.MsgTx{&genesisCoinbaseTx},
74+
}
75+
76+
var genesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
77+
0x63, 0xf4, 0x34, 0x6a, 0x4d, 0xb3, 0x4f, 0xdf,
78+
0xce, 0x29, 0xa7, 0x0f, 0x5e, 0x8d, 0x11, 0xf0,
79+
0x65, 0xf6, 0xb9, 0x16, 0x02, 0xb7, 0x03, 0x6c,
80+
0x7f, 0x22, 0xf3, 0xa0, 0x3b, 0x28, 0x89, 0x9c,
81+
})
82+
83+
func newHashFromStr(hexStr string) *chainhash.Hash {
84+
hash, err := chainhash.NewHashFromStr(hexStr)
85+
if err != nil {
86+
panic(err)
87+
}
88+
return hash
89+
}
90+
91+
// MainNetParams returns the chain configuration for mainnet.
92+
var MainNetParams = chaincfg.Params{
93+
Name: "mainnet",
94+
Net: 0xfae4aaf1,
95+
DefaultPort: "9246",
96+
97+
// Chain parameters
98+
GenesisBlock: &genesisBlock,
99+
GenesisHash: &genesisHash,
100+
101+
// Address encoding magics
102+
PubKeyHashAddrID: 85,
103+
ScriptHashAddrID: 122,
104+
PrivateKeyID: 28,
105+
WitnessPubKeyHashAddrID: 0x06, // starts with p2
106+
WitnessScriptHashAddrID: 0x0A, // starts with 7Xh
107+
BIP0034Height: 1,
108+
BIP0065Height: 200000,
109+
BIP0066Height: 200000,
110+
111+
// BIP32 hierarchical deterministic extended key magics
112+
HDPrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv
113+
HDPublicKeyID: [4]byte{0x04, 0x88, 0xb2, 0x1e}, // starts with xpub
114+
115+
// Human-readable part for Bech32 encoded segwit addresses, as defined in
116+
// BIP 173.
117+
Bech32HRPSegwit: "lbc",
118+
119+
// BIP44 coin type used in the hierarchical deterministic path for
120+
// address generation.
121+
HDCoinType: 0x8c,
122+
}
123+
124+
// TestNetParams returns the chain configuration for testnet.
125+
var TestNetParams = chaincfg.Params{
126+
Name: "testnet",
127+
Net: 0xfae4aae1,
128+
DefaultPort: "19246",
129+
130+
// Chain parameters
131+
GenesisBlock: &genesisBlock,
132+
GenesisHash: &genesisHash,
133+
134+
// Address encoding magics
135+
PubKeyHashAddrID: 111,
136+
ScriptHashAddrID: 196,
137+
PrivateKeyID: 239,
138+
139+
// BIP32 hierarchical deterministic extended key magics
140+
HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with xprv
141+
HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with xpub
142+
143+
// Human-readable part for Bech32 encoded segwit addresses, as defined in
144+
// BIP 173.
145+
Bech32HRPSegwit: "tlbc",
146+
147+
// BIP44 coin type used in the hierarchical deterministic path for
148+
// address generation.
149+
HDCoinType: 0x8c,
150+
}
151+
152+
// RegressionNetParams returns the chain configuration for regression net.
153+
var RegressionNetParams = chaincfg.Params{
154+
Name: "regtest",
155+
156+
// LBRY has 0xfae4aad1 as RegTest (same as Bitcoin's RegTest).
157+
// Setting it to an arbitrary value (leet_hex(LBRY)), so that we can
158+
// register the regtest network.
159+
Net: 0xfae4aad1,
160+
161+
// Address encoding magics
162+
PubKeyHashAddrID: 111,
163+
ScriptHashAddrID: 196,
164+
PrivateKeyID: 239,
165+
166+
// BIP32 hierarchical deterministic extended key magics
167+
HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94}, // starts with xprv
168+
HDPublicKeyID: [4]byte{0x04, 0x35, 0x87, 0xcf}, // starts with xpub
169+
170+
// Human-readable part for Bech32 encoded segwit addresses, as defined in
171+
// BIP 173.
172+
Bech32HRPSegwit: "rlbc",
173+
174+
// BIP44 coin type used in the hierarchical deterministic path for
175+
// address generation.
176+
HDCoinType: 0x8c,
177+
}

chain/lbry/lbry_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lbry_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestLBRY(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "LBRY Suite")
13+
}

0 commit comments

Comments
 (0)