Energy Economy Implementation - Week 1 Progress

What We Built Today

✅ Core Foundation Complete

We’ve implemented the foundation of the energy economy system:

1. Energy Constants (energyConsts.ts)

  • Trial credits for new users: 1000 Joules
  • Computation costs (hashing, encryption, signing)
  • Storage costs (per GB per day)
  • Network costs (bandwidth)
  • PoW difficulty range (8-24 bits)

2. Operation Types (enumerations/operationType.ts)

  • Expanded from 1 to 40+ operation types
  • Block operations (store, retrieve, delete, verify)
  • CBL operations
  • Whitening operations
  • Encryption operations
  • Quorum operations
  • Messaging operations
  • Reputation operations
  • Network operations

3. Operation Cost (operationCost.ts)

  • Tracks computation, storage, network, and PoW costs
  • Calculates total Joules
  • Utility methods (zero, add)

4. Energy Account (energyAccount.ts)

  • Balance tracking (current, earned, spent, reserved)
  • Reputation score (0.0 to 1.0)
  • Trial credits for new users
  • Reserve/release for operations
  • Charge/credit for transactions
  • Serialization (toDto/fromDto, toJson/fromJson)

5. Energy Account Store (stores/energyAccountStore.ts)

  • In-memory storage for accounts
  • Get/set/delete operations
  • GetOrCreate with automatic trial credits
  • Follows existing SimpleStore patterns

6. Energy Ledger (stores/energyLedger.ts)

  • Transaction audit trail
  • Indexed by member ID
  • Time-range queries
  • Complete transaction history

7. Tests (energyAccount.spec.ts)

  • Account creation with trial credits
  • Reserve/release/charge/credit operations
  • Reputation updates
  • Serialization round-trips
  • Store operations

How It Works

New User Flow

// 1. User joins network
const memberId = member.id;

// 2. Create account with trial credits
const account = EnergyAccount.createTrial(memberId);
// account.balance = 1000 Joules
// account.reputation = 0.5 (neutral)

// 3. Store in account store
accountStore.set(account);

Operation Flow

// 1. Calculate operation cost
const cost = new OperationCost(
  0.002,  // computation
  15.0,   // storage
  0.03,   // network
  0.8     // PoW based on reputation
);

// 2. Check if user can afford
if (account.canAfford(cost.totalJoules)) {
  // 3. Reserve energy
  account.reserve(cost.totalJoules);
  
  // 4. Perform operation
  await performOperation();
  
  // 5. Charge energy
  account.charge(cost.totalJoules);
  account.release(cost.totalJoules);
}

Transaction Tracking

// Record transaction in ledger
const tx: EnergyTransaction = {
  id: txChecksum,
  timestamp: new Date(),
  source: userMemberId,
  destination: providerMemberId,
  amount: cost.totalJoules,
  operationType: OperationType.BLOCK_STORE,
  blockId: blockChecksum,
  metadata: {
    dataSize: 1048576,  // 1 MB
    duration: 30,        // 30 days
    redundancy: 3,       // 3 copies
  },
  signature: userSignature,
};

ledger.add(tx);

What’s Next (Week 2)

Storage Contracts

  • StorageContract interface
  • Contract creation with energy payment
  • Expiration tracking
  • Link to block storage
  • Auto-renewal logic

Integration

  • Add energy checks to BlockService
  • Charge for block storage
  • Credit providers for serving blocks
  • Track energy in block metadata

Testing

Run the tests:

npx nx test brightchain-lib --testFile=energyAccount.spec.ts

Expected results:

  • ✅ Account creation with trial credits
  • ✅ Balance operations (reserve, charge, credit)
  • ✅ Reputation updates
  • ✅ Serialization
  • ✅ Store operations

Files Created

brightchain-lib/src/lib/
├── energyConsts.ts                    # Energy constants
├── energyAccount.ts                   # EnergyAccount class
├── energyAccount.spec.ts              # Tests
├── operationCost.ts                   # Updated with PoW
├── enumerations/
│   └── operationType.ts               # Expanded operations
├── interfaces/
│   ├── energyAccount.ts               # Account interfaces
│   └── energyTransaction.ts           # Updated transaction
└── stores/
    ├── energyAccountStore.ts          # Account storage
    └── energyLedger.ts                # Transaction ledger

Key Design Decisions

1. Trial Credits

  • New users get 1000 Joules automatically
  • Enough for initial exploration
  • Prevents barrier to entry
  • Can be adjusted based on testing

2. Neutral Reputation

  • New users start at 0.5 (neutral)
  • Not penalized initially
  • Not rewarded initially
  • Earn reputation through behavior

3. Reserve/Charge Pattern

  • Reserve energy before operation
  • Prevents race conditions
  • Charge after operation completes
  • Release if operation fails

4. Immutable Transaction History

  • Ledger is append-only
  • Complete audit trail
  • Indexed for fast queries
  • Supports compliance/debugging

Integration Points

Existing Systems

  • ✅ Member system (memberId)
  • ✅ Checksum (transaction IDs)
  • ✅ Store patterns (SimpleStore, ArrayStore)
  • ✅ Serialization (toDto/fromDto)

Future Integration

  • ⏳ BlockService (charge for storage)
  • ⏳ CBLService (charge for CBL operations)
  • ⏳ MessageService (charge for messaging)
  • ⏳ QuorumService (charge for quorum ops)

Success Metrics

Week 1 Goals: ✅ COMPLETE

  • Energy constants defined
  • Account system working
  • Transaction tracking working
  • Tests passing
  • Documentation complete

Week 2 Goals

  • Storage contracts implemented
  • Integration with BlockService
  • End-to-end energy flow working
  • Cost calculation service

Notes

This is a minimal viable implementation that:

  • Works with existing BrightChain patterns
  • Provides foundation for full economic model
  • Can be extended incrementally
  • Doesn’t break existing functionality

You now have energy tracking! 🎉

Next week, we’ll connect it to actual storage operations and make the economy come alive.