// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package abi

import (
	"bytes"
	"encoding/hex"
	"fmt"
	metatypes "github.com/CaduceusMetaverseProtocol/MetaTypes/types"
	"math"
	"math/big"
	"reflect"
	"strconv"
	"strings"
	"testing"
)

// TestPack tests the general pack/unpack tests in packing_test.go
func TestPack(t *testing.T) {
	t.Parallel()
	for i, test := range packUnpackTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			t.Parallel()
			encb, err := hex.DecodeString(test.packed)
			if err != nil {
				t.Fatalf("invalid hex %s: %v", test.packed, err)
			}
			inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def)
			inAbi, err := JSON(strings.NewReader(inDef))
			if err != nil {
				t.Fatalf("invalid ABI definition %s, %v", inDef, err)
			}
			var packed []byte
			packed, err = inAbi.Pack("method", test.unpacked)

			if err != nil {
				t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
			}
			if !reflect.DeepEqual(packed[4:], encb) {
				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
			}
		})
	}
}

func TestMethodPack(t *testing.T) {
	t.Parallel()
	abi, err := JSON(strings.NewReader(jsondata))
	if err != nil {
		t.Fatal(err)
	}

	sig := abi.Methods["slice"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)

	packed, err := abi.Pack("slice", []uint32{1, 2})
	if err != nil {
		t.Error(err)
	}

	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	var addrA, addrB = metatypes.Address{1}, metatypes.Address{2}
	sig = abi.Methods["sliceAddress"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{32}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrA[:], 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrB[:], 32)...)

	packed, err = abi.Pack("sliceAddress", []metatypes.Address{addrA, addrB})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	var addrC, addrD = metatypes.Address{3}, metatypes.Address{4}
	sig = abi.Methods["sliceMultiAddress"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{64}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{160}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrA[:], 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrB[:], 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrC[:], 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrD[:], 32)...)

	packed, err = abi.Pack("sliceMultiAddress", []metatypes.Address{addrA, addrB}, []metatypes.Address{addrC, addrD})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	sig = abi.Methods["slice256"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)

	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
	if err != nil {
		t.Error(err)
	}

	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
	sig = abi.Methods["nestedArray"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0xa0}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrC[:], 32)...)
	sig = append(sig, metatypes.LeftPadBytes(addrD[:], 32)...)
	packed, err = abi.Pack("nestedArray", a, []metatypes.Address{addrC, addrD})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	sig = abi.Methods["nestedArray2"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x20}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x40}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x80}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}

	sig = abi.Methods["nestedSlice"].ID
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x20}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x02}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0x40}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{0xa0}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{1}, 32)...)
	sig = append(sig, metatypes.LeftPadBytes([]byte{2}, 32)...)
	packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(packed, sig) {
		t.Errorf("expected %x got %x", sig, packed)
	}
}

func TestPackNumber(t *testing.T) {
	t.Parallel()
	tests := []struct {
		value  reflect.Value
		packed []byte
	}{
		// Protocol limits
		{reflect.ValueOf(0), metatypes.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
		{reflect.ValueOf(1), metatypes.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
		{reflect.ValueOf(-1), metatypes.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},

		// Type corner cases
		{reflect.ValueOf(uint8(math.MaxUint8)), metatypes.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
		{reflect.ValueOf(uint16(math.MaxUint16)), metatypes.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
		{reflect.ValueOf(uint32(math.MaxUint32)), metatypes.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
		{reflect.ValueOf(uint64(math.MaxUint64)), metatypes.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},

		{reflect.ValueOf(int8(math.MaxInt8)), metatypes.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
		{reflect.ValueOf(int16(math.MaxInt16)), metatypes.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
		{reflect.ValueOf(int32(math.MaxInt32)), metatypes.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
		{reflect.ValueOf(int64(math.MaxInt64)), metatypes.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},

		{reflect.ValueOf(int8(math.MinInt8)), metatypes.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
		{reflect.ValueOf(int16(math.MinInt16)), metatypes.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
		{reflect.ValueOf(int32(math.MinInt32)), metatypes.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
		{reflect.ValueOf(int64(math.MinInt64)), metatypes.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
	}
	for i, tt := range tests {
		packed := packNum(tt.value)
		if !bytes.Equal(packed, tt.packed) {
			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
		}
	}
}
