From d45b0fd0b35943076ea44efa733dd95c65d0a61c Mon Sep 17 00:00:00 2001 From: Simon Chow Date: Thu, 9 Jan 2025 19:58:42 -0500 Subject: [PATCH] Remove Ledger struct; pass lcm as param --- exp/xdrill/{ => ledger}/ledger.go | 92 ++++++++++++-------------- exp/xdrill/{ => ledger}/ledger_test.go | 49 +++++++------- 2 files changed, 67 insertions(+), 74 deletions(-) rename exp/xdrill/{ => ledger}/ledger.go (53%) rename exp/xdrill/{ => ledger}/ledger_test.go (78%) diff --git a/exp/xdrill/ledger.go b/exp/xdrill/ledger/ledger.go similarity index 53% rename from exp/xdrill/ledger.go rename to exp/xdrill/ledger/ledger.go index d794fce04a..b6cb9f9d48 100644 --- a/exp/xdrill/ledger.go +++ b/exp/xdrill/ledger/ledger.go @@ -1,4 +1,4 @@ -package xdrill +package ledger import ( "encoding/base64" @@ -9,60 +9,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 utils.NewID(int32(l.LedgerSequence()), 0, 0).ToInt64() } -func (l Ledger) ID() int64 { - return utils.NewID(int32(l.Sequence()), 0, 0).ToInt64() +func Hash(l xdr.LedgerCloseMeta) string { + return utils.HashToHexString(l.LedgerHeaderHistoryEntry().Hash) } -func (l Ledger) Hash() string { - return utils.HashToHexString(l.Ledger.LedgerHeaderHistoryEntry().Hash) +func PreviousHash(l xdr.LedgerCloseMeta) string { + return utils.HashToHexString(l.PreviousLedgerHash()) } -func (l Ledger) PreviousHash() string { - return utils.HashToHexString(l.Ledger.PreviousLedgerHash()) +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.CloseTime(), 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 { - lcmV1, ok := l.Ledger.GetV1() +func SorobanFeeWrite1Kb(l xdr.LedgerCloseMeta) *int64 { + lcmV1, ok := l.GetV1() if !ok { return nil } @@ -77,8 +73,8 @@ func (l Ledger) SorobanFeeWrite1Kb() *int64 { return &result } -func (l Ledger) TotalByteSizeOfBucketList() *uint64 { - lcmV1, ok := l.Ledger.GetV1() +func TotalByteSizeOfBucketList(l xdr.LedgerCloseMeta) *uint64 { + lcmV1, ok := l.GetV1() if !ok { return nil } @@ -88,8 +84,8 @@ func (l Ledger) TotalByteSizeOfBucketList() *uint64 { return &result } -func (l Ledger) NodeID() *string { - LedgerCloseValueSignature, ok := l.Ledger.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() +func NodeID(l xdr.LedgerCloseMeta) *string { + LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() if !ok { return nil @@ -102,8 +98,8 @@ func (l Ledger) NodeID() *string { return &nodeID } -func (l Ledger) Signature() *string { - LedgerCloseValueSignature, ok := l.Ledger.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() +func Signature(l xdr.LedgerCloseMeta) *string { + LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() if !ok { return nil } @@ -114,11 +110,11 @@ func (l Ledger) Signature() *string { } // 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 := getTransactionSet(l) - results = l.Ledger.TxProcessing() + results = l.TxProcessing() txCount := len(transactions) if txCount != len(results) { return 0, 0, false @@ -136,11 +132,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 := getTransactionSet(l) - results = l.Ledger.TxProcessing() + results = l.TxProcessing() txCount := len(transactions) if txCount != len(results) { @@ -167,19 +163,19 @@ func (l Ledger) OperationCounts() (operationCount, txSetOperationCount int32, ok return operationCount, txSetOperationCount, true } -func getTransactionSet(l Ledger) (transactionProcessing []xdr.TransactionEnvelope) { - switch l.Ledger.V { +func getTransactionSet(l xdr.LedgerCloseMeta) (transactionProcessing []xdr.TransactionEnvelope) { + switch l.V { case 0: - return l.Ledger.V0.TxSet.Txs + return l.V0.TxSet.Txs case 1: - switch l.Ledger.V1.TxSet.V { + switch l.V1.TxSet.V { case 0: - return getTransactionPhase(l.Ledger.V1.TxSet.V1TxSet.Phases) + return getTransactionPhase(l.V1.TxSet.V1TxSet.Phases) default: - panic(fmt.Sprintf("unsupported LedgerCloseMeta.V1.TxSet.V: %d", l.Ledger.V1.TxSet.V)) + panic(fmt.Sprintf("unsupported LedgerCloseMeta.V1.TxSet.V: %d", l.V1.TxSet.V)) } default: - panic(fmt.Sprintf("unsupported LedgerCloseMeta.V: %d", l.Ledger.V)) + panic(fmt.Sprintf("unsupported LedgerCloseMeta.V: %d", l.V)) } } diff --git a/exp/xdrill/ledger_test.go b/exp/xdrill/ledger/ledger_test.go similarity index 78% rename from exp/xdrill/ledger_test.go rename to exp/xdrill/ledger/ledger_test.go index 9c135dd21d..7adac0a4cb 100644 --- a/exp/xdrill/ledger_test.go +++ b/exp/xdrill/ledger/ledger_test.go @@ -1,4 +1,4 @@ -package xdrill +package ledger import ( "testing" @@ -10,53 +10,50 @@ 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() - var ok bool + 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)) - freeWrite := ledger.SorobanFeeWrite1Kb() + freeWrite := SorobanFeeWrite1Kb(ledger) assert.Equal(t, int64(12), *freeWrite) - bucketSize := ledger.TotalByteSizeOfBucketList() + bucketSize := TotalByteSizeOfBucketList(ledger) assert.Equal(t, uint64(56), *bucketSize) - nodeID := ledger.NodeID() + nodeID := NodeID(ledger) assert.Equal(t, "GARAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA76O", *nodeID) - signature := ledger.Signature() + signature := Signature(ledger) assert.Equal(t, "9g==", *signature) + var ok bool 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) { - lcm = &xdr.LedgerCloseMeta{ +func ledgerTestInput() (lcm xdr.LedgerCloseMeta) { + lcm = xdr.LedgerCloseMeta{ V: 1, V1: &xdr.LedgerCloseMetaV1{ Ext: xdr.LedgerCloseMetaExt{