Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rm sizings #747

Merged
merged 2 commits into from
Jan 16, 2025
Merged
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/jmoiron/sqlx v1.3.5
github.com/labstack/echo-contrib v0.17.1
github.com/labstack/echo/v4 v4.12.0
github.com/labstack/echo/v4 v4.13.3
github.com/lib/pq v1.10.9
github.com/libsv/go-bc v0.1.29
github.com/libsv/go-p2p v0.3.3
Expand All @@ -37,7 +37,7 @@ require (
github.com/prometheus/client_golang v1.19.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.56.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
go.opentelemetry.io/otel v1.31.0
Expand Down Expand Up @@ -163,7 +163,7 @@ require (
golang.org/x/sys v0.29.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.7.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240805194559-2c9e96a0b5d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240805194559-2c9e96a0b5d4 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ github.com/labstack/echo-contrib v0.17.1 h1:7I/he7ylVKsDUieaGRZ9XxxTYOjfQwVzHzUY
github.com/labstack/echo-contrib v0.17.1/go.mod h1:SnsCZtwHBAZm5uBSAtQtXQHI3wqEA73hvTn0bYMKnZA=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -349,6 +351,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down Expand Up @@ -452,6 +456,8 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
61 changes: 10 additions & 51 deletions internal/api/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (m ArcDefaultHandler) postTransaction(ctx echo.Context, params api.POSTTran
}
}

txs, successes, fails, e := m.processTransactions(reqCtx, txHex, transactionOptions)
successes, fails, e := m.processTransactions(reqCtx, txHex, transactionOptions)
if e != nil {
if span != nil {
attr := e.GetSpanAttributes()
Expand All @@ -285,9 +285,6 @@ func (m ArcDefaultHandler) postTransaction(ctx echo.Context, params api.POSTTran
return PostResponse{e.Status, e}
}

sizingCtx := context.WithValue(reqCtx, ContextSizings, prepareSizingInfo(txs))
ctx.SetRequest(ctx.Request().WithContext(sizingCtx))

res := successes[0]

if span != nil {
Expand Down Expand Up @@ -454,7 +451,7 @@ func (m ArcDefaultHandler) postTransactions(ctx echo.Context, params api.POSTTra
return PostResponse{int(api.StatusOK), responses}
}

txs, successes, fails, e := m.processTransactions(reqCtx, txsHex, transactionOptions)
successes, fails, e := m.processTransactions(reqCtx, txsHex, transactionOptions)
if e != nil {
if span != nil {
attr := e.GetSpanAttributes()
Expand All @@ -463,8 +460,6 @@ func (m ArcDefaultHandler) postTransactions(ctx echo.Context, params api.POSTTra
return PostResponse{e.Status, e}
}

sizingCtx := context.WithValue(reqCtx, ContextSizings, prepareSizingInfo(txs))
ctx.SetRequest(ctx.Request().WithContext(sizingCtx))
// we cannot really return any other status here
// each transaction in the slice will have the result of the transaction submission

Expand Down Expand Up @@ -612,15 +607,15 @@ func (m ArcDefaultHandler) getTxIDs(txsHex []byte) ([]string, *api.ErrorFields)
}

// processTransactions validates all the transactions in the array and submits to metamorph for processing.
func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byte, options *metamorph.TransactionOptions) (
submittedTxs []*sdkTx.Transaction, successes []*api.TransactionResponse, fails []*api.ErrorFields, processingErr *api.ErrorFields,
) {
func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byte, options *metamorph.TransactionOptions) (successes []*api.TransactionResponse, fails []*api.ErrorFields, processingErr *api.ErrorFields) {
var err error
ctx, span := tracing.StartTracing(ctx, "processTransactions", m.tracingEnabled, m.tracingAttributes...)
defer func() {
tracing.EndTracing(span, err)
}()

var submittedTxs []*sdkTx.Transaction

// decode and validate txs
var txIDs []string
for len(txsHex) != 0 {
Expand All @@ -630,7 +625,7 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
beefTx, remainingBytes, err := beef.DecodeBEEF(txsHex)
if err != nil {
errStr := errors.Join(ErrDecodingBeef, err).Error()
return nil, nil, nil, api.NewErrorFields(api.ErrStatusMalformed, errStr)
return nil, nil, api.NewErrorFields(api.ErrStatusMalformed, errStr)
}

txsHex = remainingBytes
Expand All @@ -651,7 +646,7 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
} else {
transaction, bytesUsed, err := sdkTx.NewTransactionFromStream(txsHex)
if err != nil {
return nil, nil, nil, api.NewErrorFields(api.ErrStatusBadRequest, err.Error())
return nil, nil, api.NewErrorFields(api.ErrStatusBadRequest, err.Error())
}

txsHex = txsHex[bytesUsed:]
Expand All @@ -668,13 +663,13 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
}

if len(submittedTxs) == 0 {
return nil, nil, fails, nil
return nil, fails, nil
}

// submit valid transactions to metamorph
txStatuses, e := m.submitTransactions(ctx, submittedTxs, options)
if e != nil {
return nil, nil, nil, e
return nil, nil, e
}

// prepare success results
Expand Down Expand Up @@ -703,7 +698,7 @@ func (m ArcDefaultHandler) processTransactions(ctx context.Context, txsHex []byt
})
}

return submittedTxs, successes, fails, nil
return successes, fails, nil
}

func (m ArcDefaultHandler) validateEFTransaction(ctx context.Context, txValidator validator.DefaultValidator, transaction *sdkTx.Transaction, options *metamorph.TransactionOptions) *api.ErrorFields {
Expand Down Expand Up @@ -848,42 +843,6 @@ func (ArcDefaultHandler) handleError(_ context.Context, transaction *sdkTx.Trans
return status, arcError
}

func prepareSizingInfo(txs []*sdkTx.Transaction) [][]uint64 {
sizingInfo := make([][]uint64, 0, len(txs))
for _, btTx := range txs {
normalBytes, dataBytes, feeAmount := getSizings(btTx)
sizingInfo = append(sizingInfo, []uint64{normalBytes, dataBytes, feeAmount})
}

return sizingInfo
}

func getSizings(tx *sdkTx.Transaction) (uint64, uint64, uint64) {
var feeAmount uint64

for _, in := range tx.Inputs {
feeAmount += *in.SourceTxSatoshis()
}

var dataBytes uint64
for _, out := range tx.Outputs {
if feeAmount >= out.Satoshis {
feeAmount -= out.Satoshis
} else {
feeAmount = 0
}

script := *out.LockingScript
if out.Satoshis == 0 && len(script) > 0 && (script[0] == 0x6a || (script[0] == 0x00 && script[1] == 0x6a)) {
dataBytes += uint64(len(script))
}
}

normalBytes := uint64(len(tx.Bytes())) - dataBytes

return normalBytes, dataBytes, feeAmount
}

// ContextKey type.
type ContextKey int

Expand Down
Loading