Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config changes for berlin modes #174

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions chain.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"priority-regossip-txs-per-address": 200,
"priority-regossip-addresses": ["0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"],
"validator-private-key-file": "/tmp/validator.pk",
"is-validator": true,
"trading-api-enabled": true,
"node-type": "kitkat_berghain",
"testing-api-enabled": true,
"load-from-snapshot-enabled": true,
"snapshot-file-path": "/tmp/snapshot",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08
github.com/go-cmd/cmd v1.4.1
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
Expand Down Expand Up @@ -77,7 +78,6 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/btree v1.1.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 2 additions & 0 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
)

// Backend wraps all methods required for mining.
Expand Down Expand Up @@ -65,6 +66,7 @@ func (miner *Miner) SetEtherbase(addr common.Address) {
}

func (miner *Miner) GenerateBlock(predicateContext *precompileconfig.PredicateContext) (*types.Block, error) {
log.Info("GenerateBlock", "address", miner.worker.coinbase.String())
return miner.worker.commitNewWork(predicateContext)
}

Expand Down
2 changes: 2 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (w *worker) setEtherbase(addr common.Address) {

// commitNewWork generates several new sealing tasks based on the parent block.
func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateContext) (*types.Block, error) {
log.Info("commitNewWork", "address", w.coinbase.String())
w.mu.RLock()
defer w.mu.RUnlock()

Expand Down Expand Up @@ -223,6 +224,7 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
}

orderBookTxs := w.eth.TxPool().GetOrderBookTxs()
log.Info("commitNewWork - OrderBookTxs", "orderBookTxs", len(orderBookTxs))
if len(orderBookTxs) > 0 {
txs := types.NewTransactionsByPriceAndNonce(env.signer, orderBookTxs, header.BaseFee)
w.commitTransactions(env, txs, header.Coinbase)
Expand Down
14 changes: 0 additions & 14 deletions network-configs/aylin/chain.json

This file was deleted.

16 changes: 0 additions & 16 deletions network-configs/aylin/chain_archival_node.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{
"node-type": "berghain",
"snowman-api-enabled": true,
"local-txs-enabled": true,
"priority-regossip-frequency": "1s",
"tx-regossip-max-size": 32,
"priority-regossip-max-txs": 32,
"priority-regossip-txs-per-address": 20,
"priority-regossip-addresses": ["0x06CCAD927e6B1d36E219Cb582Af3185D0705f78F"],
"coreth-admin-api-enabled": true,
"eth-apis": ["public-eth","public-eth-filter","net","web3","internal-eth","internal-blockchain","internal-transaction","internal-debug","internal-tx-pool","internal-account","debug-tracer"],
"trading-api-enabled": true,
"testing-api-enabled": true,
"snapshot-file-path": "/tmp/snapshot",
"makerbook-database-path": "/tmp/makerbook"
Expand Down
11 changes: 11 additions & 0 deletions network-configs/aylin/chain_berghain_archival.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"node-type": "berghain",
"pruning-enabled": false,
"snowman-api-enabled": true,
"local-txs-enabled": true,
"coreth-admin-api-enabled": true,
"eth-apis": ["public-eth","public-eth-filter","net","web3","internal-eth","internal-blockchain","internal-transaction","internal-debug","internal-tx-pool","internal-account","debug-tracer"],
"testing-api-enabled": true,
"snapshot-file-path": "/tmp/snapshot",
"makerbook-database-path": "/tmp/makerbook"
}
13 changes: 13 additions & 0 deletions network-configs/aylin/chain_kitkat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"node-type": "kitkat",
"snowman-api-enabled": true,
"local-txs-enabled": true,
"priority-regossip-frequency": "1s",
"tx-regossip-max-size": 32,
"priority-regossip-max-txs": 32,
"priority-regossip-txs-per-address": 20,
"validator-private-key-file": "/home/ubuntu/validator.pk",
"feeRecipient": "0x4d5C074bB156D4c8D9e91fEE416b3b423ba8f6f2",
"snapshot-file-path": "/tmp/snapshot",
"makerbook-database-path": "/tmp/makerbook"
}
5 changes: 5 additions & 0 deletions network-configs/aylin/chain_tresor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"node-type": "tresor",
"state-sync-enabled": true,
"feeRecipient": "0xB3D25D47291D7F8b97FE3884A924541cDDcbB8Be"
}
8 changes: 7 additions & 1 deletion plugin/evm/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (vm *VM) NewBlockBuilder(notifyBuildBlockChan chan<- commonEng.Message) *bl
shutdownWg: &vm.shutdownWg,
notifyBuildBlockChan: notifyBuildBlockChan,
}
log.Info("New block builder created")
b.handleBlockBuilding()
return b
}
Expand All @@ -80,13 +81,15 @@ func (b *blockBuilder) buildBlockTimerCallback() {

// If there are still transactions in the mempool, send another notification to
// the engine to retry BuildBlock.
log.Info("Timer expired, trying to generate a block")
if b.needToBuild() {
b.markBuilding()
}
}

