@@ -11,30 +11,29 @@ import (
1111 "testing"
1212 "time"
1313
14- "github.com/ethereum/go-ethereum/crypto"
1514 "github.com/joho/godotenv"
1615 kwilcrypto "github.com/kwilteam/kwil-db/core/crypto"
1716 "github.com/kwilteam/kwil-db/core/crypto/auth"
18- "github.com/trufnetwork/sdk-go/core/tnclient"
19- "github.com/trufnetwork/sdk-go/core/util"
2017)
2118
2219// Constants for the test setup
2320const (
24- networkName = "truf-test-network"
25- TestKwilProvider = "http://localhost:8484"
26- TestPrivateKey = "1111111111111111111111111111111111111111111111111111111111111111" // Test private key
27- DB_PRIVATE_KEY = "0000000000000000000000000000000000000000000000000000000000000001"
28- DB_PUBLIC_KEY = "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf"
21+ networkName = "truf-test-network"
22+ TestKwilProvider = "http://localhost:8484"
23+ managerPrivateKey = "1111111111111111111111111111111111111111111111111111111111111111" // manager wallet for system roles
24+ DB_PRIVATE_KEY = "0000000000000000000000000000000000000000000000000000000000000001" // database owner wallet
25+ DB_PUBLIC_KEY = "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf"
2926)
3027
3128// ServerFixture is a fixture for setting up and tearing down a Trufnetwork server for testing
3229type ServerFixture struct {
33- t * testing.T
34- docker * docker
35- client * tnclient.Client
36- ctx context.Context
37- containers struct {
30+ t * testing.T
31+ docker * docker
32+ ctx context.Context
33+ Endpoint string
34+ ManagerPrivateKey * kwilcrypto.Secp256k1PrivateKey
35+ DBOwnerPrivateKey * kwilcrypto.Secp256k1PrivateKey
36+ containers struct {
3837 postgres containerSpec
3938 tndb containerSpec
4039 }
@@ -61,11 +60,22 @@ type docker struct {
6160func NewServerFixture (t * testing.T ) * ServerFixture {
6261 ctx := context .Background ()
6362 d := newDocker (t )
63+ managerPk , err := kwilcrypto .Secp256k1PrivateKeyFromHex (managerPrivateKey )
64+ if err != nil {
65+ t .Fatalf ("failed to parse manager private key: %v" , err )
66+ }
67+ dbOwnerPk , err := kwilcrypto .Secp256k1PrivateKeyFromHex (DB_PRIVATE_KEY )
68+ if err != nil {
69+ t .Fatalf ("failed to parse db owner private key: %v" , err )
70+ }
6471
6572 return & ServerFixture {
66- t : t ,
67- docker : d ,
68- ctx : ctx ,
73+ t : t ,
74+ docker : d ,
75+ ctx : ctx ,
76+ Endpoint : TestKwilProvider ,
77+ ManagerPrivateKey : managerPk ,
78+ DBOwnerPrivateKey : dbOwnerPk ,
6979 containers : struct {
7080 postgres containerSpec
7181 tndb containerSpec
@@ -188,7 +198,12 @@ func (s *ServerFixture) Setup() error {
188198 } else {
189199 providerArg := fmt .Sprintf ("PROVIDER=%s" , TestKwilProvider )
190200 privateKeyArg := fmt .Sprintf ("PRIVATE_KEY=%s" , DB_PRIVATE_KEY )
191- migrateCmd := exec .CommandContext (s .ctx , "task" , "action:migrate" , providerArg , privateKeyArg )
201+ // derive 0x-address from manager private key
202+ mgrPk , _ := kwilcrypto .Secp256k1PrivateKeyFromHex (managerPrivateKey )
203+ mgrSigner := & auth.EthPersonalSigner {Key : * mgrPk }
204+ mgrAddr , _ := auth.EthSecp256k1Authenticator {}.Identifier (mgrSigner .CompactID ())
205+ adminWalletArg := fmt .Sprintf ("ADMIN_WALLET=%s" , mgrAddr )
206+ migrateCmd := exec .CommandContext (s .ctx , "task" , "action:migrate" , providerArg , privateKeyArg , adminWalletArg )
192207 migrateCmd .Dir = nodeRepoDir
193208
194209 s .t .Logf ("Executing command in %s: %s" , migrateCmd .Dir , strings .Join (migrateCmd .Args , " " ))
@@ -199,84 +214,6 @@ func (s *ServerFixture) Setup() error {
199214 }
200215 s .t .Logf ("Migration task successful. Output: %s" , string (migrateOut ))
201216 }
202-
203- // Create client
204- s .t .Log ("Creating private key..." )
205- pk , err := kwilcrypto .Secp256k1PrivateKeyFromHex (TestPrivateKey )
206- if err != nil {
207- return fmt .Errorf ("failed to parse private key: %w" , err )
208- }
209- s .t .Log ("Successfully created private key" )
210-
211- s .t .Log ("Creating TN client..." )
212- s .t .Logf ("Using provider: %s" , TestKwilProvider )
213-
214- // Get the Ethereum address from the public key
215- pubKeyBytes := pk .Public ().Bytes ()
216- // Remove the first byte which is the compression flag
217- pubKeyBytes = pubKeyBytes [1 :]
218- addr , err := util .NewEthereumAddressFromBytes (crypto .Keccak256 (pubKeyBytes )[12 :])
219- if err != nil {
220- return fmt .Errorf ("failed to get address from public key: %w" , err )
221- }
222- s .t .Logf ("Using signer with address: %s" , addr .Address ())
223-
224- s .t .Log ("Attempting to create client..." )
225- var lastErr error
226- for i := 0 ; i < 60 ; i ++ { // 60 seconds max wait
227- s .t .Logf ("Attempt %d/60: Creating client with provider URL %s" , i + 1 , TestKwilProvider )
228-
229- // First check if the server is accepting connections
230- cmd := exec .Command ("curl" , "-s" , "-w" , "\n %{http_code}" , "http://localhost:8484/api/v1/health" )
231- out , err := cmd .CombinedOutput ()
232- if err != nil {
233- lastErr = fmt .Errorf ("health check command failed: %w" , err )
234- s .t .Logf ("Health check command failed: %v" , err )
235- time .Sleep (time .Second )
236- continue
237- }
238-
239- // Split output into response body and status code
240- parts := strings .Split (string (out ), "\n " )
241- if len (parts ) != 2 {
242- lastErr = fmt .Errorf ("unexpected health check output format: %s" , string (out ))
243- s .t .Logf ("Health check output format error: %s" , string (out ))
244- time .Sleep (time .Second )
245- continue
246- }
247-
248- statusCode := strings .TrimSpace (parts [1 ])
249- s .t .Logf ("Health check response - Status: %s" , statusCode )
250- if statusCode != "200" {
251- lastErr = fmt .Errorf ("health check returned non-200 status: %s" , statusCode )
252- s .t .Logf ("Health check failed with status %s" , statusCode )
253- time .Sleep (time .Second )
254- continue
255- }
256-
257- s .t .Log ("Health check passed, attempting to create client..." )
258- // Try to create the client now that we know the server is accepting connections
259- s .client , err = tnclient .NewClient (
260- s .ctx ,
261- TestKwilProvider ,
262- tnclient .WithSigner (& auth.EthPersonalSigner {Key : * pk }),
263- )
264- if err != nil {
265- lastErr = fmt .Errorf ("failed to create TN client: %w" , err )
266- s .t .Logf ("Client creation failed: %v" , err )
267- time .Sleep (time .Second )
268- continue
269- }
270-
271- // Successfully created client
272- s .t .Log ("Client created successfully" )
273- break
274- }
275-
276- if s .client == nil {
277- return fmt .Errorf ("failed to create client after 60 attempts. Last error: %w" , lastErr )
278- }
279-
280217 return nil
281218}
282219
@@ -293,11 +230,6 @@ func (s *ServerFixture) Teardown() {
293230 s .docker .cleanup ()
294231}
295232
296- // Client returns the client for interacting with the server
297- func (s * ServerFixture ) Client () * tnclient.Client {
298- return s .client
299- }
300-
301233// newDocker creates a new docker helper
302234func newDocker (t * testing.T ) * docker {
303235 return & docker {t : t }
0 commit comments