github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/neatapi/api_test.go (about)

     1  package neatapi
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	neatAbi "github.com/neatlab/neatio/neatabi/abi"
    10  	"github.com/neatlab/neatio/utilities/common"
    11  	"github.com/neatlab/neatio/utilities/common/hexutil"
    12  	"github.com/neatlab/neatio/utilities/common/math"
    13  	"github.com/neatlab/neatio/utilities/crypto"
    14  )
    15  
    16  type MethoadParams struct {
    17  	Input   string
    18  	Args    interface{}
    19  	FunType neatAbi.FunctionType
    20  }
    21  
    22  var inputHex = "0x91e8537e000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000044c696b6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001068747470733a2f2f6c696b652e636f6d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104531353733453236384138313835303300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c4d792076616c696461746f720000000000000000000000000000000000000000"
    23  
    24  var inputArray = []*MethoadParams{
    25  	{
    26  		Input:   "0x49339f0f494e5433437046756b32634a31746539575a563177385933776b51436341355a",
    27  		Args:    neatAbi.DelegateArgs{},
    28  		FunType: neatAbi.Delegate,
    29  	},
    30  	{
    31  		Input:   "0x91e8537e000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000044c696b6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001068747470733a2f2f6c696b652e636f6d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104531353733453236384138313835303300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c4d792076616c696461746f720000000000000000000000000000000000000000",
    32  		Args:    neatAbi.EditValidatorArgs{},
    33  		FunType: neatAbi.EditValidator,
    34  	},
    35  }
    36  
    37  func TestMethodId(t *testing.T) {
    38  	method := neatAbi.ChainABI.Methods[neatAbi.Delegate.String()]
    39  	methdid := method.ID()
    40  
    41  	fmt.Printf("method id, %v", hexutil.Encode(methdid))
    42  }
    43  
    44  func TestABI_UnpackMethodInputs(t *testing.T) {
    45  
    46  	//inputByte, err := hexutil.Decode(inputHex)
    47  	//if err != nil {
    48  	//	t.Error(err)
    49  	//}
    50  	//
    51  	//function, err := neatAbi.FunctionTypeFromId(inputByte[:4])
    52  	//if err != nil {
    53  	//	t.Error(err)
    54  	//}
    55  	//fmt.Printf("function=%v\n", function)
    56  	//
    57  	////var args neatAbi.CandidateArgs
    58  	//var args neatAbi.DelegateArgs
    59  	////var args neatAbi.EditValidatorArgs
    60  	//
    61  	//err = neatAbi.ChainABI.UnpackMethodInputs(&args, function.String(), inputByte[4:])
    62  	//if err != nil {
    63  	//	t.Error(err)
    64  	//}
    65  	//fmt.Printf("unpack method %v\n", args.Moniker)
    66  	//fmt.Printf("unpack website %v\n", args.Website)
    67  	//fmt.Printf("unpack identify %v\n", args.Identity)
    68  	//fmt.Printf("unpack details %v\n", args.Details)
    69  
    70  	for _, v := range inputArray {
    71  		inputByte, err := hexutil.Decode(v.Input)
    72  		if err != nil {
    73  			t.Error(err)
    74  		}
    75  
    76  		err = checkFunType(inputByte, v.FunType)
    77  		if err != nil {
    78  			t.Error(err)
    79  		}
    80  
    81  		unpackArgs, err := unpackMethod(inputByte, v.FunType)
    82  		if err != nil {
    83  			t.Error(err)
    84  		} else {
    85  			t.Logf("unpack %v success,  args %v", v.FunType.String(), unpackArgs.Candidate.String())
    86  		}
    87  
    88  	}
    89  }
    90  
    91  func checkFunType(input []byte, funType neatAbi.FunctionType) error {
    92  	function, err := neatAbi.FunctionTypeFromId(input[:4])
    93  
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	if !bytes.Equal([]byte(function.String()), []byte(funType.String())) {
    99  		return fmt.Errorf("method mismatch want %v, but %v", funType.String(), function.String())
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func unpackMethod(input []byte, funType neatAbi.FunctionType) (unpackArgs neatAbi.DelegateArgs, err error) {
   106  	var args neatAbi.DelegateArgs
   107  
   108  	err = neatAbi.ChainABI.UnpackMethodInputs(&args, funType.String(), input[4:])
   109  	if err != nil {
   110  		return unpackArgs, err
   111  	}
   112  
   113  	return args, nil
   114  
   115  }
   116  
   117  var FromAddr = "NEAToB7XfdxkFBotPapruktdRNkALpUT"
   118  var PubKey = "0x618CEAF6AD449B826E2521222A94426B82800202332251F0929EC47B36A647C65E00D2EA34C07A8EF7953C2E1555D8321449423CCFB0B64BB13090E7A433114D68F1C1891BAA20101E5CC8E2B10E207F5D21D1A1116547E1EED5E92FDFE4F5E58119C5267B82AE06BBA5016827396B74E1ECDCC3801746242CA24C7749EB2F88"
   119  var Amount = "0x152d02c7e14af68000000"
   120  var Salt = "like"
   121  var VoteHash1 = "0xc6335e23dd8ba330b2d3c34acdeb2dfd0b07d30dfc2d5f9ca1b0d62e147788f0" // false
   122  var VoteHash2 = "0xa431ab9cb5d2750faeed74945d10c69372b938c2470d5b140de29f4d4aa22025" // true
   123  var VoteHash3 = "0xb2aa67b3cf56dcb41097d72024962c03d4fba2a9892cc37e348243b85bf58c27" // false
   124  
   125  func TestVoteHash(t *testing.T) {
   126  	pubKey, err := hexutil.Decode(PubKey)
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	byteData := [][]byte{
   131  		[]byte(FromAddr),
   132  		pubKey,
   133  		common.LeftPadBytes(math.MustParseBig256("1600000000000000000000000").Bytes(), 1),
   134  		[]byte(Salt),
   135  	}
   136  
   137  	hash := crypto.Keccak256Hash(concatCopyPreAllocate(byteData))
   138  	fmt.Printf("vote hash %v\n", hash.String())
   139  
   140  }
   141  
   142  func TestGoTime(t *testing.T) {
   143  	nowTime := time.Now().Unix()
   144  	fmt.Printf("now %v\n", nowTime)
   145  
   146  	d := 24 * time.Hour
   147  	fmt.Printf("duration %v\n", d)
   148  	fmt.Printf("duration string %v\n", d.String())
   149  	fmt.Printf("duration seconds %v\n", d.Seconds())
   150  }