// handleGenerateBlock is called from the VM immediately after BuildBlock.
func (b *blockBuilder) handleGenerateBlock() {
log.Info("handleGenerateBlock, setting timer to retry building a block")
b.buildBlockLock.Lock()
defer b.buildBlockLock.Unlock()

Expand All @@ -100,21 +103,24 @@ func (b *blockBuilder) handleGenerateBlock() {
// needToBuild returns true if there are outstanding transactions to be issued
// into a block.
func (b *blockBuilder) needToBuild() bool {
size := b.txPool.PendingSize(true)
size := b.txPool.PendingSize(true) + int(b.txPool.GetOrderBookTxsCount())
log.Info("Checking if we need to build a block", "size", size)
return size > 0
}

// markBuilding adds a PendingTxs message to the toEngine channel.
// markBuilding assumes the [buildBlockLock] is held.
func (b *blockBuilder) markBuilding() {
// If the engine has not called BuildBlock, no need to send another message.
log.Info("Marking building", "b.buildSent", b.buildSent)
if b.buildSent {
return
}
b.buildBlockTimer.Cancel() // Cancel any future attempt from the timer to send a PendingTxs message

select {
case b.notifyBuildBlockChan <- commonEng.PendingTxs:
log.Info("Sent PendingTxs message to the consensus engine.")
b.buildSent = true
default:
log.Error("Failed to push PendingTxs notification to the consensus engine.")
Expand Down
12 changes: 4 additions & 8 deletions plugin/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ const (
defaultStateSyncMinBlocks = 300_000
defaultStateSyncRequestSize = 1024 // the number of key/values to ask peers for per request

defaultIsValidator = false
defaultTradingAPIEnabled = false
defaultNodeType = "tresor"
defaultLoadFromSnapshotEnabled = true
defaultSnapshotFilePath = "/tmp/snapshot"
defaultMakerbookDatabasePath = "/tmp/makerbook"
Expand Down Expand Up @@ -236,11 +235,9 @@ type Config struct {

// Testing apis enabled
TestingApiEnabled bool `json:"testing-api-enabled"`
// IsValidator is true if this node is a validator
IsValidator bool `json:"is-validator"`

// TradingAPI is for the sdk
TradingAPIEnabled bool `json:"trading-api-enabled"`
// NodeType is the type of node among the following: "tresor", "kitkat", "berghain", meaning validator only, matching engine, rpc node respectively
NodeType string `json:"node-type"`

// LoadFromSnapshotEnabled = true if the node should load the memory db from a snapshot
LoadFromSnapshotEnabled bool `json:"load-from-snapshot-enabled"`
Expand Down Expand Up @@ -311,8 +308,7 @@ func (c *Config) SetDefaults() {
c.AcceptedCacheSize = defaultAcceptedCacheSize
c.ValidatorPrivateKeyFile = defaultValidatorPrivateKeyFile
c.TestingApiEnabled = defaultTestingApiEnabled
c.IsValidator = defaultIsValidator
c.TradingAPIEnabled = defaultTradingAPIEnabled
c.NodeType = defaultNodeType
c.LoadFromSnapshotEnabled = defaultLoadFromSnapshotEnabled
c.SnapshotFilePath = defaultSnapshotFilePath
c.MakerbookDatabasePath = defaultMakerbookDatabasePath
Expand Down
5 changes: 5 additions & 0 deletions plugin/evm/gossiper_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (n *pushGossiper) sendSignedOrders(orders []*hu.SignedOrder) error {
// #### HANDLER ####

func (h *GossipHandler) HandleSignedOrders(nodeID ids.NodeID, msg message.SignedOrdersGossip) error {
// for vanilla validators we do not care about gossiping orders
if h.vm.limitOrderProcesser.GetNodeType() == Tresor {
return nil
}

h.mu.Lock()
defer h.mu.Unlock()

Expand Down
74 changes: 69 additions & 5 deletions plugin/evm/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"

Expand All @@ -32,12 +33,23 @@ const (
snapshotInterval uint64 = 10 // save snapshot every 1000 blocks
)

type NodeType string

const (
Tresor NodeType = "tresor" // vanilla validator
Kitkat NodeType = "kitkat" // validator + matching engine
Berghain NodeType = "berghain" // rpc node
Kitkat_Berghain NodeType = "kitkat_berghain" // validator + matching engine + rpc node
)

type LimitOrderProcesser interface {
ListenAndProcessTransactions(blockBuilder *blockBuilder)
GetOrderBookAPI() *orderbook.OrderBookAPI
GetTestingAPI() *orderbook.TestingAPI
GetTradingAPI() *orderbook.TradingAPI
RunMatchingPipeline()
GetNodeType() NodeType
isMatcherNode() bool
}

type limitOrderProcesser struct {
Expand All @@ -56,19 +68,36 @@ type limitOrderProcesser struct {
hubbleDB database.Database
configService orderbook.IConfigService
blockBuilder *blockBuilder
isValidator bool
tradingAPIEnabled bool
nodeType NodeType
loadFromSnapshotEnabled bool
snapshotSavedBlockNumber uint64
snapshotFilePath string
tradingAPI *orderbook.TradingAPI
}

func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownChan <-chan struct{}, shutdownWg *sync.WaitGroup, backend *eth.EthAPIBackend, blockChain *core.BlockChain, hubbleDB database.Database, validatorPrivateKey string, config Config) LimitOrderProcesser {
func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownChan <-chan struct{}, shutdownWg *sync.WaitGroup, backend *eth.EthAPIBackend, blockChain *core.BlockChain, hubbleDB database.Database, config Config) (LimitOrderProcesser, error) {
log.Info("**** NewLimitOrderProcesser")

configService := orderbook.NewConfigService(blockChain)
memoryDb := orderbook.NewInMemoryDatabase(configService)

nodeType, err := stringToNodeType(config.NodeType)
if err != nil {
return nil, err
}
var validatorPrivateKey string
if nodeType == Kitkat || nodeType == Kitkat_Berghain {
validatorPrivateKey, err = loadPrivateKeyFromFile(config.ValidatorPrivateKeyFile)
if err != nil {
panic(fmt.Sprint("please specify correct path for validator-private-key-file in chain.json ", err))
}
if validatorPrivateKey == "" {
panic("validator private key is empty")
}
}
lotp := orderbook.NewLimitOrderTxProcessor(txPool, memoryDb, backend, validatorPrivateKey)

signedObAddy := configService.GetSignedOrderbookContract()
contractEventProcessor := orderbook.NewContractEventsProcessor(memoryDb, signedObAddy)

Expand Down Expand Up @@ -110,14 +139,40 @@ func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownCh
matchingPipeline: matchingPipeline,
filterAPI: filterAPI,
configService: configService,
isValidator: config.IsValidator,
tradingAPIEnabled: config.TradingAPIEnabled,
nodeType: nodeType,
loadFromSnapshotEnabled: config.LoadFromSnapshotEnabled,
snapshotFilePath: config.SnapshotFilePath,
}, nil
}

func loadPrivateKeyFromFile(keyFile string) (string, error) {
key, err := os.ReadFile(keyFile)
if err != nil {
return "", err
}
return strings.TrimSuffix(string(key), "\n"), nil
}

func stringToNodeType(nodeTypeString string) (NodeType, error) {
switch nodeTypeString {
case string(Tresor):
return Tresor, nil
case string(Kitkat):
return Kitkat, nil
case string(Berghain):
return Berghain, nil
case string(Kitkat_Berghain):
return Kitkat_Berghain, nil
default:
return "", fmt.Errorf("unknown NodeType: %s", nodeTypeString)
}
}

func (lop *limitOrderProcesser) ListenAndProcessTransactions(blockBuilder *blockBuilder) {
if lop.nodeType == Tresor {
return
}

lop.mu.Lock()

lastAccepted := lop.blockChain.LastAcceptedBlock()
Expand Down Expand Up @@ -174,12 +229,13 @@ func (lop *limitOrderProcesser) ListenAndProcessTransactions(blockBuilder *block
}

func (lop *limitOrderProcesser) RunMatchingPipeline() {
if !lop.isValidator {
if !lop.isMatcherNode() {
return
}
executeFuncAndRecoverPanic(func() {
matchesFound := lop.matchingPipeline.Run(new(big.Int).Add(lop.blockChain.CurrentBlock().Number, big.NewInt(1)))
if matchesFound {
log.Info("RunMatchingPipeline - matches found")
lop.blockBuilder.signalTxsReady()
}
}, orderbook.RunMatchingPipelinePanicMessage, orderbook.RunMatchingPipelinePanicsCounter)
Expand All @@ -206,6 +262,14 @@ func (lop *limitOrderProcesser) GetTestingAPI() *orderbook.TestingAPI {
return orderbook.NewTestingAPI(lop.memoryDb, lop.backend, lop.configService, lop.hubbleDB)
}

func (lop *limitOrderProcesser) GetNodeType() NodeType {
return lop.nodeType
}

func (lop *limitOrderProcesser) isMatcherNode() bool {
return lop.nodeType == Kitkat || lop.nodeType == Kitkat_Berghain
}

func (lop *limitOrderProcesser) listenAndStoreLimitOrderTransactions() {
logsCh := make(chan []*types.Log)
logsSubscription := lop.backend.SubscribeHubbleLogsEvent(logsCh)
Expand Down
1 change: 1 addition & 0 deletions plugin/evm/order_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (api *OrderAPI) PlaceSignedOrders(ctx context.Context, input string) (Place
continue
}

// we ignore the 2nd argument shouldTriggerMatching. since PlaceSignedOrders is only called in API nodes, we do not trigger matching in them
orderId, _, err := api.tradingAPI.PlaceOrder(order)
orderResponse.OrderId = orderId.String()
if err != nil {
Expand Down
Loading
Loading