Skip to content

Commit

Permalink
Fix horizon test and build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Mar 31, 2020
1 parent 284e64a commit 065c05d
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 135 deletions.
4 changes: 2 additions & 2 deletions exp/ingest/io/stats_ledger_transaction_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type StatsLedgerTransactionProcessorResults struct {

func (p *StatsLedgerTransactionProcessor) ProcessTransaction(transaction LedgerTransaction) error {
p.results.Transactions++
ops := int64(len(transaction.Envelope.Tx.Operations))
ops := int64(len(transaction.Envelope.Operations()))
p.results.Operations += ops

if transaction.Successful() {
Expand All @@ -52,7 +52,7 @@ func (p *StatsLedgerTransactionProcessor) ProcessTransaction(transaction LedgerT
p.results.OperationsInFailed += ops
}

for _, op := range transaction.Envelope.Tx.Operations {
for _, op := range transaction.Envelope.Operations() {
switch op.Body.Type {
case xdr.OperationTypeCreateAccount:
p.results.OperationsCreateAccount++
Expand Down
70 changes: 38 additions & 32 deletions exp/ingest/io/stats_ledger_transaction_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ func TestStatsLedgerTransactionProcessor(t *testing.T) {
},
},
Envelope: xdr.TransactionEnvelope{
Tx: xdr.Transaction{
Operations: []xdr.Operation{
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreateAccount}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePayment}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictReceive}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreatePassiveSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeSetOptions}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeChangeTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAllowTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAccountMerge}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeInflation}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageData}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeBumpSequence}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageBuyOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictSend}},
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Operations: []xdr.Operation{
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreateAccount}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePayment}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictReceive}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreatePassiveSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeSetOptions}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeChangeTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAllowTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAccountMerge}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeInflation}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageData}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeBumpSequence}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageBuyOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictSend}},
},
},
},
},
Expand All @@ -51,22 +54,25 @@ func TestStatsLedgerTransactionProcessor(t *testing.T) {
},
},
Envelope: xdr.TransactionEnvelope{
Tx: xdr.Transaction{
Operations: []xdr.Operation{
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreateAccount}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePayment}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictReceive}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreatePassiveSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeSetOptions}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeChangeTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAllowTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAccountMerge}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeInflation}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageData}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeBumpSequence}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageBuyOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictSend}},
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Operations: []xdr.Operation{
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreateAccount}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePayment}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictReceive}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeCreatePassiveSellOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeSetOptions}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeChangeTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAllowTrust}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeAccountMerge}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeInflation}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageData}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeBumpSequence}},
{Body: xdr.OperationBody{Type: xdr.OperationTypeManageBuyOffer}},
{Body: xdr.OperationBody{Type: xdr.OperationTypePathPaymentStrictSend}},
},
},
},
},
Expand Down
19 changes: 19 additions & 0 deletions network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ func HashFeeBumpTransaction(tx *xdr.FeeBumpTransaction, passphrase string) ([32]
return hashTx(taggedTx, passphrase)
}

// HashTransactionV0 derives the network specific hash for the provided
// legacy transaction using the network identified by the supplied passphrase. The
// resulting hash is the value that can be signed by stellar secret key to
// authorize the transaction identified by the hash to stellar validators.
func HashTransactionV0(tx *xdr.TransactionV0, passphrase string) ([32]byte, error) {
v1Tx := &xdr.Transaction{
SourceAccount: xdr.AccountId{
Type: xdr.PublicKeyTypePublicKeyTypeEd25519,
Ed25519: &tx.SourceAccountEd25519,
},
Fee: tx.Fee,
Memo: tx.Memo,
Operations: tx.Operations,
SeqNum: tx.SeqNum,
TimeBounds: tx.TimeBounds,
}
return HashTransaction(v1Tx, passphrase)
}

func hashTx(
tx xdr.TransactionSignaturePayloadTaggedTransaction,
passphrase string,
Expand Down
25 changes: 16 additions & 9 deletions network/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ func TestHashTransaction(t *testing.T) {

require.NoError(t, err)

expected := [32]byte{
0xc4, 0x92, 0xd8, 0x7c, 0x46, 0x42, 0x81, 0x5d,
0xfb, 0x3c, 0x7d, 0xcc, 0xe0, 0x1a, 0xf4, 0xef,
0xfd, 0x16, 0x2b, 0x03, 0x10, 0x64, 0x09, 0x8a,
0x0d, 0x78, 0x6b, 0x6e, 0x0a, 0x00, 0xfd, 0x74,
}
actual, err := HashTransactionV0(&txe.V0.Tx, TestNetworkPassphrase)
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
_, err = HashTransactionV0(&txe.V0.Tx, "")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "empty network passphrase")
}

