github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/context_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package protocol
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/ethereum/go-ethereum/core/vm"
    14  	"github.com/iotexproject/go-pkgs/hash"
    15  	"github.com/iotexproject/iotex-address/address"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestRegistryCtx(t *testing.T) {
    20  	require := require.New(t)
    21  	_, ok := GetRegistry(context.Background())
    22  	require.False(ok)
    23  	require.Panics(func() { MustGetRegistry(context.Background()) }, "Miss registry context")
    24  	reg := &Registry{}
    25  	ctx := WithRegistry(context.Background(), reg)
    26  	require.NotNil(ctx)
    27  	regFromCtx, ok := GetRegistry(ctx)
    28  	require.True(ok)
    29  	require.Equal(reg, regFromCtx)
    30  	require.Equal(reg, MustGetRegistry(ctx))
    31  }
    32  
    33  func TestWithBlockchainCtx(t *testing.T) {
    34  	require := require.New(t)
    35  	bcCtx := BlockchainCtx{}
    36  	require.NotNil(WithBlockchainCtx(context.Background(), bcCtx))
    37  }
    38  
    39  func TestGetBlockchainCtx(t *testing.T) {
    40  	require := require.New(t)
    41  	bcCtx := BlockchainCtx{
    42  		Tip: TipInfo{
    43  			Height:    1024,
    44  			Timestamp: time.Now(),
    45  		},
    46  		ChainID:      1,
    47  		EvmNetworkID: 100,
    48  	}
    49  	ctx := WithBlockchainCtx(context.Background(), bcCtx)
    50  	require.NotNil(ctx)
    51  	bcCtx1, ok := GetBlockchainCtx(ctx)
    52  	require.True(ok)
    53  	require.Equal(bcCtx, bcCtx1)
    54  }
    55  
    56  func TestMustGetBlockchainCtx(t *testing.T) {
    57  	require := require.New(t)
    58  	bcCtx := BlockchainCtx{}
    59  	ctx := WithBlockchainCtx(context.Background(), bcCtx)
    60  	require.NotNil(ctx)
    61  	// Case I: Normal
    62  	ret := MustGetBlockchainCtx(ctx)
    63  	require.NotNil(ret)
    64  	// Case II: Panic
    65  	require.Panics(func() { MustGetBlockchainCtx(context.Background()) }, "Miss blockchain context")
    66  }
    67  
    68  func TestWithBlockCtx(t *testing.T) {
    69  	require := require.New(t)
    70  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
    71  	require.NoError(err)
    72  	blkCtx := BlockCtx{
    73  		BlockHeight:    1111,
    74  		BlockTimeStamp: time.Now(),
    75  		GasLimit:       1,
    76  		Producer:       addr,
    77  	}
    78  	require.NotNil(WithBlockCtx(context.Background(), blkCtx))
    79  }
    80  
    81  func TestGetBlockCtx(t *testing.T) {
    82  	require := require.New(t)
    83  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
    84  	require.NoError(err)
    85  	blkCtx := BlockCtx{
    86  		BlockHeight:    1111,
    87  		BlockTimeStamp: time.Now(),
    88  		GasLimit:       1,
    89  		Producer:       addr,
    90  	}
    91  	ctx := WithBlockCtx(context.Background(), blkCtx)
    92  	require.NotNil(ctx)
    93  	ret, ok := GetBlockCtx(ctx)
    94  	require.True(ok)
    95  	require.Equal(uint64(1111), ret.BlockHeight)
    96  }
    97  
    98  func TestMustGetBlockCtx(t *testing.T) {
    99  	require := require.New(t)
   100  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
   101  	require.NoError(err)
   102  	blkCtx := BlockCtx{
   103  		BlockHeight:    1111,
   104  		BlockTimeStamp: time.Now(),
   105  		GasLimit:       1,
   106  		Producer:       addr,
   107  	}
   108  	ctx := WithBlockCtx(context.Background(), blkCtx)
   109  	require.NotNil(ctx)
   110  	// Case I: Normal
   111  	ret := MustGetBlockCtx(ctx)
   112  	require.Equal(uint64(1111), ret.BlockHeight)
   113  	// Case II: Panic
   114  	require.Panics(func() { MustGetBlockCtx(context.Background()) }, "Miss block context")
   115  }
   116  
   117  func TestWithActionCtx(t *testing.T) {
   118  	require := require.New(t)
   119  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
   120  	require.NoError(err)
   121  	actionCtx := ActionCtx{
   122  		Caller:       addr,
   123  		ActionHash:   hash.ZeroHash256,
   124  		GasPrice:     nil,
   125  		IntrinsicGas: 0,
   126  		Nonce:        0,
   127  	}
   128  	require.NotNil(WithActionCtx(context.Background(), actionCtx))
   129  }
   130  
   131  func TestGetActionCtx(t *testing.T) {
   132  	require := require.New(t)
   133  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
   134  	require.NoError(err)
   135  	actionCtx := ActionCtx{
   136  		Caller:       addr,
   137  		ActionHash:   hash.ZeroHash256,
   138  		GasPrice:     nil,
   139  		IntrinsicGas: 0,
   140  		Nonce:        0,
   141  	}
   142  	ctx := WithActionCtx(context.Background(), actionCtx)
   143  	require.NotNil(ctx)
   144  	ret, ok := GetActionCtx(ctx)
   145  	require.True(ok)
   146  	require.Equal(hash.ZeroHash256, ret.ActionHash)
   147  }
   148  
   149  func TestMustGetActionCtx(t *testing.T) {
   150  	require := require.New(t)
   151  	addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
   152  	require.NoError(err)
   153  	actionCtx := ActionCtx{
   154  		Caller:       addr,
   155  		ActionHash:   hash.ZeroHash256,
   156  		GasPrice:     nil,
   157  		IntrinsicGas: 0,
   158  		Nonce:        0,
   159  	}
   160  	ctx := WithActionCtx(context.Background(), actionCtx)
   161  	require.NotNil(ctx)
   162  	// Case I: Normal
   163  	ret := MustGetActionCtx(ctx)
   164  	require.Equal(hash.ZeroHash256, ret.ActionHash)
   165  	// Case II: Panic
   166  	require.Panics(func() { MustGetActionCtx(context.Background()) }, "Miss action context")
   167  }
   168  
   169  func TestWithVMConfigCtx(t *testing.T) {
   170  	require := require.New(t)
   171  	require.NotNil(WithVMConfigCtx(context.Background(), vm.Config{Debug: true}))
   172  }
   173  
   174  func TestGetVMConfigCtx(t *testing.T) {
   175  	require := require.New(t)
   176  	ctx := WithVMConfigCtx(context.Background(), vm.Config{Debug: true})
   177  	require.NotNil(ctx)
   178  	ret, ok := GetVMConfigCtx(ctx)
   179  	require.True(ok)
   180  	require.True(ret.Debug)
   181  }