github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/contexts/send_context_test.go (about)

     1  package contexts
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hyperledger/burrow/acm/acmstate"
     7  	"github.com/hyperledger/burrow/logging"
     8  	"github.com/hyperledger/burrow/txs/payload"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestSendContext(t *testing.T) {
    13  	accountState := acmstate.NewMemoryState()
    14  
    15  	originPrivKey := newPrivKey(t)
    16  	originAccount := newAccountFromPrivKey(originPrivKey)
    17  
    18  	targetPrivKey := newPrivKey(t)
    19  	targetAccount := newAccountFromPrivKey(targetPrivKey)
    20  
    21  	ctx := &SendContext{
    22  		State:  accountState,
    23  		Logger: logging.NewNoopLogger(),
    24  	}
    25  
    26  	callTx := &payload.CallTx{}
    27  	err := ctx.Execute(execFromTx(callTx), callTx)
    28  	require.Error(t, err, "should not continue with incorrect payload")
    29  
    30  	accountState.Accounts[originAccount.Address] = originAccount
    31  	accountState.Accounts[targetAccount.Address] = targetAccount
    32  
    33  	tests := []struct {
    34  		tx  *payload.SendTx
    35  		exp func(t *testing.T, err error)
    36  	}{
    37  		{
    38  			tx: &payload.SendTx{
    39  				Inputs: []*payload.TxInput{
    40  					&payload.TxInput{
    41  						Address: originAccount.Address,
    42  					},
    43  				},
    44  			},
    45  			exp: errCallback(func(t *testing.T, err error) {
    46  				require.Error(t, err, "should not allow zero payment")
    47  			}),
    48  		},
    49  		{
    50  			tx: &payload.SendTx{
    51  				Inputs: []*payload.TxInput{
    52  					&payload.TxInput{
    53  						Address: originAccount.Address,
    54  						Amount:  100,
    55  					},
    56  				},
    57  			},
    58  			exp: errCallback(func(t *testing.T, err error) {
    59  				require.Error(t, err, "should not allow overpayment (i.e. inputs > outputs)")
    60  			}),
    61  		},
    62  		{
    63  			tx: &payload.SendTx{
    64  				Inputs: []*payload.TxInput{
    65  					&payload.TxInput{
    66  						Address: originAccount.Address,
    67  						Amount:  100,
    68  					},
    69  				},
    70  				Outputs: []*payload.TxOutput{
    71  					&payload.TxOutput{
    72  						Address: originAccount.Address,
    73  						Amount:  100,
    74  					},
    75  				},
    76  			},
    77  			exp: errCallback(func(t *testing.T, err error) {
    78  				require.Error(t, err, "should not allow self payment")
    79  			}),
    80  		},
    81  		{
    82  			tx: &payload.SendTx{
    83  				Inputs: []*payload.TxInput{
    84  					&payload.TxInput{
    85  						Address: originAccount.Address,
    86  						Amount:  100,
    87  					},
    88  				},
    89  				Outputs: []*payload.TxOutput{
    90  					&payload.TxOutput{
    91  						Address: targetAccount.Address,
    92  						Amount:  100,
    93  					},
    94  				},
    95  			},
    96  			exp: errCallback(func(t *testing.T, err error) {
    97  				require.NoError(t, err, "should allow payment")
    98  			}),
    99  		},
   100  		{
   101  			tx: &payload.SendTx{
   102  				Inputs: []*payload.TxInput{
   103  					&payload.TxInput{
   104  						Address: originAccount.Address,
   105  						Amount:  10000,
   106  					},
   107  				},
   108  				Outputs: []*payload.TxOutput{
   109  					&payload.TxOutput{
   110  						Address: targetAccount.Address,
   111  						Amount:  10000,
   112  					},
   113  				},
   114  			},
   115  			exp: errCallback(func(t *testing.T, err error) {
   116  				require.Error(t, err, "should not allow send with insufficient funds")
   117  			}),
   118  		},
   119  	}
   120  
   121  	for _, tt := range tests {
   122  		err = ctx.Execute(execFromTx(tt.tx), tt.tx)
   123  		tt.exp(t, err)
   124  	}
   125  }