diff --git a/thor/blocklist_test.go b/thor/blocklist_test.go new file mode 100644 index 000000000..6ca4e875d --- /dev/null +++ b/thor/blocklist_test.go @@ -0,0 +1,51 @@ +package thor + +import "testing" + +func TestBlocklistInitialization(t *testing.T) { + knownBlocked := []string{ + "0x4427be8010dd870395975a8fbaa7afa9439b5332", + "0xa99bd128a454728b509ab0e41499a6d1ea0d3416", + } + + for _, addrStr := range knownBlocked { + addr := MustParseAddress(addrStr) + if !IsOriginBlocked(addr) { + t.Errorf("Address %s is expected to be blocked, but IsOriginBlocked returned false", addrStr) + } + } + + // Address known not to be in the blocklist + knownUnblocked := "0x000000000000000000000000000000000000dead" + addr := MustParseAddress(knownUnblocked) + if IsOriginBlocked(addr) { + t.Errorf("Address %s is expected to be unblocked, but IsOriginBlocked returned true", knownUnblocked) + } +} + +func TestMockBlocklist(t *testing.T) { + // Setup mock blocklist with a new set of addresses + mockAddresses := []string{ + "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", + "0xfeedbeeffeedbeeffeedbeeffeedbeeffeedbeef", + } + MockBlocklist(mockAddresses) + + // Test to ensure the mock blocklist is now in effect + tests := []struct { + address string + blocked bool + }{ + {mockAddresses[0], true}, + {mockAddresses[1], true}, + // An address known to be in the original blocklist, expecting it to be unblocked after mocking + {"0x4427be8010dd870395975a8fbaa7afa9439b5332", false}, + } + + for _, tt := range tests { + addr := MustParseAddress(tt.address) + if IsOriginBlocked(addr) != tt.blocked { + t.Errorf("MockBlocklist failed for %v: expected blocked=%v, got blocked=%v", tt.address, tt.blocked, !tt.blocked) + } + } +} diff --git a/thor/fork_config_test.go b/thor/fork_config_test.go new file mode 100644 index 000000000..3fb05b968 --- /dev/null +++ b/thor/fork_config_test.go @@ -0,0 +1,54 @@ +package thor + +import ( + "math" + "testing" +) + +// TestForkConfigString verifies that the String method returns expected values. +func TestForkConfigString(t *testing.T) { + fc := ForkConfig{ + VIP191: 1, + ETH_CONST: math.MaxUint32, + BLOCKLIST: 2, + ETH_IST: math.MaxUint32, + VIP214: math.MaxUint32, + FINALITY: math.MaxUint32, + } + + expectedStr := "VIP191: #1, BLOCKLIST: #2" + if fc.String() != expectedStr { + t.Errorf("ForkConfig.String() = %v, want %v", fc.String(), expectedStr) + } +} + +// TestNoFork verifies the NoFork variable is correctly set up. +func TestNoFork(t *testing.T) { + if NoFork.VIP191 != math.MaxUint32 || NoFork.BLOCKLIST != math.MaxUint32 { + t.Errorf("NoFork does not correctly represent a configuration with no forks") + } +} + +// TestGetForkConfig checks retrieval of fork configurations for known genesis IDs. +func TestGetForkConfig(t *testing.T) { + // You'll need to adjust these based on the actual genesis IDs and expected configurations + mainnetID := MustParseBytes32("0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a") + testnetID := MustParseBytes32("0x000000000b2bce3c70bc649a02749e8687721b09ed2e15997f466536b20bb127") + unknownID := MustParseBytes32("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + + tests := []struct { + id Bytes32 + expectFound bool + }{ + {mainnetID, true}, + {testnetID, true}, + {unknownID, false}, // Expect no config for unknown ID + } + + for _, tt := range tests { + config := GetForkConfig(tt.id) + if (config != ForkConfig{}) != tt.expectFound { + t.Errorf("GetForkConfig(%v) found = %v, want %v", tt.id, !tt.expectFound, tt.expectFound) + } + } +} diff --git a/thor/hash_test.go b/thor/hash_test.go index 8917f1535..68e2afc4e 100644 --- a/thor/hash_test.go +++ b/thor/hash_test.go @@ -11,6 +11,7 @@ import ( "math/rand" "testing" + "github.com/stretchr/testify/assert" "github.com/vechain/thor/v2/thor" "golang.org/x/crypto/sha3" ) @@ -58,3 +59,69 @@ func BenchmarkBlake2b(b *testing.B) { } }) } + +func TestNewBlake2b(t *testing.T) { + hasher := thor.NewBlake2b() + if hasher == nil { + t.Error("NewBlake2b returned nil") + } + + testString := "VeChainThor" + hasher.Write([]byte(testString)) + sum := hasher.Sum(nil) + if len(sum) != 32 { + t.Errorf("Expected BLAKE2b-256 hash length of 32, got %d", len(sum)) + } +} + +func TestBlake2b(t *testing.T) { + singleData := []byte("data") + multipleData := [][]byte{[]byte("multi"), []byte("ple"), []byte("data")} + + // Single slice of data + singleHash := thor.Blake2b(singleData) + if len(singleHash) != 32 { + t.Errorf("Expected hash length of 32, got %d", len(singleHash)) + } + + // Multiple slices of data + multiHash := thor.Blake2b(multipleData...) + if len(multiHash) != 32 { + t.Errorf("Expected hash length of 32, got %d", len(multiHash)) + } + + // Check if different data results in different hashes + if singleHash == multiHash { + t.Error("Expected different hashes for different data") + } +} + +func TestBlake2bFn(t *testing.T) { + h := thor.Blake2bFn(func(w io.Writer) { + w.Write([]byte("custom writer")) + }) + + assert.Equal(t, thor.Blake2b([]byte("custom writer")), h) +} + +func TestKeccak256(t *testing.T) { + singleData := []byte("data") + multipleData := [][]byte{[]byte("multi"), []byte("ple"), []byte("data")} + + // Single slice of data + singleHash := thor.Keccak256(singleData) + if len(singleHash) != 32 { + t.Errorf("Expected hash length of 32, got %d", len(singleHash)) + } + + // Multiple slices of data + multiHash := thor.Keccak256(multipleData...) + if len(multiHash) != 32 { + t.Errorf("Expected hash length of 32, got %d", len(multiHash)) + } + + // Check if different data results in different hashes + if singleHash == multiHash { + t.Error("Expected different hashes for different data") + } +} diff --git a/thor/receipt_correction_test.go b/thor/receipt_correction_test.go new file mode 100644 index 000000000..f261dd11b --- /dev/null +++ b/thor/receipt_correction_test.go @@ -0,0 +1,26 @@ +package thor + +import ( + "testing" +) + +// TestLoadCorrectReceiptsRootsForKeyValue tests a specific key/value pair in the map returned by LoadCorrectReceiptsRoots +func TestLoadCorrectReceiptsRootsForKeyValue(t *testing.T) { + // Define the key/value pair you expect to find + expectedKey := "0x000c2c63d845188f8390de84d1364b59cd2890f276452762f9ab0761ecc93069" + expectedValue := "0xe467857991d0e04da9b2e0365110b49f198febf9d3fa8413c536527f857bd246" + + // Load the map + actualMap := LoadCorrectReceiptsRoots() + + // Check if the key exists + actualValue, exists := actualMap[expectedKey] + if !exists { + t.Fatalf("Expected key %s not found in the map", expectedKey) + } + + // Check if the value matches + if expectedValue != actualValue { + t.Errorf("For key %s, expected value %s, got %s", expectedKey, expectedValue, actualValue) + } +}