Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ingest signers change from pre-auth transactions #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion es/ledger_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (s *ledgerSerializer) serialize() {
if transaction.Successful {
changes := s.feeRows[transaction.Index-1].Changes
s.serializeBalances(changes, transaction, nil, BalanceSourceFee)

h := ProduceSignerHistoryFromTxMeta(transaction)
if h != nil {
SerializeForBulk(h, s.buffer)
}
}

s.serializeOperations(transactionRow, transaction)
Expand All @@ -64,7 +69,7 @@ func (s *ledgerSerializer) serializeOperations(transactionRow db.TxHistoryRow, t

s.serializeTrades(result, transaction, operation, effectsCount)

h := ProduceSignerHistory(operation)
h := ProduceSignerHistoryFromOperation(operation)
if h != nil {
SerializeForBulk(h, s.buffer)
}
Expand Down
83 changes: 81 additions & 2 deletions es/signer_history.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package es

import (
"github.com/stellar/go/xdr"
"time"
)

Expand All @@ -17,8 +18,86 @@ type SignerHistory struct {
LedgerCloseTime time.Time `json:"ledger_close_time"`
}

// ProduceSignerHistory creates new signer history entry
func ProduceSignerHistory(o *Operation) (h *SignerHistory) {
// ProduceSignerHistoryFromTxMeta handles pre-authorized transactions
//
// Stellar allows user to create pre-authorized transactions, which
// can be submitted later by other users. This mechanism is implemented
// using multi-sig. When such pre-authorized transaction is executed, the "signer"
// for this transaction is deleted. We should ingest this change too
//
// You can read about pre-authorized transactions process here:
// https://medium.com/@katopz/stellar-pre-signed-transaction-d93e91191c15
func ProduceSignerHistoryFromTxMeta(tx *Transaction) (h *SignerHistory) {
currentSigners := make(map[string][]xdr.Signer)

for _, change := range tx.meta.TxChanges {
if change.EntryType() != xdr.LedgerEntryTypeAccount {
continue
}

switch t := change.Type; t {
case xdr.LedgerEntryChangeTypeLedgerEntryState:
accountData := change.MustState().Data.MustAccount()
accountId := accountData.AccountId.Address()

currentSigners[accountId] = accountData.Signers

case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
accountData := change.MustUpdated().Data.MustAccount()
accountId := accountData.AccountId.Address()

if len(currentSigners[accountId]) == len(accountData.Signers) {
continue
}

for _, signer := range currentSigners[accountId] {
if contains(accountData.Signers, signer) {
continue
}

s := NewSigner(&signer)

token := PagingToken{
LedgerSeq: tx.Seq,
TransactionOrder: tx.Index,
}

entry := &SignerHistory{
PagingToken: token,
AccountID: accountId,
Signer: s.ID,
Type: s.Type,
Weight: 0,
TxIndex: tx.Index,
Index: 0,
Seq: tx.Seq,
LedgerCloseTime: tx.CloseTime,
}

// we can return quickly, because there can be only one
// signer change in pre-auth tx
return entry
}
}
}

return nil
}

// contains checks, whether given array of signers contains given
// particular signer or nor
func contains(array []xdr.Signer, signer xdr.Signer) bool {
for _, s := range array {
if s.Key.Address() == signer.Key.Address() && s.Weight == signer.Weight {
return true
}
}

return false
}

// ProduceSignerHistoryFromOperation creates new signer history entry
func ProduceSignerHistoryFromOperation(o *Operation) (h *SignerHistory) {
s := o.Signer

if s == nil {
Expand Down
3 changes: 3 additions & 0 deletions es/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Transaction struct {
ResultCode int `json:"result_code"`
SourceAccountID string `json:"source_account_id"`

meta xdr.TransactionMetaV1

*TimeBounds `json:"time_bounds,omitempty"`
*Memo `json:"memo,omitempty"`
}
Expand All @@ -41,6 +43,7 @@ func NewTransaction(row *db.TxHistoryRow, t time.Time) *Transaction {
Successful: resultCode == xdr.TransactionResultCodeTxSuccess,
ResultCode: int(resultCode),
SourceAccountID: row.Envelope.Tx.SourceAccount.Address(),
meta: row.Meta.MustV1(),
}

if row.Envelope.Tx.Memo.Type != xdr.MemoTypeMemoNone {
Expand Down