github.com/annchain/OG@v0.0.9/vm/types/log_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"github.com/annchain/OG/arefactor/og/types"
    23  	"github.com/annchain/OG/common"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/annchain/OG/common/hexutil"
    28  	"github.com/davecgh/go-spew/spew"
    29  )
    30  
    31  var unmarshalLogTests = map[string]struct {
    32  	input     string
    33  	want      *Log
    34  	wantError error
    35  }{
    36  	"ok": {
    37  		input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
    38  		want: &Log{
    39  			Address:    common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
    40  			BlockHash:  types.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
    41  			SequenceID: 2019236,
    42  			Data:       hexutil.MustDecode("0x000000000000000000000000000000000000000000000001a055690d9db80000"),
    43  			Index:      2,
    44  			TxIndex:    3,
    45  			TxHash:     types.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
    46  			Topics: types.Hashes{
    47  				types.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
    48  				types.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"),
    49  			},
    50  		},
    51  	},
    52  	"empty data": {
    53  		input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
    54  		want: &Log{
    55  			Address:    common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
    56  			BlockHash:  types.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
    57  			SequenceID: 2019236,
    58  			Data:       []byte{},
    59  			Index:      2,
    60  			TxIndex:    3,
    61  			TxHash:     types.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
    62  			Topics: types.Hashes{
    63  				types.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
    64  				types.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"),
    65  			},
    66  		},
    67  	},
    68  	"missing block fields (pending logs)": {
    69  		input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","data":"0x","logIndex":"0x0","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
    70  		want: &Log{
    71  			Address:    common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
    72  			BlockHash:  types.Hash{},
    73  			SequenceID: 0,
    74  			Data:       []byte{},
    75  			Index:      0,
    76  			TxIndex:    3,
    77  			TxHash:     types.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
    78  			Topics: types.Hashes{
    79  				types.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
    80  			},
    81  		},
    82  	},
    83  	"Removed: true": {
    84  		input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3","removed":true}`,
    85  		want: &Log{
    86  			Address:    common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
    87  			BlockHash:  types.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
    88  			SequenceID: 2019236,
    89  			Data:       []byte{},
    90  			Index:      2,
    91  			TxIndex:    3,
    92  			TxHash:     types.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
    93  			Topics: types.Hashes{
    94  				types.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
    95  			},
    96  			Removed: true,
    97  		},
    98  	},
    99  	"missing data": {
   100  		input:     `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
   101  		wantError: fmt.Errorf("missing required field 'data' for Log"),
   102  	},
   103  }
   104  
   105  func TestUnmarshalLog(t *testing.T) {
   106  	dumper := spew.ConfigState{DisableMethods: true, Indent: "    "}
   107  	for name, test := range unmarshalLogTests {
   108  		var log *Log
   109  		err := json.Unmarshal([]byte(test.input), &log)
   110  		checkError(t, name, err, test.wantError)
   111  		if test.wantError == nil && err == nil {
   112  			if !reflect.DeepEqual(log, test.want) {
   113  				t.Errorf("test %q:\nGOT %sWANT %s", name, dumper.Sdump(log), dumper.Sdump(test.want))
   114  			}
   115  		}
   116  	}
   117  }
   118  
   119  func checkError(t *testing.T, testname string, got, want error) bool {
   120  	if got == nil {
   121  		if want != nil {
   122  			t.Errorf("test %q: got no error, want %q", testname, want)
   123  			return false
   124  		}
   125  		return true
   126  	}
   127  	if want == nil {
   128  		t.Errorf("test %q: unexpected error %q", testname, got)
   129  	} else if got.Error() != want.Error() {
   130  		t.Errorf("test %q: got error %q, want %q", testname, got, want)
   131  	}
   132  	return false
   133  }