diff --git a/validator/txstatus/models.go b/validator/txstatus/models.go index 68cae89..9c5e843 100644 --- a/validator/txstatus/models.go +++ b/validator/txstatus/models.go @@ -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{ @@ -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 diff --git a/validator/txstatus/models_test.go b/validator/txstatus/models_test.go index 0ab5802..7dd5c6f 100644 --- a/validator/txstatus/models_test.go +++ b/validator/txstatus/models_test.go @@ -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()) } diff --git a/validator/txstatus/validator.go b/validator/txstatus/validator.go index d29ce6c..9eb8f77 100644 --- a/validator/txstatus/validator.go +++ b/validator/txstatus/validator.go @@ -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") }