Skip to content

Commit

Permalink
feat: add mode change operation to transaction history. (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
afa7789 authored Oct 28, 2024
1 parent b323a7c commit dae09e3
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion zk/txpool/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
Add Operation = iota
Remove
Update
ModeChange
)

func (p Operation) ToByte() byte {
Expand Down Expand Up @@ -58,6 +59,8 @@ func (p Operation) String() string {
return "remove"
case Update:
return "update"
case ModeChange:
return "mode change"
default:
return "unknown operation"
}
Expand All @@ -69,6 +72,7 @@ type ACLTypeBinary byte
const (
AllowListTypeB ACLTypeBinary = iota
BlockListTypeB
DisabledModeB
)

func (p ACLTypeBinary) ToByte() byte {
Expand All @@ -86,6 +90,8 @@ func ResolveACLTypeToBinary(aclType string) ACLTypeBinary {
return AllowListTypeB
case string(BlockListType):
return BlockListTypeB
case string(DisabledMode):
return DisabledModeB
}
return BlockListTypeB
}
Expand All @@ -97,6 +103,8 @@ func ACLTypeBinaryFromByte(b byte) ACLTypeBinary {
return AllowListTypeB
case byte(BlockListTypeB):
return BlockListTypeB
case byte(DisabledModeB):
return DisabledModeB
default:
return BlockListTypeB // Default or error handling can be added here
}
Expand All @@ -109,6 +117,8 @@ func (p ACLTypeBinary) String() string {
return "allowlist"
case BlockListTypeB:
return "blocklist"
case DisabledModeB:
return "disabled"
default:
return "Unknown ACLTypeBinary"
}
Expand Down Expand Up @@ -390,6 +400,15 @@ func LastPolicyTransactions(ctx context.Context, aclDB kv.RwDB, count int) ([]Po
}

func byteToPolicyTransaction(value []byte) (PolicyTransaction, error) {
// if the length is the size of mode change = 9, then it is a mode change transaction
if len(value) == 9 {
return PolicyTransaction{
aclType: ACLTypeBinary(value[0]),
timeTx: bytesToTimestamp(value[1:9]),
operation: ModeChange,
}, nil
}

// Check for expected length:
// 1 byte for aclType,
// 1 byte for operation,
Expand Down Expand Up @@ -428,6 +447,15 @@ func byteToPolicyTransaction(value []byte) (PolicyTransaction, error) {
}

func (pt PolicyTransaction) ToString() string {
// on mode change we only have aclType and timeTx
// so we need to check if the operation is ModeChange
// to print the correct information to display
if pt.operation == ModeChange {
return fmt.Sprintf("ACLType: %s, Operation: %s, Time: %s",
pt.aclType.String(),
pt.operation.String(),
pt.timeTx.Format(time.RFC3339)) // Use RFC3339 format for the
}
return fmt.Sprintf("ACLType: %s, Address: %s, Policy: %s, Operation: %s, Time: %s",
pt.aclType.String(),
hex.EncodeToString(pt.addr[:]), // Convert address to hexadecimal string representation
Expand Down Expand Up @@ -605,7 +633,18 @@ func SetMode(ctx context.Context, aclDB kv.RwDB, mode string) error {
}

return aclDB.Update(ctx, func(tx kv.RwTx) error {
return tx.Put(Config, []byte(modeKey), []byte(m))
err := tx.Put(Config, []byte(modeKey), []byte(m))

// Timestamp bytes + single byte.
mb := ResolveACLTypeToBinary(mode)
unixBytes := timestampToBytes(time.Now())
addressMode := append(mb.ToByteArray(), unixBytes...)
value := append([]byte{mb.ToByte()}, unixBytes...)
if err = tx.Put(PolicyTransactions, addressMode, value); err != nil {
return err
}

return err
})
}

Expand Down

0 comments on commit dae09e3

Please sign in to comment.