Skip to content

Commit

Permalink
Merge pull request #10 from ko-matsu/feature/add-initialblockdownload…
Browse files Browse the repository at this point in the history
…-opt

feat: add checking initialblockdownload option
  • Loading branch information
k-matsuzawa authored May 8, 2023
2 parents 90d7226 + f0b6b95 commit 6808e9d
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 55 deletions.
26 changes: 15 additions & 11 deletions cmd/generateblock/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ const (
)

type argument struct {
Host string `help:"connection host & port"`
FedpegScript string `arg:"-s" help:"fedpeg script on dynafed"`
Pak []string `arg:"-p,separate" help:"pak entries"`
Network string `arg:"-n" help:"network. (bitcoin:mainnet/testnet/regtest, liquid:liquidv1/liquidregtest/elementsregtest)"`
Address string `arg:"-a" help:"bitcoin address for generatetoaddress"`
RpcUserID string `help:"connection rpc userID"`
RpcPassword string `help:"connection rpc password"`
Logging bool `arg:"-l" help:"log output"`
PollingTime time.Duration `arg:"-t" help:"polling duration time"`
GenerateCount uint `arg:"-c" help:"generate count"`
IgnoreEmptyMempool bool `arg:"-m" help:"ignore empty mempool"`
Host string `help:"connection host & port"`
FedpegScript string `arg:"-s" help:"fedpeg script on dynafed"`
Pak []string `arg:"-p,separate" help:"pak entries"`
Network string `arg:"-n" help:"network. (bitcoin:mainnet/testnet/regtest, liquid:liquidv1/liquidregtest/elementsregtest)"`
Address string `arg:"-a" help:"bitcoin address for generatetoaddress"`
RpcUserID string `help:"connection rpc userID"`
RpcPassword string `help:"connection rpc password"`
Logging bool `arg:"-l" help:"log output"`
PollingTime time.Duration `arg:"-t" help:"polling duration time"`
GenerateCount uint `arg:"-c" help:"generate count"`
IgnoreEmptyMempool bool `arg:"-m" help:"ignore empty mempool"`
HasCheckInitialBlkDl bool `arg:"-i" help:"check initial block download flag"`
}

// Error returns the error string.
Expand Down Expand Up @@ -55,6 +56,9 @@ func (a *argument) setValueFromEnvironment(env *environment) {
if !a.IgnoreEmptyMempool {
a.IgnoreEmptyMempool = env.IgnoreEmptyMempool
}
if !a.HasCheckInitialBlkDl {
a.HasCheckInitialBlkDl = env.HasCheckInitialBlkDl
}
}

