|
| 1 | +package bind |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum" |
| 9 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/ethereum/go-ethereum/crypto" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + tmPrivatePayloadHash common.EncryptedPayloadHash |
| 18 | + tmPrivateTxHash common.EncryptedPayloadHash |
| 19 | +) |
| 20 | + |
| 21 | +func init() { |
| 22 | + payloadHash := crypto.Keccak512([]byte("encrypted-private-payload")) |
| 23 | + privateTxHash := crypto.Keccak512([]byte("encrypted-private-tx")) |
| 24 | + for i := 0; i < 64; i++ { |
| 25 | + tmPrivatePayloadHash[i] = payloadHash[i] |
| 26 | + tmPrivateTxHash[i] = privateTxHash[i] |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestBoundContract_Transact_ContractCreation_PrivateTransaction(t *testing.T) { |
| 31 | + transactor := &mockTransactor{} |
| 32 | + |
| 33 | + c := NewBoundContract(common.Address{}, abi.ABI{}, nil, transactor, nil) |
| 34 | + |
| 35 | + senderNonce := 1 |
| 36 | + |
| 37 | + opts := &TransactOpts{ |
| 38 | + Nonce: big.NewInt(int64(senderNonce)), |
| 39 | + PrivateFor: []string{"tm1"}, |
| 40 | + |
| 41 | + // arbitrary values to skip the logic we're not testing |
| 42 | + GasPrice: big.NewInt(0), |
| 43 | + GasLimit: uint64(1), |
| 44 | + Signer: passthroughSigner, |
| 45 | + } |
| 46 | + |
| 47 | + tx, err := c.transact(opts, nil, nil) |
| 48 | + |
| 49 | + wantNonce := uint64(senderNonce) |
| 50 | + wantTo := (*common.Address)(nil) |
| 51 | + wantData := tmPrivatePayloadHash.Bytes() |
| 52 | + |
| 53 | + require.NoError(t, err) |
| 54 | + require.NotNil(t, tx) |
| 55 | + require.Equal(t, wantNonce, tx.Nonce()) |
| 56 | + require.Equal(t, wantTo, tx.To()) |
| 57 | + require.Equal(t, wantData, tx.Data()) |
| 58 | + require.True(t, tx.IsPrivate()) |
| 59 | +} |
| 60 | + |
| 61 | +func TestBoundContract_Transact_ContractCreation_PrivacyPrecompile(t *testing.T) { |
| 62 | + transactor := &mockTransactor{} |
| 63 | + |
| 64 | + c := NewBoundContract(common.Address{}, abi.ABI{}, nil, transactor, nil) |
| 65 | + |
| 66 | + senderNonce := 1 |
| 67 | + |
| 68 | + opts := &TransactOpts{ |
| 69 | + Nonce: big.NewInt(int64(senderNonce)), |
| 70 | + PrivateFor: []string{"tm1"}, |
| 71 | + IsUsingPrivacyPrecompile: true, |
| 72 | + |
| 73 | + // arbitrary values to skip the logic we're not testing |
| 74 | + GasPrice: big.NewInt(0), |
| 75 | + GasLimit: uint64(1), |
| 76 | + Signer: passthroughSigner, |
| 77 | + } |
| 78 | + |
| 79 | + pmt, err := c.transact(opts, nil, nil) |
| 80 | + |
| 81 | + require.NoError(t, err) |
| 82 | + require.NotNil(t, pmt) |
| 83 | + |
| 84 | + // verify the private marker transaction |
| 85 | + wantPMTNonce := uint64(senderNonce) |
| 86 | + wantPMTTo := common.QuorumPrivacyPrecompileContractAddress() |
| 87 | + wantPMTData := tmPrivateTxHash.Bytes() |
| 88 | + |
| 89 | + require.Equal(t, wantPMTNonce, pmt.Nonce()) |
| 90 | + require.Equal(t, &wantPMTTo, pmt.To()) |
| 91 | + require.Equal(t, wantPMTData, pmt.Data()) |
| 92 | + require.False(t, pmt.IsPrivate()) |
| 93 | + |
| 94 | + // verify the captured internal private transaction |
| 95 | + pvtTx := transactor.capturedInternalPrivateTransaction |
| 96 | + pvtTxArgs := transactor.capturedInternalPrivateTransactionArgs |
| 97 | + |
| 98 | + wantPvtTxNonce := uint64(senderNonce) |
| 99 | + wantPvtTxTo := (*common.Address)(nil) |
| 100 | + wantPvtTxData := tmPrivatePayloadHash.Bytes() |
| 101 | + |
| 102 | + require.NotNil(t, pvtTx) |
| 103 | + require.Equal(t, wantPvtTxNonce, pvtTx.Nonce()) |
| 104 | + require.Equal(t, wantPvtTxTo, pvtTx.To()) |
| 105 | + require.Equal(t, wantPvtTxData, pvtTx.Data()) |
| 106 | + require.True(t, pvtTx.IsPrivate()) |
| 107 | + |
| 108 | + require.Equal(t, []string{"tm1"}, pvtTxArgs.PrivateFor) |
| 109 | +} |
| 110 | + |
| 111 | +func TestBoundContract_Transact_Transaction_PrivateTransaction(t *testing.T) { |
| 112 | + transactor := &mockTransactor{} |
| 113 | + |
| 114 | + contractAddr := common.HexToAddress("0x1932c48b2bf8102ba33b4a6b545c32236e342f34") |
| 115 | + c := NewBoundContract(contractAddr, abi.ABI{}, nil, transactor, nil) |
| 116 | + |
| 117 | + senderNonce := 1 |
| 118 | + |
| 119 | + opts := &TransactOpts{ |
| 120 | + Nonce: big.NewInt(int64(senderNonce)), |
| 121 | + PrivateFor: []string{"tm1"}, |
| 122 | + |
| 123 | + // arbitrary values to skip the logic we're not testing |
| 124 | + GasPrice: big.NewInt(0), |
| 125 | + GasLimit: uint64(1), |
| 126 | + Signer: passthroughSigner, |
| 127 | + } |
| 128 | + |
| 129 | + tx, err := c.transact(opts, &contractAddr, nil) |
| 130 | + |
| 131 | + wantNonce := uint64(senderNonce) |
| 132 | + wantTo := &contractAddr |
| 133 | + wantData := tmPrivatePayloadHash.Bytes() |
| 134 | + |
| 135 | + require.NoError(t, err) |
| 136 | + require.NotNil(t, tx) |
| 137 | + require.Equal(t, wantNonce, tx.Nonce()) |
| 138 | + require.Equal(t, wantTo, tx.To()) |
| 139 | + require.Equal(t, wantData, tx.Data()) |
| 140 | + require.True(t, tx.IsPrivate()) |
| 141 | +} |
| 142 | + |
| 143 | +func TestBoundContract_Transact_Transaction_PrivacyPrecompile(t *testing.T) { |
| 144 | + transactor := &mockTransactor{} |
| 145 | + |
| 146 | + contractAddr := common.HexToAddress("0x1932c48b2bf8102ba33b4a6b545c32236e342f34") |
| 147 | + c := NewBoundContract(contractAddr, abi.ABI{}, nil, transactor, nil) |
| 148 | + |
| 149 | + senderNonce := 1 |
| 150 | + |
| 151 | + opts := &TransactOpts{ |
| 152 | + Nonce: big.NewInt(int64(senderNonce)), |
| 153 | + PrivateFor: []string{"tm1"}, |
| 154 | + IsUsingPrivacyPrecompile: true, |
| 155 | + |
| 156 | + // arbitrary values to skip the logic we're not testing |
| 157 | + GasPrice: big.NewInt(0), |
| 158 | + GasLimit: uint64(1), |
| 159 | + Signer: passthroughSigner, |
| 160 | + } |
| 161 | + |
| 162 | + pmt, err := c.transact(opts, &contractAddr, nil) |
| 163 | + |
| 164 | + require.NoError(t, err) |
| 165 | + require.NotNil(t, pmt) |
| 166 | + |
| 167 | + // verify the private marker transaction |
| 168 | + wantPMTNonce := uint64(senderNonce) |
| 169 | + wantPMTTo := common.QuorumPrivacyPrecompileContractAddress() |
| 170 | + wantPMTData := tmPrivateTxHash.Bytes() |
| 171 | + |
| 172 | + require.Equal(t, wantPMTNonce, pmt.Nonce()) |
| 173 | + require.Equal(t, &wantPMTTo, pmt.To()) |
| 174 | + require.Equal(t, wantPMTData, pmt.Data()) |
| 175 | + require.False(t, pmt.IsPrivate()) |
| 176 | + |
| 177 | + // verify the captured internal private transaction |
| 178 | + pvtTx := transactor.capturedInternalPrivateTransaction |
| 179 | + pvtTxArgs := transactor.capturedInternalPrivateTransactionArgs |
| 180 | + |
| 181 | + wantPvtTxNonce := uint64(senderNonce) |
| 182 | + wantPvtTxTo := &contractAddr |
| 183 | + wantPvtTxData := tmPrivatePayloadHash.Bytes() |
| 184 | + |
| 185 | + require.NotNil(t, pvtTx) |
| 186 | + require.Equal(t, wantPvtTxNonce, pvtTx.Nonce()) |
| 187 | + require.Equal(t, wantPvtTxTo, pvtTx.To()) |
| 188 | + require.Equal(t, wantPvtTxData, pvtTx.Data()) |
| 189 | + require.True(t, pvtTx.IsPrivate()) |
| 190 | + |
| 191 | + require.Equal(t, []string{"tm1"}, pvtTxArgs.PrivateFor) |
| 192 | +} |
| 193 | + |
| 194 | +func passthroughSigner(_ types.Signer, _ common.Address, tx *types.Transaction) (*types.Transaction, error) { |
| 195 | + return tx, nil |
| 196 | +} |
| 197 | + |
| 198 | +type mockTransactor struct { |
| 199 | + capturedInternalPrivateTransaction *types.Transaction |
| 200 | + capturedInternalPrivateTransactionArgs PrivateTxArgs |
| 201 | +} |
| 202 | + |
| 203 | +func (s *mockTransactor) PreparePrivateTransaction(_ []byte, _ string) (common.EncryptedPayloadHash, error) { |
| 204 | + return tmPrivatePayloadHash, nil |
| 205 | +} |
| 206 | + |
| 207 | +func (s *mockTransactor) DistributeTransaction(_ context.Context, tx *types.Transaction, args PrivateTxArgs) (string, error) { |
| 208 | + s.capturedInternalPrivateTransaction = tx |
| 209 | + s.capturedInternalPrivateTransactionArgs = args |
| 210 | + return tmPrivateTxHash.Hex(), nil |
| 211 | +} |
| 212 | + |
| 213 | +func (s *mockTransactor) SendTransaction(_ context.Context, _ *types.Transaction, _ PrivateTxArgs) error { |
| 214 | + return nil |
| 215 | +} |
| 216 | + |
| 217 | +func (s *mockTransactor) PendingCodeAt(_ context.Context, _ common.Address) ([]byte, error) { |
| 218 | + panic("implement me") |
| 219 | +} |
| 220 | + |
| 221 | +func (s *mockTransactor) PendingNonceAt(_ context.Context, _ common.Address) (uint64, error) { |
| 222 | + panic("implement me") |
| 223 | +} |
| 224 | + |
| 225 | +func (s *mockTransactor) SuggestGasPrice(_ context.Context) (*big.Int, error) { |
| 226 | + panic("implement me") |
| 227 | +} |
| 228 | + |
| 229 | +func (s *mockTransactor) EstimateGas(_ context.Context, _ ethereum.CallMsg) (gas uint64, err error) { |
| 230 | + panic("implement me") |
| 231 | +} |
0 commit comments