github.com/igggame/nebulas-go@v2.1.0+incompatible/core/transaction_payload_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package core
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/nebulasio/go-nebulas/util"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestLoadBinaryPayload(t *testing.T) {
    29  
    30  	tests := []struct {
    31  		name      string
    32  		bytes     []byte
    33  		want      *BinaryPayload
    34  		wantEqual bool
    35  	}{
    36  		{
    37  			name:      "none",
    38  			bytes:     nil,
    39  			want:      NewBinaryPayload(nil),
    40  			wantEqual: true,
    41  		},
    42  
    43  		{
    44  			name:      "normal",
    45  			bytes:     []byte("data"),
    46  			want:      NewBinaryPayload([]byte("data")),
    47  			wantEqual: true,
    48  		},
    49  
    50  		{
    51  			name:      "not equal",
    52  			bytes:     []byte("data"),
    53  			want:      NewBinaryPayload([]byte("data1")),
    54  			wantEqual: false,
    55  		},
    56  	}
    57  
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			got, err := LoadBinaryPayload(tt.bytes)
    61  			assert.Nil(t, err)
    62  			if tt.wantEqual {
    63  				assert.Equal(t, tt.want, got)
    64  			} else {
    65  				assert.NotEqual(t, tt.want, got)
    66  			}
    67  		})
    68  	}
    69  
    70  }
    71  
    72  func TestLoadCallPayload(t *testing.T) {
    73  	want3, _ := NewCallPayload("func", "[0]")
    74  	want4, _ := NewCallPayload("func", "[0]")
    75  	tests := []struct {
    76  		name      string
    77  		bytes     []byte
    78  		parse     bool
    79  		want      *CallPayload
    80  		wantEqual bool
    81  		wantErr   error
    82  	}{
    83  		{
    84  			name:      "none",
    85  			bytes:     nil,
    86  			parse:     false,
    87  			want:      nil,
    88  			wantEqual: false,
    89  			wantErr:   ErrInvalidArgument,
    90  		},
    91  
    92  		{
    93  			name:      "parse faild",
    94  			bytes:     []byte("data"),
    95  			parse:     false,
    96  			want:      nil,
    97  			wantEqual: false,
    98  			wantErr:   ErrInvalidArgument,
    99  		},
   100  
   101  		{
   102  			name:      "no func",
   103  			bytes:     []byte(`{"args": "[0]"}`),
   104  			parse:     false,
   105  			want:      nil,
   106  			wantEqual: true,
   107  			wantErr:   ErrInvalidCallFunction,
   108  		},
   109  
   110  		{
   111  			name:      "not args",
   112  			bytes:     []byte(`{"function":"func"}`),
   113  			parse:     false,
   114  			want:      nil,
   115  			wantEqual: true,
   116  			wantErr:   nil,
   117  		},
   118  
   119  		{
   120  			name:      "normal",
   121  			bytes:     []byte(`{"function":"func","args":"[0]"}`),
   122  			parse:     true,
   123  			want:      want3,
   124  			wantEqual: true,
   125  			wantErr:   nil,
   126  		},
   127  
   128  		{
   129  			name:      "not equal",
   130  			bytes:     []byte(`{"function":"func", "args":"[1]"}`),
   131  			parse:     true,
   132  			want:      want4,
   133  			wantEqual: false,
   134  			wantErr:   nil,
   135  		},
   136  	}
   137  
   138  	for _, tt := range tests {
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			t.Log(tt.name)
   141  			got, err := LoadCallPayload(tt.bytes)
   142  			if tt.parse {
   143  				assert.Nil(t, err)
   144  				if tt.wantEqual {
   145  					assert.Equal(t, tt.want, got)
   146  				} else {
   147  					assert.NotEqual(t, tt.want, got)
   148  				}
   149  			} else {
   150  				assert.Equal(t, err, tt.wantErr)
   151  			}
   152  		})
   153  	}
   154  
   155  }
   156  
   157  func TestLoadDeployPayload(t *testing.T) {
   158  
   159  	deployTx := mockDeployTransaction(0, 0)
   160  	deployPayload, _ := deployTx.LoadPayload()
   161  	deployData, _ := deployPayload.ToBytes()
   162  
   163  	tests := []struct {
   164  		name      string
   165  		bytes     []byte
   166  		parse     bool
   167  		want      TxPayload
   168  		wantEqual bool
   169  	}{
   170  		{
   171  			name:      "none",
   172  			bytes:     nil,
   173  			parse:     false,
   174  			want:      nil,
   175  			wantEqual: false,
   176  		},
   177  
   178  		{
   179  			name:      "parse faild",
   180  			bytes:     []byte("data"),
   181  			parse:     false,
   182  			want:      nil,
   183  			wantEqual: false,
   184  		},
   185  
   186  		{
   187  			name:      "deploy",
   188  			bytes:     deployData,
   189  			parse:     true,
   190  			want:      deployPayload,
   191  			wantEqual: true,
   192  		},
   193  	}
   194  
   195  	for _, tt := range tests {
   196  		t.Run(tt.name, func(t *testing.T) {
   197  			got, err := LoadDeployPayload(tt.bytes)
   198  			if tt.parse {
   199  				assert.Nil(t, err)
   200  				if tt.wantEqual {
   201  					assert.Equal(t, tt.want, got)
   202  				} else {
   203  					assert.NotEqual(t, tt.want, got)
   204  				}
   205  			} else {
   206  				assert.NotNil(t, err)
   207  			}
   208  		})
   209  	}
   210  }
   211  
   212  func TestPayload_Execute(t *testing.T) {
   213  	type testPayload struct {
   214  		name     string
   215  		payload  TxPayload
   216  		tx       *Transaction
   217  		block    *Block
   218  		want     *util.Uint128
   219  		wantErr  error
   220  		giveback bool
   221  		limit    *util.Uint128
   222  	}
   223  
   224  	neb := testNeb(t)
   225  	bc := neb.chain
   226  	block := bc.tailBlock
   227  	block.Begin()
   228  
   229  	tests := []testPayload{
   230  		{
   231  			name:     "normal none",
   232  			payload:  NewBinaryPayload(nil),
   233  			tx:       mockNormalTransaction(bc.chainID, 0),
   234  			block:    block,
   235  			want:     util.NewUint128(),
   236  			wantErr:  nil,
   237  			giveback: false,
   238  			limit:    util.Uint128Zero(),
   239  		},
   240  		{
   241  			name:     "normal",
   242  			payload:  NewBinaryPayload([]byte("data")),
   243  			tx:       mockNormalTransaction(bc.chainID, 0),
   244  			block:    block,
   245  			want:     util.NewUint128(),
   246  			wantErr:  nil,
   247  			giveback: false,
   248  			limit:    util.Uint128Zero(),
   249  		},
   250  	}
   251  
   252  	deployTx := mockDeployTransaction(bc.chainID, 0)
   253  	deployPayload, _ := deployTx.LoadPayload()
   254  	want, _ := util.NewUint128FromInt(100)
   255  	limit, _ := util.NewUint128FromInt(100)
   256  	tests = append(tests, testPayload{
   257  		name:     "deploy",
   258  		payload:  deployPayload,
   259  		tx:       deployTx,
   260  		block:    block,
   261  		want:     want,
   262  		wantErr:  nil,
   263  		giveback: false,
   264  		limit:    limit,
   265  	})
   266  
   267  	callTx := mockCallTransaction(bc.chainID, 1, "totalSupply", "")
   268  	callTx.to, _ = deployTx.GenerateContractAddress()
   269  	callPayload, _ := callTx.LoadPayload()
   270  	limit, _ = util.NewUint128FromInt(1)
   271  	tests = append(tests, testPayload{
   272  		name:     "call",
   273  		payload:  callPayload,
   274  		tx:       callTx,
   275  		block:    block,
   276  		want:     util.NewUint128(),
   277  		wantErr:  ErrContractCheckFailed,
   278  		giveback: false,
   279  		limit:    limit,
   280  	})
   281  
   282  	// TODO: @robin need more unittests.
   283  
   284  	for _, tt := range tests {
   285  		t.Run(tt.name, func(t *testing.T) {
   286  			var err error
   287  			tt.tx.hash, err = tt.tx.calHash()
   288  			assert.Nil(t, err)
   289  			got, _, err := tt.payload.Execute(tt.limit, tt.tx, block, block.WorldState())
   290  			assert.Equal(t, tt.wantErr, err)
   291  			assert.Equal(t, tt.want, got)
   292  			giveback, err := AcceptTransaction(tt.tx, block.WorldState())
   293  			assert.Nil(t, err)
   294  			assert.Equal(t, giveback, tt.giveback)
   295  		})
   296  	}
   297  
   298  	block.RollBack()
   299  }