github.com/Night-mk/quorum@v21.1.0+incompatible/extension/backend_test.go (about) 1 package extension 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/ethereum/go-ethereum" 8 "github.com/ethereum/go-ethereum/accounts" 9 "github.com/ethereum/go-ethereum/common" 10 "github.com/ethereum/go-ethereum/common/hexutil" 11 "github.com/ethereum/go-ethereum/core/types" 12 "github.com/ethereum/go-ethereum/event" 13 "github.com/ethereum/go-ethereum/internal/ethapi" 14 ) 15 16 type MockBackend struct { 17 wallets []accounts.Wallet 18 } 19 20 func (backend *MockBackend) Wallets() []accounts.Wallet { 21 return backend.wallets 22 } 23 24 func (backend *MockBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { 25 return nil 26 } 27 28 type MockWallet struct { 29 isContained bool 30 } 31 32 func (wallet *MockWallet) URL() accounts.URL { panic("not implemented") } 33 34 func (wallet *MockWallet) Status() (string, error) { panic("not implemented") } 35 36 func (wallet *MockWallet) Open(passphrase string) error { panic("not implemented") } 37 38 func (wallet *MockWallet) Close() error { panic("not implemented") } 39 40 func (wallet *MockWallet) Accounts() []accounts.Account { panic("not implemented") } 41 42 func (wallet *MockWallet) Contains(account accounts.Account) bool { return wallet.isContained } 43 44 func (wallet *MockWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { 45 panic("not implemented") 46 } 47 48 func (wallet *MockWallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) { 49 panic("not implemented") 50 } 51 52 func (wallet *MockWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { 53 panic("not implemented") 54 } 55 56 func (wallet *MockWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) { 57 panic("not implemented") 58 } 59 60 func (wallet *MockWallet) SignText(account accounts.Account, text []byte) ([]byte, error) { 61 panic("not implemented") 62 } 63 64 func (wallet *MockWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 65 panic("not implemented") 66 } 67 68 func (wallet *MockWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 69 panic("not implemented") 70 } 71 72 func (wallet *MockWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) { 73 panic("not implemented") 74 } 75 76 func TestGenerateTransactionOptionsErrorsWhenNoPrivateParticipants(t *testing.T) { 77 sendTxArgs := ethapi.SendTxArgs{ 78 From: common.Address{}, 79 } 80 81 mockBackend := MockBackend{} 82 accountManager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, &mockBackend) 83 84 service := &PrivacyService{ 85 accountManager: accountManager, 86 } 87 88 _, err := service.GenerateTransactOptions(sendTxArgs) 89 if err == nil { 90 t.Errorf("expected err to not be nil") 91 return 92 } 93 94 expectedErr := "must specify private participants" 95 if err.Error() != expectedErr { 96 t.Errorf("expected err to be '%s', but was '%s'", expectedErr, err.Error()) 97 } 98 } 99 100 func TestGenerateTransactionOptionsErrorsWhenAccountNotFound(t *testing.T) { 101 privateTxArgs := ethapi.PrivateTxArgs{PrivateFor: []string{}} 102 sendTxArgs := ethapi.SendTxArgs{ 103 From: common.Address{}, 104 PrivateTxArgs: privateTxArgs, 105 } 106 107 mockBackend := MockBackend{} 108 accountManager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, &mockBackend) 109 110 service := &PrivacyService{ 111 accountManager: accountManager, 112 } 113 114 _, err := service.GenerateTransactOptions(sendTxArgs) 115 if err == nil { 116 t.Errorf("expected err to not be nil") 117 return 118 } 119 120 expectedErr := "no wallet found for account 0x0000000000000000000000000000000000000000" 121 if err.Error() != expectedErr { 122 t.Errorf("expected err to be '%s', but was '%s'", expectedErr, err.Error()) 123 } 124 } 125 126 func TestGenerateTransactionOptionsGivesDefaults(t *testing.T) { 127 from := common.HexToAddress("0x2222222222222222222222222222222222222222") 128 129 privateTxArgs := ethapi.PrivateTxArgs{PrivateFor: []string{"privateFor1", "privateFor2"}, PrivateFrom: "privateFrom"} 130 131 sendTxArgs := ethapi.SendTxArgs{ 132 From: from, 133 PrivateTxArgs: privateTxArgs, 134 } 135 136 mockWallet := &MockWallet{isContained: true} 137 mockBackend := MockBackend{wallets: []accounts.Wallet{mockWallet}} 138 accountManager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, &mockBackend) 139 service := &PrivacyService{ 140 accountManager: accountManager, 141 } 142 143 generatedOptions, err := service.GenerateTransactOptions(sendTxArgs) 144 if err != nil { 145 t.Errorf("expected err to be '%s', but was '%s'", "nil", err.Error()) 146 return 147 } 148 149 if generatedOptions.PrivateFrom != sendTxArgs.PrivateFrom { 150 t.Errorf("expected PrivateFrom to be '%s', but was '%s'", sendTxArgs.PrivateFrom, generatedOptions.PrivateFrom) 151 return 152 } 153 154 if len(generatedOptions.PrivateFor) != 2 || generatedOptions.PrivateFor[0] != sendTxArgs.PrivateFor[0] || generatedOptions.PrivateFor[1] != sendTxArgs.PrivateFor[1] { 155 t.Errorf("expected PrivateFor to be '%s', but was '%s'", sendTxArgs.PrivateFor, generatedOptions.PrivateFor) 156 return 157 } 158 159 if generatedOptions.GasLimit != 4712384 { 160 t.Errorf("expected GasLimit to be '%d', but was '%d'", 4712384, generatedOptions.GasLimit) 161 return 162 } 163 164 if generatedOptions.GasPrice == nil || generatedOptions.GasPrice.Cmp(new(big.Int)) != 0 { 165 t.Errorf("expected GasLimit to be '%d', but was '%d'", new(big.Int), generatedOptions.GasPrice) 166 return 167 } 168 169 if generatedOptions.From != from { 170 t.Errorf("expected From to be '%d', but was '%d'", from, generatedOptions.From) 171 return 172 } 173 } 174 175 func TestGenerateTransactionOptionsGivesNonDefaultsWhenSpecified(t *testing.T) { 176 from := common.HexToAddress("0x2222222222222222222222222222222222222222") 177 gasLimit := hexutil.Uint64(5000) 178 gasPrice := hexutil.Big(*big.NewInt(50)) 179 180 privateTxArgs := ethapi.PrivateTxArgs{PrivateFor: []string{}} 181 182 sendTxArgs := ethapi.SendTxArgs{ 183 From: from, 184 Gas: &gasLimit, 185 GasPrice: &gasPrice, 186 PrivateTxArgs: privateTxArgs, 187 } 188 189 mockWallet := &MockWallet{isContained: true} 190 mockBackend := MockBackend{wallets: []accounts.Wallet{mockWallet}} 191 accountManager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, &mockBackend) 192 service := &PrivacyService{ 193 accountManager: accountManager, 194 } 195 196 generatedOptions, err := service.GenerateTransactOptions(sendTxArgs) 197 if err != nil { 198 t.Errorf("expected err to be '%s', but was '%s'", "nil", err.Error()) 199 return 200 } 201 202 if generatedOptions.GasLimit != 5000 { 203 t.Errorf("expected GasLimit to be '%d', but was '%d'", 5000, generatedOptions.GasLimit) 204 return 205 } 206 207 if generatedOptions.GasPrice == nil || generatedOptions.GasPrice.Cmp(big.NewInt(50)) != 0 { 208 t.Errorf("expected GasLimit to be '%d', but was '%d'", big.NewInt(50), generatedOptions.GasPrice) 209 return 210 } 211 }