github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/ledger_test.go (about)

     1  package transform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stellar/stellar-etl/internal/utils"
     9  
    10  	"github.com/stellar/go/historyarchive"
    11  	"github.com/stellar/go/xdr"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestTransformLedger(t *testing.T) {
    16  	type transformTest struct {
    17  		input      historyarchive.Ledger
    18  		wantOutput LedgerOutput
    19  		wantErr    error
    20  	}
    21  	hardCodedLedger, err := makeLedgerTestInput()
    22  	assert.NoError(t, err)
    23  
    24  	hardCodedOutput, err := makeLedgerTestOutput()
    25  	assert.NoError(t, err)
    26  
    27  	tests := []transformTest{
    28  		{
    29  			historyarchive.Ledger{
    30  				Header: xdr.LedgerHeaderHistoryEntry{
    31  					Header: xdr.LedgerHeader{
    32  						TotalCoins: -1,
    33  					},
    34  				},
    35  			},
    36  			LedgerOutput{},
    37  			fmt.Errorf("the total number of coins (-1) is negative for ledger 0 (ledger id=0)"),
    38  		},
    39  		{
    40  			historyarchive.Ledger{
    41  				Header: xdr.LedgerHeaderHistoryEntry{
    42  					Header: xdr.LedgerHeader{
    43  						FeePool: -1,
    44  					},
    45  				},
    46  			},
    47  			LedgerOutput{},
    48  			fmt.Errorf("the fee pool (-1) is negative for ledger 0 (ledger id=0)"),
    49  		},
    50  		{
    51  			hardCodedLedger,
    52  			hardCodedOutput,
    53  			nil,
    54  		},
    55  	}
    56  
    57  	for _, test := range tests {
    58  		actualOutput, actualError := TransformLedger(test.input)
    59  		assert.Equal(t, test.wantErr, actualError)
    60  		assert.Equal(t, test.wantOutput, actualOutput)
    61  	}
    62  }
    63  
    64  func makeLedgerTestOutput() (output LedgerOutput, err error) {
    65  	correctTime, err := time.Parse("2006-1-2 15:04:05 MST", "2020-07-12 20:09:07 UTC")
    66  	if err != nil {
    67  		return
    68  	}
    69  
    70  	correctBytes := []byte{0x41, 0x41, 0x41, 0x41, 0x44, 0x66, 0x59, 0x38, 0x46, 0x64, 0x44, 0x71, 0x39, 0x49, 0x72, 0x37, 0x31, 0x31, 0x47, 0x6b, 0x78, 0x4e, 0x2b, 0x74, 0x35, 0x55, 0x6f, 0x30, 0x53, 0x41, 0x55, 0x38, 0x52, 0x38, 0x57, 0x6e, 0x48, 0x57, 0x49, 0x6d, 0x61, 0x4b, 0x34, 0x4d, 0x77, 0x71, 0x49, 0x49, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x58, 0x77, 0x74, 0x74, 0x34, 0x77, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x48, 0x53, 0x6d, 0x53, 0x55, 0x4f, 0x6f, 0x68, 0x36, 0x7a, 0x37, 0x48, 0x6c, 0x62, 0x59, 0x51, 0x41, 0x41, 0x45, 0x49, 0x4c, 0x41, 0x79, 0x55, 0x61, 0x4a, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x5a, 0x41, 0x42, 0x4d, 0x53, 0x30, 0x41, 0x41, 0x41, 0x41, 0x50, 0x6f, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41}
    71  	output = LedgerOutput{
    72  		Sequence:           uint32(30578981),
    73  		LedgerID:           131335723340005376,
    74  		LedgerHash:         "26932dc4d84b5fabe9ae744cb43ce4c6daccf98c86a991b2a14945b1adac4d59",
    75  		PreviousLedgerHash: "f63c15d0eaf48afbd751a4c4dfade54a3448053c47c5a71d622668ae0cc2a208",
    76  		LedgerHeader:       string(correctBytes),
    77  		ClosedAt:           correctTime,
    78  
    79  		TotalCoins:      1054439020873472865,
    80  		FeePool:         18153766209161,
    81  		BaseFee:         100,
    82  		BaseReserve:     5000000,
    83  		MaxTxSetSize:    1000,
    84  		ProtocolVersion: 13,
    85  
    86  		TransactionCount:           1,
    87  		OperationCount:             10,
    88  		SuccessfulTransactionCount: 1,
    89  		FailedTransactionCount:     1,
    90  		TxSetOperationCount:        "13",
    91  	}
    92  	return
    93  }
    94  
    95  func makeLedgerTestInput() (lcm historyarchive.Ledger, err error) {
    96  	hardCodedTxSet := xdr.TransactionSet{
    97  		Txs: []xdr.TransactionEnvelope{
    98  			utils.CreateSampleTx(0, 3),
    99  			utils.CreateSampleTx(1, 10),
   100  		},
   101  	}
   102  	hardCodedTxProcessing := []xdr.TransactionResultPair{
   103  		utils.CreateSampleResultPair(false, 3),
   104  		utils.CreateSampleResultPair(true, 10),
   105  	}
   106  	lcm = historyarchive.Ledger{
   107  		Header: xdr.LedgerHeaderHistoryEntry{
   108  			Header: xdr.LedgerHeader{
   109  				LedgerSeq:          30578981,
   110  				TotalCoins:         1054439020873472865,
   111  				FeePool:            18153766209161,
   112  				BaseFee:            100,
   113  				BaseReserve:        5000000,
   114  				MaxTxSetSize:       1000,
   115  				LedgerVersion:      13,
   116  				PreviousLedgerHash: xdr.Hash{0xf6, 0x3c, 0x15, 0xd0, 0xea, 0xf4, 0x8a, 0xfb, 0xd7, 0x51, 0xa4, 0xc4, 0xdf, 0xad, 0xe5, 0x4a, 0x34, 0x48, 0x5, 0x3c, 0x47, 0xc5, 0xa7, 0x1d, 0x62, 0x26, 0x68, 0xae, 0xc, 0xc2, 0xa2, 0x8},
   117  				ScpValue:           xdr.StellarValue{CloseTime: 1594584547},
   118  			},
   119  			Hash: xdr.Hash{0x26, 0x93, 0x2d, 0xc4, 0xd8, 0x4b, 0x5f, 0xab, 0xe9, 0xae, 0x74, 0x4c, 0xb4, 0x3c, 0xe4, 0xc6, 0xda, 0xcc, 0xf9, 0x8c, 0x86, 0xa9, 0x91, 0xb2, 0xa1, 0x49, 0x45, 0xb1, 0xad, 0xac, 0x4d, 0x59},
   120  		},
   121  		Transaction: xdr.TransactionHistoryEntry{
   122  			LedgerSeq: 30578981,
   123  			TxSet:     hardCodedTxSet,
   124  		},
   125  		TransactionResult: xdr.TransactionHistoryResultEntry{
   126  			LedgerSeq: 30578981,
   127  			TxResultSet: xdr.TransactionResultSet{
   128  				Results: hardCodedTxProcessing,
   129  			},
   130  			Ext: xdr.TransactionHistoryResultEntryExt{},
   131  		},
   132  	}
   133  	return lcm, nil
   134  }