Skip to content

Commit

Permalink
fix(grpc): change bytes type to hex string (#1371)
Browse files Browse the repository at this point in the history
Co-authored-by: b00f <[email protected]>
  • Loading branch information
Ja7ad and b00f authored Jun 27, 2024
1 parent 8955d0c commit 30a1233
Show file tree
Hide file tree
Showing 61 changed files with 4,582 additions and 2,521 deletions.
3 changes: 2 additions & 1 deletion crypto/bls/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Signature struct {
data []byte // Raw signature data.
}

// SignatureFromString constructs a BLS signature from a hex-encoded string.
// SignatureFromString decodes the input string and returns the Signature
// if the string is a valid hex encoding of a BLS signature.
func SignatureFromString(text string) (*Signature, error) {
data, err := hex.DecodeString(text)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion tests/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"encoding/hex"
"fmt"
"testing"

Expand All @@ -18,7 +19,7 @@ func sendRawTx(t *testing.T, raw []byte) error {
t.Helper()

_, err := tTransaction.BroadcastTransaction(tCtx,
&pactus.BroadcastTransactionRequest{SignedRawTransaction: raw})
&pactus.BroadcastTransactionRequest{SignedRawTransaction: hex.EncodeToString(raw)})

return err
}
Expand Down
7 changes: 4 additions & 3 deletions wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallet

import (
"context"
"encoding/hex"
"errors"

"github.com/pactus-project/pactus/crypto/hash"
Expand Down Expand Up @@ -122,12 +123,12 @@ func (c *grpcClient) sendTx(trx *tx.Tx) (tx.ID, error) {
return hash.UndefHash, err
}
res, err := c.transactionClient.BroadcastTransaction(c.ctx,
&pactus.BroadcastTransactionRequest{SignedRawTransaction: data})
&pactus.BroadcastTransactionRequest{SignedRawTransaction: hex.EncodeToString(data)})
if err != nil {
return hash.UndefHash, err
}

return hash.FromBytes(res.Id)
return hash.FromString(res.Id)
}

