Skip to content

Commit

Permalink
block call to contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Jun 1, 2024
1 parent 86c17e2 commit 3217eff
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 84 deletions.
59 changes: 59 additions & 0 deletions blacklist/blacklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package blacklist

import (
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/internal/common"
"github.com/aergoio/aergo/v2/internal/enc/hex"
)

type Blacklist struct {
sourcelist []string // account address (b58 encoded like Am...) or id (32 bytes in hex = 64 bytes)
blocked map[string]bool // all above converted to account id (32 bytes)
}

var globalBlacklist *Blacklist

// Initialize sets up the blacklist with the given addresses.
// This function should be called only once at the start.
func Initialize(addresses []string) {
conf := &Blacklist{}
conf.sourcelist = make([]string, len(addresses))
copy(conf.sourcelist, addresses)
conf.blocked = make(map[string]bool)
for _, v := range addresses {
key, err := toKey(v)
if err == nil {
conf.blocked[key] = true
} else {
// Handle invalid address, log or take other actions as needed
}
}
globalBlacklist = conf
}

func Check(address string) bool {
if globalBlacklist == nil {
return false
}
key, err := toKey(address)
if err != nil {
return false
}
return globalBlacklist.blocked[key]
}

func toKey(address string) (string, error) {
var key []byte
var err error
if len(address) == 64 {
key, err = hex.Decode(address)
} else {
var addr []byte
addr, err = types.DecodeAddress(address)
if err != nil {
return "", err
}
key = common.Hasher(addr)
}
return string(key), err
}
11 changes: 11 additions & 0 deletions contract/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
"github.com/aergoio/aergo/v2/blacklist"
jsoniter "github.com/json-iterator/go"
)

Expand Down Expand Up @@ -292,6 +293,16 @@ func newExecutor(
}
ctx.callDepth++

if blacklist.Check(types.EncodeAddress(contractId)) {
ce := &executor{
code: contract,
ctx: ctx,
}
ce.err = fmt.Errorf("contract not available")
ctrLgr.Error().Err(ce.err).Str("contract", types.EncodeAddress(contractId)).Msg("blocked contract")
return ce
}

ce := &executor{
code: contract,
L: GetLState(),
Expand Down
80 changes: 0 additions & 80 deletions mempool/blacklist.go

This file was deleted.

7 changes: 3 additions & 4 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/message"
"github.com/aergoio/aergo/v2/blacklist"
)

const (
Expand Down Expand Up @@ -74,7 +75,6 @@ type MemPool struct {
acceptChainIdHash []byte
isPublic bool
whitelist *whitelistConf
blacklist *blacklistConf
blockDeploy bool
// followings are for test
testConfig bool
Expand Down Expand Up @@ -113,7 +113,7 @@ func NewMemPoolService(cfg *cfg.Config, cs *chain.ChainService) *MemPool {
evictPeriod = time.Duration(cfg.Mempool.FadeoutPeriod) * time.Hour
}
if cfg.Mempool.Blacklist != nil {
actor.blacklist = newBlacklistConf(actor, cfg.Mempool.Blacklist)
blacklist.Initialize(cfg.Mempool.Blacklist)
}
return actor
}
Expand Down Expand Up @@ -312,7 +312,6 @@ func (mp *MemPool) Statistics() *map[string]interface{} {
"dead": mp.deadtx,
"config": mp.cfg.Mempool,
}
ret["blacklist"] = mp.blacklist.GetBlacklist()
if !mp.isPublic {
ret["whitelist"] = mp.whitelist.GetWhitelist()
ret["whitelist_on"] = mp.whitelist.GetOn()
Expand Down Expand Up @@ -622,7 +621,7 @@ func (mp *MemPool) validateTx(tx types.Transaction, account types.Address) error
if !mp.whitelist.Check(types.EncodeAddress(account)) {
return types.ErrTxNotAllowedAccount
}
if mp.blacklist.Check(types.EncodeAddress(account)) {
if blacklist.Check(types.EncodeAddress(account)) {
return types.ErrTxNotAllowedAccount
}
ns, err := mp.getAccountState(account)
Expand Down

0 comments on commit 3217eff

Please sign in to comment.