Skip to content

Commit 79ed58b

Browse files
committed
core,internal,light,miner,node,metadium: Added TRS(Transaction Restriction 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.
1 parent 7bc0173 commit 79ed58b

File tree

12 files changed

+580
-21
lines changed

12 files changed

+580
-21
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ BEGIN { print "package metadium\n"; } \
154154
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
155155
n = "Gov"; \
156156
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
157+
} \
158+
/^var TRSListImp_contract/ { \
159+
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
160+
n = "TRSList"; \
161+
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
157162
}'
158163

159164
metadium/governance_abi.go: metadium/contracts/MetadiumGovernance.js

core/error.go

+4
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@ var (
108108
// ErrSenderInsufficientFunds is returned if the value cost of executing a transaction
109109
// is higher than the balance of the sender's account.
110110
ErrSenderInsufficientFunds = errors.New("fee delegation: insufficient sender's funds for value")
111+
112+
// Add TRS
113+
// ErrIncludedTRSList is returned if the address included in the TRSList.
114+
ErrIncludedTRSList = errors.New("included in the TRSList")
111115
)

core/tx_pool.go

+80
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/core/types"
3535
"github.com/ethereum/go-ethereum/event"
3636
"github.com/ethereum/go-ethereum/log"
37+
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
3738
"github.com/ethereum/go-ethereum/metrics"
3839
"github.com/ethereum/go-ethereum/params"
3940
)
@@ -95,6 +96,8 @@ var (
9596
var (
9697
evictionInterval = time.Minute // Time interval to check for evictable transactions
9798
statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats
99+
// Add TRS
100+
trsTickerInterval = 3 * time.Hour // Time interval to check for TRS transactions
98101
)
99102

100103
var (
@@ -356,12 +359,16 @@ func (pool *TxPool) loop() {
356359
report = time.NewTicker(statsReportInterval)
357360
evict = time.NewTicker(evictionInterval)
358361
journal = time.NewTicker(pool.config.Rejournal)
362+
// Add TRS
363+
trsTicker = time.NewTicker(trsTickerInterval)
359364
// Track the previous head headers for transaction reorgs
360365
head = pool.chain.CurrentBlock()
361366
)
362367
defer report.Stop()
363368
defer evict.Stop()
364369
defer journal.Stop()
370+
// Add TRS
371+
defer trsTicker.Stop()
365372

366373
// Notify tests that the init phase is done
367374
close(pool.initDoneCh)
@@ -433,6 +440,26 @@ func (pool *TxPool) loop() {
433440
}
434441
pool.mu.Unlock()
435442
}
443+
// Add TRS
444+
case <-trsTicker.C:
445+
// Removes the transaction included in trsList regardless of TRS subscription.
446+
if !metaminer.IsPoW() {
447+
trsListMap, _, _ := metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
448+
if len(trsListMap) > 0 {
449+
pool.mu.Lock()
450+
for addr := range pool.pending {
451+
list := pool.pending[addr].Flatten()
452+
for _, tx := range list {
453+
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
454+
log.Debug("Discard pending transaction included in trsList", "hash", tx.Hash(), "addr", addr)
455+
pool.removeTx(tx.Hash(), true)
456+
pendingDiscardMeter.Mark(int64(1))
457+
}
458+
}
459+
}
460+
pool.mu.Unlock()
461+
}
462+
}
436463
}
437464
}
438465
}
@@ -701,6 +728,17 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
701728
if pool.currentState.GetNonce(from) > tx.Nonce() {
702729
return ErrNonceTooLow
703730
}
731+
// Add TRS
732+
// Only nodes that subscribe to TRS removes transactions included in trsList.
733+
if !metaminer.IsPoW() {
734+
trsListMap, trsSubscribe, _ := metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
735+
if len(trsListMap) > 0 && trsSubscribe {
736+
if trsListMap[from] || (tx.To() != nil && trsListMap[*tx.To()]) {
737+
return ErrIncludedTRSList
738+
}
739+
}
740+
}
741+
704742
// Transactor should have enough funds to cover the costs
705743
// cost == V + GP * GL
706744

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