tx := xdr.Transaction{
SourceAccount: txe.SourceAccount(),
Fee: xdr.Uint32(txe.Fee()),
Expand All @@ -23,15 +38,7 @@ func TestHashTransaction(t *testing.T) {
SeqNum: xdr.SequenceNumber(txe.SeqNum()),
TimeBounds: txe.TimeBounds(),
}

expected := [32]byte{
0xc4, 0x92, 0xd8, 0x7c, 0x46, 0x42, 0x81, 0x5d,
0xfb, 0x3c, 0x7d, 0xcc, 0xe0, 0x1a, 0xf4, 0xef,
0xfd, 0x16, 0x2b, 0x03, 0x10, 0x64, 0x09, 0x8a,
0x0d, 0x78, 0x6b, 0x6e, 0x0a, 0x00, 0xfd, 0x74,
}

actual, err := HashTransaction(&tx, TestNetworkPassphrase)
actual, err = HashTransaction(&tx, TestNetworkPassphrase)
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
Expand Down
2 changes: 1 addition & 1 deletion services/friendbot/internal/friendbot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestFriendbot_Pay(t *testing.T) {
if !assert.NoError(t, err) {
return
}
expectedTxn := "AAAAAPgDPeMpTqVvOr8vkcb38bMFP4Vi6w7PvWjJgxtmQ/4YAAAAZAAAAAAAAAACAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAEAAAAA9dDyCPKtUdrjtrokM+RUfA88MrE1ETZAFxqYDgm+U4YAAAAAAAAAANKG+t565vUtbO/pb3cn4FE6XozEDoYDJcXLzr81BYYUAAAAF0h26AAAAAAAAAAAAmZD/hgAAABANEsSWMNVgAudOT2YNx5AR3k+uNDITctQCOy0jJNYfm39M/3T0XrpOAR8EUozFIoXp+Rrtm49xKzjSLHgCiYSCgm+U4YAAABA9Iazzw7Be5vPtRPqcWG+EXjsRB9o6yaIiw6SODNSuYGjKklBOYwxuB6LHSR1t8epLvn6J58ml1cs0UOt4afGAQ=="
expectedTxn := "AAAAAgAAAAD4Az3jKU6lbzq/L5HG9/GzBT+FYusOz71oyYMbZkP+GAAAAGQAAAAAAAAAAgAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAPXQ8gjyrVHa47a6JDPkVHwPPDKxNRE2QBcamA4JvlOGAAAAAAAAAADShvreeub1LWzv6W93J+BROl6MxA6GAyXFy86/NQWGFAAAABdIdugAAAAAAAAAAAJmQ/4YAAAAQDRLEljDVYALnTk9mDceQEd5PrjQyE3LUAjstIyTWH5t/TP909F66TgEfBFKMxSKF6fka7ZuPcSs40ix4AomEgoJvlOGAAAAQPSGs88OwXubz7UT6nFhvhF47EQfaOsmiIsOkjgzUrmBoypJQTmMMbgeix0kdbfHqS75+iefJpdXLNFDreGnxgE="
assert.Equal(t, expectedTxn, txSuccess.Env)

// Don't assert on tx values below, since the completion order is unknown.
Expand Down
31 changes: 14 additions & 17 deletions services/horizon/internal/db2/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/utf8"
"github.com/stellar/go/strkey"
"github.com/stellar/go/xdr"
)

// Base64Signatures returns a slice of strings where each element is a base64
// encoded representation of a signature attached to this transaction.
func (tx *Transaction) Base64Signatures() []string {
raw := tx.Envelope.Signatures
raw := tx.Envelope.Signatures()
results := make([]string, len(raw))

for i := range raw {
Expand All @@ -36,7 +35,7 @@ func (tx *Transaction) EnvelopeXDR() string {

// Fee returns the max fee that was set for `tx`
func (tx *Transaction) MaxFee() int32 {
return int32(tx.Envelope.Tx.Fee)
return int32(tx.Envelope.Fee())
}

// FeeCharged returns the fee that was actually charged for `tx`
Expand All @@ -55,35 +54,36 @@ func (tx *Transaction) Memo() null.String {
value string
valid bool
)
switch tx.Envelope.Tx.Memo.Type {
memo := tx.Envelope.Memo()
switch memo.Type {
case xdr.MemoTypeMemoNone:
value, valid = "", false
case xdr.MemoTypeMemoText:
scrubbed := utf8.Scrub(tx.Envelope.Tx.Memo.MustText())
scrubbed := utf8.Scrub(memo.MustText())
notnull := strings.Join(strings.Split(scrubbed, "\x00"), "")
value, valid = notnull, true
case xdr.MemoTypeMemoId:
value, valid = fmt.Sprintf("%d", tx.Envelope.Tx.Memo.MustId()), true
value, valid = fmt.Sprintf("%d", memo.MustId()), true
case xdr.MemoTypeMemoHash:
hash := tx.Envelope.Tx.Memo.MustHash()
hash := memo.MustHash()
value, valid =
base64.StdEncoding.EncodeToString(hash[:]),
true
case xdr.MemoTypeMemoReturn:
hash := tx.Envelope.Tx.Memo.MustRetHash()
hash := memo.MustRetHash()
value, valid =
base64.StdEncoding.EncodeToString(hash[:]),
true
default:
panic(fmt.Errorf("invalid memo type: %v", tx.Envelope.Tx.Memo.Type))
panic(fmt.Errorf("invalid memo type: %v", memo.Type))
}

return null.NewString(value, valid)
}

// MemoType returns the memo type for this transaction
func (tx *Transaction) MemoType() string {
switch tx.Envelope.Tx.Memo.Type {
switch tx.Envelope.Memo().Type {
case xdr.MemoTypeMemoNone:
return "none"
case xdr.MemoTypeMemoText:
Expand All @@ -95,7 +95,7 @@ func (tx *Transaction) MemoType() string {
case xdr.MemoTypeMemoReturn:
return "return"
default:
panic(fmt.Errorf("invalid memo type: %v", tx.Envelope.Tx.Memo.Type))
panic(fmt.Errorf("invalid memo type: %v", tx.Envelope.Memo().Type))
}
}

Expand All @@ -119,17 +119,14 @@ func (tx *Transaction) ResultMetaXDR() string {

// Sequence returns the sequence number for `tx`
func (tx *Transaction) Sequence() int64 {
return int64(tx.Envelope.Tx.SeqNum)
return int64(tx.Envelope.SeqNum())
}

// SourceAddress returns the strkey-encoded account id that paid the fee for
// `tx`.
func (tx *Transaction) SourceAddress() string {
sa := tx.Envelope.Tx.SourceAccount
pubkey := sa.MustEd25519()
raw := make([]byte, 32)
copy(raw, pubkey[:])
return strkey.MustEncode(strkey.VersionByteAccountID, raw)
sa := tx.Envelope.SourceAccount()
return sa.Address()
}

// TransactionByHashAfterLedger is a query that loads a single row from the `txhistory`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,33 @@ func (i *transactionBatchInsertBuilder) Exec() error {
}

func formatTimeBounds(transaction io.LedgerTransaction) interface{} {
if transaction.Envelope.Tx.TimeBounds == nil {
timeBounds := transaction.Envelope.TimeBounds()
if timeBounds == nil {
return nil
}

if transaction.Envelope.Tx.TimeBounds.MaxTime == 0 {
return sq.Expr("int8range(?,?)", transaction.Envelope.Tx.TimeBounds.MinTime, nil)
if timeBounds.MaxTime == 0 {
return sq.Expr("int8range(?,?)", timeBounds.MinTime, nil)
}

maxTime := transaction.Envelope.Tx.TimeBounds.MaxTime
maxTime := timeBounds.MaxTime
if maxTime > math.MaxInt64 {
maxTime = math.MaxInt64
}

return sq.Expr("int8range(?,?)", transaction.Envelope.Tx.TimeBounds.MinTime, maxTime)
return sq.Expr("int8range(?,?)", timeBounds.MinTime, maxTime)
}

func signatures(transaction io.LedgerTransaction) []string {
signatures := make([]string, len(transaction.Envelope.Signatures))
for i, sig := range transaction.Envelope.Signatures {
signatures := make([]string, len(transaction.Envelope.Signatures()))
for i, sig := range transaction.Envelope.Signatures() {
signatures[i] = base64.StdEncoding.EncodeToString(sig.Signature)
}
return signatures
}

func memoType(transaction io.LedgerTransaction) string {
switch transaction.Envelope.Tx.Memo.Type {
switch transaction.Envelope.Memo().Type {
case xdr.MemoTypeMemoNone:
return "none"
case xdr.MemoTypeMemoText:
Expand All @@ -93,7 +94,7 @@ func memoType(transaction io.LedgerTransaction) string {
case xdr.MemoTypeMemoReturn:
return "return"
default:
panic(fmt.Errorf("invalid memo type: %v", transaction.Envelope.Tx.Memo.Type))
panic(fmt.Errorf("invalid memo type: %v", transaction.Envelope.Memo().Type))
}
}

Expand All @@ -102,27 +103,28 @@ func memo(transaction io.LedgerTransaction) null.String {
value string
valid bool
)
switch transaction.Envelope.Tx.Memo.Type {
memo := transaction.Envelope.Memo()
switch memo.Type {
case xdr.MemoTypeMemoNone:
value, valid = "", false
case xdr.MemoTypeMemoText:
scrubbed := utf8.Scrub(transaction.Envelope.Tx.Memo.MustText())
scrubbed := utf8.Scrub(memo.MustText())
notnull := strings.Join(strings.Split(scrubbed, "\x00"), "")
value, valid = notnull, true
case xdr.MemoTypeMemoId:
value, valid = fmt.Sprintf("%d", transaction.Envelope.Tx.Memo.MustId()), true
value, valid = fmt.Sprintf("%d", memo.MustId()), true
case xdr.MemoTypeMemoHash:
hash := transaction.Envelope.Tx.Memo.MustHash()
hash := memo.MustHash()
value, valid =
base64.StdEncoding.EncodeToString(hash[:]),
true
case xdr.MemoTypeMemoReturn:
hash := transaction.Envelope.Tx.Memo.MustRetHash()
hash := memo.MustRetHash()
value, valid =
base64.StdEncoding.EncodeToString(hash[:]),
true
default:
panic(fmt.Errorf("invalid memo type: %v", transaction.Envelope.Tx.Memo.Type))
panic(fmt.Errorf("invalid memo type: %v", memo.Type))
}

return null.NewString(value, valid)
Expand All @@ -146,16 +148,17 @@ func transactionToMap(transaction io.LedgerTransaction, sequence uint32) (map[st
return nil, err
}

sourceAccount := transaction.Envelope.SourceAccount()
return map[string]interface{}{
"id": toid.New(int32(sequence), int32(transaction.Index), 0).ToInt64(),
"transaction_hash": hex.EncodeToString(transaction.Result.TransactionHash[:]),
"ledger_sequence": sequence,
"application_order": int32(transaction.Index),
"account": transaction.Envelope.Tx.SourceAccount.Address(),
"account_sequence": strconv.FormatInt(int64(transaction.Envelope.Tx.SeqNum), 10),
"max_fee": int32(transaction.Envelope.Tx.Fee),
"account": sourceAccount.Address(),
"account_sequence": strconv.FormatInt(transaction.Envelope.SeqNum(), 10),
"max_fee": int32(transaction.Envelope.Fee()),
"fee_charged": int32(transaction.Result.Result.FeeCharged),
"operation_count": int32(len(transaction.Envelope.Tx.Operations)),
"operation_count": int32(len(transaction.Envelope.Operations())),
"tx_envelope": envelopeBase64,
"tx_result": resultBase64,
"tx_meta": metaBase64,
Expand Down
7 changes: 5 additions & 2 deletions services/horizon/internal/expingest/fake_ledger_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ func (f fakeLedgerBackend) GetLedger(sequence uint32) (bool, ledgerbackend.Ledge
},
}
ledgerCloseMeta.TransactionEnvelope[i] = xdr.TransactionEnvelope{
Tx: xdr.Transaction{
SourceAccount: xdr.MustAddress("GAHK7EEG2WWHVKDNT4CEQFZGKF2LGDSW2IVM4S5DP42RBW3K6BTODB4A"),
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
SourceAccount: xdr.MustAddress("GAHK7EEG2WWHVKDNT4CEQFZGKF2LGDSW2IVM4S5DP42RBW3K6BTODB4A"),
},
},
}
ledgerCloseMeta.TransactionMeta[i] = xdr.TransactionMeta{
Expand Down
Loading

0 comments on commit 065c05d

Please sign in to comment.