github.com/ethereum-optimism/optimism@v1.7.2/op-node/withdrawals/utils_test.go (about)

     1  package withdrawals
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/ethereum-optimism/optimism/op-bindings/bindings"
    11  	"github.com/ethereum-optimism/optimism/op-service/testutils"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/ethereum/go-ethereum/common/hexutil"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestParseMessagePassed(t *testing.T) {
    19  	tests := []struct {
    20  		name     string
    21  		file     string
    22  		expected *bindings.L2ToL1MessagePasserMessagePassed
    23  	}{
    24  		{
    25  			"withdrawal through bridge",
    26  			"bridge-withdrawal.json",
    27  			&bindings.L2ToL1MessagePasserMessagePassed{
    28  				Nonce:    new(big.Int),
    29  				Sender:   common.HexToAddress("0x4200000000000000000000000000000000000007"),
    30  				Target:   common.HexToAddress("0x6900000000000000000000000000000000000002"),
    31  				Value:    new(big.Int),
    32  				GasLimit: big.NewInt(203648),
    33  				Data: hexutil.MustDecode(
    34  					"0xd764ad0b00010000000000000000000000000000000000000000000000000000" +
    35  						"00000000000000000000000000000000420000000000000000000000000000000000" +
    36  						"00100000000000000000000000006900000000000000000000000000000000000003" +
    37  						"00000000000000000000000000000000000000000000000000000000000000000000" +
    38  						"00000000000000000000000000000000000000000000000000000000000000000000" +
    39  						"000000000000000000000000000000000000000000000000000000c0000000000000" +
    40  						"00000000000000000000000000000000000000000000000000e40166a07a00000000" +
    41  						"000000000000000089d51be807d98fc974a0f41b2e67a8228d7846ef000000000000" +
    42  						"0000000000007c6b91d9be155a6db01f749217d76ff02a7227f20000000000000000" +
    43  						"00000000c20c5ec92fda6e611a08485123cdc0d5b84bd3a200000000000000000000" +
    44  						"0000c20c5ec92fda6e611a08485123cdc0d5b84bd3a2000000000000000000000000" +
    45  						"00000000000000000000000000000000000001f40000000000000000000000000000" +
    46  						"0000000000000000000000000000000000c000000000000000000000000000000000" +
    47  						"00000000000000000000000000000000000000000000000000000000000000000000" +
    48  						"00000000000000000000",
    49  				),
    50  				WithdrawalHash: common.HexToHash("0x0d827f8148288e3a2466018f71b968ece4ea9f9e2a81c30da9bd46cce2868285"),
    51  				Raw: types.Log{
    52  					Address: common.HexToAddress("0x4200000000000000000000000000000000000016"),
    53  					Topics: []common.Hash{
    54  						common.HexToHash("0x02a52367d10742d8032712c1bb8e0144ff1ec5ffda1ed7d70bb05a2744955054"),
    55  						common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
    56  						common.HexToHash("0x0000000000000000000000004200000000000000000000000000000000000007"),
    57  						common.HexToHash("0x0000000000000000000000006900000000000000000000000000000000000002"),
    58  					},
    59  					Data: hexutil.MustDecode(
    60  						"0x00000000000000000000000000000000000000000000000000000000000000" +
    61  							"000000000000000000000000000000000000000000000000000000000000031b80" +
    62  							"00000000000000000000000000000000000000000000000000000000000000800d" +
    63  							"827f8148288e3a2466018f71b968ece4ea9f9e2a81c30da9bd46cce28682850000" +
    64  							"0000000000000000000000000000000000000000000000000000000001e4d764ad" +
    65  							"0b0001000000000000000000000000000000000000000000000000000000000000" +
    66  							"000000000000000000000000420000000000000000000000000000000000001000" +
    67  							"000000000000000000000069000000000000000000000000000000000000030000" +
    68  							"000000000000000000000000000000000000000000000000000000000000000000" +
    69  							"000000000000000000000000000000000000000000000000000000000000000000" +
    70  							"000000000000000000000000000000000000000000000000000000c00000000000" +
    71  							"0000000000000000000000000000000000000000000000000000e40166a07a0000" +
    72  							"0000000000000000000089d51be807d98fc974a0f41b2e67a8228d7846ef000000" +
    73  							"0000000000000000007c6b91d9be155a6db01f749217d76ff02a7227f200000000" +
    74  							"0000000000000000c20c5ec92fda6e611a08485123cdc0d5b84bd3a20000000000" +
    75  							"00000000000000c20c5ec92fda6e611a08485123cdc0d5b84bd3a2000000000000" +
    76  							"00000000000000000000000000000000000000000000000001f400000000000000" +
    77  							"000000000000000000000000000000000000000000000000c00000000000000000" +
    78  							"000000000000000000000000000000000000000000000000000000000000000000" +
    79  							"000000000000000000000000000000000000000000000000000000000000000000" +
    80  							"0000000000000000000000000000",
    81  					),
    82  					BlockNumber: 0x36,
    83  					TxHash:      common.HexToHash("0x9346381068b59d2098495baa72ed2f773c1e09458610a7a208984859dff73add"),
    84  					TxIndex:     1,
    85  					BlockHash:   common.HexToHash("0xfdd4ad8a984b45687aca0463db491cbd0e85273d970019a3f8bf618b614938df"),
    86  					Index:       2,
    87  					Removed:     false,
    88  				},
    89  			},
    90  		},
    91  	}
    92  	for _, test := range tests {
    93  		t.Run(test.name, func(t *testing.T) {
    94  			f, err := os.Open(path.Join("testdata", test.file))
    95  			require.NoError(t, err)
    96  			dec := json.NewDecoder(f)
    97  			receipt := new(types.Receipt)
    98  			require.NoError(t, dec.Decode(receipt))
    99  			parsed, err := ParseMessagePassed(receipt)
   100  			require.NoError(t, err)
   101  
   102  			// Have to do this weird thing to compare zero bigints.
   103  			// When they're deserialized from JSON, the internal byte
   104  			// array is an empty array whereas it is nil in the expectation.
   105  			parsedNonce := parsed.Nonce
   106  			parsedValue := parsed.Value
   107  			expNonce := test.expected.Nonce
   108  			expValue := test.expected.Value
   109  			testutils.RequireBigEqual(t, expNonce, parsedNonce)
   110  			testutils.RequireBigEqual(t, expValue, parsedValue)
   111  			parsed.Nonce = nil
   112  			parsed.Value = nil
   113  			test.expected.Nonce = nil
   114  			test.expected.Value = nil
   115  
   116  			require.EqualValues(t, test.expected, parsed)
   117  		})
   118  	}
   119  }