1450+
// Add TRS
1451+
var trsListMap map[common.Address]bool
1452+
var trsSubscribe bool
1453+
if !metaminer.IsPoW() {
1454+
trsListMap, trsSubscribe, _ = metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
1455+
}
1456+
14121457
// Iterate over all accounts and promote any executable transactions
14131458
for _, addr := range accounts {
14141459
list := pool.queue[addr]
@@ -1425,6 +1470,20 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
14251470
// Drop all transactions that are too costly (low balance or out of gas)
14261471
drops, _ := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
14271472

1473+
// Add TRS
1474+
// Only nodes that subscribe to TRS removes transactions included in trsList.
1475+
if !metaminer.IsPoW() {
1476+
if len(trsListMap) > 0 && trsSubscribe {
1477+
for _, tx := range list.Flatten() {
1478+
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
1479+
log.Trace("Removed queued transaction included in trsList", "hash", tx.Hash(), "addr", addr)
1480+
list.Remove(tx)
1481+
drops = append(drops, tx)
1482+
}
1483+
}
1484+
}
1485+
}
1486+
14281487
// fee delegation
14291488
if pool.feedelegation {
14301489
for _, tx := range list.Flatten() {
@@ -1623,6 +1682,13 @@ func (pool *TxPool) truncateQueue() {
16231682
// is always explicitly triggered by SetBaseFee and it would be unnecessary and wasteful
16241683
// to trigger a re-heap is this function
16251684
func (pool *TxPool) demoteUnexecutables() {
1685+
// Add TRS
1686+
var trsListMap map[common.Address]bool
1687+
var trsSubscribe bool
1688+
if !metaminer.IsPoW() {
1689+
trsListMap, trsSubscribe, _ = metaminer.GetTRSListMap(pool.chain.CurrentBlock().Number())
1690+
}
1691+
16261692
// Iterate over all accounts and demote any non-executable transactions
16271693
for addr, list := range pool.pending {
16281694
nonce := pool.currentState.GetNonce(addr)
@@ -1637,6 +1703,20 @@ func (pool *TxPool) demoteUnexecutables() {
16371703
// Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
16381704
drops, invalids := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
16391705

1706+
// Add TRS
1707+
// Only nodes that subscribe to TRS removes transactions included in trsList.
1708+
if !metaminer.IsPoW() {
1709+
if len(trsListMap) > 0 && trsSubscribe {
1710+
for _, tx := range list.Flatten() {
1711+
if trsListMap[addr] || (tx.To() != nil && trsListMap[*tx.To()]) {
1712+
log.Trace("Removed pending transaction included in trsList", "hash", tx.Hash(), "addr", addr)
1713+
list.Remove(tx)
1714+
drops = append(drops, tx)
1715+
}
1716+
}
1717+
}
1718+
}
1719+
16401720
// fee delegation
16411721
if pool.feedelegation {
16421722
for _, tx := range list.Flatten() {

internal/web3ext/web3ext.go

+6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ web3._extend({
249249
call: 'admin_etcdDeleteWork',
250250
params: 0
251251
}),
252+
new web3._extend.Method({
253+
name: 'trsInfo',
254+
call: 'admin_trsInfo',
255+
params: 1,
256+
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
257+
}),
252258
],
253259
properties: [
254260
new web3._extend.Property({

light/txpool.go

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/ethdb"
3232
"github.com/ethereum/go-ethereum/event"
3333
"github.com/ethereum/go-ethereum/log"
34+
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
3435
"github.com/ethereum/go-ethereum/params"
3536
)
3637

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

386+
// Add TRS
387+
// Only nodes that subscribe to TRS reject transactions included in trsList.
388+
if !metaminer.IsPoW() {
389+
trsListMap, trsSubscribe, _ := metaminer.GetTRSListMap(pool.chain.CurrentHeader().Number)
390+
if len(trsListMap) > 0 && trsSubscribe {
391+
if trsListMap[from] || (tx.To() != nil && trsListMap[*tx.To()]) {
392+
return core.ErrIncludedTRSList
393+
}
394+
}
395+
}
396+
385397
// Transactor should have enough funds to cover the costs
386398
// cost == V + GP * GL
387399

0 commit comments

Comments
 (0)