Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _testmain.go

/loop-debug
/loopd-debug
/tools/llformat

output*.log

Expand Down
8 changes: 5 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ linters:
- funcorder
- wsl_v5
- noinlineerr
# llformat now owns vertical whitespace for handwritten Go. The
# whitespace linter cannot express the project rule that multi-line
# function signatures keep a body separator while single-return control
# blocks stay compact.
- whitespace
settings:
gosec:
excludes:
Expand All @@ -67,9 +72,6 @@ linters:
case:
rules:
json: snake
whitespace:
multi-if: true
multi-func: true
exclusions:
generated: lax
presets:
Expand Down
22 changes: 17 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ GOTEST := GO111MODULE=on go test -v


GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports
LLFORMAT_PKG := github.com/bhandras/llformat/cmd/llformat

GO_BIN := ${GOPATH}/bin
GOIMPORTS_BIN := $(GO_BIN)/gosimports
LLFORMAT_BIN := $(CURDIR)/$(TOOLS_DIR)/llformat

GOBUILD := CGO_ENABLED=0 GO111MODULE=on go build -v
GOINSTALL := CGO_ENABLED=0 GO111MODULE=on go install -v
Expand All @@ -35,6 +37,7 @@ UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)
ifneq ($(workers),)
LINT_WORKERS = --concurrency=$(workers)
endif
FMT_BASE := $(if $(base),$(base),origin/master)

DOCKER_TOOLS = docker run \
--rm \
Expand Down Expand Up @@ -64,6 +67,11 @@ $(GOIMPORTS_BIN):
@$(call print, "Installing goimports.")
cd $(TOOLS_DIR); go install -trimpath $(GOIMPORTS_PKG)

$(LLFORMAT_BIN): $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum
@$(call print, "Installing llformat.")
cd $(TOOLS_DIR); GOBIN="$(CURDIR)/$(TOOLS_DIR)" \
go install -trimpath $(LLFORMAT_PKG)


# ============
# INSTALLATION
Expand Down Expand Up @@ -132,11 +140,15 @@ unit-postgres-race:
# UTILITIES
# =========

fmt: $(GOIMPORTS_BIN)
@$(call print, "Fixing imports.")
gosimports -w $(GOFILES_NOVENDOR)
@$(call print, "Formatting source.")
gofmt -l -w -s $(GOFILES_NOVENDOR)
fmt: $(LLFORMAT_BIN)
@$(call print, "Formatting all handwritten Go source.")
@./scripts/llformat-files.sh all | \
xargs -0 -n 1 $(LLFORMAT_BIN) -w
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Invoking the formatter with xargs -n 1 is inefficient as it forks a new process for every single file. Formatter tools like llformat typically support multiple file arguments, so you should allow xargs to pass as many files as possible in a single invocation to improve performance.

		xargs -0 $(LLFORMAT_BIN) -w


fmt-changed: $(LLFORMAT_BIN)
@$(call print, "Formatting Go source changes against $(FMT_BASE).")
@./scripts/llformat-files.sh changed "$(FMT_BASE)" | \
xargs -0 -n 1 $(LLFORMAT_BIN) -w

