Skip to content

Commit

Permalink
Merge pull request #49 from bjwswang/codecov
Browse files Browse the repository at this point in the history
fix: optimize rwset parsing
  • Loading branch information
bjwswang authored May 4, 2023
2 parents e3f7aee + 4449db0 commit 26d4534
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/internal/hyperledger/fabric/protoutil/txutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package protoutil

import (
"encoding/json"
"strings"

"github.com/bestchains/bc-explorer/pkg/internal/hyperledger/fabric/rwsetutil"
"github.com/bestchains/bc-explorer/pkg/models"
Expand Down Expand Up @@ -119,7 +120,34 @@ func GetTransactionFromEnvelope(txEnvelopBytes []byte) (*models.Transaction, err
if err != nil {
return nil, err
}
raw, err := json.Marshal(txRWSet)

fabRWSets := make([]models.FabRWSet, len(txRWSet.NsRwSets))
for index, rwset := range txRWSet.NsRwSets {
fabRWSet := models.FabRWSet{
Namespace: rwset.NameSpace,
}
reads := make([]models.Read, 0)
writes := make([]models.Write, 0)
for _, read := range rwset.KvRwSet.Reads {
reads = append(reads, models.Read{
Key: strings.Replace(read.GetKey(), "\u0000", "", -1),
Version: read.GetVersion().String(),
})
}
for _, write := range rwset.KvRwSet.Writes {
writes = append(writes, models.Write{
Key: strings.Replace(write.GetKey(), "\u0000", "", -1),
Value: string(write.GetValue()),
IsDelete: write.IsDelete,
})
}
fabRWSet.Reads = reads
fabRWSet.Writes = writes

fabRWSets[index] = fabRWSet
}

raw, err := json.Marshal(fabRWSets)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ const (
EndorserTransaction TxType = "EndorserTransaction"
)

type Read struct {
Key string `json:"key,omitempty"`
Version string `json:"version,omitempty"`
}

type Write struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
IsDelete bool `json:"isDelete,omitempty"`
}

type FabRWSet struct {
Namespace string `json:"namespace,omitempty"`
Reads []Read `json:"reads,omitempty"`
Writes []Write `json:"writes,omitempty"`
}

type Transaction struct {
ID string `pg:"id,pk" json:"id"`
Network string `pg:"network" json:"network"`
Expand Down

0 comments on commit 26d4534

Please sign in to comment.