SDK API Reference
geth-mamoru-core-sdk
This implementation of the geth-mamoru-core-sdk requires the use of go-ethereum
version > 1.11.0
.
Usage
Install the package in an Ethereum-based project
go get github.com/Mamoru-Foundation/geth-mamoru-core-sdk
For light mode (--syncmode light)
Add the following to import statements in the file go-ethereum/light/lightchain.go
:
import (
mamoru "github.com/Mamoru-Foundation/geth-mamoru-core-sdk"
"github.com/Mamoru-Foundation/geth-mamoru-core-sdk/call_tracer"
)
Then, paste the following code into the Ethereum light client file go-ethereum/light/lightchain.go
:
Insert this code to end of function InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)
////////////////////////////////////////////////////////////////////////////
if !mamoru.IsSnifferEnable() || !mamoru.Connect() {
return 0, nil
}
ctx := context.Background()
lastBlock, err := lc.GetBlockByNumber(ctx, block.NumberU64())
if err != nil {
return 0, err
}
parentBlock, err := lc.GetBlockByHash(ctx, block.ParentHash())
if err != nil {
return 0, err
}
stateDb := NewState(ctx, parentBlock.Header(), lc.Odr())
receipts, err := GetBlockReceipts(ctx, lc.Odr(), lastBlock.Hash(), lastBlock.Number().Uint64())
if err != nil {
return 0, err
}
startTime := time.Now()
log.Info("Mamoru Eth Sniffer start", "number", block.NumberU64(), "ctx", mamoru.CtxLightchain)
tracer := mamoru.NewTracer(mamoru.NewFeed(lc.Config()))
tracer.FeedBlock(block)
tracer.FeedTransactions(block.Number(), block.Transactions(), receipts)
tracer.FeedEvents(receipts)
//Launch EVM and Collect Call Trace data
txTrace, err := call_tracer.TraceBlock(ctx, call_tracer.NewTracerConfig(stateDb.Copy(), lc.Config(), lc), lastBlock)
if err != nil {
log.Error("Mamoru Eth Sniffer Error", "err", err, "ctx", mamoru.CtxLightchain)
return 0, err
}
for _, call := range txTrace {
callFrames := call.Result
tracer.FeedCalTraces(callFrames, block.NumberU64())
}
tracer.Send(startTime, block.Number(), block.Hash(), mamoru.CtxLightchain)
////////////////////////////////////////////////////////////////////////////
For full/snap mode (--syncmode full|snap)
Add the following to import statements in the file go-ethereum/core/blockchain.go
import (
mamoru "github.com/Mamoru-Foundation/geth-mamoru-core-sdk"
)
Enable debug mode and insert tracer instance to function func NewBlockChain()
...
diffPeersToDiffHashes: make(map[string]map[common.Hash]struct{}),
}
bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine)
bc.forker = NewForkChoice(bc, shouldPreserve)
bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.processor = NewStateProcessor(chainConfig, bc, engine)
var err error
//////////////////////////////////////////////////////////////
// Enable Debug mod and Set Tracer
if !mamoru.IsSnifferEnable() || !mamoru.Connect() {
tracer, err := mamoru.NewCallTracer(false)
if err != nil {
return nil, err
}
bc.vmConfig.Tracer = tracer
bc.vmConfig.Debug = true
}
//////////////////////////////////////////////////////////////
...
Insert the main tracer code at the end of the function func (bc *BlockChain) writeBlockAndSetHead()
...
////////////////////////////////////////////////////////////
if !mamoru.IsSnifferEnable() || !mamoru.Connect() {
return 0, nil
}
startTime := time.Now()
log.Info("Mamoru Sniffer start", "number", block.NumberU64(), "ctx", mamoru.CtxBlockchain)
tracer := mamoru.NewTracer(mamoru.NewFeed(bc.chainConfig))
tracer.FeedBlock(block)
tracer.FeedTransactions(block.Number(), block.Transactions(), receipts)
tracer.FeedEvents(receipts)
// Collect Call Trace data from EVM
if callTracer, ok := bc.GetVMConfig().Tracer.(*mamoru.CallTracer); ok {
callFrames, err := callTracer.GetResult()
if err != nil {
log.Error("Mamoru Sniffer Tracer Error", "err", err, "ctx", mamoru.CtxBlockchain)
return 0, err
}
tracer.FeedCalTraces(callFrames, block.NumberU64())
}
tracer.Send(startTime, block.Number(), block.Hash(), mamoru.CtxBlockchain)
////////////////////////////////////////////////////////////
return status, nil
}
For Txpool and full/snap mode (--syncmode full|snap)
Add the following to import statements in the file go-ethereum/eth/backend.go
import (
mamoru "github.com/Mamoru-Foundation/geth-mamoru-core-sdk"
"github.com/Mamoru-Foundation/geth-mamoru-core-sdk/mempool"
)
Insert the main tracer code in function func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error)
...
eth.txPool = txpool.NewTxPool(config.TxPool, eth.blockchain.Config(), eth.blockchain)
////////////////////////////////////////////////////////
// Attach txpool sniffer
sniffer := mempool.NewSniffer(context.Background(), eth.txPool, eth.blockchain, eth.blockchain.Config(), mamoru.NewFeed(eth.blockchain.Config()))
go sniffer.SnifferLoop()
////////////////////////////////////////////////////////
Build the project:
make geth
Export Mamoru Environment Variables
export MAMORU_CHAIN_TYPE=<chain-type>
export MAMORU_SNIFFER_ENABLE=true
export MAMORU_PRIVATE_KEY=<private-key>
export MAMORU_CHAIN_ID=<validations-chain-id>
export MAMORU_ENDPOINT="https://validation-chain-url"
Run the following command to check if it works
geth --syncmode light --goerli
It's really easy, enjoy
Current version Ethereum client:
go-ethereum@v1.11.5