-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds two `Session` implementations (`LiveSession` and `SingleLedgerSession`) and a simple `horizon-demo` tool. The goal of this commit is to implement the last missing component of `exp/ingest` that connects all the existing packages together: `Session`. `Session` is connecting to history archives and/or ledger backend and passes data to one or two pipelines (state pipeline and ledger pipeline). `Session` supports one of the use cases developers can interact with Stellar ledger. For example: `LiveSession` initializes the state and then follows the new ledgers and processes transactions (it's running indefinitely). On the contrary `SingleLedgerSession` processes the state of a single ledger and terminates. More sessions will be added in a future (ex. `RangeSession` that processes data between two ledgers). It also contains a simple demo app called `horizon-demo` (`go run ./exp/tools/horizon-demo`) that's using `LiveSession` internally. `horizon-demo` is reading data from history archives and ledger backend and 1) updates accounts for signers, 2) inserts transactions to a database and 3) updates in-memory orderbook graph.
- Loading branch information
Showing
43 changed files
with
2,102 additions
and
496 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ingestadapters | ||
package adapters | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
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. | ||
// It contains fee changes, transaction changes and operation changes in that | ||
// order. | ||
func (t *LedgerTransaction) GetChanges() []Change { | ||
// Fee meta | ||
changes := getChangesFromLedgerEntryChanges(t.FeeChanges) | ||
|
||
// Transaction meta | ||
v1Meta, ok := t.Meta.GetV1() | ||
if ok { | ||
txChanges := getChangesFromLedgerEntryChanges(v1Meta.TxChanges) | ||
changes = append(changes, txChanges...) | ||
} | ||
|
||
// Operation meta | ||
for _, operationMeta := range t.Meta.OperationsMeta() { | ||
ledgerEntryChanges := operationMeta.Changes | ||
opChanges := getChangesFromLedgerEntryChanges(ledgerEntryChanges) | ||
|
||
changes = append(changes, opChanges...) | ||
} | ||
|
||
return changes | ||
} | ||
|
||
// getChangesFromLedgerEntryChanges transforms LedgerEntryChanges to []Change. | ||
// Each `update` and `removed` is preceded with `state` and `create` changes | ||
// are alone, without `state`. The transformation we're doing is to move each | ||
// change (state/update, state/removed or create) to an array of pre/post pairs. | ||
// Then: | ||
// - for create, pre is null and post is a new entry, | ||
// - for update, pre is previous state and post is the current state, | ||
// - for removed, pre is previous state and post is null. | ||
// | ||
// stellar-core source: | ||
// https://github.com/stellar/stellar-core/blob/e584b43/src/ledger/LedgerTxn.cpp#L582 | ||
func getChangesFromLedgerEntryChanges(ledgerEntryChanges xdr.LedgerEntryChanges) []Change { | ||
changes := []Change{} | ||
|
||
for i, entryChange := range ledgerEntryChanges { | ||
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() | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.