From 176eafd5a224d311e714f0a8a939450718d68ffc Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 28 Nov 2024 09:36:11 +0100 Subject: [PATCH 1/3] plugins: start work on plugin system --- plugins/interface.go | 78 ++++++++++++++++++ plugins/internal/chain.go | 165 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 plugins/interface.go create mode 100644 plugins/internal/chain.go diff --git a/plugins/interface.go b/plugins/interface.go new file mode 100644 index 0000000000..b94a651b87 --- /dev/null +++ b/plugins/interface.go @@ -0,0 +1,78 @@ +// Copyright 2024 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package plugins + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/types" + "github.com/holiman/uint256" +) + +// Plugin is an interface that allows 3rd-party developers to build plugins +// for go-ethereum which can be used to add additional functionality to geth +// without modifying the chain logic. +type Plugin interface { + // Setup is called on startup when the Plugin is initialized. + Setup(chain Chain) + // Close is called when the geth node is torn down. + Close() + // NewHead is called when a new head is set. + NewHead() +} + +// The Chain interface allows for interacting with the chain from a plugin. +type Chain interface { + // Head returns the number of the current head and finalized block. + Head() (uint64, uint64) + // Header returns a header in the canonical chain. + Header(number uint64) *types.Header + // Block returns a block in the canonical chain. + Block(number uint64) *types.Block + // Receipts returns the receipts of a block in the canonical chain. + Receipts(number uint64) types.Receipts + // State returns the state at a certain root. + State(root common.Hash) State +} + +// The State interface allows for interacting with a specific state from a plugin. +// Please note that State might hold internal references which interferes with garbage collection. +// Make sure to not hold references to State for long. +type State interface { + // Account retrieves an account from the state. + Account(addr common.Address) Account + // AccountIterator creates an iterator to iterate over accounts. + AccountIterator(seek common.Hash) snapshot.AccountIterator + // NewAccount interprets an rlp slim account as an Account. + NewAccount(addr common.Address, account []byte) Account +} + +// The Account interface allows for interacting with a specific account from a plugin. +// Please note that Account might hold internal references which interferes with garbage collection. +// Make sure to not hold references to Account for long. +type Account interface { + // Balance returns the balance of an account. + Balance() *uint256.Int + // Nonce returns the nonce of an account. + Nonce() uint64 + // Code returns the code of an account. + Code() []byte + // Storage returns a storage slot. + Storage(slot common.Hash) common.Hash + // StorageIterator creates an iterator over the storage slots of an account. + StorageIterator(seek common.Hash) snapshot.StorageIterator +} diff --git a/plugins/internal/chain.go b/plugins/internal/chain.go new file mode 100644 index 0000000000..44ddc7e383 --- /dev/null +++ b/plugins/internal/chain.go @@ -0,0 +1,165 @@ +// Copyright 2024 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package internal + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/plugins" + "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" +) + +var _ = plugins.Chain(Chain{}) + +type Chain struct { + chain *core.BlockChain +} + +func (c Chain) Head() (uint64, uint64) { + return c.chain.CurrentHeader().Number.Uint64(), c.chain.CurrentFinalBlock().Number.Uint64() +} + +func (c Chain) Header(number uint64) *types.Header { + hash := c.chain.GetCanonicalHash(number) + return c.chain.GetHeaderByHash(hash) +} + +func (c Chain) Block(number uint64) *types.Block { + hash := c.chain.GetCanonicalHash(number) + return c.chain.GetBlockByHash(hash) +} + +func (c Chain) Receipts(number uint64) types.Receipts { + hash := c.chain.GetCanonicalHash(number) + return c.chain.GetReceiptsByHash(hash) +} + +func (c Chain) State(root common.Hash) plugins.State { + reader, err := c.chain.StateCache().Reader(root) + if err != nil { + return nil + } + + return State{ + root: root, + cache: c.chain.StateCache(), + reader: reader, + } +} + +type State struct { + root common.Hash + cache state.Database + reader state.Reader +} + +func (s State) Account(addr common.Address) plugins.Account { + hash := crypto.Keccak256Hash(addr.Bytes()) + reader, err := s.cache.Reader(s.root) + if err != nil { + return nil + } + account, err := reader.Account(addr) + if err != nil { + return nil + } + return Account{ + root: s.root, + hash: hash, + account: account, + cache: s.cache, + } +} + +func (s State) AccountIterator(seek common.Hash) snapshot.AccountIterator { + if it, err := s.cache.Snapshot().AccountIterator(s.root, seek); err == nil { + return it + } + return nil +} + +func (s State) NewAccount(addr common.Address, accRLP []byte) plugins.Account { + hash := crypto.Keccak256Hash(addr.Bytes()) + var slim *types.SlimAccount + if err := rlp.DecodeBytes(accRLP, &slim); err != nil { + return nil + } + account := &types.StateAccount{ + Nonce: slim.Nonce, + Balance: slim.Balance, + CodeHash: slim.CodeHash, + Root: common.BytesToHash(slim.Root), + } + if len(account.CodeHash) == 0 { + account.CodeHash = types.EmptyCodeHash.Bytes() + } + if account.Root == (common.Hash{}) { + account.Root = types.EmptyRootHash + } + return Account{ + root: s.root, + hash: hash, + account: account, + cache: s.cache, + } +} + +type Account struct { + addr common.Address + root common.Hash + hash common.Hash + account *types.StateAccount + cache state.Database +} + +func (a Account) Balance() *uint256.Int { + return a.account.Balance +} + +func (a Account) Nonce() uint64 { + return a.account.Nonce +} + +func (a Account) Code() []byte { + if code, err := a.cache.ContractCode(a.addr, a.account.Root); err == nil { + return code + } + return nil +} + +func (a Account) Storage(slot common.Hash) common.Hash { + reader, err := a.cache.Reader(a.root) + if err != nil { + return common.Hash{} + } + if storage, err := reader.Storage(a.addr, slot); err == nil { + return storage + } + return common.Hash{} +} + +func (a Account) StorageIterator(seek common.Hash) snapshot.StorageIterator { + if it, err := a.cache.Snapshot().StorageIterator(a.root, a.hash, seek); err == nil { + return it + } + return nil +} From b93908ea09eccfca666201ae5b22b0f860fb448a Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 28 Nov 2024 09:50:57 +0100 Subject: [PATCH 2/3] plugins: add SetHooks struct --- plugins/interface.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/interface.go b/plugins/interface.go index b94a651b87..52e6fd6bec 100644 --- a/plugins/interface.go +++ b/plugins/interface.go @@ -19,6 +19,7 @@ package plugins import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/holiman/uint256" ) @@ -28,7 +29,7 @@ import ( // without modifying the chain logic. type Plugin interface { // Setup is called on startup when the Plugin is initialized. - Setup(chain Chain) + Setup(chain Chain, hooks SetHooks) // Close is called when the geth node is torn down. Close() // NewHead is called when a new head is set. @@ -76,3 +77,11 @@ type Account interface { // StorageIterator creates an iterator over the storage slots of an account. StorageIterator(seek common.Hash) snapshot.StorageIterator } + +// SetHooks allows the plugin to install hooks dynamically on the node. +type SetHooks struct { + // SetTracingHooks allows a plugin to set tracing hooks. + SetTracingHooks func(hooks *tracing.Hooks) + // In the future we could add hooks here for plugins to hook deeper into + // block production, adding hooks into the txpool, etc. +} From 73618f1793fba1ad83541ad3a4285fa86906f046 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 28 Nov 2024 11:06:23 +0100 Subject: [PATCH 3/3] plugins/internal: better show which interface the plugin needs --- plugins/internal/chain.go | 49 ++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/plugins/internal/chain.go b/plugins/internal/chain.go index 44ddc7e383..bcd180b885 100644 --- a/plugins/internal/chain.go +++ b/plugins/internal/chain.go @@ -28,38 +28,49 @@ import ( "github.com/holiman/uint256" ) -var _ = plugins.Chain(Chain{}) +var _ = plugins.Chain(&Chain{}) +var _ = Blockchain(&core.BlockChain{}) -type Chain struct { - chain *core.BlockChain +type Blockchain interface { + CurrentHeader() *types.Header + GetCanonicalHash(uint64) common.Hash + CurrentFinalBlock() *types.Header + GetHeaderByHash(common.Hash) *types.Header + GetBlockByHash(common.Hash) *types.Block + GetReceiptsByHash(common.Hash) types.Receipts + StateCache() state.Database } -func (c Chain) Head() (uint64, uint64) { +type Chain struct { + chain Blockchain +} + +func (c *Chain) Head() (uint64, uint64) { return c.chain.CurrentHeader().Number.Uint64(), c.chain.CurrentFinalBlock().Number.Uint64() } -func (c Chain) Header(number uint64) *types.Header { +func (c *Chain) Header(number uint64) *types.Header { hash := c.chain.GetCanonicalHash(number) return c.chain.GetHeaderByHash(hash) } -func (c Chain) Block(number uint64) *types.Block { +func (c *Chain) Block(number uint64) *types.Block { hash := c.chain.GetCanonicalHash(number) return c.chain.GetBlockByHash(hash) } -func (c Chain) Receipts(number uint64) types.Receipts { +func (c *Chain) Receipts(number uint64) types.Receipts { hash := c.chain.GetCanonicalHash(number) return c.chain.GetReceiptsByHash(hash) } -func (c Chain) State(root common.Hash) plugins.State { +func (c *Chain) State(root common.Hash) plugins.State { reader, err := c.chain.StateCache().Reader(root) if err != nil { return nil } - return State{ + return &State{ root: root, cache: c.chain.StateCache(), reader: reader, @@ -72,7 +83,7 @@ type State struct { reader state.Reader } -func (s State) Account(addr common.Address) plugins.Account { +func (s *State) Account(addr common.Address) plugins.Account { hash := crypto.Keccak256Hash(addr.Bytes()) reader, err := s.cache.Reader(s.root) if err != nil { @@ -82,7 +93,7 @@ func (s State) Account(addr common.Address) plugins.Account { if err != nil { return nil } - return Account{ + return &Account{ root: s.root, hash: hash, account: account, @@ -90,14 +101,14 @@ func (s State) Account(addr common.Address) plugins.Account { } } -func (s State) AccountIterator(seek common.Hash) snapshot.AccountIterator { +func (s *State) AccountIterator(seek common.Hash) snapshot.AccountIterator { if it, err := s.cache.Snapshot().AccountIterator(s.root, seek); err == nil { return it } return nil } -func (s State) NewAccount(addr common.Address, accRLP []byte) plugins.Account { +func (s *State) NewAccount(addr common.Address, accRLP []byte) plugins.Account { hash := crypto.Keccak256Hash(addr.Bytes()) var slim *types.SlimAccount if err := rlp.DecodeBytes(accRLP, &slim); err != nil { @@ -115,7 +126,7 @@ func (s State) NewAccount(addr common.Address, accRLP []byte) plugins.Account { if account.Root == (common.Hash{}) { account.Root = types.EmptyRootHash } - return Account{ + return &Account{ root: s.root, hash: hash, account: account, @@ -131,22 +142,22 @@ type Account struct { cache state.Database } -func (a Account) Balance() *uint256.Int { +func (a *Account) Balance() *uint256.Int { return a.account.Balance } -func (a Account) Nonce() uint64 { +func (a *Account) Nonce() uint64 { return a.account.Nonce } -func (a Account) Code() []byte { +func (a *Account) Code() []byte { if code, err := a.cache.ContractCode(a.addr, a.account.Root); err == nil { return code } return nil } -func (a Account) Storage(slot common.Hash) common.Hash { +func (a *Account) Storage(slot common.Hash) common.Hash { reader, err := a.cache.Reader(a.root) if err != nil { return common.Hash{} @@ -157,7 +168,7 @@ func (a Account) Storage(slot common.Hash) common.Hash { return common.Hash{} } -func (a Account) StorageIterator(seek common.Hash) snapshot.StorageIterator { +func (a *Account) StorageIterator(seek common.Hash) snapshot.StorageIterator { if it, err := a.cache.Snapshot().StorageIterator(a.root, a.hash, seek); err == nil { return it }