From ab417ea3d24de3c0dc1fe910324c778b022bff99 Mon Sep 17 00:00:00 2001 From: Bartek Nowotarski Date: Tue, 4 Jul 2017 15:57:36 +0200 Subject: [PATCH 1/2] Revert "Empty signature fix" This reverts commit d485af7746cdfd088b337d465aa2e5c4664d0e92. --- .../stellar/horizon/db2/core/transaction.go | 8 ++------ .../stellar/horizon/db2/core/transaction_test.go | 12 ------------ 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/github.com/stellar/horizon/db2/core/transaction.go b/src/github.com/stellar/horizon/db2/core/transaction.go index 2b9feca0..5d0450eb 100644 --- a/src/github.com/stellar/horizon/db2/core/transaction.go +++ b/src/github.com/stellar/horizon/db2/core/transaction.go @@ -17,14 +17,10 @@ import ( // encoded representation of a signature attached to this transaction. func (tx *Transaction) Base64Signatures() []string { raw := tx.Envelope.Signatures - // Maximum number of signatures TransactionEnvelope can contain is 20 - results := make([]string, 0, 20) + results := make([]string, len(raw)) for i := range raw { - // Fix for https://github.com/stellar/stellar-core/issues/1225 - if len(raw[i].Signature) > 0 { - results = append(results, base64.StdEncoding.EncodeToString(raw[i].Signature)) - } + results[i] = base64.StdEncoding.EncodeToString(raw[i].Signature) } return results } diff --git a/src/github.com/stellar/horizon/db2/core/transaction_test.go b/src/github.com/stellar/horizon/db2/core/transaction_test.go index a11fd216..01633f49 100644 --- a/src/github.com/stellar/horizon/db2/core/transaction_test.go +++ b/src/github.com/stellar/horizon/db2/core/transaction_test.go @@ -39,15 +39,3 @@ func TestMemo(t *testing.T) { tt.Assert.Equal("1.0.1owerride me", tx.Memo().String) } - -func TestSignatures(t *testing.T) { - tt := test.Start(t).Scenario("base") - defer tt.Finish() - - var tx Transaction - - // https://github.com/stellar/stellar-core/issues/1225 - xdr.SafeUnmarshalBase64("AAAAAMIK9djC7k75ziKOLJcvMAIBG7tnBuoeI34x+Pi6zqcZAAAAZAAZphYAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAynnCTTyw53VVRLOWX6XKTva63IM1LslPNW01YB0hz/8AAAAAAAAAAlQL5AAAAAAAAAAAAh0hz/8AAABA8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAnhSuFUAAAAA", &tx.Envelope) - - tt.Assert.Equal(1, len(tx.Base64Signatures())) -} From 64330e9b86b5d15512c17ac20f5a00e3aca439fc Mon Sep 17 00:00:00 2001 From: Bartek Nowotarski Date: Tue, 4 Jul 2017 18:23:38 +0200 Subject: [PATCH 2/2] Update Ingestion.Transaction to insert empty signatures --- .../horizon/db2/core/transaction_test.go | 16 ++++++ .../stellar/horizon/ingest/ingestion.go | 30 +++++++---- .../stellar/horizon/ingest/ingestion_test.go | 51 +++++++++++++++++++ 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 src/github.com/stellar/horizon/ingest/ingestion_test.go diff --git a/src/github.com/stellar/horizon/db2/core/transaction_test.go b/src/github.com/stellar/horizon/db2/core/transaction_test.go index 01633f49..d59e5125 100644 --- a/src/github.com/stellar/horizon/db2/core/transaction_test.go +++ b/src/github.com/stellar/horizon/db2/core/transaction_test.go @@ -39,3 +39,19 @@ func TestMemo(t *testing.T) { tt.Assert.Equal("1.0.1owerride me", tx.Memo().String) } + +func TestSignatures(t *testing.T) { + tt := test.Start(t).Scenario("base") + defer tt.Finish() + + var tx Transaction + + // https://github.com/stellar/stellar-core/issues/1225 + xdr.SafeUnmarshalBase64("AAAAAMIK9djC7k75ziKOLJcvMAIBG7tnBuoeI34x+Pi6zqcZAAAAZAAZphYAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAynnCTTyw53VVRLOWX6XKTva63IM1LslPNW01YB0hz/8AAAAAAAAAAlQL5AAAAAAAAAAAAh0hz/8AAABA8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAnhSuFUAAAAA", &tx.Envelope) + + signatures := tx.Base64Signatures() + + tt.Assert.Equal(2, len(signatures)) + tt.Assert.Equal("8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAg==", signatures[0]) + tt.Assert.Equal("", signatures[1]) +} diff --git a/src/github.com/stellar/horizon/ingest/ingestion.go b/src/github.com/stellar/horizon/ingest/ingestion.go index d246554f..f08fc02f 100644 --- a/src/github.com/stellar/horizon/ingest/ingestion.go +++ b/src/github.com/stellar/horizon/ingest/ingestion.go @@ -184,15 +184,17 @@ func (ingest *Ingestion) Start() (err error) { return } -// Transaction ingests the provided transaction data into a new row in the -// `history_transactions` table -func (ingest *Ingestion) Transaction( - id int64, - tx *core.Transaction, - fee *core.TransactionFee, -) error { +// transactionInsertBuilder returns sql.InsertBuilder for a single transaction +func (ingest *Ingestion) transactionInsertBuilder(id int64, tx *core.Transaction, fee *core.TransactionFee) sq.InsertBuilder { + // Enquote empty signatures + signatures := tx.Base64Signatures() + for i, sig := range signatures { + if len(sig) == 0 { + signatures[i] = `""` + } + } - sql := ingest.transactions.Values( + return ingest.transactions.Values( id, tx.TransactionHash, tx.LedgerSequence, @@ -205,14 +207,24 @@ func (ingest *Ingestion) Transaction( tx.ResultXDR(), tx.ResultMetaXDR(), fee.ChangesXDR(), - sqx.StringArray(tx.Base64Signatures()), + sqx.StringArray(signatures), ingest.formatTimeBounds(tx.Envelope.Tx.TimeBounds), tx.MemoType(), tx.Memo(), time.Now().UTC(), time.Now().UTC(), ) +} + +// Transaction ingests the provided transaction data into a new row in the +// `history_transactions` table +func (ingest *Ingestion) Transaction( + id int64, + tx *core.Transaction, + fee *core.TransactionFee, +) error { + sql := ingest.transactionInsertBuilder(id, tx, fee) _, err := ingest.DB.Exec(sql) if err != nil { return err diff --git a/src/github.com/stellar/horizon/ingest/ingestion_test.go b/src/github.com/stellar/horizon/ingest/ingestion_test.go new file mode 100644 index 00000000..b51ad88b --- /dev/null +++ b/src/github.com/stellar/horizon/ingest/ingestion_test.go @@ -0,0 +1,51 @@ +package ingest + +import ( + "testing" + + "github.com/stellar/go/support/db" + "github.com/stellar/go/xdr" + "github.com/stellar/horizon/db2/core" + testDB "github.com/stellar/horizon/test/db" + "github.com/stretchr/testify/assert" +) + +func TestEmptySignature(t *testing.T) { + ingestion := Ingestion{ + DB: &db.Session{ + DB: testDB.Horizon(), + }, + } + ingestion.Start() + + envelope := xdr.TransactionEnvelope{} + resultPair := xdr.TransactionResultPair{} + meta := xdr.TransactionMeta{} + + xdr.SafeUnmarshalBase64("AAAAAMIK9djC7k75ziKOLJcvMAIBG7tnBuoeI34x+Pi6zqcZAAAAZAAZphYAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAynnCTTyw53VVRLOWX6XKTva63IM1LslPNW01YB0hz/8AAAAAAAAAAlQL5AAAAAAAAAAAAh0hz/8AAABA8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAnhSuFUAAAAA", &envelope) + xdr.SafeUnmarshalBase64("AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=", &resultPair.Result) + xdr.SafeUnmarshalBase64("AAAAAAAAAAEAAAADAAAAAQAZphoAAAAAAAAAAMIK9djC7k75ziKOLJcvMAIBG7tnBuoeI34x+Pi6zqcZAAAAF0h255wAGaYWAAAAAQAAAAMAAAAAAAAAAAAAAAADBQUFAAAAAwAAAAAtkqVYLPLYhqNMmQLPc+T9eTWp8LIE8eFlR5K4wNJKTQAAAAMAAAAAynnCTTyw53VVRLOWX6XKTva63IM1LslPNW01YB0hz/8AAAADAAAAAuOwxEKY/BwUmvv0yJlvuSQnrkHkZJuTTKSVmRt4UrhVAAAAAwAAAAAAAAAAAAAAAwAZphYAAAAAAAAAAMp5wk08sOd1VUSzll+lyk72utyDNS7JTzVtNWAdIc//AAAAF0h26AAAGaYWAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAQAZphoAAAAAAAAAAMp5wk08sOd1VUSzll+lyk72utyDNS7JTzVtNWAdIc//AAAAGZyCzAAAGaYWAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAA", &meta) + + transaction := &core.Transaction{ + TransactionHash: "1939a8de30981e4171e1aaeca54a058a7fb06684864facba0620ab8cc5076d4f", + LedgerSequence: 1680922, + Index: 1, + Envelope: envelope, + Result: resultPair, + ResultMeta: meta, + } + + transactionFee := &core.TransactionFee{} + + builder := ingestion.transactionInsertBuilder(1, transaction, transactionFee) + sql, args, err := builder.ToSql() + assert.Equal(t, "INSERT INTO history_transactions (id,transaction_hash,ledger_sequence,application_order,account,account_sequence,fee_paid,operation_count,tx_envelope,tx_result,tx_meta,tx_fee_meta,signatures,time_bounds,memo_type,memo,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?::character varying[],?,?,?,?,?)", sql) + assert.Equal(t, `{8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAg==,""}`, args[12]) + assert.NoError(t, err) + + err = ingestion.Transaction(1, transaction, transactionFee) + assert.NoError(t, err) + + err = ingestion.Close() + assert.NoError(t, err) +}