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

5570/xdrill contract events #5583

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a1f0f73
xdrill for ledgerCloseMeta
chowbao Jan 8, 2025
f1f97a9
go mod tidy
chowbao Jan 8, 2025
36cfcbc
Move util functions from ledger.go to utils.go
chowbao Jan 9, 2025
d064296
change some return values to pointers
chowbao Jan 9, 2025
15fe1e5
Change ledger to Ledger
chowbao Jan 9, 2025
d45b0fd
Remove Ledger struct; pass lcm as param
chowbao Jan 10, 2025
e9de99c
xdrill transaction helper functions
chowbao Jan 9, 2025
40d5057
Move xdrill ledger functions to ingest as subpackage
chowbao Jan 10, 2025
8942168
Change Ledger to use lcm as param instead
chowbao Jan 10, 2025
b5feb2c
Move xdrill ledger functions to ingest as subpackage
chowbao Jan 10, 2025
4aedfdb
Change Ledger to use lcm as param instead
chowbao Jan 10, 2025
a5bd927
Move transaction helper functions to ingest
chowbao Jan 10, 2025
0b62fe2
xdrill transaction helper functions
chowbao Jan 9, 2025
cce0cfe
Move xdrill ledger functions to ingest as subpackage
chowbao Jan 10, 2025
dab3bd9
Move transaction helper functions to ingest
chowbao Jan 10, 2025
219391d
Merge remote-tracking branch 'origin/5550/xdrill-transactions' into 5…
chowbao Jan 10, 2025
58fcfe2
fix merge issues
chowbao Jan 10, 2025
18dd712
fix merge errors
chowbao Jan 10, 2025
314b605
xdrill for operations
chowbao Jan 10, 2025
d385526
Merge branch 'master' into 5549/xdrill-ledgers
chowbao Jan 21, 2025
be020c9
address comments
chowbao Jan 21, 2025
e2ce9bd
Merge branch '5549/xdrill-ledgers' into 5550/xdrill-transactions
chowbao Jan 21, 2025
69f9bf2
Change count to uint32
chowbao Jan 21, 2025
44e627f
Merge branch '5549/xdrill-ledgers' into 5550/xdrill-transactions
chowbao Jan 21, 2025
10b4031
wip
chowbao Jan 21, 2025
93f9639
add txEnvelope OperationsCount()
chowbao Jan 21, 2025
b890631
Merge branch '5549/xdrill-ledgers' into 5550/xdrill-transactions
chowbao Jan 21, 2025
47ecec8
update timebound
chowbao Jan 21, 2025
c2a3616
add InclusionFee()
chowbao Jan 21, 2025
019a3fd
rename to InclusionFeeCharged
chowbao Jan 21, 2025
ee153f8
fix tests
chowbao Jan 21, 2025
16d94ff
Merge branch '5549/xdrill-ledgers' into 5550/xdrill-transactions
chowbao Jan 21, 2025
e9d5511
Merge branch '5550/xdrill-transactions' into 5551/xdrill-operations
chowbao Jan 21, 2025
3bc82f4
update tests
chowbao Jan 21, 2025
21dd54e
address comments
chowbao Jan 21, 2025
b6851fc
Merge branch '5549/xdrill-ledgers' into 5550/xdrill-transactions
chowbao Jan 21, 2025
3d7876c
Merge branch '5550/xdrill-transactions' into 5551/xdrill-operations
chowbao Jan 21, 2025
c8c4caf
xdrill functions for ledger entry changes
chowbao Jan 29, 2025
dd68880
xdrill functions for contract events
chowbao Jan 30, 2025
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
659 changes: 659 additions & 0 deletions ingest/change.go

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions ingest/diagnostic_event/diagnostic_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package diagnosticevent

import (
"encoding/json"
"fmt"

"github.com/chowbao/go-stellar-xdr-json/xdr2json"

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 1.22)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 1.23)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.22, 16)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.23, 12)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.23, 16)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: move xdr2json to it's own repo stellar/stellar-rpc#350

"github.com/stellar/go/strkey"
"github.com/stellar/go/xdr"
)

func Successful(d xdr.DiagnosticEvent) bool {
return d.InSuccessfulContractCall
}

func Type(d xdr.DiagnosticEvent) int32 {
return int32(d.Event.Type)
}

func ContractID(d xdr.DiagnosticEvent) (string, bool, error) {
if d.Event.ContractId == nil {
return "", false, nil
}

var err error
var contractIdByte []byte
var contractIDString string
contractId := *d.Event.ContractId
contractIdByte, err = contractId.MarshalBinary()
if err != nil {
return "", false, nil
}
contractIDString, err = strkey.Encode(strkey.VersionByteContract, contractIdByte)
if err != nil {
return "", false, nil
}

return contractIDString, true, nil
}

func Topics(d xdr.DiagnosticEvent) ([]interface{}, error) {
topics, err := GetEventTopics(d)
if err != nil {
return []interface{}{}, err
}

return serializeScValArray(topics)
}

func Data(d xdr.DiagnosticEvent) (interface{}, error) {
data, err := GetEventData(d)
if err != nil {
return []interface{}{}, err
}

return serializeScVal(data)
}

func GetEventTopics(d xdr.DiagnosticEvent) ([]xdr.ScVal, error) {
switch d.Event.Body.V {
case 0:
contractEventV0 := d.Event.Body.MustV0()
return contractEventV0.Topics, nil
default:
return []xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V))
}
}

