forked from xplorfin/lndmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcd.go
109 lines (95 loc) · 3.1 KB
/
btcd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package mock
import (
"fmt"
"strconv"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
)
// CreateBtcdContainer creates a btccontainer with a mining address address
func (c LightningMocker) createBtcdContainerWithAddress(address string) (ctn BtcdContainer, err error) {
ctn.c = &c
ctn.PortMap = c.portsToMap([]int{8334, 18333, 18334, 18555, 18556, 28901, 28902})
newEnvArgs := append(EnvArgs(), fmt.Sprintf("%s=%s", MiningAddressName, address))
created, err := c.CreateContainer(&container.Config{
Image: "ghcr.io/xplorfin/btcd:latest",
Env: newEnvArgs,
Tty: false,
Entrypoint: []string{"./start-btcd.sh"},
Labels: c.GetSessionLabels(),
}, &container.HostConfig{
NetworkMode: NetworkName,
// expose all btcd ports (these will vary across all boot sand for debugging)
PortBindings: ctn.PortMap.NatMap(),
Mounts: []mount.Mount{
{
Source: "shared",
Target: "/rpc",
Type: mount.TypeVolume,
},
{
Source: "bitcoin",
Target: "/data",
Type: mount.TypeVolume,
},
},
}, nil, nil, "blockchain")
if err != nil {
return ctn, err
}
ctn.id = created.ID
err = c.ContainerStart(c.Ctx, created.ID, types.ContainerStartOptions{})
if err != nil {
return ctn, err
}
c.PrintContainerLogs(created.ID)
return ctn, nil
}
// CreateBtcdContainer creates a BtcdContainer and starts it so it can
// respond to rpc requests. Mining must be done manually and a mining address
// should be set using BtcdContainer.MineToAddress.
func (c LightningMocker) CreateBtcdContainer() (ctn BtcdContainer, err error) {
return c.createBtcdContainerWithAddress("")
}
// BtcdContainer object contains methods that allow us to interact with a created
// btcd container
type BtcdContainer struct {
// id of the current docker container
id string
// c reference to the lightning mocker object
c *LightningMocker
// PortMap contains a list of ports on the host to the internal container port
PortMap PortMap
}
// Mine a given number of blocks
func (b *BtcdContainer) Mine(blocks int) (err error) {
_, err = b.c.Exec(b.id, []string{"/start-btcctl.sh", "generate", strconv.Itoa(blocks)})
return err
}
// MineToAddress mines a given number of block rewards to an address
func (b *BtcdContainer) MineToAddress(address string, blocks int) (err error) {
b.id, err = b.recreateWithMiningAddress(b.id, address)
if err != nil {
return err
}
// generate n-blocks
return b.Mine(blocks)
}
// recreateWithMiningAddress recreates the btcd container with a mining address
// (any subsequent blocks rewards will go to this address)
func (b *BtcdContainer) recreateWithMiningAddress(containerID string, miningAddress string) (id string, err error) {
// remove the old container
err = b.c.StopContainer(containerID)
if err != nil {
return containerID, err
}
err = b.c.ContainerRemove(b.c.Ctx, containerID, types.ContainerRemoveOptions{})
if err != nil {
return containerID, err
}
ctn, err := b.c.createBtcdContainerWithAddress(miningAddress)
if err != nil {
return containerID, err
}
return ctn.id, nil
}