Skip to content

Commit

Permalink
added logic to skip tx status that is not part of tick transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Jun 19, 2024
1 parent a2ca03b commit b2a88f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
32 changes: 31 additions & 1 deletion validator/txstatus/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import (
"github.com/pkg/errors"
"github.com/qubic/go-archiver/protobuff"
"github.com/qubic/go-node-connector/types"
"log"
)

func qubicToProto(model types.TransactionStatus) (*protobuff.TickTransactionsStatus, error) {
func qubicToProto(txs types.Transactions, model types.TransactionStatus) (*protobuff.TickTransactionsStatus, error) {
tickTransactions := make([]*protobuff.TransactionStatus, 0, model.TxCount)
txsIdMap, err := createTxsIdMap(txs)
if err != nil {
return nil, errors.Wrap(err, "error creating txs id map")
}

for index, txDigest := range model.TransactionDigests {
var id types.Identity
id, err := id.FromPubKey(txDigest, true)
if err != nil {
return nil, errors.Wrap(err, "converting digest to id")
}
if _, ok := txsIdMap[id.String()]; !ok {
log.Printf("Skipping tx status with id: %s\n", id.String())
continue
}

moneyFlew := getMoneyFlewFromBits(model.MoneyFlew, index)

tx := &protobuff.TransactionStatus{
Expand All @@ -27,6 +38,25 @@ func qubicToProto(model types.TransactionStatus) (*protobuff.TickTransactionsSta
return &protobuff.TickTransactionsStatus{Transactions: tickTransactions}, nil
}

func createTxsIdMap(txs types.Transactions) (map[string]struct{}, error) {
txsIdMap := make(map[string]struct{}, len(txs))
for _, tx := range txs {
digest, err := tx.Digest()
if err != nil {
return nil, errors.Wrapf(err, "creating tx digest for tx with src pubkey %s", tx.SourcePublicKey)
}

id, err := tx.ID()
if err != nil {
return nil, errors.Wrapf(err, "converting tx with digest %s to id", digest)
}

txsIdMap[id] = struct{}{}
}

return txsIdMap, nil
}

func getMoneyFlewFromBits(input [(types.NumberOfTransactionsPerTick + 7) / 8]byte, digestIndex int) bool {
pos := digestIndex / 8
bitIndex := digestIndex % 8
Expand Down
2 changes: 1 addition & 1 deletion validator/txstatus/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestQubicToProto(t *testing.T) {
},
}

res, err := qubicToProto(tickTransactionStatus)
res, err := qubicToProto(types.Transactions{}, tickTransactionStatus)
if err != nil {
t.Fatalf("Got err when converting qubic to proto. err: %s", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions validator/txstatus/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ func Validate(ctx context.Context, tickTxStatus types.TransactionStatus, tickTxs
//if tickTxStatus.TxCount != uint32(len(tickTxs)) {
// return nil, errors.Errorf("Mismatched tx length. Tick tx status count: %d - len(tickTx): %d", tickTxStatus.TxCount, len(tickTxs))
//}
//

//tickTxDigests, err := getTickTxDigests(tickTxs)
//if err != nil {
// return nil, errors.Wrap(err, "getting tick tx digests")
//}
//

//if !equalDigests(tickTxDigests, tickTxStatus.TransactionDigests) {
// return nil, errors.New("digests not equal")
//}

proto, err := qubicToProto(tickTxStatus)
proto, err := qubicToProto(tickTxs, tickTxStatus)
if err != nil {
return nil, errors.Wrap(err, "qubic to proto")
}
Expand Down

0 comments on commit b2a88f7

Please sign in to comment.