Skip to content

Commit

Permalink
feat: add new mempool transaction types and improve the mempool fetch…
Browse files Browse the repository at this point in the history
…ing logic. bump deps
  • Loading branch information
adrianbrad committed Jan 27, 2024
1 parent 47b1910 commit d3745de
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 224 deletions.
22 changes: 22 additions & 0 deletions btcsuite/btcsuite_rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,28 @@ func (c RPCClient) GetCoinbaseValue(context.Context) (int64, error) {
return v, nil
}

// GetTransactionOutputs returns the outputs of a transaction.
func (c RPCClient) GetTransactionOutputs(
ctx context.Context,
txHash string,
) ([]privatebtc.MempoolTransactionOutput, error) {
tx, err := c.GetTransaction(ctx, txHash)
if err != nil {
return nil, fmt.Errorf("get transaction: %w", err)
}

outputs := make([]privatebtc.MempoolTransactionOutput, len(tx.Vout))

for i, v := range tx.Vout {
outputs[i] = privatebtc.MempoolTransactionOutput{
Address: v.ScriptPubKey.Address,
Value: v.Value,
}
}

return outputs, nil
}

var _ privatebtc.RPCClientFactory = (*RPCClientFactory)(nil)

// RPCClientFactory is a factory for RPC clients.
Expand Down
6 changes: 3 additions & 3 deletions chain_reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (c *ChainReorgWithAssertion) MineBlocksOnNetwork(

discBestBlockHash, err := c.ChainReorg.disconnectedNode.RPCClient().GetBestBlockHash(ctx)
if err != nil {
return nil, fmt.Errorf("get disconnected node best block hash: %w", err)
return nil, fmt.Errorf("get disconnected node best block Hash: %w", err)
}

if bestBlockHash == discBestBlockHash {
Expand Down Expand Up @@ -516,7 +516,7 @@ func (c *ChainReorgWithAssertion) ReconnectNode(ctx context.Context) error {
if syncNetwork := dcBc > nBc; syncNetwork {
blockHash, err := c.disconnectedNode.RPCClient().GetBestBlockHash(ctx)
if err != nil {
return fmt.Errorf("get disconnected node best block hash: %w", err)
return fmt.Errorf("get disconnected node best block Hash: %w", err)
}

const timeout = 5 * time.Second
Expand All @@ -532,7 +532,7 @@ func (c *ChainReorgWithAssertion) ReconnectNode(ctx context.Context) error {
if syncDisconnected := nBc > dcBc; syncDisconnected {
blockHash, err := c.networkNodes[0].RPCClient().GetBestBlockHash(ctx)
if err != nil {
return fmt.Errorf("get network best block hash: %w", err)
return fmt.Errorf("get network best block Hash: %w", err)
}

const timeout = 5 * time.Second
Expand Down
62 changes: 31 additions & 31 deletions chain_reorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

c.GetRawMempoolFunc = func(ctx context.Context) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

disconnectedNodeRPCClient := newChainReorgSuccessRPCClient(peerCount)
Expand All @@ -295,7 +295,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: require.NoError,
expectedHash: "hash",
expectedHash: "Hash",
},
"MustDisconnectFirstError": {
chainReorgArgs: chainReorgArgs{
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

c.GetRawMempoolFunc = func(ctx context.Context) ([]string, error) {
Expand Down Expand Up @@ -409,11 +409,11 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

c.GetRawMempoolFunc = func(ctx context.Context) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

disconnectedNodeRPCClient := newChainReorgSuccessRPCClient(peerCount)
Expand Down Expand Up @@ -464,11 +464,11 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

c.GetRawMempoolFunc = func(ctx context.Context) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

return newPrivateNetworkStartSuccessRPCClientFactory(
Expand Down Expand Up @@ -552,13 +552,13 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

disconenctedNodeRPCClient.GetRawMempoolFunc = func(
ctx context.Context,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

networkRPCClient := newChainReorgSuccessRPCClient(peerCount)
Expand All @@ -585,7 +585,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: require.NoError,
expectedHash: "hash",
expectedHash: "Hash",
},
"MustDisconnectFirstError": {
chainReorgArgs: chainReorgArgs{
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

c.GetRawMempoolFunc = func(ctx context.Context) ([]string, error) {
Expand Down Expand Up @@ -698,13 +698,13 @@ func TestChainReorg(t *testing.T) {
string,
float64,
) (string, error) {
return "hash", nil
return "Hash", nil
}

disconnectedNodeRPCClient.GetRawMempoolFunc = func(
ctx context.Context,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

networkRPCClient := newChainReorgSuccessRPCClient(peerCount)
Expand Down Expand Up @@ -805,13 +805,13 @@ func TestChainReorg(t *testing.T) {
int64,
string,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

networkClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

return newRPCClientFactoryWithDetachedNode(
Expand All @@ -828,7 +828,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: require.NoError,
expectedHashes: []string{"hash"},
expectedHashes: []string{"Hash"},
},
"MustDisconnectFirstError": {
chainReorgArgs: chainReorgArgs{
Expand Down Expand Up @@ -898,7 +898,7 @@ func TestChainReorg(t *testing.T) {
int64,
string,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

c.GetBestBlockHashFunc = func(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -945,13 +945,13 @@ func TestChainReorg(t *testing.T) {
int64,
string,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

networkClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

return newRPCClientFactoryWithDetachedNode(
Expand All @@ -968,7 +968,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: func(t require.TestingT, err error, i ...any) {
require.ErrorContains(t, err, "get disconnected node best block hash")
require.ErrorContains(t, err, "get disconnected node best block Hash")
require.ErrorIs(t, err, assert.AnError)
},
expectedHashes: nil,
Expand All @@ -987,11 +987,11 @@ func TestChainReorg(t *testing.T) {
int64,
string,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

c.GetBestBlockHashFunc = func(ctx context.Context) (string, error) {
return "hash", nil
return "Hash", nil
}

return newPrivateNetworkStartSuccessRPCClientFactory(c)
Expand Down Expand Up @@ -1063,7 +1063,7 @@ func TestChainReorg(t *testing.T) {
networkNodeClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

disconnectedNodeClient := newChainReorgSuccessRPCClient(peerCount)
Expand All @@ -1073,13 +1073,13 @@ func TestChainReorg(t *testing.T) {
int64,
string,
) ([]string, error) {
return []string{"hash"}, nil
return []string{"Hash"}, nil
}

disconnectedNodeClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

return newRPCClientFactoryWithDetachedNode(
Expand All @@ -1096,7 +1096,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: require.NoError,
expectedHashes: []string{"hash"},
expectedHashes: []string{"Hash"},
},
"MustDisconnectFirstError": {
chainReorgArgs: chainReorgArgs{
Expand Down Expand Up @@ -1359,7 +1359,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: func(t require.TestingT, err error, i ...any) {
require.ErrorContains(t, err, "get disconnected node best block hash")
require.ErrorContains(t, err, "get disconnected node best block Hash")
require.ErrorIs(t, err, assert.AnError)
},
},
Expand Down Expand Up @@ -1400,7 +1400,7 @@ func TestChainReorg(t *testing.T) {
disconnectedNodeClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

return newRPCClientFactoryWithDetachedNode(
Expand Down Expand Up @@ -1464,7 +1464,7 @@ func TestChainReorg(t *testing.T) {
},
newChainReorgFunc: newChainReorgWithDisconnect,
assertErr: func(t require.TestingT, err error, i ...any) {
require.ErrorContains(t, err, "get network best block hash")
require.ErrorContains(t, err, "get network best block Hash")
require.ErrorIs(t, err, assert.AnError)
},
},
Expand All @@ -1491,7 +1491,7 @@ func TestChainReorg(t *testing.T) {
networkNodeClient.GetBestBlockHashFunc = func(
ctx context.Context,
) (string, error) {
return "hash", nil
return "Hash", nil
}

disconnectedNodeClient := newChainReorgSuccessRPCClient(peerCount)
Expand Down

0 comments on commit d3745de

Please sign in to comment.