Skip to content

Commit

Permalink
removed unnecessary field from transfer transaction output
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Mar 12, 2024
1 parent d8858e4 commit 0a6ad51
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 764 deletions.
1,041 changes: 487 additions & 554 deletions protobuff/archive.pb.go

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions protobuff/archive.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ message GetTransactionResponse{
Transaction transaction = 1;
}

message Transactions {
repeated Transaction transactions = 1;
}

message GetTickTransactionsRequest {
uint32 tick_number = 1;
}
Expand Down Expand Up @@ -128,7 +124,7 @@ message IdentityInfo {
message TransferTransactionsPerTick {
uint32 tick_number = 1;
string identity = 2;
Transactions transactions = 3;
repeated Transaction transactions = 3;
}

message GetIdentityInfoRequest {
Expand Down
2 changes: 1 addition & 1 deletion rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Server) GetTickTransactions(ctx context.Context, req *protobuff.GetTick
return nil, status.Errorf(codes.Internal, "getting tick transactions: %v", err)
}

return &protobuff.GetTickTransactionsResponse{Transactions: txs.Transactions}, nil
return &protobuff.GetTickTransactionsResponse{Transactions: txs}, nil
}
func (s *Server) GetTransaction(ctx context.Context, req *protobuff.GetTransactionRequest) (*protobuff.GetTransactionResponse, error) {
tx, err := s.store.GetTransaction(ctx, req.TxId)
Expand Down
10 changes: 5 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ func (s *PebbleStore) SetComputors(ctx context.Context, epoch uint32, computors
return nil
}

func (s *PebbleStore) SetTransactions(ctx context.Context, txs *protobuff.Transactions) error {
batch := s.db.NewBatchWithSize(len(txs.GetTransactions()))
func (s *PebbleStore) SetTransactions(ctx context.Context, txs []*protobuff.Transaction) error {
batch := s.db.NewBatchWithSize(len(txs))
defer batch.Close()

for _, tx := range txs.GetTransactions() {
for _, tx := range txs {
key, err := tickTxKey(tx.TxId)
if err != nil {
return errors.Wrapf(err, "creating tx key for id: %s", tx.TxId)
Expand All @@ -160,7 +160,7 @@ func (s *PebbleStore) SetTransactions(ctx context.Context, txs *protobuff.Transa
return nil
}

func (s *PebbleStore) GetTickTransactions(ctx context.Context, tickNumber uint64) (*protobuff.Transactions, error) {
func (s *PebbleStore) GetTickTransactions(ctx context.Context, tickNumber uint64) ([]*protobuff.Transaction, error) {
td, err := s.GetTickData(ctx, tickNumber)
if err != nil {
if errors.Is(err, ErrNotFound) {
Expand All @@ -184,7 +184,7 @@ func (s *PebbleStore) GetTickTransactions(ctx context.Context, tickNumber uint64
txs = append(txs, tx)
}

return &protobuff.Transactions{Transactions: txs}, nil
return txs, nil
}

func (s *PebbleStore) GetTransaction(ctx context.Context, txID string) (*protobuff.Transaction, error) {
Expand Down
167 changes: 77 additions & 90 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,28 @@ func TestPebbleStore_TickTransactions(t *testing.T) {
logger, _ := zap.NewDevelopment()
store := NewPebbleStore(db, logger)

transactions := &pb.Transactions{
Transactions: []*pb.Transaction{
{
SourceId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
DestId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
Amount: 100,
TickNumber: 101,
InputType: 1,
InputSize: 256,
InputHex: "input1",
SignatureHex: "signature1",
TxId: "ff01",
},
{
SourceId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
DestId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
Amount: 100,
TickNumber: 101,
InputType: 1,
InputSize: 256,
InputHex: "input1",
SignatureHex: "signature2",
TxId: "cd01",
},
// Add more transactions as needed
transactions := []*pb.Transaction{
{
SourceId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
DestId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
Amount: 100,
TickNumber: 101,
InputType: 1,
InputSize: 256,
InputHex: "input1",
SignatureHex: "signature1",
TxId: "ff01",
},
{
SourceId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
DestId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
Amount: 100,
TickNumber: 101,
InputType: 1,
InputSize: 256,
InputHex: "input1",
SignatureHex: "signature2",
TxId: "cd01",
},
}

Expand Down Expand Up @@ -247,9 +244,9 @@ func TestPebbleStore_TickTransactions(t *testing.T) {
require.NotNil(t, retrievedTransactions)

// Validate the retrieved transactions
require.Len(t, retrievedTransactions.Transactions, len(transactions.Transactions))
for i, tx := range transactions.Transactions {
retrievedTx := retrievedTransactions.Transactions[i]
require.Len(t, retrievedTransactions, len(transactions))
for i, tx := range transactions {
retrievedTx := retrievedTransactions[i]
require.Equal(t, tx.SourceId, retrievedTx.SourceId)
// Continue with other fields...
}
Expand Down Expand Up @@ -286,12 +283,7 @@ func TestPebbleStore_GetTransaction(t *testing.T) {
SignatureHex: "signature_target",
TxId: "cd01",
}
transactions := &pb.Transactions{
Transactions: []*pb.Transaction{
targetTransaction,
// Additional transactions as needed
},
}
transactions := []*pb.Transaction{targetTransaction}

// Use SetTransactions to store the transactions
err = store.SetTransactions(ctx, transactions)
Expand Down Expand Up @@ -441,61 +433,57 @@ func TestPebbleStore_TransferTransactions(t *testing.T) {
forTickOne := pb.TransferTransactionsPerTick{
TickNumber: 12,
Identity: idOne,
Transactions: &pb.Transactions{
Transactions: []*pb.Transaction{
{
SourceId: "aaaaa",
DestId: "bbbbb",
Amount: 15,
TickNumber: 12,
InputType: 87,
InputSize: 122,
InputHex: "dddd",
SignatureHex: "ffff",
TxId: "eeee",
},
{
SourceId: "bbbbb",
DestId: "aaaaa",
Amount: 25,
TickNumber: 12,
InputType: 65,
InputSize: 24,
InputHex: "ffff",
SignatureHex: "dddd",
TxId: "cccc",
},
Transactions: []*pb.Transaction{
{
SourceId: "aaaaa",
DestId: "bbbbb",
Amount: 15,
TickNumber: 12,
InputType: 87,
InputSize: 122,
InputHex: "dddd",
SignatureHex: "ffff",
TxId: "eeee",
},
{
SourceId: "bbbbb",
DestId: "aaaaa",
Amount: 25,
TickNumber: 12,
InputType: 65,
InputSize: 24,
InputHex: "ffff",
SignatureHex: "dddd",
TxId: "cccc",
},
},
}

forTickTwo := pb.TransferTransactionsPerTick{
TickNumber: 15,
Identity: idTwo,
Transactions: &pb.Transactions{
Transactions: []*pb.Transaction{
{
SourceId: "aaaaa",
DestId: "bbbbb",
Amount: 15,
TickNumber: 15,
InputType: 87,
InputSize: 122,
InputHex: "dddd",
SignatureHex: "ffff",
TxId: "eeee",
},
{
SourceId: "bbbbb",
DestId: "aaaaa",
Amount: 25,
TickNumber: 15,
InputType: 65,
InputSize: 24,
InputHex: "ffff",
SignatureHex: "dddd",
TxId: "cccc",
},
Transactions: []*pb.Transaction{
{
SourceId: "aaaaa",
DestId: "bbbbb",
Amount: 15,
TickNumber: 15,
InputType: 87,
InputSize: 122,
InputHex: "dddd",
SignatureHex: "ffff",
TxId: "eeee",
},
{
SourceId: "bbbbb",
DestId: "aaaaa",
Amount: 25,
TickNumber: 15,
InputType: 65,
InputSize: 24,
InputHex: "ffff",
SignatureHex: "dddd",
TxId: "cccc",
},
},
}
Expand All @@ -508,43 +496,42 @@ func TestPebbleStore_TransferTransactions(t *testing.T) {

got, err := store.GetTransferTransactions(ctx, idOne, 12, 12)
require.NoError(t, err)
diff := cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickOne}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff := cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickOne}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparing first TransferTransactionsPerTick for idOne, forTickOne")

got, err = store.GetTransferTransactions(ctx, idOne, 13, 13)
require.NoError(t, err)
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickOne}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickOne}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparing second TransferTransactionsPerTick for idOne, forTickOne")

err = store.PutTransferTransactionsPerTick(ctx, idTwo, 15, &forTickTwo)
require.NoError(t, err)
got, err = store.GetTransferTransactions(ctx, idTwo, 15, 15)
require.NoError(t, err)
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickTwo}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{&forTickTwo}, got, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparing TransferTransactionsPerTick for idTwo, forTickTwo")

perIdentityTx, err := store.GetTransferTransactions(ctx, idOne, 12, 13)
require.NoError(t, err)
require.Equal(t, 2, len(perIdentityTx))

expected := []*pb.TransferTransactionsPerTick{&forTickOne, &forTickOne}
require.NoError(t, err)
diff = cmp.Diff(expected, perIdentityTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff = cmp.Diff(expected, perIdentityTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparing perIdentityTx")

// not existing identity means no transfers
perIdentityTx, err = store.GetTransferTransactions(ctx, idThree, 1, 20)
require.NoError(t, err)
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{}, perIdentityTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{}, perIdentityTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparison of perIdentityTx for idThree")

// not existing tick means no transfers
perTickTx, err := store.GetTransferTransactions(ctx, idOne, 14, 14)
require.NoError(t, err)
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{}, perTickTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}, pb.Transactions{}))
diff = cmp.Diff([]*pb.TransferTransactionsPerTick{}, perTickTx, cmpopts.IgnoreUnexported(pb.TransferTransactionsPerTick{}, pb.Transaction{}))
require.Equal(t, "", diff, "comparison of perTickTx for idOne and tick 14")
}

func TestPebbleStore_QChainHash(t *testing.T) {
ctx := context.Background()

Expand Down
10 changes: 4 additions & 6 deletions validator/tx/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import (
"github.com/qubic/go-node-connector/types"
)

func qubicToProto(txs types.Transactions) (*protobuff.Transactions, error) {
protoTxs := protobuff.Transactions{
Transactions: make([]*protobuff.Transaction, len(txs)),
}
func qubicToProto(txs types.Transactions) ([]*protobuff.Transaction, error) {
protoTxs := make([]*protobuff.Transaction, len(txs))
for i, tx := range txs {
txProto, err := txToProto(tx)
if err != nil {
return nil, errors.Wrapf(err, "converting tx to proto")
}
protoTxs.Transactions[i] = txProto
protoTxs[i] = txProto
}

return &protoTxs, nil
return protoTxs, nil
}

func txToProto(tx types.Transaction) (*protobuff.Transaction, error) {
Expand Down
48 changes: 23 additions & 25 deletions validator/tx/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,36 @@ func TestQubicToProto(t *testing.T) {
t.Fatalf("idTwo.FromPubKey() unexpected error: %v", err)
}

expectedProtoTxs := &protobuff.Transactions{
Transactions: []*protobuff.Transaction{
&protobuff.Transaction{
SourceId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
DestId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
Amount: 100,
TickNumber: 20,
InputType: 0,
InputSize: 100,
InputHex: hex.EncodeToString(qubicTransactions[0].Input[:]),
SignatureHex: "01020304050607080910010203040506070809100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
TxId: idOne.String(),
},
&protobuff.Transaction{
SourceId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
DestId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
Amount: 15,
TickNumber: 20,
InputType: 0,
InputSize: 120,
InputHex: hex.EncodeToString(qubicTransactions[1].Input[:]),
SignatureHex: "01020304ff0607080910010203040506070809100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
TxId: idTwo.String(),
},
expectedProtoTxs := []*protobuff.Transaction{
{
SourceId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
DestId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
Amount: 100,
TickNumber: 20,
InputType: 0,
InputSize: 100,
InputHex: hex.EncodeToString(qubicTransactions[0].Input[:]),
SignatureHex: "01020304050607080910010203040506070809100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
TxId: idOne.String(),
},
{
SourceId: "IXTSDANOXIVIWGNDCNZVWSAVAEPBGLGSQTLSVHHBWEGKSEKPRQGWIJJCTUZB",
DestId: "QJRRSSKMJRDKUDTYVNYGAMQPULKAMILQQYOWBEXUDEUWQUMNGDHQYLOAJMEB",
Amount: 15,
TickNumber: 20,
InputType: 0,
InputSize: 120,
InputHex: hex.EncodeToString(qubicTransactions[1].Input[:]),
SignatureHex: "01020304ff0607080910010203040506070809100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
TxId: idTwo.String(),
},
}

got, err := qubicToProto(qubicTransactions)
if err != nil {
t.Fatalf("qubicToProto() unexpected error: %v", err)
}
if diff := cmp.Diff(got, expectedProtoTxs, cmpopts.IgnoreUnexported(protobuff.Transactions{}, protobuff.Transaction{})); diff != "" {
if diff := cmp.Diff(got, expectedProtoTxs, cmpopts.IgnoreUnexported(protobuff.Transaction{})); diff != "" {
t.Fatalf("qubicToProto() mismatch (-got +want):\n%s", diff)
}
}
Loading

0 comments on commit 0a6ad51

Please sign in to comment.