Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chowbao committed Jan 21, 2025
1 parent d385526 commit be020c9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
46 changes: 23 additions & 23 deletions ingest/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package ledger

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

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

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

func ID(l xdr.LedgerCloseMeta) int64 {
return toid.New(int32(l.LedgerSequence()), 0, 0).ToInt64()
}

func Hash(l xdr.LedgerCloseMeta) string {
return l.LedgerHeaderHistoryEntry().Hash.HexString()
}
Expand Down Expand Up @@ -79,10 +75,10 @@ func TotalByteSizeOfBucketList(l xdr.LedgerCloseMeta) (uint64, bool) {
return uint64(lcmV1.TotalByteSizeOfBucketList), true
}

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

}
return LedgerCloseValueSignature.NodeId.GetAddress()
Expand All @@ -97,15 +93,17 @@ func Signature(l xdr.LedgerCloseMeta) (string, bool) {
return base64.StdEncoding.EncodeToString(LedgerCloseValueSignature.Signature), true
}

// Add docstring to larger, more complicated functions
func TransactionCounts(l xdr.LedgerCloseMeta) (successTxCount, failedTxCount int32, ok bool) {
var results []xdr.TransactionResultMeta

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

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

for i := 0; i < txCount; i++ {
Expand All @@ -116,31 +114,33 @@ func TransactionCounts(l xdr.LedgerCloseMeta) (successTxCount, failedTxCount int
}
}

return successTxCount, failedTxCount, true
return successTxCount, failedTxCount
}

// Add docstring to larger, more complicated functions
func OperationCounts(l xdr.LedgerCloseMeta) (operationCount, txSetOperationCount int32, ok bool) {
var results []xdr.TransactionResultMeta

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

for i, result := range results {
operations := transactions[i].Operations()
numberOfOps := int32(len(operations))
txSetOperationCount += numberOfOps
totalOperationCount += numberOfOps

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

operationCount += int32(len(operationResults))
successfulOperationCount += int32(len(operationResults))
}
}

return operationCount, txSetOperationCount, true
return successfulOperationCount, totalOperationCount
}
12 changes: 5 additions & 7 deletions ingest/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestLedger(t *testing.T) {
ledger := ledgerTestInput()

assert.Equal(t, uint32(30578981), Sequence(ledger))
assert.Equal(t, int64(131335723340005376), ID(ledger))
assert.Equal(t, "26932dc4d84b5fabe9ae744cb43ce4c6daccf98c86a991b2a14945b1adac4d59", Hash(ledger))
assert.Equal(t, "f63c15d0eaf48afbd751a4c4dfade54a3448053c47c5a71d622668ae0cc2a208", PreviousHash(ledger))
assert.Equal(t, int64(1594584547), CloseTime(ledger))
Expand All @@ -38,8 +37,9 @@ func TestLedger(t *testing.T) {
assert.Equal(t, uint64(56), bucketSize)

var nodeID string
nodeID, ok = NodeID(ledger)
assert.Equal(t, true, ok)
var err error
nodeID, err = NodeID(ledger)
assert.Equal(t, nil, err)
assert.Equal(t, "GARAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA76O", nodeID)

var signature string
Expand All @@ -49,13 +49,11 @@ func TestLedger(t *testing.T) {

var success int32
var failed int32
success, failed, ok = TransactionCounts(ledger)
assert.Equal(t, true, ok)
success, failed = TransactionCounts(ledger)
assert.Equal(t, int32(1), success)
assert.Equal(t, int32(1), failed)

success, failed, ok = OperationCounts(ledger)
assert.Equal(t, true, ok)
success, failed = OperationCounts(ledger)
assert.Equal(t, int32(1), success)
assert.Equal(t, int32(13), failed)
}
Expand Down
8 changes: 4 additions & 4 deletions xdr/ledger_close_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func (l LedgerCloseMeta) EvictedPersistentLedgerEntries() ([]LedgerEntry, error)
}

// TxProcessing returns the TransactionResultMeta in this ledger
func (l LedgerCloseMeta) TxProcessing() []TransactionResultMeta {
func (l LedgerCloseMeta) TxProcessing() ([]TransactionResultMeta, error) {
switch l.V {
case 0:
return l.MustV0().TxProcessing
return l.MustV0().TxProcessing, nil
case 1:
return l.MustV1().TxProcessing
return l.MustV1().TxProcessing, nil
default:
panic(fmt.Sprintf("Unsupported LedgerCloseMeta.V: %d", l.V))
return []TransactionResultMeta{}, fmt.Errorf("Unsupported LedgerCloseMeta.V: %d", l.V)
}
}
16 changes: 10 additions & 6 deletions xdr/node_id.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package xdr

import "github.com/stellar/go/strkey"
import (
"fmt"

func (n NodeId) GetAddress() (string, bool) {
"github.com/stellar/go/strkey"
)

func (n NodeId) GetAddress() (string, error) {
switch n.Type {
case PublicKeyTypePublicKeyTypeEd25519:
ed, ok := n.GetEd25519()
if !ok {
return "", false
return "", fmt.Errorf("could not get NodeID.Ed25519")
}
raw := make([]byte, 32)
copy(raw, ed[:])
encodedAddress, err := strkey.Encode(strkey.VersionByteAccountID, raw)
if err != nil {
return "", false
return "", err
}
return encodedAddress, true
return encodedAddress, nil
default:
return "", false
return "", fmt.Errorf("unknown NodeId.PublicKeyType")
}
}

0 comments on commit be020c9

Please sign in to comment.