Skip to content

Commit

Permalink
feat: add update operation to policy transactions and log updates in …
Browse files Browse the repository at this point in the history
…UpdatePolicies
  • Loading branch information
afa7789 committed Oct 11, 2024
1 parent 7dfdee6 commit 7f776de
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions zk/txpool/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Operation byte
const (
Add Operation = iota
Remove
Update
)

func (p Operation) ToByte() byte {
Expand All @@ -41,6 +42,8 @@ func OperationFromByte(b byte) Operation {
return Add
case byte(Remove):
return Remove
case byte(Update):
return Update
default:
return Add // Default or error handling can be added here
}
Expand All @@ -53,6 +56,8 @@ func (p Operation) String() string {
return "add"
case Remove:
return "remove"
case Update:
return "update"
default:
return "unknown operation"
}
Expand Down Expand Up @@ -254,7 +259,6 @@ func checkIfAccountHasPolicy(ctx context.Context, aclDB kv.RwDB, addr common.Add
}

// UpdatePolicies sets a policy for an address
// TODO check this arthur.
func UpdatePolicies(ctx context.Context, aclDB kv.RwDB, aclType string, addrs []common.Address, policies [][]Policy) error {
table, err := resolveTable(aclType)
if err != nil {
Expand All @@ -263,17 +267,29 @@ func UpdatePolicies(ctx context.Context, aclDB kv.RwDB, aclType string, addrs []

return aclDB.Update(ctx, func(tx kv.RwTx) error {
for i, addr := range addrs {

// Insert policy transaction for adding or updating the policy
// I'm omiting the policies in this case.
err := InsertPolicyTransaction(ctx, aclDB, PolicyTransaction{
aclType: ResolveACLTypeToBinary(aclType),
addr: addr,
operation: Update,
timeTx: time.Now(),
})
if err != nil {
return err
}

if len(policies[i]) > 0 {
// just update the policies for the address to match the one provided
policyBytes := make([]byte, 0, len(policies[i]))
for _, p := range policies[i] {
policyBytes = append(policyBytes, p.ToByte())
}

// Update the policies in the table
if err := tx.Put(table, addr.Bytes(), policyBytes); err != nil {
return err
}

continue
}

Expand Down

0 comments on commit 7f776de

Please sign in to comment.