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

Decode transparent notes amount #1296

Merged
merged 1 commit into from
Feb 15, 2022
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
17 changes: 17 additions & 0 deletions pkg/core/data/ipc/transactions/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"

"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/encoding"
"github.com/sirupsen/logrus"
)

// Note types.
Expand Down Expand Up @@ -97,3 +98,19 @@ func UnmarshalNote(r *bytes.Buffer, f *Note) (err error) {

return nil
}

// DecodeTxAmount return the amount of the tx (if transparent).
func (f *Note) DecodeTxAmount() uint64 {
if f.Type != NoteTypeTransparent {
logrus.Warn("Note is not transaprent")
return 0
}

var txAmount uint64
if err := encoding.ReadUint64LE(bytes.NewBuffer(f.EncryptedData), &txAmount); err != nil {
logrus.WithError(err).Warn("could not read EncryptedData")
return 0
}

return txAmount
}
15 changes: 3 additions & 12 deletions pkg/gql/query/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
package query

import (
"bytes"
"encoding/hex"
"errors"
"time"

"github.com/dusk-network/dusk-blockchain/pkg/core/data/block"
core "github.com/dusk-network/dusk-blockchain/pkg/core/data/ipc/transactions"
"github.com/dusk-network/dusk-blockchain/pkg/core/database"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/encoding"
"github.com/graphql-go/graphql"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -67,27 +64,21 @@ func newQueryBlock(b *block.Block) queryBlock {
qb.Header.Hash = b.Header.Hash
qb.Header.StateHash = b.Header.StateHash

reward := uint32(0)
reward := uint64(0)
feesPaid := uint64(0)

for _, tx := range b.Txs {
feesPaid += tx.GasSpent()

if tx.Type() == core.Distribute {
distribute, _ := tx.Decode()

for _, note := range distribute.Notes {
var r uint32
if err := encoding.ReadUint32LE(bytes.NewBuffer(note.ValueCommitment), &r); err != nil {
logrus.WithError(err).Warn("could not read value commitment")
}

reward += r
reward += note.DecodeTxAmount()
}
}
}

qb.Header.Reward = uint64(reward)
qb.Header.Reward = reward
qb.Header.FeesPaid = feesPaid

return qb
Expand Down
2 changes: 2 additions & 0 deletions pkg/p2p/wire/message/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func TestCoinbaseTransaction(t *testing.T) {
assert.Zero(decoded.Fee.GasLimit, "GasLimit should be 0")
assert.Zero(decoded.Fee.GasPrice, "GasPrice should be 0")
assert.Empty(decoded.Nullifiers, "Nullifiers should not be present")
assert.EqualValues(uint64(28113864), decoded.Notes[0].DecodeTxAmount())
assert.EqualValues(uint64(253024782), decoded.Notes[1].DecodeTxAmount())

hash, err := decoded.Hash(txdummy.TxType)
if err != nil {
Expand Down