lint: docker-tools
@$(call print, "Linting source.")
Expand Down
51 changes: 30 additions & 21 deletions assets/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type TapdConfig struct {
// assets daemon.
func DefaultTapdConfig() *TapdConfig {
defaultConf := tapcfg.DefaultConfig()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pls regenerate this commit with only the baseline and then add the lint diff as a separate commit? This'd help with just mechanical validation that the diff is indeed created with llformat.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now contains the lint-diff only.

return &TapdConfig{
Activate: false,
Host: "localhost:10029",
Expand Down Expand Up @@ -83,14 +84,18 @@ func NewTapdClient(config *TapdConfig) (*TapdClient, error) {

// Create the TapdClient.
client := &TapdClient{
assetNameCache: make(map[string]string),
cc: conn,
cfg: config,
TaprootAssetsClient: taprpc.NewTaprootAssetsClient(conn),
TaprootAssetChannelsClient: tapchannelrpc.NewTaprootAssetChannelsClient(conn),
PriceOracleClient: priceoraclerpc.NewPriceOracleClient(conn),
RfqClient: rfqrpc.NewRfqClient(conn),
UniverseClient: universerpc.NewUniverseClient(conn),
assetNameCache: make(map[string]string),
cc: conn,
cfg: config,
TaprootAssetsClient: taprpc.NewTaprootAssetsClient(conn),
TaprootAssetChannelsClient: tapchannelrpc.NewTaprootAssetChannelsClient(
conn,
),
PriceOracleClient: priceoraclerpc.NewPriceOracleClient(
conn,
),
RfqClient: rfqrpc.NewRfqClient(conn),
UniverseClient: universerpc.NewUniverseClient(conn),
}

return client, nil
Expand All @@ -104,9 +109,8 @@ func (c *TapdClient) Close() {
// GetRfqForAsset returns a RFQ for the given asset with the given amount and
// to the given peer.
func (c *TapdClient) GetRfqForAsset(ctx context.Context,
satAmount btcutil.Amount, assetId, peerPubkey []byte,
expiry int64, feeLimitMultiplier float64) (
*rfqrpc.PeerAcceptedSellQuote, error) {
satAmount btcutil.Amount, assetId, peerPubkey []byte, expiry int64,
feeLimitMultiplier float64) (*rfqrpc.PeerAcceptedSellQuote, error) {

// paymentMaxAmt is the maximum amount we are willing to pay for the
// payment.
Expand Down Expand Up @@ -149,8 +153,8 @@ func (c *TapdClient) GetRfqForAsset(ctx context.Context,
}

// GetAssetName returns the human-readable name of the asset.
func (c *TapdClient) GetAssetName(ctx context.Context,
assetId []byte) (string, error) {
func (c *TapdClient) GetAssetName(ctx context.Context, assetId []byte) (string,
error) {

c.assetNameMutex.Lock()
defer c.assetNameMutex.Unlock()
Expand Down Expand Up @@ -220,13 +224,15 @@ func (c *TapdClient) GetAssetPrice(ctx context.Context, assetID string,
}

if rfq.GetInvalidQuote() != nil {
return 0, fmt.Errorf("peer %v sent an invalid quote response %v for "+
"asset %v", peerPubkey, rfq.GetInvalidQuote(), assetID)
return 0, fmt.Errorf("peer %v sent an invalid quote response "+
"%v for asset %v", peerPubkey, rfq.GetInvalidQuote(),
assetID)
}

if rfq.GetRejectedQuote() != nil {
return 0, fmt.Errorf("peer %v rejected the quote request for "+
"asset %v, %v", peerPubkey, assetID, rfq.GetRejectedQuote())
"asset %v, %v", peerPubkey, assetID,
rfq.GetRejectedQuote())
}

acceptedRes := rfq.GetAcceptedQuote()
Expand All @@ -240,8 +246,8 @@ func (c *TapdClient) GetAssetPrice(ctx context.Context, assetID string,

// getSatsFromAssetAmt returns the amount in satoshis for the given asset amount
// and asset rate.
func getSatsFromAssetAmt(assetAmt uint64, assetRate *rfqrpc.FixedPoint) (
btcutil.Amount, error) {
func getSatsFromAssetAmt(assetAmt uint64,
assetRate *rfqrpc.FixedPoint) (btcutil.Amount, error) {

rateFP, err := rpcutils.UnmarshalRfqFixedPoint(assetRate)
if err != nil {
Expand All @@ -257,8 +263,8 @@ func getSatsFromAssetAmt(assetAmt uint64, assetRate *rfqrpc.FixedPoint) (

// getPaymentMaxAmount returns the milisat amount we are willing to pay for the
// payment.
func getPaymentMaxAmount(satAmount btcutil.Amount, feeLimitMultiplier float64) (
lnwire.MilliSatoshi, error) {
func getPaymentMaxAmount(satAmount btcutil.Amount,
feeLimitMultiplier float64) (lnwire.MilliSatoshi, error) {

if satAmount == 0 {
return 0, fmt.Errorf("satAmount cannot be zero")
Expand All @@ -273,7 +279,10 @@ func getPaymentMaxAmount(satAmount btcutil.Amount, feeLimitMultiplier float64) (
// The resulting maximum amount we're willing to pay is 300k sats.
// The response asset amount will be for those 300k sats.
return lnrpc.UnmarshallAmt(
int64(satAmount.MulF64(feeLimitMultiplier)), 0,
int64(
satAmount.MulF64(feeLimitMultiplier),
),
0,
)
}

Expand Down
25 changes: 18 additions & 7 deletions assets/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,38 @@ func TestGetSatsFromAssetAmt(t *testing.T) {
expectError bool
}{
{
assetAmt: 1000,
assetRate: &rfqrpc.FixedPoint{Coefficient: "100000", Scale: 0},
assetAmt: 1000,
assetRate: &rfqrpc.FixedPoint{
Coefficient: "100000",
Scale: 0,
},
expected: btcutil.Amount(1000000),
expectError: false,
},
{
assetAmt: 500000,
assetRate: &rfqrpc.FixedPoint{Coefficient: "200000000", Scale: 0},
assetAmt: 500000,
assetRate: &rfqrpc.FixedPoint{
Coefficient: "200000000",
Scale: 0,
},
expected: btcutil.Amount(250000),
expectError: false,
},
{
assetAmt: 0,
assetRate: &rfqrpc.FixedPoint{Coefficient: "100000000", Scale: 0},
assetAmt: 0,
assetRate: &rfqrpc.FixedPoint{
Coefficient: "100000000",
Scale: 0,
},
expected: btcutil.Amount(0),
expectError: false,
},
}

for _, test := range tests {
result, err := getSatsFromAssetAmt(test.assetAmt, test.assetRate)
result, err := getSatsFromAssetAmt(
test.assetAmt, test.assetRate,
)
if test.expectError {
require.NotNil(t, err)
} else {
Expand Down
Loading
Loading