Skip to content

Commit

Permalink
Use namespace in DB (#295)
Browse files Browse the repository at this point in the history
### TL;DR
Added database namespace support based on private key and contract address

### What changed?
- Introduced a new `BuildNamespace` function that generates a unique namespace using the node's private key and contract address
- Modified the replication service to use namespaced database connections
- The namespace is a 12-character hex string derived from the Keccak256 hash of the private key and contract address

### How to test?
1. Start multiple nodes with different private keys
2. Verify each node creates its database tables under a unique namespace
3. Confirm that data remains isolated between different node instances

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Introduced a new method for database initialization that supports namespacing.
	- Added a function to compute a namespace based on server options.
	- Added a `--broadcast` option for contract deployment commands, enabling transaction broadcasting.

- **Bug Fixes**
	- Enhanced error handling during database and component initialization to ensure graceful application exits.
	- Improved reliability of deployment scripts with better error handling and control flow.
	- Updated deployment scripts to reflect changes in contract deployment processes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
neekolas authored Dec 2, 2024
1 parent 6170b9b commit a50c51e
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-pre-baked-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: "FOUNDRY_VERSION=nightly-2044faec64f99a21f0e5f0094458a973612d0712"
build-args: "FOUNDRY_VERSION=nightly-ac81a53d1d5823919ffbadd3c65f081927aa11f2"
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: 'nightly-2044faec64f99a21f0e5f0094458a973612d0712'
version: "nightly-ac81a53d1d5823919ffbadd3c65f081927aa11f2"
- run: forge --version
- name: Run Forge fmt
# only format code, we do not want to format LIB
run: forge fmt contracts/src --check
run: forge fmt contracts/src --check
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: 'nightly-2044faec64f99a21f0e5f0094458a973612d0712'
version: "nightly-ac81a53d1d5823919ffbadd3c65f081927aa11f2"
- run: dev/contracts/deploy-local
- name: Run Tests
run: |
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: 'nightly-2044faec64f99a21f0e5f0094458a973612d0712'
version: "nightly-ac81a53d1d5823919ffbadd3c65f081927aa11f2"

- name: Run Forge build
working-directory: contracts
Expand Down
3 changes: 2 additions & 1 deletion cmd/replication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func main() {
var wg sync.WaitGroup
doneC := make(chan bool, 1)
tracing.GoPanicWrap(ctx, &wg, "main", func(ctx context.Context) {
db, err := db.NewDB(
db, err := db.NewNamespacedDB(
ctx,
options.DB.WriterConnectionString,
utils.BuildNamespace(options),
options.DB.WaitForDB,
options.DB.ReadTimeout,
)
Expand Down
2 changes: 1 addition & 1 deletion dev/contracts/deploy-ephemeral
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ source dev/contracts/.env

cd ./contracts

forge create --legacy --json --rpc-url $DOCKER_RPC_URL --private-key $PRIVATE_KEY "$1:$2"
forge create --legacy --json --broadcast --rpc-url $DOCKER_RPC_URL --private-key $PRIVATE_KEY "$1:$2"
4 changes: 3 additions & 1 deletion dev/contracts/deploy-local
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
# Deploy the smart contracts to the local anvil node

set -euo pipefail

source dev/contracts/.env

# Make sure the build directory exists
Expand All @@ -10,7 +12,7 @@ cd ./contracts

# Deploy a contract and save the output (which includes the contract address) to a JSON file to be used in tests
function deploy_contract() {
forge create --legacy --json --rpc-url $DOCKER_RPC_URL --private-key $PRIVATE_KEY "$1:$2" > ../build/$2.json
forge create --broadcast --legacy --json --rpc-url $DOCKER_RPC_URL --private-key $PRIVATE_KEY "$1:$2" > ../build/$2.json
}

deploy_contract src/GroupMessages.sol GroupMessages
Expand Down
1 change: 1 addition & 0 deletions dev/contracts/deploy-testnet
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cd ./contracts
function deploy_contract() {
forge create \
--rpc-url $RPC_URL \
--broadcast \
--chain=241320161 \
--compiler-version=0.8.28 \
--verify \
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import (
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/xmtp/xmtpd/pkg/config"
)

func BuildNamespace(options config.ServerOptions) string {
hash := ethcrypto.Keccak256(
[]byte(options.Signer.PrivateKey),
[]byte(options.Contracts.NodesContractAddress),
)

return HexEncode(hash)[:12]
}

0 comments on commit a50c51e

Please sign in to comment.