From 62b3e976ec8dd6b2d6f970fa95550604c2d01295 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Mon, 28 Oct 2024 14:01:02 +0900 Subject: [PATCH 1/5] test: policy lastPolicyTransactions test Signed-off-by: Ji Hwan --- zk/txpool/policy_test.go | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index 82456b66700..6ce8e815c2d 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -69,6 +69,31 @@ func newTestTxPoolDB(tb testing.TB, dir string) kv.RwDB { return txPoolDB } +func policyTransactionSliceEqual(a, b []PolicyTransaction) bool { + // Check if lengths are different + if len(a) != len(b) { + return false + } + + // Check each element, excluding timeTx + for i := range a { + if a[i].aclType != b[i].aclType { + return false + } + if ACLTypeBinary(a[i].operation) != ACLTypeBinary(b[i].operation) { + return false + } + if a[i].addr != b[i].addr { + return false + } + if a[i].policy != b[i].policy { + return false + } + } + + return true +} + func TestCheckDBsCreation(t *testing.T) { t.Parallel() @@ -409,6 +434,51 @@ func TestUpdatePolicies(t *testing.T) { }) } +func TestLastPolicyTransactions(t *testing.T) { + db := newTestACLDB(t, "") + ctx := context.Background() + + SetMode(ctx, db, BlocklistMode) + + // Create a test address and policy + addrOne := common.HexToAddress("0x1234567890abcdef") + policyOne := SendTx + + // addrTwo := common.HexToAddress("0xabcdef1234567890") + // policyTwo := SendTx + + // Add the policy to the ACL + require.NoError(t, AddPolicy(ctx, db, "blocklist", addrOne, policyOne)) + // require.NoError(t, AddPolicy(ctx, db, "blocklist", addrTwo, policyTwo)) + + // Create expected policyTransaction output and append to []PolicyTransaction + policyTransactionOne := PolicyTransaction{addr: common.HexToAddress("0x1234567890abcdef"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} + // policyTransactionTwo := PolicyTransaction{addr: common.HexToAddress("0xabcdef1234567890"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} + + var policyTransactionSlice []PolicyTransaction + policyTransactionSlice = append(policyTransactionSlice, policyTransactionOne) + // policyTransactionSlice = append(policyTransactionSlice, policyTransactionTwo) + + // Table driven test + var tests = []struct { + count int + want []PolicyTransaction + }{ + {1, policyTransactionSlice}, + } + for _, tt := range tests { + t.Run("LastPolicyTransactions", func(t *testing.T) { + ans, _ := LastPolicyTransactions(ctx, db, tt.count) + // if err != nil { + // t.Errorf("LastPolicyTransactions did not execute successfully: %v", err) + // } + if !policyTransactionSliceEqual(ans, tt.want) { + t.Errorf("got %v, want %v", ans, tt.want) + } + }) + } +} + func TestIsActionAllowed(t *testing.T) { db := newTestACLDB(t, "") ctx := context.Background() From 7aa55a6d5b1bb0bd86feb4f30900155b269c3318 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Mon, 28 Oct 2024 17:40:34 +0900 Subject: [PATCH 2/5] fix: LastPolicyTransactions with 0 length err Signed-off-by: Ji Hwan --- zk/txpool/policy.go | 2 +- zk/txpool/policy_test.go | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/zk/txpool/policy.go b/zk/txpool/policy.go index f4d23292fb5..242ebb15421 100644 --- a/zk/txpool/policy.go +++ b/zk/txpool/policy.go @@ -371,7 +371,7 @@ func LastPolicyTransactions(ctx context.Context, aclDB kv.RwDB, count int) ([]Po } pts = append(pts, pt) - for i := 0; i < count; i++ { + for i := 1; i < count; i++ { _, value, err = c.Prev() if err != nil { return err diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index 6ce8e815c2d..6211af69726 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -444,34 +444,41 @@ func TestLastPolicyTransactions(t *testing.T) { addrOne := common.HexToAddress("0x1234567890abcdef") policyOne := SendTx - // addrTwo := common.HexToAddress("0xabcdef1234567890") - // policyTwo := SendTx + addrTwo := common.HexToAddress("0xabcdef1234567890") + policyTwo := SendTx // Add the policy to the ACL require.NoError(t, AddPolicy(ctx, db, "blocklist", addrOne, policyOne)) - // require.NoError(t, AddPolicy(ctx, db, "blocklist", addrTwo, policyTwo)) + require.NoError(t, AddPolicy(ctx, db, "blocklist", addrTwo, policyTwo)) // Create expected policyTransaction output and append to []PolicyTransaction policyTransactionOne := PolicyTransaction{addr: common.HexToAddress("0x1234567890abcdef"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} - // policyTransactionTwo := PolicyTransaction{addr: common.HexToAddress("0xabcdef1234567890"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} + policyTransactionTwo := PolicyTransaction{addr: common.HexToAddress("0xabcdef1234567890"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} - var policyTransactionSlice []PolicyTransaction - policyTransactionSlice = append(policyTransactionSlice, policyTransactionOne) - // policyTransactionSlice = append(policyTransactionSlice, policyTransactionTwo) + // LastPolicyTransactions seems to append in reverse order than this test function. So the order of elements is also reversed + // Single element in PolicyTransaction slice + var policyTransactionSliceSingle []PolicyTransaction + policyTransactionSliceSingle = append(policyTransactionSliceSingle, policyTransactionTwo) + + // Two elements in PolicyTransaction slice + var policyTransactionSliceDouble []PolicyTransaction + policyTransactionSliceDouble = append(policyTransactionSliceDouble, policyTransactionTwo) + policyTransactionSliceDouble = append(policyTransactionSliceDouble, policyTransactionOne) // Table driven test var tests = []struct { count int want []PolicyTransaction }{ - {1, policyTransactionSlice}, + {1, policyTransactionSliceSingle}, + {2, policyTransactionSliceDouble}, } for _, tt := range tests { t.Run("LastPolicyTransactions", func(t *testing.T) { - ans, _ := LastPolicyTransactions(ctx, db, tt.count) - // if err != nil { - // t.Errorf("LastPolicyTransactions did not execute successfully: %v", err) - // } + ans, err := LastPolicyTransactions(ctx, db, tt.count) + if err != nil { + t.Errorf("LastPolicyTransactions did not execute successfully: %v", err) + } if !policyTransactionSliceEqual(ans, tt.want) { t.Errorf("got %v, want %v", ans, tt.want) } From 4239217f033a4553a495920b3bfb8b40b45f3445 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Mon, 28 Oct 2024 17:54:35 +0900 Subject: [PATCH 3/5] fix: count 0 handling for LastPolicyTransactions Signed-off-by: Ji Hwan --- zk/txpool/policy.go | 3 +++ zk/txpool/policy_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/zk/txpool/policy.go b/zk/txpool/policy.go index 242ebb15421..336f79245b6 100644 --- a/zk/txpool/policy.go +++ b/zk/txpool/policy.go @@ -355,6 +355,9 @@ func bytesToTimestamp(b []byte) time.Time { func LastPolicyTransactions(ctx context.Context, aclDB kv.RwDB, count int) ([]PolicyTransaction, error) { var pts []PolicyTransaction err := aclDB.View(ctx, func(tx kv.Tx) error { + if count == 0 { + return nil + } c, err := tx.Cursor(PolicyTransactions) if err != nil { return err diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index 6211af69726..ce6ef1d81ce 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -456,6 +456,9 @@ func TestLastPolicyTransactions(t *testing.T) { policyTransactionTwo := PolicyTransaction{addr: common.HexToAddress("0xabcdef1234567890"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} // LastPolicyTransactions seems to append in reverse order than this test function. So the order of elements is also reversed + // Single element in PolicyTransaction slice + var policyTransactionSliceNone []PolicyTransaction + // Single element in PolicyTransaction slice var policyTransactionSliceSingle []PolicyTransaction policyTransactionSliceSingle = append(policyTransactionSliceSingle, policyTransactionTwo) @@ -470,6 +473,7 @@ func TestLastPolicyTransactions(t *testing.T) { count int want []PolicyTransaction }{ + {0, policyTransactionSliceNone}, {1, policyTransactionSliceSingle}, {2, policyTransactionSliceDouble}, } From 9d96f3fff1774328a23aa2fa12355dc468041ce3 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Tue, 29 Oct 2024 10:48:26 +0900 Subject: [PATCH 4/5] test: fix TestLastPolicyTransactions with introduction of new Operation, ModeChange Signed-off-by: Ji Hwan --- zk/txpool/policy.go | 2 ++ zk/txpool/policy_test.go | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/zk/txpool/policy.go b/zk/txpool/policy.go index 975c31a3f18..5755928eb37 100644 --- a/zk/txpool/policy.go +++ b/zk/txpool/policy.go @@ -45,6 +45,8 @@ func OperationFromByte(b byte) Operation { return Remove case byte(Update): return Update + case byte(ModeChange): + return ModeChange default: return Add // Default or error handling can be added here } diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index 746751942a2..76d30e2b976 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -441,6 +441,9 @@ func TestLastPolicyTransactions(t *testing.T) { SetMode(ctx, db, BlocklistMode) // Create a test address and policy + addrInit := common.HexToAddress("0x0000000000000000") + policyInit := SendTx + addrOne := common.HexToAddress("0x1234567890abcdef") policyOne := SendTx @@ -448,10 +451,12 @@ func TestLastPolicyTransactions(t *testing.T) { policyTwo := SendTx // Add the policy to the ACL + require.NoError(t, AddPolicy(ctx, db, "blocklist", addrInit, policyInit)) require.NoError(t, AddPolicy(ctx, db, "blocklist", addrOne, policyOne)) require.NoError(t, AddPolicy(ctx, db, "blocklist", addrTwo, policyTwo)) // Create expected policyTransaction output and append to []PolicyTransaction + policyTransactionInit := PolicyTransaction{addr: common.HexToAddress("0x0000000000000000"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(ModeChange.ToByte())} policyTransactionOne := PolicyTransaction{addr: common.HexToAddress("0x1234567890abcdef"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} policyTransactionTwo := PolicyTransaction{addr: common.HexToAddress("0xabcdef1234567890"), aclType: ResolveACLTypeToBinary("blocklist"), policy: Policy(SendTx.ToByte()), operation: Operation(Add.ToByte())} @@ -459,14 +464,20 @@ func TestLastPolicyTransactions(t *testing.T) { // No element in PolicyTransaction slice var policyTransactionSliceNone []PolicyTransaction - // Single element in PolicyTransaction slice + // Single element in PolicyTransaction slice, always starting with policyTransactionInit var policyTransactionSliceSingle []PolicyTransaction - policyTransactionSliceSingle = append(policyTransactionSliceSingle, policyTransactionTwo) + policyTransactionSliceSingle = append(policyTransactionSliceSingle, policyTransactionInit) - // Two elements in PolicyTransaction slice + // Two elements in PolicyTransaction slice, always starting with policyTransactionInit var policyTransactionSliceDouble []PolicyTransaction + policyTransactionSliceDouble = append(policyTransactionSliceDouble, policyTransactionInit) policyTransactionSliceDouble = append(policyTransactionSliceDouble, policyTransactionTwo) - policyTransactionSliceDouble = append(policyTransactionSliceDouble, policyTransactionOne) + + // Three elements in PolicyTransaction slice, always starting with policyTransactionInit + var policyTransactionSliceTriple []PolicyTransaction + policyTransactionSliceTriple = append(policyTransactionSliceTriple, policyTransactionInit) + policyTransactionSliceTriple = append(policyTransactionSliceTriple, policyTransactionTwo) + policyTransactionSliceTriple = append(policyTransactionSliceTriple, policyTransactionOne) // Table driven test var tests = []struct { @@ -476,6 +487,7 @@ func TestLastPolicyTransactions(t *testing.T) { {0, policyTransactionSliceNone}, {1, policyTransactionSliceSingle}, {2, policyTransactionSliceDouble}, + {3, policyTransactionSliceTriple}, } for _, tt := range tests { t.Run("LastPolicyTransactions", func(t *testing.T) { From cb938486194c19857f88d143039f7680389ae9b5 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Tue, 29 Oct 2024 13:45:34 +0900 Subject: [PATCH 5/5] test: add test for policyMapping Signed-off-by: Ji Hwan --- zk/txpool/policy_test.go | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index 82456b66700..66fae9cc9c0 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -189,6 +189,51 @@ func TestRemovePolicy(t *testing.T) { }) } +func TestPolicyMapping(t *testing.T) { + // All policies + var policiesAll []byte + var pListAll []Policy + policiesAll = append(policiesAll, SendTx.ToByte()) + policiesAll = append(policiesAll, Deploy.ToByte()) + pListAll = append(pListAll, SendTx) + pListAll = append(pListAll, Deploy) + + // Only sendTx policy + var policiesSendTx []byte + var pListSendTx []Policy + policiesSendTx = append(policiesSendTx, SendTx.ToByte()) + pListSendTx = append(pListSendTx, SendTx) + + // Only deploy policy + var policiesDeploy []byte + var pListDeploy []Policy + policiesDeploy = append(policiesDeploy, Deploy.ToByte()) + pListDeploy = append(pListDeploy, Deploy) + + // No policy + var policiesNone []byte + var pListNone []Policy + + var tests = []struct { + policies []byte + pList []Policy + want string + }{ + {policiesAll, pListAll, "\tsendTx: true\n\tdeploy: true"}, + {policiesSendTx, pListSendTx, "\tsendTx: true"}, + {policiesDeploy, pListDeploy, "\tdeploy: true"}, + {policiesNone, pListNone, ""}, + } + for _, tt := range tests { + t.Run("PolicyMapping", func(t *testing.T) { + ans := policyMapping(tt.policies, tt.pList) + if ans != tt.want { + t.Errorf("got %v, want %v", ans, tt.want) + } + }) + } +} + func TestAddPolicy(t *testing.T) { t.Parallel()