github.com/datachainlab/burrow@v0.25.0/execution/evm/log_event_test.go (about)

     1  // Copyright 2017 Monax Industries Limited
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package evm
    16  
    17  import (
    18  	"testing"
    19  
    20  	"bytes"
    21  	"reflect"
    22  
    23  	"github.com/hyperledger/burrow/acm"
    24  	. "github.com/hyperledger/burrow/binary"
    25  	"github.com/hyperledger/burrow/crypto"
    26  	. "github.com/hyperledger/burrow/execution/evm/asm"
    27  	"github.com/hyperledger/burrow/execution/exec"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  var expectedData = []byte{0x10}
    32  var expectedHeight uint64 = 0
    33  var expectedTopics = []Word256{
    34  	Int64ToWord256(1),
    35  	Int64ToWord256(2),
    36  	Int64ToWord256(3),
    37  	Int64ToWord256(4)}
    38  
    39  // Tests logs and events.
    40  func TestLog4(t *testing.T) {
    41  	st := newAppState()
    42  	cache := NewState(st, blockHashGetter)
    43  	// Create accounts
    44  	account1 := &acm.Account{
    45  		Address: crypto.Address{1, 3, 5, 7, 9},
    46  	}
    47  	account2 := &acm.Account{
    48  		Address: crypto.Address{2, 4, 6, 8, 10},
    49  	}
    50  	st.accounts[account1.Address] = account1
    51  	st.accounts[account2.Address] = account2
    52  
    53  	ourVm := NewVM(newParams(), crypto.ZeroAddress, nil, logger)
    54  
    55  	txe := new(exec.TxExecution)
    56  
    57  	var gas uint64 = 100000
    58  
    59  	mstore8 := byte(MSTORE8)
    60  	push1 := byte(PUSH1)
    61  	log4 := byte(LOG4)
    62  	stop := byte(STOP)
    63  
    64  	code := []byte{
    65  		push1, 16, // data value
    66  		push1, 0, // memory slot
    67  		mstore8,
    68  		push1, 4, // topic 4
    69  		push1, 3, // topic 3
    70  		push1, 2, // topic 2
    71  		push1, 1, // topic 1
    72  		push1, 1, // size of data
    73  		push1, 0, // data starts at this offset
    74  		log4,
    75  		stop,
    76  	}
    77  
    78  	_, err := ourVm.Call(cache, txe, account1.Address, account2.Address, code, []byte{}, 0, &gas)
    79  	require.NoError(t, err)
    80  
    81  	for _, ev := range txe.Events {
    82  		if ev.Log != nil {
    83  			if !reflect.DeepEqual(ev.Log.Topics, expectedTopics) {
    84  				t.Errorf("Event topics are wrong. Got: %v. Expected: %v", ev.Log.Topics, expectedTopics)
    85  			}
    86  			if !bytes.Equal(ev.Log.Data, expectedData) {
    87  				t.Errorf("Event data is wrong. Got: %s. Expected: %s", ev.Log.Data, expectedData)
    88  			}
    89  			if ev.Header.Height != expectedHeight {
    90  				t.Errorf("Event block height is wrong. Got: %d. Expected: %d", ev.Header.Height, expectedHeight)
    91  			}
    92  			return
    93  		}
    94  	}
    95  	t.Fatalf("Did not see LogEvent")
    96  }