1717package mainnet_tests
1818
1919import (
20- "bufio"
2120 "context"
2221 "errors"
2322 "fmt"
@@ -37,33 +36,38 @@ const (
3736const (
3837 TEST_RAW_URL = "TEST_RAW_URL"
3938 TEST_BLOCK_NUMBER = "TEST_BLOCK_NUMBER"
39+ TEST_LOCAL_CACHE = "TEST_LOCAL_CACHE"
4040)
4141
4242// TestConfig holds configuration params for mainnet tests
4343type TestConfig struct {
4444 RawURL string
4545 BlockNumber * big.Int
46+ LocalCache bool
4647}
4748
4849// DefaultTestConfig is the default TestConfig
4950var DefaultTestConfig = TestConfig {
5051 RawURL : "http://127.0.0.1:8545" ,
51- BlockNumber : big .NewInt (12914665 ),
52+ BlockNumber : big .NewInt (12914664 ),
53+ LocalCache : true ,
5254}
5355
5456// TestBlocksAndReceiptsFromEnv retrieves the block and receipts using env variables to override default config
5557func TestBlocksAndReceiptsFromEnv () (* types.Block , types.Receipts , error ) {
5658 conf := DefaultTestConfig
5759 rawURL := os .Getenv (TEST_RAW_URL )
5860 if rawURL == "" {
59- fmt .Println ("Warning: no raw url configured for statediffing mainnet tests" )
61+ fmt .Printf ("Warning: no raw url configured for statediffing mainnet tests, will look for local file and" +
62+ "then try default endpoint (%s)\r \n " , DefaultTestConfig .RawURL )
6063 } else {
6164 conf .RawURL = rawURL
6265 }
6366 blockNumberStr := os .Getenv (TEST_BLOCK_NUMBER )
6467 blockNumber , ok := new (big.Int ).SetString (blockNumberStr , 10 )
6568 if ! ok {
66- fmt .Println ("Warning: no blockNumber configured for statediffing mainnet tests" )
69+ fmt .Printf ("Warning: no blockNumber configured for statediffing mainnet tests, using default (%d)\r \n " ,
70+ DefaultTestConfig .BlockNumber )
6771 } else {
6872 conf .BlockNumber = blockNumber
6973 }
@@ -77,10 +81,12 @@ func TestBlocksAndReceipts(conf TestConfig) (*types.Block, types.Receipts, error
7781 var err error
7882 var block * types.Block
7983 var receipts types.Receipts
80- blockFilePath := fmt .Sprintf ("%s. %s.rlp" , defaultBlockFilePath , conf .BlockNumber .String ())
84+ blockFilePath := fmt .Sprintf ("%s_ %s.rlp" , defaultBlockFilePath , conf .BlockNumber .String ())
8185 if _ , err = os .Stat (blockFilePath ); ! errors .Is (err , os .ErrNotExist ) {
86+ fmt .Printf ("local file (%s) found for block %s\n " , blockFilePath , conf .BlockNumber .String ())
8287 block , err = LoadBlockRLP (blockFilePath )
8388 if err != nil {
89+ fmt .Printf ("loading local file (%s) failed (%s), dialing remote client at %s\n " , blockFilePath , err .Error (), conf .RawURL )
8490 cli , err = ethclient .Dial (conf .RawURL )
8591 if err != nil {
8692 return nil , nil , err
@@ -89,8 +95,14 @@ func TestBlocksAndReceipts(conf TestConfig) (*types.Block, types.Receipts, error
8995 if err != nil {
9096 return nil , nil , err
9197 }
98+ if conf .LocalCache {
99+ if err := WriteBlockRLP (blockFilePath , block ); err != nil {
100+ return nil , nil , err
101+ }
102+ }
92103 }
93104 } else {
105+ fmt .Printf ("no local file found for block %s, dialing remote client at %s\n " , conf .BlockNumber .String (), conf .RawURL )
94106 cli , err = ethclient .Dial (conf .RawURL )
95107 if err != nil {
96108 return nil , nil , err
@@ -99,11 +111,18 @@ func TestBlocksAndReceipts(conf TestConfig) (*types.Block, types.Receipts, error
99111 if err != nil {
100112 return nil , nil , err
101113 }
114+ if conf .LocalCache {
115+ if err := WriteBlockRLP (blockFilePath , block ); err != nil {
116+ return nil , nil , err
117+ }
118+ }
102119 }
103- receiptsFilePath := fmt .Sprintf ("%s. %s.enc " , defaultReceiptsFilePath , conf .BlockNumber .String ())
120+ receiptsFilePath := fmt .Sprintf ("%s_ %s.rlp " , defaultReceiptsFilePath , conf .BlockNumber .String ())
104121 if _ , err = os .Stat (receiptsFilePath ); ! errors .Is (err , os .ErrNotExist ) {
122+ fmt .Printf ("local file (%s) found for block %s receipts\n " , receiptsFilePath , conf .BlockNumber .String ())
105123 receipts , err = LoadReceiptsEncoding (receiptsFilePath , len (block .Transactions ()))
106124 if err != nil {
125+ fmt .Printf ("loading local file (%s) failed (%s), dialing remote client at %s\n " , receiptsFilePath , err .Error (), conf .RawURL )
107126 if cli == nil {
108127 cli , err = ethclient .Dial (conf .RawURL )
109128 if err != nil {
@@ -114,8 +133,14 @@ func TestBlocksAndReceipts(conf TestConfig) (*types.Block, types.Receipts, error
114133 if err != nil {
115134 return nil , nil , err
116135 }
136+ if conf .LocalCache {
137+ if err := WriteReceiptsEncoding (receiptsFilePath , block .Number (), receipts ); err != nil {
138+ return nil , nil , err
139+ }
140+ }
117141 }
118142 } else {
143+ fmt .Printf ("no local file found for block %s receipts, dialing remote client at %s\n " , conf .BlockNumber .String (), conf .RawURL )
119144 if cli == nil {
120145 cli , err = ethclient .Dial (conf .RawURL )
121146 if err != nil {
@@ -126,6 +151,11 @@ func TestBlocksAndReceipts(conf TestConfig) (*types.Block, types.Receipts, error
126151 if err != nil {
127152 return nil , nil , err
128153 }
154+ if conf .LocalCache {
155+ if err := WriteReceiptsEncoding (receiptsFilePath , block .Number (), receipts ); err != nil {
156+ return nil , nil , err
157+ }
158+ }
129159 }
130160 return block , receipts , nil
131161}
@@ -160,6 +190,7 @@ func WriteBlockRLP(filePath string, block *types.Block) error {
160190 if err != nil {
161191 return fmt .Errorf ("unable to create file (%s), err: %v" , filePath , err )
162192 }
193+ fmt .Printf ("writing block rlp to file at %s\r \n " , filePath )
163194 if err := block .EncodeRLP (file ); err != nil {
164195 return err
165196 }
@@ -178,28 +209,18 @@ func LoadBlockRLP(filePath string) (*types.Block, error) {
178209
179210// LoadReceiptsEncoding loads receipts from the encoding at filePath
180211func LoadReceiptsEncoding (filePath string , cap int ) (types.Receipts , error ) {
181- file , err := os .Open (filePath )
212+ rctsBytes , err := os .ReadFile (filePath )
182213 if err != nil {
183214 return nil , err
184215 }
185- defer file .Close ()
186- scanner := bufio .NewScanner (file )
187- receipts := make (types.Receipts , 0 , cap )
188- for scanner .Scan () {
189- rctBinary := scanner .Bytes ()
190- rct := new (types.Receipt )
191- if err := rct .UnmarshalBinary (rctBinary ); err != nil {
192- return nil , err
193- }
194- receipts = append (receipts , rct )
195- }
196- return receipts , nil
216+ receipts := new (types.Receipts )
217+ return * receipts , rlp .DecodeBytes (rctsBytes , receipts )
197218}
198219
199220// WriteReceiptsEncoding writes out the consensus encoding of the receipts to the provided io.WriteCloser
200221func WriteReceiptsEncoding (filePath string , blockNumber * big.Int , receipts types.Receipts ) error {
201222 if filePath == "" {
202- filePath = fmt .Sprintf ("%s_%s.enc " , defaultReceiptsFilePath , blockNumber .String ())
223+ filePath = fmt .Sprintf ("%s_%s.rlp " , defaultReceiptsFilePath , blockNumber .String ())
203224 }
204225 if _ , err := os .Stat (filePath ); ! errors .Is (err , os .ErrNotExist ) {
205226 return fmt .Errorf ("cannot create file, file (%s) already exists" , filePath )
@@ -208,17 +229,7 @@ func WriteReceiptsEncoding(filePath string, blockNumber *big.Int, receipts types
208229 if err != nil {
209230 return fmt .Errorf ("unable to create file (%s), err: %v" , filePath , err )
210231 }
211- for _ , rct := range receipts {
212- rctEncoding , err := rct .MarshalBinary ()
213- if err != nil {
214- return err
215- }
216- if _ , err := file .Write (rctEncoding ); err != nil {
217- return err
218- }
219- if _ , err := file .Write ([]byte ("\n " )); err != nil {
220- return err
221- }
222- }
223- return file .Close ()
232+ defer file .Close ()
233+ fmt .Printf ("writing receipts rlp to file at %s\r \n " , filePath )
234+ return rlp .Encode (file , receipts )
224235}
0 commit comments