func GetEventData(d xdr.DiagnosticEvent) (xdr.ScVal, error) {
switch d.Event.Body.V {
case 0:
contractEventV0 := d.Event.Body.MustV0()
return contractEventV0.Data, nil
default:
return xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V))
}
}

func serializeScVal(scVal xdr.ScVal) (interface{}, error) {
var serializedDataDecoded interface{}
serializedDataDecoded = "n/a"

if _, ok := scVal.ArmForSwitch(int32(scVal.Type)); ok {
var err error
var raw []byte
var jsonMessage json.RawMessage
raw, err = scVal.MarshalBinary()
if err != nil {
return nil, err
}

jsonMessage, err = xdr2json.ConvertBytes(xdr.ScVal{}, raw)
if err != nil {
return nil, err
}

serializedDataDecoded = jsonMessage
}

return serializedDataDecoded, nil
}

func serializeScValArray(scVals []xdr.ScVal) ([]interface{}, error) {
dataDecoded := make([]interface{}, 0, len(scVals))

for _, scVal := range scVals {
serializedDataDecoded, err := serializeScVal(scVal)
if err != nil {
return nil, err
}
dataDecoded = append(dataDecoded, serializedDataDecoded)
}

return dataDecoded, nil
}
143 changes: 143 additions & 0 deletions ingest/ledger/ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package ledger

import (
"encoding/base64"
"fmt"
"time"

"github.com/stellar/go/xdr"
)

func Sequence(l xdr.LedgerCloseMeta) uint32 {
return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerSeq)
}

func Hash(l xdr.LedgerCloseMeta) string {
return l.LedgerHeaderHistoryEntry().Hash.HexString()
}

func PreviousHash(l xdr.LedgerCloseMeta) string {
return l.PreviousLedgerHash().HexString()
}

func CloseTime(l xdr.LedgerCloseMeta) int64 {
return l.LedgerCloseTime()
}

func ClosedAt(l xdr.LedgerCloseMeta) time.Time {
return time.Unix(l.LedgerCloseTime(), 0).UTC()
}

func TotalCoins(l xdr.LedgerCloseMeta) int64 {
return int64(l.LedgerHeaderHistoryEntry().Header.TotalCoins)
}

func FeePool(l xdr.LedgerCloseMeta) int64 {
return int64(l.LedgerHeaderHistoryEntry().Header.FeePool)
}

func BaseFee(l xdr.LedgerCloseMeta) uint32 {
return uint32(l.LedgerHeaderHistoryEntry().Header.BaseFee)
}

func BaseReserve(l xdr.LedgerCloseMeta) uint32 {
return uint32(l.LedgerHeaderHistoryEntry().Header.BaseReserve)
}

func MaxTxSetSize(l xdr.LedgerCloseMeta) uint32 {
return uint32(l.LedgerHeaderHistoryEntry().Header.MaxTxSetSize)
}

func LedgerVersion(l xdr.LedgerCloseMeta) uint32 {
return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerVersion)
}

func SorobanFeeWrite1Kb(l xdr.LedgerCloseMeta) (int64, bool) {
lcmV1, ok := l.GetV1()
if !ok {
return 0, false
}

extV1, ok := lcmV1.Ext.GetV1()
if !ok {
return 0, false
}

return int64(extV1.SorobanFeeWrite1Kb), true
}

func TotalByteSizeOfBucketList(l xdr.LedgerCloseMeta) (uint64, bool) {
lcmV1, ok := l.GetV1()
if !ok {
return 0, false
}

return uint64(lcmV1.TotalByteSizeOfBucketList), true
}

func NodeID(l xdr.LedgerCloseMeta) (string, error) {
LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature()
if !ok {
return "", fmt.Errorf("could not get LedgerCloseValueSignature")

}
return LedgerCloseValueSignature.NodeId.GetAddress()
}

func Signature(l xdr.LedgerCloseMeta) (string, bool) {
LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature()
if !ok {
return "", false
}

return base64.StdEncoding.EncodeToString(LedgerCloseValueSignature.Signature), true
}

// TransactionCounts calculates and returns the number of successful and total transactions
func TransactionCounts(l xdr.LedgerCloseMeta) (successTxCount, totalTxCount uint32) {
transactions := l.TransactionEnvelopes()
results, err := l.TxProcessing()
if err != nil {
panic(err)
}

txCount := len(transactions)
if txCount != len(results) {
panic("transaction count and number of TransactionResultMeta not equal")
}

for i := 0; i < txCount; i++ {
if results[i].Result.Successful() {
successTxCount++
}
}

return successTxCount, uint32(txCount)
}

// OperationCounts calculates and returns the number of successful operations and the total operations within
// a LedgerCloseMeta
func OperationCounts(l xdr.LedgerCloseMeta) (successfulOperationCount, totalOperationCount uint32) {
transactions := l.TransactionEnvelopes()
results, err := l.TxProcessing()
if err != nil {
panic(err)
}

for i, result := range results {
operations := transactions[i].OperationsCount()
totalOperationCount += operations

// for successful transactions, the operation count is based on the operations results slice
if result.Result.Successful() {
operationResults, ok := result.Result.OperationResults()
if !ok {
panic("could not get OperationResults")
}

successfulOperationCount += uint32(len(operationResults))
}
}

return successfulOperationCount, totalOperationCount
}
Loading
Loading