github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/bind/base_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:30</date>
    10  //</624450060431659008>
    11  
    12  package bind_test
    13  
    14  import (
    15  	"context"
    16  	"math/big"
    17  	"testing"
    18  
    19  	ethereum "github.com/ethereum/go-ethereum"
    20  	"github.com/ethereum/go-ethereum/accounts/abi"
    21  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    22  	"github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  type mockCaller struct {
    26  	codeAtBlockNumber       *big.Int
    27  	callContractBlockNumber *big.Int
    28  }
    29  
    30  func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
    31  	mc.codeAtBlockNumber = blockNumber
    32  	return []byte{1, 2, 3}, nil
    33  }
    34  
    35  func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
    36  	mc.callContractBlockNumber = blockNumber
    37  	return nil, nil
    38  }
    39  
    40  func TestPassingBlockNumber(t *testing.T) {
    41  
    42  	mc := &mockCaller{}
    43  
    44  	bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{
    45  		Methods: map[string]abi.Method{
    46  			"something": {
    47  				Name:    "something",
    48  				Outputs: abi.Arguments{},
    49  			},
    50  		},
    51  	}, mc, nil, nil)
    52  	var ret string
    53  
    54  	blockNumber := big.NewInt(42)
    55  
    56  	bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something")
    57  
    58  	if mc.callContractBlockNumber != blockNumber {
    59  		t.Fatalf("CallContract() was not passed the block number")
    60  	}
    61  
    62  	if mc.codeAtBlockNumber != blockNumber {
    63  		t.Fatalf("CodeAt() was not passed the block number")
    64  	}
    65  
    66  	bc.Call(&bind.CallOpts{}, &ret, "something")
    67  
    68  	if mc.callContractBlockNumber != nil {
    69  		t.Fatalf("CallContract() was passed a block number when it should not have been")
    70  	}
    71  
    72  	if mc.codeAtBlockNumber != nil {
    73  		t.Fatalf("CodeAt() was passed a block number when it should not have been")
    74  	}
    75  }
    76