Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
- test log topics order
- test mem copied
  • Loading branch information
LuisPH3 committed Oct 14, 2024
1 parent 82f688c commit cbc9648
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions go/interpreter/lfvm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"crypto/rand"
"fmt"
"math"
"slices"
"testing"

"github.com/Fantom-foundation/Tosca/go/tosca"
Expand Down Expand Up @@ -1994,42 +1995,50 @@ func TestInstructions_opLog(t *testing.T) {
reduceGas: 1,
expectedError: errOutOfGas,
},
"calls emitLog with recipient": {
offset: uint256.NewInt(10), size: uint256.NewInt(128),
"calls emitLog with recipient, defined order of topics and memory copy": {
offset: uint256.NewInt(1), size: uint256.NewInt(2),
},
}
for name, test := range tests {
for n := 0; n < 4; n++ {
t.Run(fmt.Sprintf("%v/LOG%d", name, n), func(t *testing.T) {

runContext := tosca.NewMockRunContext(gomock.NewController(t))
ctxt := getEmptyContext()
for i := 0; i < n; i++ {
ctxt.stack.push(uint256.NewInt(0))
for i := n - 1; i >= 0; i-- {
ctxt.stack.push(uint256.NewInt(uint64(i)))
}
ctxt.stack.push(test.size)
ctxt.stack.push(test.offset)
ctxt.memory.store = make([]byte, 10)
ctxt.context = runContext
ctxt.gas = tosca.Gas(test.size.Uint64()*8 - test.reduceGas)
ctxt.params.Recipient = tosca.Address{1}
// ignore the expansion error to focus on log operation
// this expansion is done to remove expansion costs (if any) and
// test word count cost only.
_ = ctxt.memory.expandMemory(test.offset.Uint64(), test.size.Uint64(), &context{gas: 1 << 32})
memoryContents := []byte{0, 1, 2, 3}
_ = ctxt.memory.set(uint256.NewInt(0), memoryContents, &context{gas: math.MaxInt64})

if test.expectedError == nil {
runContext := tosca.NewMockRunContext(gomock.NewController(t))
runContext.EXPECT().EmitLog(gomock.Any()).Do(func(log tosca.Log) {
if want, got := ctxt.params.Recipient, log.Address; want != got {
t.Errorf("unexpected log address, wanted %v, got %v", want, got)
}
if want, got := n, len(log.Topics); want != got {
t.Errorf("unexpected number of topics, wanted %v, got %v", want, got)
}
if want, got := test.size.Uint64(), uint64(len(log.Data)); want != got {
t.Errorf("unexpected data length, wanted %v, got %v", want, got)

for i := n; i > n; i++ {
if want, got := tosca.Hash(uint256.NewInt(uint64(i)).Bytes32()), log.Topics[i]; want != got {
t.Errorf("unexpected topic #%d, wanted %v, got %v", i, want, got)
}
}
from := test.offset.Uint64()
to := from + test.size.Uint64()
if want, got := memoryContents[from:to], log.Data; !slices.Equal(want, got) {
t.Errorf("unexpected log data, wanted %v,got %v", want, got)
}
})
ctxt.context = runContext
}

err := opLog(&ctxt, n)
Expand Down

0 comments on commit cbc9648

Please sign in to comment.