-
Notifications
You must be signed in to change notification settings - Fork 503
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
exp/ingest: Ingest Session #1456
Changes from 13 commits
fa32ff8
84bb499
80f1bdb
cdfddf6
3639528
1eb61e3
7ce506c
d378406
b969d9c
c9e7fc7
2371386
5d98b52
c06d856
ad63703
10ac842
709ad99
90440d4
6c7cb38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"fmt" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"fmt" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"testing" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,15 +9,6 @@ import ( | |
"github.com/stellar/go/xdr" | ||
) | ||
|
||
// LedgerTransaction represents the data for a single transaction within a ledger. | ||
type LedgerTransaction struct { | ||
Index uint32 | ||
Envelope xdr.TransactionEnvelope | ||
Result xdr.TransactionResultPair | ||
Meta xdr.TransactionMeta | ||
FeeChanges xdr.LedgerEntryChanges | ||
} | ||
|
||
// DBLedgerReadCloser is a database-backed implementation of the io.LedgerReadCloser interface. | ||
type DBLedgerReadCloser struct { | ||
sequence uint32 | ||
|
@@ -33,11 +24,19 @@ type DBLedgerReadCloser struct { | |
var _ LedgerReadCloser = (*DBLedgerReadCloser)(nil) | ||
|
||
// MakeLedgerReadCloser is a factory method for LedgerReadCloser. | ||
func MakeLedgerReadCloser(sequence uint32, backend ledgerbackend.LedgerBackend) *DBLedgerReadCloser { | ||
return &DBLedgerReadCloser{ | ||
func MakeLedgerReadCloser(sequence uint32, backend ledgerbackend.LedgerBackend) (*DBLedgerReadCloser, error) { | ||
reader := &DBLedgerReadCloser{ | ||
sequence: sequence, | ||
backend: backend, | ||
} | ||
|
||
var err error | ||
reader.initOnce.Do(func() { err = reader.init() }) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return reader, nil | ||
} | ||
|
||
// GetSequence returns the sequence number of the ledger data stored by this object. | ||
|
@@ -46,13 +45,15 @@ func (dblrc *DBLedgerReadCloser) GetSequence() uint32 { | |
} | ||
|
||
// GetHeader returns the XDR Header data associated with the stored ledger. | ||
func (dblrc *DBLedgerReadCloser) GetHeader() (xdr.LedgerHeaderHistoryEntry, error) { | ||
func (dblrc *DBLedgerReadCloser) GetHeader() xdr.LedgerHeaderHistoryEntry { | ||
var err error | ||
dblrc.initOnce.Do(func() { err = dblrc.init() }) | ||
if err != nil { | ||
return xdr.LedgerHeaderHistoryEntry{}, err | ||
// TODO, object should be initialized in constructor. | ||
// Not returning error here, makes this much simpler. | ||
panic(err) | ||
} | ||
return dblrc.header, nil | ||
return dblrc.header | ||
} | ||
|
||
// Read returns the next transaction in the ledger, ordered by tx number, each time it is called. When there | ||
|
@@ -92,7 +93,7 @@ func (dblrc *DBLedgerReadCloser) init() error { | |
return errors.Wrap(err, "error reading ledger from backend") | ||
} | ||
if !exists { | ||
return errors.Wrap(err, "ledger was not found") | ||
return ErrNotFound | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting note: it was actually always returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh wow, that's nasty. Very interesting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added it here if you're interested: dominikh/go-tools#529 |
||
|
||
dblrc.header = ledgerCloseMeta.LedgerHeader | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io | ||
|
||
import ( | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
// Change is a developer friendly representation of LedgerEntryChanges. | ||
// It also provides some helper functions to quickly check if a given | ||
// change has occured in an entry. | ||
// | ||
// If an entry is created: Pre is nil and Post is not nil. | ||
// If an entry is updated: Pre is not nil and Post is not nil. | ||
// If an entry is removed: Pre is not nil and Post is nil. | ||
type Change struct { | ||
Type xdr.LedgerEntryType | ||
Pre *xdr.LedgerEntryData | ||
Post *xdr.LedgerEntryData | ||
} | ||
|
||
func (c *Change) AccountSignersChanged() bool { | ||
if c.Type != xdr.LedgerEntryTypeAccount { | ||
panic("This should not be called on changes other than Account changes") | ||
} | ||
|
||
// Signers must be removed before merging an account and it's | ||
// impossible to add signers during a creation of a new account. | ||
if c.Pre == nil || c.Post == nil { | ||
return false | ||
} | ||
|
||
if len(c.Pre.MustAccount().Signers) != len(c.Post.MustAccount().Signers) { | ||
return true | ||
} | ||
|
||
signers := map[string]uint32{} // signer => weight | ||
|
||
for _, signer := range c.Pre.MustAccount().Signers { | ||
signers[signer.Key.Address()] = uint32(signer.Weight) | ||
} | ||
|
||
for _, signer := range c.Post.MustAccount().Signers { | ||
weight, exist := signers[signer.Key.Address()] | ||
if !exist { | ||
return false | ||
} | ||
|
||
if weight != uint32(signer.Weight) { | ||
return false | ||
} | ||
} | ||
|
||
// TODO should it also change on master key weight changes? | ||
|
||
return false | ||
} | ||
|
||
// GetChanges returns a developer friendly representation of LedgerEntryChanges. | ||
// Currently it results operations related LedgerEntryChanges only. | ||
// TODO this should include TransactionMetaV1.txChanges and fee changes too! | ||
func (t *LedgerTransaction) GetChanges() []Change { | ||
changes := []Change{} | ||
|
||
for _, operationMeta := range t.Meta.OperationsMeta() { | ||
ledgerEntryChanges := operationMeta.Changes | ||
for i := 0; i < len(ledgerEntryChanges); i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you use a range for loop here?
|
||
entryChange := ledgerEntryChanges[i] | ||
|
||
switch entryChange.Type { | ||
case xdr.LedgerEntryChangeTypeLedgerEntryCreated: | ||
created := entryChange.MustCreated() | ||
changes = append(changes, Change{ | ||
Type: created.Data.Type, | ||
Pre: nil, | ||
Post: &created.Data, | ||
}) | ||
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated: | ||
state := ledgerEntryChanges[i-1].MustState() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with the operations meta data format. Could it be possible that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has specific format and I checked it in stellar-core. The algorithm is:
However, I will confirm it with the core team to be sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does that mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it results from the algorithm above. However, I will confirm it with the core team next week. |
||
updated := entryChange.MustUpdated() | ||
changes = append(changes, Change{ | ||
Type: state.Data.Type, | ||
Pre: &state.Data, | ||
Post: &updated.Data, | ||
}) | ||
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved: | ||
state := ledgerEntryChanges[i-1].MustState() | ||
changes = append(changes, Change{ | ||
Type: state.Data.Type, | ||
Pre: &state.Data, | ||
Post: nil, | ||
}) | ||
case xdr.LedgerEntryChangeTypeLedgerEntryState: | ||
continue | ||
default: | ||
panic("Invalid LedgerEntryChangeType") | ||
} | ||
} | ||
} | ||
|
||
return changes | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related to #1433 . because
reader
is created a few lines above. it seems strangereader
initialize it usinginitOnce.Do()
. fixing this might be outside the scope of this PR thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we'll decide on #1433 next week and change all the instances in another PR.