github.com/ConsenSys/Quorum@v20.10.0+incompatible/graphql/graphql_test.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package graphql 18 19 import ( 20 "context" 21 "math/big" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/core/types" 26 "github.com/ethereum/go-ethereum/private" 27 "github.com/ethereum/go-ethereum/private/engine" 28 "github.com/ethereum/go-ethereum/private/engine/notinuse" 29 ) 30 31 func TestBuildSchema(t *testing.T) { 32 // Make sure the schema can be parsed and matched up to the object model. 33 if _, err := newHandler(nil); err != nil { 34 t.Errorf("Could not construct GraphQL handler: %v", err) 35 } 36 } 37 38 // Quorum 39 // Test Quorum specific GraphQL schema for private transaction 40 func TestQuorumSchema(t *testing.T) { 41 saved := private.P 42 defer func() { 43 private.P = saved 44 }() 45 arbitraryPayloadHash := common.BytesToEncryptedPayloadHash([]byte("arbitrary key")) 46 private.P = &StubPrivateTransactionManager{ 47 responses: map[common.EncryptedPayloadHash][]interface{}{ 48 arbitraryPayloadHash: { 49 []byte("private payload"), // equals to 0x70726976617465207061796c6f6164 after converting to bytes 50 nil, 51 }, 52 }, 53 } 54 // Test private transaction 55 privateTx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), arbitraryPayloadHash.Bytes()) 56 privateTx.SetPrivate() 57 privateTxQuery := &Transaction{tx: privateTx} 58 isPrivate, err := privateTxQuery.IsPrivate(context.Background()) 59 if err != nil { 60 t.Fatalf("Expect no error: %v", err) 61 } 62 if !*isPrivate { 63 t.Fatalf("Expect isPrivate to be true for private TX") 64 } 65 privateInputData, err := privateTxQuery.PrivateInputData(context.Background()) 66 if err != nil { 67 t.Fatalf("Expect no error: %v", err) 68 } 69 if privateInputData.String() != "0x70726976617465207061796c6f6164" { 70 t.Fatalf("Expect privateInputData to be: \"0x70726976617465207061796c6f6164\" for private TX, actual: %v", privateInputData.String()) 71 } 72 // Test public transaction 73 publicTx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 0, big.NewInt(0), []byte("key")) 74 publicTxQuery := &Transaction{tx: publicTx} 75 isPrivate, err = publicTxQuery.IsPrivate(context.Background()) 76 if err != nil { 77 t.Fatalf("Expect no error: %v", err) 78 } 79 if *isPrivate { 80 t.Fatalf("Expect isPrivate to be false for public TX") 81 } 82 privateInputData, err = publicTxQuery.PrivateInputData(context.Background()) 83 if err != nil { 84 t.Fatalf("Expect no error: %v", err) 85 } 86 if privateInputData.String() != "0x" { 87 t.Fatalf("Expect privateInputData to be: \"0x\" for public TX, actual: %v", privateInputData.String()) 88 } 89 } 90 91 type StubPrivateTransactionManager struct { 92 notinuse.PrivateTransactionManager 93 responses map[common.EncryptedPayloadHash][]interface{} 94 } 95 96 func (spm *StubPrivateTransactionManager) HasFeature(f engine.PrivateTransactionManagerFeature) bool { 97 return true 98 } 99 100 func (spm *StubPrivateTransactionManager) Receive(txHash common.EncryptedPayloadHash) ([]byte, *engine.ExtraMetadata, error) { 101 res := spm.responses[txHash] 102 if err, ok := res[1].(error); ok { 103 return nil, nil, err 104 } 105 if ret, ok := res[0].([]byte); ok { 106 return ret, &engine.ExtraMetadata{ 107 PrivacyFlag: engine.PrivacyFlagStandardPrivate, 108 }, nil 109 } 110 return nil, nil, nil 111 } 112 113 func (spm *StubPrivateTransactionManager) ReceiveRaw(data common.EncryptedPayloadHash) ([]byte, *engine.ExtraMetadata, error) { 114 return spm.Receive(data) 115 }