From 89421684c4efea72386c74203e1dcbc79c5413e6 Mon Sep 17 00:00:00 2001 From: Simon Chow Date: Fri, 10 Jan 2025 15:29:16 -0700 Subject: [PATCH] Change Ledger to use lcm as param instead --- ingest/ledger/ledger.go | 80 +++++++++++++++++------------------- ingest/ledger/ledger_test.go | 43 +++++++++---------- 2 files changed, 58 insertions(+), 65 deletions(-) diff --git a/ingest/ledger/ledger.go b/ingest/ledger/ledger.go index fb58dbd9a5..c37cb50b3b 100644 --- a/ingest/ledger/ledger.go +++ b/ingest/ledger/ledger.go @@ -8,60 +8,56 @@ import ( "github.com/stellar/go/xdr" ) -type Ledger struct { - Ledger xdr.LedgerCloseMeta +func Sequence(l xdr.LedgerCloseMeta) uint32 { + return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerSeq) } -func (l Ledger) Sequence() uint32 { - return uint32(l.Ledger.LedgerHeaderHistoryEntry().Header.LedgerSeq) +func ID(l xdr.LedgerCloseMeta) int64 { + return toid.New(int32(l.LedgerSequence()), 0, 0).ToInt64() } -func (l Ledger) ID() int64 { - return toid.New(int32(l.Ledger.LedgerSequence()), 0, 0).ToInt64() +func Hash(l xdr.LedgerCloseMeta) string { + return l.LedgerHeaderHistoryEntry().Hash.HexString() } -func (l Ledger) Hash() string { - return l.Ledger.LedgerHeaderHistoryEntry().Hash.HexString() +func PreviousHash(l xdr.LedgerCloseMeta) string { + return l.PreviousLedgerHash().HexString() } -func (l Ledger) PreviousHash() string { - return l.Ledger.PreviousLedgerHash().HexString() +func CloseTime(l xdr.LedgerCloseMeta) int64 { + return l.LedgerCloseTime() } -func (l Ledger) CloseTime() int64 { - return l.Ledger.LedgerCloseTime() +func ClosedAt(l xdr.LedgerCloseMeta) time.Time { + return time.Unix(l.LedgerCloseTime(), 0).UTC() } -func (l Ledger) ClosedAt() time.Time { - return time.Unix(l.Ledger.LedgerCloseTime(), 0).UTC() +func TotalCoins(l xdr.LedgerCloseMeta) int64 { + return int64(l.LedgerHeaderHistoryEntry().Header.TotalCoins) } -func (l Ledger) TotalCoins() int64 { - return int64(l.Ledger.LedgerHeaderHistoryEntry().Header.TotalCoins) +func FeePool(l xdr.LedgerCloseMeta) int64 { + return int64(l.LedgerHeaderHistoryEntry().Header.FeePool) } -func (l Ledger) FeePool() int64 { - return int64(l.Ledger.LedgerHeaderHistoryEntry().Header.FeePool) +func BaseFee(l xdr.LedgerCloseMeta) uint32 { + return uint32(l.LedgerHeaderHistoryEntry().Header.BaseFee) } -func (l Ledger) BaseFee() uint32 { - return uint32(l.Ledger.LedgerHeaderHistoryEntry().Header.BaseFee) +func BaseReserve(l xdr.LedgerCloseMeta) uint32 { + return uint32(l.LedgerHeaderHistoryEntry().Header.BaseReserve) } -func (l Ledger) BaseReserve() uint32 { - return uint32(l.Ledger.LedgerHeaderHistoryEntry().Header.BaseReserve) +func MaxTxSetSize(l xdr.LedgerCloseMeta) uint32 { + return uint32(l.LedgerHeaderHistoryEntry().Header.MaxTxSetSize) } -func (l Ledger) MaxTxSetSize() uint32 { - return uint32(l.Ledger.LedgerHeaderHistoryEntry().Header.MaxTxSetSize) +func LedgerVersion(l xdr.LedgerCloseMeta) uint32 { + return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerVersion) } -func (l Ledger) LedgerVersion() uint32 { - return uint32(l.Ledger.LedgerHeaderHistoryEntry().Header.LedgerVersion) -} - -func (l Ledger) SorobanFeeWrite1Kb() (int64, bool) { - lcmV1, ok := l.Ledger.GetV1() +func SorobanFeeWrite1Kb(l xdr.LedgerCloseMeta) (int64, bool) { + lcmV1, ok := l.GetV1() if !ok { return 0, false } @@ -74,8 +70,8 @@ func (l Ledger) SorobanFeeWrite1Kb() (int64, bool) { return int64(extV1.SorobanFeeWrite1Kb), true } -func (l Ledger) TotalByteSizeOfBucketList() (uint64, bool) { - lcmV1, ok := l.Ledger.GetV1() +func TotalByteSizeOfBucketList(l xdr.LedgerCloseMeta) (uint64, bool) { + lcmV1, ok := l.GetV1() if !ok { return 0, false } @@ -83,8 +79,8 @@ func (l Ledger) TotalByteSizeOfBucketList() (uint64, bool) { return uint64(lcmV1.TotalByteSizeOfBucketList), true } -func (l Ledger) NodeID() (string, bool) { - LedgerCloseValueSignature, ok := l.Ledger.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() +func NodeID(l xdr.LedgerCloseMeta) (string, bool) { + LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() if !ok { return "", false @@ -92,8 +88,8 @@ func (l Ledger) NodeID() (string, bool) { return LedgerCloseValueSignature.NodeId.GetAddress() } -func (l Ledger) Signature() (string, bool) { - LedgerCloseValueSignature, ok := l.Ledger.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() +func Signature(l xdr.LedgerCloseMeta) (string, bool) { + LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() if !ok { return "", false } @@ -102,11 +98,11 @@ func (l Ledger) Signature() (string, bool) { } // Add docstring to larger, more complicated functions -func (l Ledger) TransactionCounts() (successTxCount, failedTxCount int32, ok bool) { +func TransactionCounts(l xdr.LedgerCloseMeta) (successTxCount, failedTxCount int32, ok bool) { var results []xdr.TransactionResultMeta - transactions := l.Ledger.TransactionEnvelopes() - results = l.Ledger.TxProcessing() + transactions := l.TransactionEnvelopes() + results = l.TxProcessing() txCount := len(transactions) if txCount != len(results) { return 0, 0, false @@ -124,11 +120,11 @@ func (l Ledger) TransactionCounts() (successTxCount, failedTxCount int32, ok boo } // Add docstring to larger, more complicated functions -func (l Ledger) OperationCounts() (operationCount, txSetOperationCount int32, ok bool) { +func OperationCounts(l xdr.LedgerCloseMeta) (operationCount, txSetOperationCount int32, ok bool) { var results []xdr.TransactionResultMeta - transactions := l.Ledger.TransactionEnvelopes() - results = l.Ledger.TxProcessing() + transactions := l.TransactionEnvelopes() + results = l.TxProcessing() for i, result := range results { operations := transactions[i].Operations() diff --git a/ingest/ledger/ledger_test.go b/ingest/ledger/ledger_test.go index 3c4b388ec8..2345d46792 100644 --- a/ingest/ledger/ledger_test.go +++ b/ingest/ledger/ledger_test.go @@ -11,56 +11,53 @@ import ( ) func TestLedger(t *testing.T) { - ledger := Ledger{ - Ledger: ledgerTestInput(), - } - - assert.Equal(t, uint32(30578981), ledger.Sequence()) - assert.Equal(t, int64(131335723340005376), ledger.ID()) - assert.Equal(t, "26932dc4d84b5fabe9ae744cb43ce4c6daccf98c86a991b2a14945b1adac4d59", ledger.Hash()) - assert.Equal(t, "f63c15d0eaf48afbd751a4c4dfade54a3448053c47c5a71d622668ae0cc2a208", ledger.PreviousHash()) - assert.Equal(t, int64(1594584547), ledger.CloseTime()) - assert.Equal(t, time.Time(time.Date(2020, time.July, 12, 20, 9, 7, 0, time.UTC)), ledger.ClosedAt()) - assert.Equal(t, int64(1054439020873472865), ledger.TotalCoins()) - assert.Equal(t, int64(18153766209161), ledger.FeePool()) - assert.Equal(t, uint32(100), ledger.BaseFee()) - assert.Equal(t, uint32(5000000), ledger.BaseReserve()) - assert.Equal(t, uint32(1000), ledger.MaxTxSetSize()) - assert.Equal(t, uint32(13), ledger.LedgerVersion()) + 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)) + assert.Equal(t, time.Time(time.Date(2020, time.July, 12, 20, 9, 7, 0, time.UTC)), ClosedAt(ledger)) + assert.Equal(t, int64(1054439020873472865), TotalCoins(ledger)) + assert.Equal(t, int64(18153766209161), FeePool(ledger)) + assert.Equal(t, uint32(100), BaseFee(ledger)) + assert.Equal(t, uint32(5000000), BaseReserve(ledger)) + assert.Equal(t, uint32(1000), MaxTxSetSize(ledger)) + assert.Equal(t, uint32(13), LedgerVersion(ledger)) var ok bool var freeWrite int64 - freeWrite, ok = ledger.SorobanFeeWrite1Kb() + freeWrite, ok = SorobanFeeWrite1Kb(ledger) assert.Equal(t, true, ok) assert.Equal(t, int64(12), freeWrite) var bucketSize uint64 - bucketSize, ok = ledger.TotalByteSizeOfBucketList() + bucketSize, ok = TotalByteSizeOfBucketList(ledger) assert.Equal(t, true, ok) assert.Equal(t, uint64(56), bucketSize) var nodeID string - nodeID, ok = ledger.NodeID() + nodeID, ok = NodeID(ledger) assert.Equal(t, true, ok) assert.Equal(t, "GARAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA76O", nodeID) var signature string - signature, ok = ledger.Signature() + signature, ok = Signature(ledger) assert.Equal(t, true, ok) assert.Equal(t, "9g==", signature) var success int32 var failed int32 - success, failed, ok = ledger.TransactionCounts() + success, failed, ok = TransactionCounts(ledger) assert.Equal(t, true, ok) assert.Equal(t, int32(1), success) assert.Equal(t, int32(1), failed) - success, failed, ok = ledger.OperationCounts() + success, failed, ok = OperationCounts(ledger) assert.Equal(t, true, ok) assert.Equal(t, int32(1), success) assert.Equal(t, int32(13), failed) - } func ledgerTestInput() (lcm xdr.LedgerCloseMeta) {