Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Jun 3, 2024
1 parent 09c512a commit aa4b8a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
32 changes: 27 additions & 5 deletions zk/txpool/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,38 @@ func containsPolicy(policies []byte, policy PolicyName) bool {
}

// create a method checkpolicy to check an address according to passed policy in the method
func (p *TxPool) checkPolicy(addr common.Address, policy PolicyName, mode string) (bool, error) {
var table string
func (p *TxPool) checkPolicy(addr common.Address, policy PolicyName) (bool, error) {
// Retrieve the mode configuration
var mode string
err := p.aclDB.View(context.TODO(), func(tx kv.Tx) error {
value, err := tx.GetOne(Config, []byte("mode"))
if err != nil {
return err
}
if value == nil || string(value) == "disabled" {
mode = "disabled"
return nil
}

mode = string(value)
return nil
})
if err != nil {
return false, err
}

if mode == "disabled" {
return true, nil
}

// Determine the appropriate table based on the mode
table := Blacklist
if mode == "allowlist" {
table = Whitelist
} else {
table = Blacklist
}

var policyBytes []byte
err := p.aclDB.View(context.TODO(), func(tx kv.Tx) error {
err = p.aclDB.View(context.TODO(), func(tx kv.Tx) error {
value, err := tx.GetOne(table, addr.Bytes())
if err != nil {
return err
Expand Down
44 changes: 16 additions & 28 deletions zk/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,37 +713,25 @@ func (p *TxPool) validateTx(txn *types.TxSlot, isLocal bool, stateCache kvcache.
return InsufficientFunds
}

var mode string
p.aclDB.View(context.TODO(), func(tx kv.Tx) error {
value, err := tx.GetOne(Config, []byte("mode"))
switch resolvePolicy(txn) {
case SendTx:
var allow bool
allow, err := p.checkPolicy(from, SendTx)
if err != nil {
panic(err)
}
mode = string(value)
return nil
})

if mode != "disabled" {
switch resolvePolicy(txn) {
case SendTx:
var allow bool
allow, err := p.checkPolicy(from, SendTx, mode)
if err != nil {
panic(err)
}
if !allow {
return SenderDisallowedSendTx
}
case Deploy:
var allow bool
// check that sender may deploy contracts
allow, err := p.checkPolicy(from, Deploy, mode)
if err != nil {
panic(err)
}
if !allow {
return SenderDisallowedDeploy
}
if !allow {
return SenderDisallowedSendTx
}
case Deploy:
var allow bool
// check that sender may deploy contracts
allow, err := p.checkPolicy(from, Deploy)
if err != nil {
panic(err)
}
if !allow {
return SenderDisallowedDeploy
}
}

Expand Down

0 comments on commit aa4b8a4

Please sign in to comment.