func (a *argument) Validate() error {
Expand Down
19 changes: 10 additions & 9 deletions cmd/generateblock/environment.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

type environment struct {
Host string `env:"GENERATE_BLOCK_CONNECTION_HOST"`
FedpegScript string `env:"DYNAFED_FEDPEG_SCRIPT"`
PakEntries []string `env:"DYNAFED_PAK" envSeparator:","`
Network string `env:"GENERATE_BLOCK_CONNECTION_NETWORK"`
Address string `env:"GENERATE_BLOCK_GENERATETOADDRESS"`
RpcUserID string `env:"CONNECTION_PRC_USERID"`
RpcPassword string `env:"CONNECTION_PRC_PASSWORD"`
GenerateCount uint `env:"GENERATE_BLOCK_COUNT" envDefault:"1"`
IgnoreEmptyMempool bool `env:"IGNORE_EMPTY_MEMPOOL"`
Host string `env:"GENERATE_BLOCK_CONNECTION_HOST"`
FedpegScript string `env:"DYNAFED_FEDPEG_SCRIPT"`
PakEntries []string `env:"DYNAFED_PAK" envSeparator:","`
Network string `env:"GENERATE_BLOCK_CONNECTION_NETWORK"`
Address string `env:"GENERATE_BLOCK_GENERATETOADDRESS"`
RpcUserID string `env:"CONNECTION_PRC_USERID"`
RpcPassword string `env:"CONNECTION_PRC_PASSWORD"`
GenerateCount uint `env:"GENERATE_BLOCK_COUNT" envDefault:"1"`
IgnoreEmptyMempool bool `env:"IGNORE_EMPTY_MEMPOOL"`
HasCheckInitialBlkDl bool `env:"HAS_CHECK_INITIAL_BLK_DL"`
}
2 changes: 2 additions & 0 deletions cmd/generateblock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func main() {
argObj.Address,
argObj.GenerateCount,
argObj.IgnoreEmptyMempool,
argObj.HasCheckInitialBlkDl,
); err != nil {
if err == pkgerror.ErrEmptyMempoolTx {
logger.Debug("empty mempool tx. skip generate block.")
Expand Down Expand Up @@ -119,6 +120,7 @@ func run(handle handler.Handler, network string, pollingTime time.Duration) erro
argObj.Address,
argObj.GenerateCount,
argObj.IgnoreEmptyMempool,
argObj.HasCheckInitialBlkDl,
); err != nil {
if err == pkgerror.ErrEmptyMempoolTx {
logger.Debug("empty mempool tx. skip generate block.")
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ services:
- testing_network
command: ["generateblock", "-l", "-t", "10s"]

generateblock-elements-loop:
build:
context: .
dockerfile: ./docker/debian11.dockerfile
volumes:
- .:/workspace
working_dir: /workspace/
environment: *elements-node-env
networks:
- testing_network
command: ["generateblock", "-l", "-t", "10s"]

exec-test:
image: ghcr.io/cryptogarageinc/elements-testing:v0.2.5
volumes:
Expand Down
22 changes: 17 additions & 5 deletions generateblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type Generator struct {
handle handler.Handler
network string

ignoreEmptyMempool bool
ignoreEmptyMempool bool
hasCheckInitialBlkDl bool
}

func NewGenerator(
Expand All @@ -37,12 +38,19 @@ func (g *Generator) WithIgnoreEmptyMempool(ignoreEmptyMempool bool) *Generator {
return g
}

func (g *Generator) WithCheckInitialBlockDownload(hasCheckInitialBlkDl bool) *Generator {
g.hasCheckInitialBlkDl = hasCheckInitialBlkDl
return g
}

func (g *Generator) GenerateBlock(
ctx context.Context,
address string,
generateCount uint,
) error {
return g.handle.GenerateBlock(ctx, g.network, "", []string{}, address, generateCount, g.ignoreEmptyMempool)
return g.handle.GenerateBlock(
ctx, g.network, "", []string{}, address, generateCount,
g.ignoreEmptyMempool, g.hasCheckInitialBlkDl)
}

func (g *Generator) GenerateElementsDynafedBlock(
Expand All @@ -51,7 +59,9 @@ func (g *Generator) GenerateElementsDynafedBlock(
pakEntries []string,
generateCount uint,
) error {
return g.handle.GenerateBlock(ctx, g.network, fedpegScript, pakEntries, "", generateCount, g.ignoreEmptyMempool)
return g.handle.GenerateBlock(
ctx, g.network, fedpegScript, pakEntries, "", generateCount,
g.ignoreEmptyMempool, g.hasCheckInitialBlkDl)
}

func GenerateBlock(
Expand All @@ -62,7 +72,8 @@ func GenerateBlock(
generateCount uint,
) error {
handle := newHandler(nodeInfo.Host, nodeInfo.RpcUserID, nodeInfo.RpcPassword)
return handle.GenerateBlock(ctx, network, "", []string{}, address, generateCount, false)
return handle.GenerateBlock(ctx, network, "", []string{}, address,
generateCount, false, false)
}

func GenerateElementsDynafedBlock(
Expand All @@ -74,7 +85,8 @@ func GenerateElementsDynafedBlock(
generateCount uint,
) error {
handle := newHandler(nodeInfo.Host, nodeInfo.RpcUserID, nodeInfo.RpcPassword)
return handle.GenerateBlock(ctx, network, fedpegScript, pakEntries, "", generateCount, false)
return handle.GenerateBlock(ctx, network, fedpegScript, pakEntries, "",
generateCount, false, false)
}

func newHandler(
Expand Down
7 changes: 5 additions & 2 deletions internal/domain/model/blockchaininfo.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package model

type BlockChainInfo struct {
Blocks uint64
BestBlockHash string
Blocks uint64
BestBlockHash string
IsInitialBlockDownload bool

// elements only parameters
CurrentFedpegScript string
ExtensionSpace []string
}
13 changes: 7 additions & 6 deletions internal/domain/model/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ func ValidateNetworkType(network string) error {
}

type Configuration struct {
Network NetworkType
FedpegScript string
PakEntries []string
Address string
GenerateCount uint
IgnoreEmptyMempool bool
Network NetworkType
FedpegScript string
PakEntries []string
Address string
GenerateCount uint
IgnoreEmptyMempool bool
HasCheckInitialBlkDl bool // check the initialblockdownload
}

func (c *Configuration) GetGenerateCount() uint {
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/repository/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Blockchain interface {
address string,
) (blockHashes []string, err error)
GetMempoolTXIDs(ctx context.Context) ([]string, error)
GetBlockChainInfoWithElements(
GetBlockChainInfo(
ctx context.Context,
) (*model.BlockChainInfo, error)
}
23 changes: 17 additions & 6 deletions internal/domain/service/generate_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ type GenerateBlock interface {
) error
GenerateToAddress(ctx context.Context, address string) error
ExistMempool(ctx context.Context) (bool, error)
IsInitialBlockDownload(ctx context.Context) (bool, error)
CompareDynafed(
ctx context.Context,
fedpegScript string,
pakEntries []string,
) (bool, error)
) (bool, bool, error)
}

const (
Expand Down Expand Up @@ -63,19 +64,29 @@ func (g *generateBlock) ExistMempool(
return len(txIDs) > 0, nil
}

func (g *generateBlock) IsInitialBlockDownload(
ctx context.Context,
) (bool, error) {
bi, err := g.blockchainRepo.GetBlockChainInfo(ctx)
if err != nil {
return false, err
}
return bi.IsInitialBlockDownload, nil
}

func (g *generateBlock) CompareDynafed(
ctx context.Context,
fedpegScript string,
pakEntries []string,
) (bool, error) {
bi, err := g.blockchainRepo.GetBlockChainInfoWithElements(ctx)
) (bool, bool, error) {
bi, err := g.blockchainRepo.GetBlockChainInfo(ctx)
if err != nil {
return false, err
return false, false, err
}
if bi.CurrentFedpegScript != fedpegScript || !g.compareSlice(bi.ExtensionSpace, pakEntries) {
return false, nil
return false, bi.IsInitialBlockDownload, nil
}
return true, nil
return true, bi.IsInitialBlockDownload, nil
}

func (g *generateBlock) compareSlice(src []string, dst []string) bool {
Expand Down
15 changes: 9 additions & 6 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Handler interface {
address string,
generateCount uint,
ignoreEmptyMempool bool,
hasCheckInitialBlkDl bool,
) error
}

Expand All @@ -32,6 +33,7 @@ func (h *handler) GenerateBlock(
address string,
generateCount uint,
ignoreEmptyMempool bool,
hasCheckInitialBlkDl bool,
) error {
if networkType == "" {
return errors.New("networkType is empty")
Expand All @@ -40,12 +42,13 @@ func (h *handler) GenerateBlock(
return err
}
return h.usecase.GenerateBlock(ctx, &model.Configuration{
Network: model.NewNetworkType(networkType),
FedpegScript: fedpegScript,
PakEntries: pak,
Address: address,
GenerateCount: generateCount,
IgnoreEmptyMempool: ignoreEmptyMempool,
Network: model.NewNetworkType(networkType),
FedpegScript: fedpegScript,
PakEntries: pak,
Address: address,
GenerateCount: generateCount,
IgnoreEmptyMempool: ignoreEmptyMempool,
HasCheckInitialBlkDl: hasCheckInitialBlkDl,
})
}

Expand Down
16 changes: 10 additions & 6 deletions internal/infrastructure/repository/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (b *blockchainRpc) GetMempoolTXIDs(ctx context.Context) ([]string, error) {
return txIDs, nil
}

func (b *blockchainRpc) GetBlockChainInfoWithElements(ctx context.Context) (*model.BlockChainInfo, error) {
func (b *blockchainRpc) GetBlockChainInfo(ctx context.Context) (*model.BlockChainInfo, error) {
result, _, err := b.post(ctx, "getblockchaininfo")
if err != nil {
return nil, err
Expand All @@ -95,11 +95,15 @@ func (b *blockchainRpc) GetBlockChainInfoWithElements(ctx context.Context) (*mod
rawBlockchainInfo := result.(map[string]interface{})
blockchainInfo.Blocks = uint64(rawBlockchainInfo["blocks"].(float64))
blockchainInfo.BestBlockHash = rawBlockchainInfo["bestblockhash"].(string)
blockchainInfo.CurrentFedpegScript = rawBlockchainInfo["current_fedpeg_script"].(string)
extensionSpace := rawBlockchainInfo["extension_space"].([]interface{})
blockchainInfo.ExtensionSpace = make([]string, len(extensionSpace))
for i := range extensionSpace {
blockchainInfo.ExtensionSpace[i] = extensionSpace[i].(string)
blockchainInfo.IsInitialBlockDownload = rawBlockchainInfo["initialblockdownload"].(bool)

if rawBlockchainInfo["current_fedpeg_script"] != nil {
blockchainInfo.CurrentFedpegScript = rawBlockchainInfo["current_fedpeg_script"].(string)
extensionSpace := rawBlockchainInfo["extension_space"].([]interface{})
blockchainInfo.ExtensionSpace = make([]string, len(extensionSpace))
for i := range extensionSpace {
blockchainInfo.ExtensionSpace[i] = extensionSpace[i].(string)
}
}
return blockchainInfo, nil
}
Expand Down
21 changes: 18 additions & 3 deletions internal/usecase/generate_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,29 @@ func (g *generateBlock) GenerateBlock(
case err != nil:
return err
case !exist && !config.CanDynafed():
return pkgerror.ErrEmptyMempoolTx
if config.HasCheckInitialBlkDl {
isInitialBlkDl, err := g.generateBlockService.IsInitialBlockDownload(ctx)
if err != nil {
return err
} else if isInitialBlkDl {
// need to generate block
} else {
return pkgerror.ErrEmptyMempoolTx
}
} else {
return pkgerror.ErrEmptyMempoolTx
}
case !exist && config.CanDynafed():
compare, err := g.generateBlockService.CompareDynafed(
compare, isInitialBlkDl, err := g.generateBlockService.CompareDynafed(
ctx, config.FedpegScript, config.PakEntries)
if err != nil {
return err
} else if compare {
return pkgerror.ErrEmptyMempoolTx
if config.HasCheckInitialBlkDl && isInitialBlkDl {
// need to generate block
} else {
return pkgerror.ErrEmptyMempoolTx
}
}
// need to generate block.
}
Expand Down

0 comments on commit 6808e9d

Please sign in to comment.