// TODO: check the return value type.
Expand All @@ -138,7 +139,7 @@ func (c *grpcClient) getTransaction(id tx.ID) (*pactus.GetTransactionResponse, e

res, err := c.transactionClient.GetTransaction(c.ctx,
&pactus.GetTransactionRequest{
Id: id.Bytes(),
Id: id.String(),
Verbosity: pactus.TransactionVerbosity_TRANSACTION_INFO,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions wallet/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (h *history) addActivity(addr string, amt amount.Amount, trx *pactus.GetTra
h.Activities[addr] = make([]activity, 0, 1)
}
act := activity{
TxID: hex.EncodeToString(trx.Transaction.Id),
TxID: trx.Transaction.Id,
Amount: amt,
}
h.Activities[addr] = append(h.Activities[addr], act)
Expand All @@ -72,7 +72,7 @@ func (h *history) addActivity(addr string, amt amount.Amount, trx *pactus.GetTra
BlockHeight: trx.BlockHeight,
BlockTime: trx.BlockTime,
PayloadType: payload.Type(trx.Transaction.PayloadType).String(),
Data: hex.EncodeToString(trx.Transaction.Data),
Data: trx.Transaction.Data,
}
}

Expand Down
35 changes: 18 additions & 17 deletions www/grpc/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpc

import (
"context"
"encoding/hex"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/hash"
Expand Down Expand Up @@ -34,7 +35,7 @@ func (s *blockchainServer) GetBlockchainInfo(_ context.Context,

return &pactus.GetBlockchainInfoResponse{
LastBlockHeight: s.state.LastBlockHeight(),
LastBlockHash: s.state.LastBlockHash().Bytes(),
LastBlockHash: s.state.LastBlockHash().String(),
TotalAccounts: s.state.TotalAccounts(),
TotalValidators: s.state.TotalValidators(),
TotalPower: s.state.TotalPower(),
Expand Down Expand Up @@ -78,14 +79,14 @@ func (s *blockchainServer) GetBlockHash(_ context.Context,
}

return &pactus.GetBlockHashResponse{
Hash: h.Bytes(),
Hash: h.String(),
}, nil
}

func (s *blockchainServer) GetBlockHeight(_ context.Context,
req *pactus.GetBlockHeightRequest,
) (*pactus.GetBlockHeightResponse, error) {
h, err := hash.FromBytes(req.GetHash())
h, err := hash.FromString(req.GetHash())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid hash: %v", err)
}
Expand All @@ -109,12 +110,12 @@ func (s *blockchainServer) GetBlock(_ context.Context,
}
res := &pactus.GetBlockResponse{
Height: committedBlock.Height,
Hash: committedBlock.BlockHash.Bytes(),
Hash: committedBlock.BlockHash.String(),
}

switch req.Verbosity {
case pactus.BlockVerbosity_BLOCK_DATA:
res.Data = committedBlock.Data
res.Data = hex.EncodeToString(committedBlock.Data)

case pactus.BlockVerbosity_BLOCK_INFO,
pactus.BlockVerbosity_BLOCK_TRANSACTIONS:
Expand All @@ -137,18 +138,18 @@ func (s *blockchainServer) GetBlock(_ context.Context,
absentees[i] = n
}
prevCert = &pactus.CertificateInfo{
Hash: cert.Hash().Bytes(),
Hash: cert.Hash().String(),
Round: int32(cert.Round()),
Committers: committers,
Absentees: absentees,
Signature: cert.Signature().Bytes(),
Signature: cert.Signature().String(),
}
}
header := &pactus.BlockHeaderInfo{
Version: int32(block.Header().Version()),
PrevBlockHash: block.Header().PrevBlockHash().Bytes(),
StateRoot: block.Header().StateRoot().Bytes(),
SortitionSeed: seed[:],
PrevBlockHash: block.Header().PrevBlockHash().String(),
StateRoot: block.Header().StateRoot().String(),
SortitionSeed: hex.EncodeToString(seed[:]),
ProposerAddress: block.Header().ProposerAddress().String(),
}

Expand All @@ -157,8 +158,8 @@ func (s *blockchainServer) GetBlock(_ context.Context,
if req.Verbosity == pactus.BlockVerbosity_BLOCK_INFO {
data, _ := trx.Bytes()
trxs = append(trxs, &pactus.TransactionInfo{
Id: trx.ID().Bytes(),
Data: data,
Id: trx.ID().String(),
Data: hex.EncodeToString(data),
})
} else {
trxs = append(trxs, transactionToProto(trx))
Expand Down Expand Up @@ -267,8 +268,8 @@ func (s *blockchainServer) validatorToProto(val *validator.Validator) *pactus.Va
data, _ := val.Bytes()

return &pactus.ValidatorInfo{
Hash: val.Hash().Bytes(),
Data: data,
Hash: val.Hash().String(),
Data: hex.EncodeToString(data),
PublicKey: val.PublicKey().String(),
Address: val.Address().String(),
Number: val.Number(),
Expand All @@ -284,8 +285,8 @@ func (*blockchainServer) accountToProto(addr crypto.Address, acc *account.Accoun
data, _ := acc.Bytes()

return &pactus.AccountInfo{
Hash: acc.Hash().Bytes(),
Data: data,
Hash: acc.Hash().String(),
Data: hex.EncodeToString(data),
Number: acc.Number(),
Balance: acc.Balance().ToNanoPAC(),
Address: addr.String(),
Expand All @@ -303,7 +304,7 @@ func (*blockchainServer) voteToProto(v *vote.Vote) *pactus.VoteInfo {
return &pactus.VoteInfo{
Type: pactus.VoteType(v.Type()),
Voter: v.Signer().String(),
BlockHash: v.BlockHash().Bytes(),
BlockHash: v.BlockHash().String(),
Round: int32(v.Round()),
CpRound: cpRound,
CpValue: cpValue,
Expand Down
29 changes: 15 additions & 14 deletions www/grpc/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpc

import (
"context"
"encoding/hex"
"testing"

pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
Expand Down Expand Up @@ -33,8 +34,8 @@ func TestGetBlock(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.Height, height)
assert.Equal(t, res.Hash, b.Hash().Bytes())
assert.Equal(t, res.Data, data)
assert.Equal(t, res.Hash, b.Hash().String())
assert.Equal(t, res.Data, hex.EncodeToString(data))
assert.Empty(t, res.Header)
assert.Empty(t, res.Txs)
})
Expand All @@ -46,17 +47,17 @@ func TestGetBlock(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.Height, height)
assert.Equal(t, res.Hash, b.Hash().Bytes())
assert.Equal(t, res.Hash, b.Hash().String())
assert.Empty(t, res.Data)
assert.NotEmpty(t, res.Header)
assert.Equal(t, res.PrevCert.Committers, b.PrevCertificate().Committers())
assert.Equal(t, res.PrevCert.Absentees, b.PrevCertificate().Absentees())
for i, trx := range res.Txs {
blockTrx := b.Transactions()[i]
trxData, _ := blockTrx.Bytes()

assert.Equal(t, blockTrx.ID().Bytes(), trx.Id)
assert.Equal(t, trxData, trx.Data)
b, err := blockTrx.Bytes()
assert.NoError(t, err)
assert.Equal(t, blockTrx.ID().String(), trx.Id)
assert.Equal(t, hex.EncodeToString(b), trx.Data)
assert.Zero(t, trx.LockTime)
assert.Empty(t, trx.Signature)
assert.Empty(t, trx.PublicKey)
Expand All @@ -70,17 +71,17 @@ func TestGetBlock(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.Height, height)
assert.Equal(t, res.Hash, b.Hash().Bytes())
assert.Equal(t, res.Hash, b.Hash().String())
assert.Empty(t, res.Data)
assert.NotEmpty(t, res.Header)
assert.NotEmpty(t, res.Txs)
for i, trx := range res.Txs {
blockTrx := b.Transactions()[i]

assert.Equal(t, blockTrx.ID().Bytes(), trx.Id)
assert.Equal(t, blockTrx.ID().String(), trx.Id)
assert.Empty(t, trx.Data)
assert.Equal(t, blockTrx.LockTime(), trx.LockTime)
assert.Equal(t, blockTrx.Signature().Bytes(), trx.Signature)
assert.Equal(t, blockTrx.Signature().String(), trx.Signature)
assert.Equal(t, blockTrx.PublicKey().String(), trx.PublicKey)
}
})
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestGetBlockHash(t *testing.T) {
&pactus.GetBlockHashRequest{Height: 100})

assert.NoError(t, err)
assert.Equal(t, b.Hash().Bytes(), res.Hash)
assert.Equal(t, b.Hash().String(), res.Hash)
})

assert.Nil(t, conn.Close(), "Error closing connection")
Expand All @@ -123,23 +124,23 @@ func TestGetBlockHeight(t *testing.T) {

t.Run("Should return error for invalid hash", func(t *testing.T) {
res, err := client.GetBlockHeight(context.Background(),
&pactus.GetBlockHeightRequest{Hash: nil})
&pactus.GetBlockHeightRequest{Hash: ""})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return error for non existing block", func(t *testing.T) {
res, err := client.GetBlockHeight(context.Background(),
&pactus.GetBlockHeightRequest{Hash: td.RandHash().Bytes()})
&pactus.GetBlockHeightRequest{Hash: td.RandHash().String()})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return height of existing block", func(t *testing.T) {
res, err := client.GetBlockHeight(context.Background(),
&pactus.GetBlockHeightRequest{Hash: b.Hash().Bytes()})
&pactus.GetBlockHeightRequest{Hash: b.Hash().String()})

assert.NoError(t, err)
assert.Equal(t, uint32(100), res.Height)
Expand Down
2 changes: 1 addition & 1 deletion www/grpc/buf/grpc-gateway.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ http:
- selector: pactus.Blockchain.GetPublicKey
get: "/pactus/blockchain/get_public_key"

- selector: pactus.Transaction.GetTxPoolContent
- selector: pactus.Blockchain.GetTxPoolContent
get: "/pactus/blockchain/get_txpool_content"

# Transaction APIs
Expand Down
Loading

0 comments on commit 30a1233

Please sign in to comment.