// (c) 2019-2020, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. package platformvm import ( "bytes" "testing" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/json" ) func TestBuildGenesis(t *testing.T) { expected := []byte{ 0x00, 0x00, 0x00, 0x01, 0x01, 0x5c, 0xce, 0x6c, 0x55, 0xd6, 0xb5, 0x09, 0x84, 0x5c, 0x8c, 0x4e, 0x30, 0xbe, 0xd9, 0x8d, 0x39, 0x1a, 0xe7, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x01, 0x5c, 0xce, 0x6c, 0x55, 0xd6, 0xb5, 0x09, 0x84, 0x5c, 0x8c, 0x4e, 0x30, 0xbe, 0xd9, 0x8d, 0x39, 0x1a, 0xe7, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3a, 0xde, 0x68, 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5c, 0xce, 0x6c, 0x55, 0xd6, 0xb5, 0x09, 0x84, 0x5c, 0x8c, 0x4e, 0x30, 0xbe, 0xd9, 0x8d, 0x39, 0x1a, 0xe7, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x4d, 0x79, 0x20, 0x46, 0x61, 0x76, 0x6f, 0x72, 0x69, 0x74, 0x65, 0x20, 0x45, 0x70, 0x69, 0x73, 0x6f, 0x64, 0x65, 0x53, 0x6f, 0x75, 0x74, 0x68, 0x20, 0x50, 0x61, 0x72, 0x6b, 0x20, 0x65, 0x70, 0x69, 0x73, 0x6f, 0x64, 0x65, 0x20, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x53, 0x63, 0x6f, 0x74, 0x74, 0x20, 0x54, 0x65, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20, 0x64, 0x69, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, } addr, _ := ids.ShortFromString("8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z") genesisData := formatting.CB58{} genesisData.FromString("CGgRrQ3nws7RRMGyDV59cetJBAwmsmDyCSgku") vmID, _ := ids.FromString("dkFD29iYU9e9jah2nrnksTWJUy2VVpg5Lnqd7nQqvCJgR26H4") account := APIAccount{ Address: addr, Balance: 123456789, } weight := json.Uint64(987654321) validator := APIDefaultSubnetValidator{ APIValidator: APIValidator{ EndTime: 15, Weight: &weight, ID: addr, }, Destination: addr, } chains := APIChain{ GenesisData: genesisData, VMID: vmID, Name: "My Favorite Episode", } args := BuildGenesisArgs{ Accounts: []APIAccount{ account, }, Validators: []APIDefaultSubnetValidator{ validator, }, Chains: []APIChain{ chains, }, Time: 5, } reply := BuildGenesisReply{} ss := StaticService{} if err := ss.BuildGenesis(nil, &args, &reply); err != nil { t.Fatal(err) } if !bytes.Equal(reply.Bytes.Bytes, expected) { t.Fatalf("StaticService.BuildGenesis:\nReturned:\n%s\nExpected:\n%s", formatting.DumpBytes{Bytes: reply.Bytes.Bytes}, formatting.DumpBytes{Bytes: expected}) } } func TestBuildGenesisInvalidAccountBalance(t *testing.T) { id, _ := ids.ShortFromString("8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z") account := APIAccount{ Address: id, Balance: 0, } weight := json.Uint64(987654321) validator := APIDefaultSubnetValidator{ APIValidator: APIValidator{ EndTime: 15, Weight: &weight, ID: id, }, Destination: id, } args := BuildGenesisArgs{ Accounts: []APIAccount{ account, }, Validators: []APIDefaultSubnetValidator{ validator, }, Time: 5, } reply := BuildGenesisReply{} ss := StaticService{} if err := ss.BuildGenesis(nil, &args, &reply); err == nil { t.Fatalf("Should have errored due to an invalid balance") } } func TestBuildGenesisInvalidAmount(t *testing.T) { id, _ := ids.ShortFromString("8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z") account := APIAccount{ Address: id, Balance: 123456789, } weight := json.Uint64(0) validator := APIDefaultSubnetValidator{ APIValidator: APIValidator{ StartTime: 0, EndTime: 15, Weight: &weight, ID: id, }, Destination: id, } args := BuildGenesisArgs{ Accounts: []APIAccount{ account, }, Validators: []APIDefaultSubnetValidator{ validator, }, Time: 5, } reply := BuildGenesisReply{} ss := StaticService{} if err := ss.BuildGenesis(nil, &args, &reply); err == nil { t.Fatalf("Should have errored due to an invalid amount") } } func TestBuildGenesisInvalidEndtime(t *testing.T) { id, _ := ids.ShortFromString("8CrVPQZ4VSqgL8zTdvL14G8HqAfrBr4z") account := APIAccount{ Address: id, Balance: 123456789, } weight := json.Uint64(987654321) validator := APIDefaultSubnetValidator{ APIValidator: APIValidator{ StartTime: 0, EndTime: 5, Weight: &weight, ID: id, }, Destination: id, } args := BuildGenesisArgs{ Accounts: []APIAccount{ account, }, Validators: []APIDefaultSubnetValidator{ validator, }, Time: 5, } reply := BuildGenesisReply{} ss := StaticService{} if err := ss.BuildGenesis(nil, &args, &reply); err == nil { t.Fatalf("Should have errored due to an invalid end time") } }