Skip to content

Commit

Permalink
Increase coverage for tracers (vechain#670)
Browse files Browse the repository at this point in the history
* Increase coverage for tracers

* Replace mockHash with BytesToHash

* small tweak

---------

Co-authored-by: tony <[email protected]>
  • Loading branch information
MakisChristou and libotony authored Mar 15, 2024
1 parent 85d1b30 commit 1ba1783
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tracers/logger/logger_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package logger

import (
"bytes"
"encoding/json"
"io"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/vechain/thor/v2/vm"
)

// Implement the StateDB interface partially for testing
type mockStateDB struct {
vm.StateDB
refund uint64
}

func (m *mockStateDB) GetRefund() uint64 {
return m.refund
}

func TestJSONLogger(t *testing.T) {
var buf bytes.Buffer

logger := NewJSONLogger(nil, &buf)
env := &vm.EVM{}
stateDB := &mockStateDB{refund: 100}
env.StateDB = stateDB

logger.CaptureStart(env, common.Address{}, common.Address{}, false, nil, 0, big.NewInt(0))

memory := vm.NewMemory()
stack := &vm.Stack{}
contract := vm.NewContract(vm.AccountRef(common.Address{}), vm.AccountRef(common.Address{}), big.NewInt(0), 0)

logger.CaptureState(0, vm.ADD, 10, 2, memory, stack, contract, nil, 1, nil)
logger.CaptureEnd(nil, 0, nil)

var logs []json.RawMessage
decoder := json.NewDecoder(&buf)
for {
var log json.RawMessage
if err := decoder.Decode(&log); err == io.EOF {
break
} else if err != nil {
t.Fatalf("Failed to decode log: %v", err)
}
logs = append(logs, log)
}

if len(logs) != 2 {
t.Errorf("Expected 2 logs, got %d", len(logs))
}
}
113 changes: 113 additions & 0 deletions tracers/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
package logger

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/vm"
)

Expand Down Expand Up @@ -105,3 +110,111 @@ func TestStructLogMarshalingOmitEmpty(t *testing.T) {
})
}
}

func TestFormatLogs(t *testing.T) {
logs := []StructLog{
{Pc: 1, Op: vm.PUSH1, Gas: 100, GasCost: 2, Depth: 1, Memory: []byte("test"), Stack: []uint256.Int{*uint256.NewInt(1)}},
}

formattedLogs := formatLogs(logs)
if len(formattedLogs) != len(logs) {
t.Errorf("Expected %d formatted logs, got %d", len(logs), len(formattedLogs))
}
}

func TestCaptureStart(t *testing.T) {
logger, _ := NewStructLogger(nil)
env := &vm.EVM{}

logger.CaptureStart(env, common.Address{}, common.Address{}, false, nil, 0, nil)
logger.CaptureEnd(nil, 1234, fmt.Errorf("Some Error"))
logger.CaptureClauseEnd(10000)
logger.Stop(fmt.Errorf("Some Error"))
logger.CaptureClauseStart(1234)
logger.CaptureClauseEnd(1234)
logger.Reset()
}

func TestNewMarkdownLogger(t *testing.T) {
writer := &bytes.Buffer{}
cfg := &Config{EnableMemory: true}
logger := NewMarkdownLogger(cfg, writer)

if logger.cfg != cfg {
t.Errorf("Expected cfg to be set correctly")
}

env := &vm.EVM{}

logger.CaptureStart(env, common.Address{}, common.Address{}, false, nil, 0, nil)
logger.CaptureEnd(nil, 1234, fmt.Errorf("Some Error"))
logger.CaptureClauseEnd(10000)
logger.CaptureClauseStart(1234)
logger.CaptureClauseEnd(1234)
}

func TestWriteLogs(t *testing.T) {
writer := &bytes.Buffer{}

logs := []*types.Log{
{
Address: common.HexToAddress("0x1"),
Topics: []common.Hash{common.BytesToHash([]byte("topic1")), common.BytesToHash([]byte("topic2"))},
Data: []byte("data1"),
BlockNumber: 100,
TxHash: common.BytesToHash([]byte("txhash1")),
TxIndex: 0,
BlockHash: common.BytesToHash([]byte("blockhash1")),
Index: 0,
Removed: false,
},
{
Address: common.HexToAddress("0x2"),
Topics: []common.Hash{common.BytesToHash([]byte("topic3")), common.BytesToHash([]byte("topic4"))},
Data: []byte("data2"),
BlockNumber: 101,
TxHash: common.BytesToHash([]byte("txhash2")),
TxIndex: 1,
BlockHash: common.BytesToHash([]byte("blockhash2")),
Index: 1,
Removed: false,
},
}

WriteLogs(writer, logs)
assert.NotNil(t, writer)
}

func TestWriteTrace(t *testing.T) {
writer := &bytes.Buffer{}

logs := []StructLog{
{
Pc: 1,
Op: vm.PUSH1,
Gas: 21000,
GasCost: 3,
Memory: []byte("example memory"),
MemorySize: len("example memory"),
Stack: []uint256.Int{*uint256.NewInt(2)},
ReturnData: []byte("return data"),
Storage: make(map[common.Hash]common.Hash),
Depth: 0,
RefundCounter: 100,
Err: nil,
},
}

WriteTrace(writer, logs)
assert.NotNil(t, writer)
}

func TestGetResult(t *testing.T) {
logger, _ := NewStructLogger(nil)

rawMessage, err := logger.GetResult()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.NotNil(t, rawMessage)
}

0 comments on commit 1ba1783

Please sign in to comment.