Skip to content

Commit

Permalink
core,internal,light,miner,node,metadium: Added TRS(Transaction Restri…
Browse files Browse the repository at this point in the history
…ction Service) implementation.

 - Added function read trsParameters(trsList,subscribe) from contract.
 - Added function check trs subscribe.
 - Added function to restriction transactions from addresses included in trsList.
 - Added function to periodically discard transactions included in trsList from the pending txpool.
 - Added admin_trsInfo RPC API.
  • Loading branch information
cp-wjhan committed May 27, 2024
1 parent 7bc0173 commit 79ed58b
Show file tree
Hide file tree
Showing 12 changed files with 580 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ BEGIN { print "package metadium\n"; } \
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
n = "Gov"; \
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
} \
/^var TRSListImp_contract/ { \
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
n = "TRSList"; \
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
}'

metadium/governance_abi.go: metadium/contracts/MetadiumGovernance.js
Expand Down
4 changes: 4 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ var (
// ErrSenderInsufficientFunds is returned if the value cost of executing a transaction
// is higher than the balance of the sender's account.
ErrSenderInsufficientFunds = errors.New("fee delegation: insufficient sender's funds for value")

// Add TRS
// ErrIncludedTRSList is returned if the address included in the TRSList.
ErrIncludedTRSList = errors.New("included in the TRSList")
)
80 changes: 80 additions & 0 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
)
Expand Down Expand Up @@ -95,6 +96,8 @@ var (
var (
evictionInterval = time.Minute // Time interval to check for evictable transactions
statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats
// Add TRS
trsTickerInterval = 3 * time.Hour // Time interval to check for TRS transactions
)

var (
Expand Down Expand Up @@ -356,12 +359,16 @@ func (pool *TxPool) loop() {
report = time.NewTicker(statsReportInterval)
evict = time.NewTicker(evictionInterval)
journal = time.NewTicker(pool.config.Rejournal)
// Add TRS
trsTicker = time.NewTicker(trsTickerInterval)
// Track the previous head headers for transaction reorgs
head = pool.chain.CurrentBlock()
)
defer report.Stop()
defer evict.Stop()
defer journal.Stop()
// Add TRS
defer trsTicker.Stop()

// Notify tests that the init phase is done
close(pool.initDoneCh)
Expand Down Expand Up @@ -433,6 +440,26 @@ func (pool *TxPool) loop() {
}
pool.mu.Unlock()
}
// Add TRS
case <-trsTicker.C:
// Removes the transaction included in trsList regardless of TRS subscription.
if !metaminer.IsPoW() {
trsListMap, _, _ := metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
if len(trsListMap) > 0 {
pool.mu.Lock()
for addr := range pool.pending {
list := pool.pending[addr].Flatten()
for _, tx := range list {
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
log.Debug("Discard pending transaction included in trsList", "hash", tx.Hash(), "addr", addr)
pool.removeTx(tx.Hash(), true)
pendingDiscardMeter.Mark(int64(1))
}
}
}
pool.mu.Unlock()
}
}
}
}
}
Expand Down Expand Up @@ -701,6 +728,17 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
if pool.currentState.GetNonce(from) > tx.Nonce() {
return ErrNonceTooLow
}
// Add TRS
// Only nodes that subscribe to TRS removes transactions included in trsList.
if !metaminer.IsPoW() {
trsListMap, trsSubscribe, _ := metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
if len(trsListMap) > 0 && trsSubscribe {
if trsListMap[from] || (tx.To() != nil && trsListMap[*tx.To()]) {
return ErrIncludedTRSList
}
}
}

// Transactor should have enough funds to cover the costs
// cost == V + GP * GL

Expand Down Expand Up @@ -1409,6 +1447,13 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
// Track the promoted transactions to broadcast them at once
var promoted []*types.Transaction

// Add TRS
var trsListMap map[common.Address]bool
var trsSubscribe bool
if !metaminer.IsPoW() {
trsListMap, trsSubscribe, _ = metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
}

// Iterate over all accounts and promote any executable transactions
for _, addr := range accounts {
list := pool.queue[addr]
Expand All @@ -1425,6 +1470,20 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
// Drop all transactions that are too costly (low balance or out of gas)
drops, _ := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)

// Add TRS
// Only nodes that subscribe to TRS removes transactions included in trsList.
if !metaminer.IsPoW() {
if len(trsListMap) > 0 && trsSubscribe {
for _, tx := range list.Flatten() {
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
log.Trace("Removed queued transaction included in trsList", "hash", tx.Hash(), "addr", addr)
list.Remove(tx)
drops = append(drops, tx)
}
}
}
}

// fee delegation
if pool.feedelegation {
for _, tx := range list.Flatten() {
Expand Down Expand Up @@ -1623,6 +1682,13 @@ func (pool *TxPool) truncateQueue() {
// is always explicitly triggered by SetBaseFee and it would be unnecessary and wasteful
// to trigger a re-heap is this function
func (pool *TxPool) demoteUnexecutables() {
// Add TRS
var trsListMap map[common.Address]bool
var trsSubscribe bool
if !metaminer.IsPoW() {
trsListMap, trsSubscribe, _ = metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
}

// Iterate over all accounts and demote any non-executable transactions
for addr, list := range pool.pending {
nonce := pool.currentState.GetNonce(addr)
Expand All @@ -1637,6 +1703,20 @@ func (pool *TxPool) demoteUnexecutables() {
// Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
drops, invalids := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)

// Add TRS
// Only nodes that subscribe to TRS removes transactions included in trsList.
if !metaminer.IsPoW() {
if len(trsListMap) > 0 && trsSubscribe {
for _, tx := range list.Flatten() {
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
log.Trace("Removed pending transaction included in trsList", "hash", tx.Hash(), "addr", addr)
list.Remove(tx)
drops = append(drops, tx)
}
}
}
}

// fee delegation
if pool.feedelegation {
for _, tx := range list.Flatten() {
Expand Down
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ web3._extend({
call: 'admin_etcdDeleteWork',
params: 0
}),
new web3._extend.Method({
name: 'trsInfo',
call: 'admin_trsInfo',
params: 1,
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
}),
],
properties: [
new web3._extend.Property({
Expand Down
12 changes: 12 additions & 0 deletions light/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
"github.com/ethereum/go-ethereum/params"
)

Expand Down Expand Up @@ -382,6 +383,17 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error
return core.ErrNegativeValue
}

// Add TRS
// Only nodes that subscribe to TRS reject transactions included in trsList.
if !metaminer.IsPoW() {
trsListMap, trsSubscribe, _ := metaminer.GetTRSListMap(pool.chain.CurrentHeader().Number)
if len(trsListMap) > 0 && trsSubscribe {
if trsListMap[from] || (tx.To() != nil && trsListMap[*tx.To()]) {
return core.ErrIncludedTRSList
}
}
}

// Transactor should have enough funds to cover the costs
// cost == V + GP * GL

Expand Down
Loading

0 comments on commit 79ed58b

Please sign in to comment.