Skip to content

Commit

Permalink
Mamoru sniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gofmanaa committed Jan 31, 2024
1 parent 8f7eb9c commit 89c9394
Show file tree
Hide file tree
Showing 17 changed files with 675 additions and 419 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
build/_workspace
build/_bin
tests/testdata
tmp_node
23 changes: 0 additions & 23 deletions .github/workflows/go.yml

This file was deleted.

47 changes: 47 additions & 0 deletions .github/workflows/mamoru-build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build Test

on:
push:
branches:
- master
- mamoru
- develop


pull_request:
branches:
- master
- mamoru
- develop


jobs:
build-test:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v3

- uses: actions/cache@v3
with:
# In order:
# * Module download cache
# * Build cache (Linux)
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test Build
run: |
go mod download
make geth
47 changes: 47 additions & 0 deletions .github/workflows/mamoru-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "Build docker image"
on:
push:
branches:
- master
- mamoru
- develop

env:
REPOSITORY: mamorufoundation/go-ethereum-sniffer

jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Get current date
id: date
run: echo "::set-output name=date::$(date -u +'%Y-%m-%d')"

- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile
build-args: |
GIT_REVISION=${{ github.sha }}
BUILD_DATE=${{ steps.date.outputs.date }}
PROFILE=release
push: true
tags: |
${{ env.REPOSITORY }}:latest
${{ env.REPOSITORY }}:${{ github.sha }}
cache-to: type=local,dest=/tmp/docker-cache
cache-from: type=local,src=/tmp/docker-cache,mode=max
54 changes: 54 additions & 0 deletions .github/workflows/mamoru-unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Unit Test

on:
push:
branches:
- master
- mamoru
- develop

pull_request:
branches:
- master
- mamoru
- develop

jobs:
unit-test:

runs-on: self-hosted

steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
- run: go version

- name: Checkout code
uses: actions/checkout@v3

- uses: actions/cache@v3
with:
# In order:
# * Module download cache
# * Build cache (Linux)
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Unit Test
env:
ANDROID_HOME: "" # Skip android test
run: |
go mod download
make test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/**/*tx_database*
*/**/*dapps*
build/_vendor/pkg

tmp_node
#*
.#*
*#
Expand Down
39 changes: 30 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ ARG VERSION=""
ARG BUILDNUM=""

# Build Geth in a stock Go builder container
FROM golang:1.21-alpine as builder
FROM golang:1.21 as builder

RUN apk add --no-cache gcc musl-dev linux-headers git
RUN apt-get update \
&& apt-get install -y gcc musl-dev git curl tar libc6-dev

# Get dependencies - will also be cached if we won't change go.mod/go.sum
COPY go.mod /go-ethereum/
COPY go.sum /go-ethereum/
RUN cd /go-ethereum && go mod download

ADD . /go-ethereum
RUN cd /go-ethereum && go run build/ci.go install -static ./cmd/geth
RUN cd /go-ethereum && GO111MODULE=on go run build/ci.go install ./cmd/geth

# Pull Geth into a second stage deploy alpine container
FROM alpine:latest
# Install the Lighthouse Consensus Client
ENV LIGHTHOUSE_VERSION=v4.6.0
RUN curl -LO https://github.com/sigp/lighthouse/releases/download/${LIGHTHOUSE_VERSION}/lighthouse-${LIGHTHOUSE_VERSION}-x86_64-unknown-linux-gnu.tar.gz
RUN tar xvf lighthouse-${LIGHTHOUSE_VERSION}-x86_64-unknown-linux-gnu.tar.gz \
&& cp lighthouse /usr/local/bin \
&& rm lighthouse-${LIGHTHOUSE_VERSION}-x86_64-unknown-linux-gnu.tar.gz \
&& rm lighthouse

RUN apk add --no-cache ca-certificates
COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/
# Pull Geth into a second stage deploy debian container
FROM debian:12.0-slim
#debian:bullseye-slim

