Skip to content

Commit

Permalink
added test; refactor tracer; clear config from worker
Browse files Browse the repository at this point in the history
  • Loading branch information
gofmanaa committed Oct 24, 2023
1 parent cd1c95d commit 6bf206f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 6 deletions.
20 changes: 15 additions & 5 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ type BlockChain struct {
vmConfig vm.Config

Sniffer *mamoru.Sniffer // Mamoru Sniffer
// mamoru Feeder
MamoruFeeder mamoru.Feeder
}

// NewBlockChain returns a fully initialised block chain using information
Expand Down Expand Up @@ -276,7 +278,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
engine: engine,
vmConfig: vmConfig,

Sniffer: mamoru.NewSniffer(), // Mamoru Sniffer
Sniffer: mamoru.NewSniffer(), // Mamoru Sniffer
MamoruFeeder: nil,
}
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
bc.forker = NewForkChoice(bc, shouldPreserve)
Expand Down Expand Up @@ -1765,15 +1768,18 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
}
}
//////////////////////////////////////////////////////////////
var vmConfig vm.Config
if bc.Sniffer.CheckRequirements() {
bc.vmConfig.Tracer = mamoru.NewCallStackTracer(block.Transactions().Len(), mamoru.RandStr(8), false, mamoru.CtxBlockchain)
vmConfig = bc.vmConfig
vmConfig.Tracer = mamoru.NewCallStackTracer(block.Transactions().Len(), mamoru.RandStr(8), false, mamoru.CtxBlockchain)
} else {
bc.vmConfig.Tracer = nil
vmConfig = bc.vmConfig
vmConfig.Tracer = nil
}
//////////////////////////////////////////////////////////////
// Process block using the parent state as reference point
pstart := time.Now()
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, vmConfig)
if err != nil {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
Expand Down Expand Up @@ -1843,8 +1849,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
startTime := time.Now()
log.Info("Mamoru Sniffer start", "number", block.NumberU64(), "ctx", mamoru.CtxBlockchain)

tracer := mamoru.NewTracer(mamoru.NewFeed(bc.chainConfig, statistics.NewStatsBlockchain()))
feeder := bc.MamoruFeeder
if feeder == nil {
feeder = mamoru.NewFeed(bc.chainConfig, statistics.NewStatsBlockchain())
}

tracer := mamoru.NewTracer(feeder)
tracer.FeedBlock(block)
tracer.FeedTransactions(block.Number(), block.Time(), block.Transactions(), receipts)
tracer.FeedEvents(receipts)
Expand Down
181 changes: 181 additions & 0 deletions core/mamoru_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package core

import (
"math/big"
"os"
"strings"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"

mamoru "github.com/Mamoru-Foundation/geth-mamoru-core-sdk"
statistics "github.com/Mamoru-Foundation/geth-mamoru-core-sdk/stats"
"github.com/Mamoru-Foundation/mamoru-sniffer-go/mamoru_sniffer"
)

// TestMamoruBlockchainTracer test mamoru blockchain tracer
func TestMamoruBlockchainTracer(t *testing.T) {
t.Run("Blockchain Mamoru sniffer TRUE ", func(t *testing.T) {
_ = os.Setenv("MAMORU_SNIFFER_ENABLE", "true")

var (
gendb = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
address = crypto.PubkeyToAddress(key.PublicKey)
funds = big.NewInt(100000000000000000)
gspec = &Genesis{
Config: params.TestChainConfig,
Alloc: GenesisAlloc{address: {Balance: funds}},
BaseFee: big.NewInt(params.InitialBaseFee),
}
genesis = gspec.MustCommit(gendb)
signer = types.LatestSigner(gspec.Config)
)
height := uint64(2)
txlen := 10
blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), gendb, int(height), func(i int, block *BlockGen) {
for j := 0; j < txlen; j++ {
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(int64(1000+j)), params.TxGas, block.header.BaseFee, nil), signer, key)
if err != nil {
panic(err)
}
block.AddTx(tx)
}
})
// mock connect to sniffer
mamoru.SnifferConnectFunc = func() (*mamoru_sniffer.Sniffer, error) { return nil, nil }

var db = rawdb.NewMemoryDatabase()
gspec.MustCommit(db)

// NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(header *types.Header) bool, txLookupLimit *uint64)
var blockchain, err = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
if err != nil {
t.Fatal(err)
}

// Set the sniffer
blockchain.Sniffer = mamoru.NewSniffer()
feeder := NewTestFeeder(statistics.NewStatsBlockchain())
// Set the feeder
blockchain.MamoruFeeder = feeder

defer blockchain.Stop()

for _, block := range blocks {
_, err = blockchain.InsertChain(types.Blocks{block})
if err != nil {
t.Log(err)
}

callTraces := feeder.CallFrames()
for i, call := range callTraces {
assert.Empty(t, call.Error, "error must be empty")
assert.NotNil(t, call.Type, "type must be not nil")
assert.Equal(t, addrToHexT(address), strings.ToLower(call.From), "address must be equal")
assert.Equal(t, uint32(i), call.TxIndex, "tx index must be equal")
assert.Equal(t, uint32(0), call.Depth, "depth must be equal")
}

assert.Equal(t, uint64(1), feeder.Stats().GetBlocks(), "blocks must be equal")
assert.Equal(t, uint64(feeder.Txs().Len()), feeder.Stats().GetTxs(), "txs must be equal")
assert.Equal(t, uint64(feeder.Receipts().Len()), feeder.Stats().GetEvents(), "events must be equal")
assert.Equal(t, uint64(len(feeder.CallFrames())), feeder.Stats().GetTraces(), "call traces must be equal")
// reset feeder
feeder.Reset()
}
})
}

type TestFeeder struct {
mu sync.RWMutex
stats statistics.Stats
block []*types.Block
txs types.Transactions
receipts types.Receipts
callFrames []*mamoru.CallFrame
}

func NewTestFeeder(stats statistics.Stats) *TestFeeder {
return &TestFeeder{stats: stats}
}

func (f *TestFeeder) Stats() statistics.Stats {
return f.stats
}

func (f *TestFeeder) FeedBlock(block *types.Block) mamoru_sniffer.Block {
f.mu.RLock()
defer f.mu.RUnlock()
f.block = append(f.block, block)
f.stats.MarkBlocks()

return mamoru_sniffer.Block{}
}

func (f *TestFeeder) FeedTransactions(blockNumber *big.Int, blockTime uint64, txs types.Transactions, receipts types.Receipts) []mamoru_sniffer.Transaction {
f.mu.RLock()
defer f.mu.RUnlock()
f.txs = append(f.txs, txs...)
f.stats.MarkTxs(uint64(len(txs)))

return []mamoru_sniffer.Transaction{}
}

func (f *TestFeeder) FeedEvents(receipts types.Receipts) []mamoru_sniffer.Event {
f.mu.RLock()
defer f.mu.RUnlock()
f.receipts = append(f.receipts, receipts...)
f.stats.MarkEvents(uint64(len(receipts)))

return []mamoru_sniffer.Event{}
}

func (f *TestFeeder) FeedCallTraces(callFrames []*mamoru.CallFrame, _ uint64) []mamoru_sniffer.CallTrace {
f.mu.RLock()
defer f.mu.RUnlock()
f.callFrames = append(f.callFrames, callFrames...)
f.stats.MarkCallTraces(uint64(len(f.callFrames)))

return []mamoru_sniffer.CallTrace{}
}

func (f *TestFeeder) Txs() types.Transactions {
f.mu.Lock()
defer f.mu.Unlock()
return f.txs
}

func (f *TestFeeder) Receipts() types.Receipts {
f.mu.Lock()
defer f.mu.Unlock()
return f.receipts
}

func (f *TestFeeder) CallFrames() []*mamoru.CallFrame {
f.mu.Lock()
defer f.mu.Unlock()
return f.callFrames
}

func (f *TestFeeder) Reset() {
f.mu.Lock()
defer f.mu.Unlock()
f.block = nil
f.txs = nil
f.receipts = nil
f.callFrames = nil
f.stats.Reset()
}

func addrToHexT(a common.Address) string {
return strings.ToLower(a.Hex())
}
3 changes: 2 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package miner
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/vm"

"math/big"
"sync"
"sync/atomic"
Expand All @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down

0 comments on commit 6bf206f

Please sign in to comment.