@@ -8,9 +8,22 @@ import (
88 "github.com/stellar/go/xdr"
99)
1010
11+ type txVersion int
12+
13+ const (
14+ v0 = iota
15+ v1 = iota
16+ )
17+
18+ type txSetMember struct {
19+ v txVersion
20+ xdrV0 * xdr.TransactionV0
21+ xdrV1 * xdr.Transaction
22+ }
23+
1124type ledgerSerializerXDR struct {
1225 ledgerHeader * LedgerHeader
13- transactions []xdr. Transaction
26+ txSet [] txSetMember
1427 transactionResults []xdr.TransactionResultPair
1528 transactionMetas []xdr.TransactionMeta
1629 changes []xdr.LedgerEntryChanges
@@ -20,33 +33,30 @@ type ledgerSerializerXDR struct {
2033
2134// SerializeLedger serializes ledger data into ES bulk index data
2235func SerializeLedgerFromHistory (meta xdr.LedgerCloseMeta , buffer * bytes.Buffer ) {
23- transactions := make ([]xdr.Transaction , len (meta .V0 .TxSet .Txs ))
24- transactionResults := make ([]xdr.TransactionResultPair , len (meta .V0 .TxProcessing ))
25- changes := make ([]xdr.LedgerEntryChanges , len (meta .V0 .TxProcessing ))
26- transactionMetas := make ([]xdr.TransactionMeta , len (meta .V0 .TxProcessing ))
36+ serializer := & ledgerSerializerXDR {
37+ txSet : make ([]txSetMember , len (meta .V0 .TxSet .Txs )),
38+ ledgerHeader : NewLedgerHeaderFromHistory (meta .V0 .LedgerHeader ),
39+ transactionResults : make ([]xdr.TransactionResultPair , len (meta .V0 .TxProcessing )),
40+ changes : make ([]xdr.LedgerEntryChanges , len (meta .V0 .TxProcessing )),
41+ transactionMetas : make ([]xdr.TransactionMeta , len (meta .V0 .TxProcessing )),
42+ buffer : buffer ,
43+ }
2744
2845 for i , txe := range meta .V0 .TxSet .Txs {
2946 switch txe .Type {
3047 case xdr .EnvelopeTypeEnvelopeTypeTxV0 :
31- transactions [i ] = txe .V0 .Tx
48+ serializer . txSet [i ] = txSetMember { v : v0 , xdrV0 : & txe .V0 .Tx }
3249 case xdr .EnvelopeTypeEnvelopeTypeTx :
33- transactions [i ] = txe .V1 .Tx
50+ serializer .txSet [i ] = txSetMember {v : v1 , xdrV1 : & txe .V1 .Tx }
51+ default :
52+ log .Fatal ("Unknown type" )
3453 }
3554 }
3655
3756 for i , txp := range meta .V0 .TxProcessing {
38- transactionResults [i ] = txp .Result
39- changes [i ] = txp .FeeProcessing
40- transactionMetas [i ] = txp .TxApplyProcessing
41- }
42-
43- serializer := & ledgerSerializerXDR {
44- ledgerHeader : NewLedgerHeaderFromHistory (meta .V0 .LedgerHeader ),
45- transactions : transactions ,
46- transactionResults : transactionResults ,
47- transactionMetas : transactionMetas ,
48- changes : changes ,
49- buffer : buffer ,
57+ serializer .transactionResults [i ] = txp .Result
58+ serializer .changes [i ] = txp .FeeProcessing
59+ serializer .transactionMetas [i ] = txp .TxApplyProcessing
5060 }
5161
5262 serializer .serialize ()
@@ -55,16 +65,24 @@ func SerializeLedgerFromHistory(meta xdr.LedgerCloseMeta, buffer *bytes.Buffer)
5565func (s * ledgerSerializerXDR ) serialize () {
5666 SerializeForBulk (s .ledgerHeader , s .buffer )
5767
58- for i , tx := range s .transactions {
59- transaction , err := NewTransactionFromXDR (
60- & transactionData {
61- xdr : tx ,
62- result : s .transactionResults [i ],
63- index : i + 1 ,
64- ledgerSeq : s .ledgerHeader .Seq ,
65- closeTime : s .ledgerHeader .CloseTime ,
66- },
67- )
68+ for i , tx := range s .txSet {
69+ txData := transactionData {
70+ result : s .transactionResults [i ],
71+ index : i + 1 ,
72+ ledgerSeq : s .ledgerHeader .Seq ,
73+ closeTime : s .ledgerHeader .CloseTime ,
74+ }
75+
76+ switch tx .v {
77+ case v0 :
78+ txData .v = v0
79+ txData .xdrV0 = tx .xdrV0
80+ case v1 :
81+ txData .v = v1
82+ txData .xdrV1 = tx .xdrV1
83+ }
84+
85+ transaction , err := NewTransactionFromXDR (& txData )
6886
6987 if err != nil {
7088 log .Fatal (err )
@@ -81,12 +99,17 @@ func (s *ledgerSerializerXDR) serialize() {
8199 }
82100}
83101
84- func (s * ledgerSerializerXDR ) serializeOperations (operations []xdr.Operation , operationResults * []xdr.OperationResult , transaction * Transaction ) {
102+ func (s * ledgerSerializerXDR ) serializeOperations (operations []xdr.Operation , operationResults * []xdr.OperationResult , transaction * Transaction ) error {
85103 // effectsCount := 0
86104
87105 for index , operation := range operations {
88106 result := (* operationResults )[index ]
89- operation := ProduceOperation (transaction , & operation , & result , index + 1 )
107+ operation , err := ProduceOperation (transaction , & operation , & result , index + 1 )
108+
109+ if err != nil {
110+ return err
111+ }
112+
90113 SerializeForBulk (operation , s .buffer )
91114
92115 if transaction .Successful {
@@ -104,6 +127,8 @@ func (s *ledgerSerializerXDR) serializeOperations(operations []xdr.Operation, op
104127 }
105128 }
106129 }
130+
131+ return nil
107132}
108133
109134func (s * ledgerSerializerXDR ) serializeBalances (changes xdr.LedgerEntryChanges , transaction * Transaction , operation * Operation , source BalanceSource ) int {
0 commit comments