EXPOSE 8545 8546 30303 30303/udp
ENTRYPOINT ["geth"]
COPY docker/cron/cron.conf /etc/cron.d/cron.conf
COPY docker/cron/prune.sh /prune.sh
COPY docker/supervisord/gethlighthousebn.conf /etc/supervisor/conf.d/supervisord.conf
# Install Supervisor and create the Unix socket
RUN touch /var/run/supervisor.sock

RUN apt-get update \
&& apt-get install -y ca-certificates jq unzip bash grep curl sed htop procps cron supervisor \
&& apt-get clean \
&& crontab /etc/cron.d/cron.conf

COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/
COPY --from=builder /usr/local/bin/lighthouse /usr/local/bin/

EXPOSE 9000 8545 8546 8551 30303 30303/udp

ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Add some metadata labels to help programatic image consumption
ARG COMMIT=""
ARG VERSION=""
Expand Down
74 changes: 71 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import (
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
"golang.org/x/exp/slices"

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

var (
Expand Down Expand Up @@ -259,6 +262,10 @@ type BlockChain struct {
processor Processor // Block transaction processor interface
forker *ForkChoice
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 @@ -302,6 +309,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks),
engine: engine,
vmConfig: vmConfig,

Sniffer: mamoru.NewSniffer(), // Mamoru Sniffer
MamoruFeeder: nil,
}
bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit))
bc.forker = NewForkChoice(bc, shouldPreserve)
Expand All @@ -311,6 +321,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
bc.processor = NewStateProcessor(chainConfig, bc, engine)

var err error

bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.insertStopped)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1753,7 +1764,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps)

go func(start time.Time, followup *types.Block, throwaway *state.StateDB) {
bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt)
//////////////////////////// Mamoru /////////////////////////////////
vmConfig := vm.Config{
Tracer: nil,
NoBaseFee: bc.vmConfig.NoBaseFee,
EnablePreimageRecording: bc.vmConfig.EnablePreimageRecording,
ExtraEips: bc.vmConfig.ExtraEips,
}
// bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt)
//////////////////////////// Mamoru /////////////////////////////////
bc.prefetcher.Prefetch(followup, throwaway, vmConfig, &followupInterrupt)

blockPrefetchExecuteTimer.Update(time.Since(start))
if followupInterrupt.Load() {
Expand All @@ -1762,10 +1782,58 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
}(time.Now(), followup, throwaway)
}
}

//////////////////////////////////////////////////////////////
var vmConfig vm.Config
if bc.Sniffer.CheckRequirements() {
vmConfig = bc.vmConfig
vmConfig.Tracer = mamoru.NewCallStackTracer(block.Transactions(), mamoru.RandStr(8), false, mamoru.CtxBlockchain)
} else {
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 bc.Sniffer.CheckRequirements() && vmConfig.Tracer != nil {
startTime := time.Now()
log.Info("Mamoru Sniffer start", "number", block.NumberU64(), "ctx", mamoru.CtxBlockchain)

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)
// Collect Call Trace data from EVM
if callTracer, ok := vmConfig.Tracer.(*mamoru.CallStackTracer); ok {
callFrames, err := callTracer.TakeResult()
if err != nil {
log.Error("Mamoru Sniffer Tracer Error", "err", err, "ctx", mamoru.CtxBlockchain)
//return it.index, err
} else {
var bytesLength int
for i := 0; i < len(callFrames); i++ {
bytesLength += len(callFrames[i].Input)
}

log.Info("Mamoru finish collected", "number", block.NumberU64(), "txs", block.Transactions().Len(),
"receipts", receipts.Len(), "callFrames", len(callFrames), "callFrames.input.len", bytesLength, "ctx", mamoru.CtxBlockchain)

tracer.FeedCallTraces(callFrames, block.NumberU64())
}
}

tracer.Send(startTime, block.Number(), block.Hash(), mamoru.CtxBlockchain)
}

////////////////////////////////////////////////////////////

if err != nil {
bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true)
Expand Down
Loading

0 comments on commit 89c9394

Please sign in to comment.