SECTION 1: LEARNING OBJECTIVES

By the end of this lesson, you will be able to:

  • Define agile development and its application to blockchain projects.

  • Explain key agile principles and methodologies (Scrum, Kanban).

  • Understand sprint planning and execution for blockchain development.

  • Describe testing strategies for smart contracts.

  • Differentiate between traditional and blockchain-specific testing.

  • Identify continuous integration and deployment practices.

  • Implement a sprint planning simulation in Python.

  • Develop a framework for agile blockchain development.


SECTION 2: WHY AGILE FOR BLOCKCHAIN?

2.1 Agile Principles

 
 
Principle Application to Blockchain
Individuals and Interactions Cross-functional teams (devs, security, legal)
Working Software Functional smart contracts and DApps
Customer Collaboration Community feedback and governance
Responding to Change Adapting to evolving technology and regulation

2.2 Agile vs Waterfall

 
 
Aspect Agile Waterfall
Planning Iterative, continuous Fixed, upfront
Delivery Frequent, incremental Single, final
Requirements Evolving Fixed
Testing Continuous End of project
Risk Early detection Late discovery
Audits Integrated Final
Best For Complex, evolving Simple, well-defined

2.3 Blockchain-Specific Agile Considerations

 
 
Consideration Description
Security-First Security cannot be sacrificed for speed
Governance Integration Community input must be incorporated
Audit Requirements External audits are mandatory
Deployment Complexity Testnet and mainnet deployment
Gas Optimisation Cost considerations

SECTION 3: AGILE METHODOLOGIES

3.1 Scrum for Blockchain

text
┌─────────────────────────────────────────────────────────────────────────────┐
│                    SCRUM FRAMEWORK FOR BLOCKCHAIN                           │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  Product Backlog                                                           │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │  • Features, user stories, technical tasks                           │   │
│  │  • Prioritised by business value                                     │   │
│  │  • Refined continuously                                              │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  Sprint Planning                                                           │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │  • Select items for the sprint                                       │   │
│  │  • Estimate effort                                                   │   │
│  │  • Define sprint goal                                                │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  Sprint (2-4 weeks)                                                       │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │  • Daily standups                                                    │   │
│  │  • Development                                                       │   │
│  │  • Testing (unit, integration, security)                            │   │
│  │  • Code review                                                       │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  Sprint Review                                                             │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │  • Demo completed work                                              │   │
│  │  • Gather feedback                                                  │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  Sprint Retrospective                                                      │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │  • Reflect on process                                               │   │
│  │  • Identify improvements                                            │   │
│  │  • Action items                                                     │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

3.2 Kanban for Blockchain

 
 
Practice Description
Visualise Work Use Kanban board to track tasks
Limit Work in Progress Focus on completing tasks
Manage Flow Optimise throughput
Continuous Delivery Deploy frequently
Feedback Loops Regular reviews

SECTION 4: SMART CONTRACT DEVELOPMENT CYCLE

4.1 Development Phases

text
┌─────────────────────────────────────────────────────────────────────────────┐
│                    SMART CONTRACT DEVELOPMENT CYCLE                         │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  1. DESIGN                                                                 │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Requirements analysis                                              │   │
│  │ • Interface design                                                   │   │
│  │ • Security architecture                                              │   │
│  │ • Gas optimisation planning                                          │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  2. IMPLEMENTATION                                                         │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Write smart contract code                                         │   │
│  │ • Use standard libraries                                             │   │
│  │ • Follow best practices                                              │   │
│  │ • Code review                                                        │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  3. TESTING                                                                │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Unit tests                                                         │   │
│  │ • Integration tests                                                  │   │
│  │ • Fuzzing                                                           │   │
│  │ • Security tests                                                     │   │
│  │ • Gas analysis                                                       │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  4. AUDIT                                                                 │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Internal security review                                          │   │
│  │ • External audit                                                    │   │
│  │ • Formal verification (optional)                                   │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  5. DEPLOYMENT                                                             │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Deploy to testnet                                                 │   │
│  │ • Test in testnet                                                   │   │
│  │ • Deploy to mainnet                                                │   │
│  │ • Verify source                                                     │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  6. MONITORING                                                             │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │ • Monitor contract activity                                        │   │
│  │ • Track gas costs                                                  │   │
│  │ • Detect anomalies                                                 │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

SECTION 5: TESTING STRATEGIES

5.1 Testing Types

 
 
Test Type Description Tools
Unit Testing Test individual functions Hardhat, Foundry
Integration Testing Test contract interactions Hardhat, Foundry
Fuzzing Random input testing Echidna, Foundry
Formal Verification Mathematical proofs Certora, K Framework
Gas Analysis Optimise gas usage Hardhat, Slither
Security Testing Vulnerability detection Slither, Mythril
Testnet Deployment Production-like testing Testnets

5.2 Test Coverage Targets

 
 
Coverage Type Target Description
Line Coverage >90% Lines of code executed
Branch Coverage >85% All possible execution paths
Function Coverage >95% All functions called
Gas Coverage Optimised Cost efficiency

SECTION 6: IMPLEMENTATION IN PYTHON

python
# ===================================================================
# MODULE 7, LESSON 6: AGILE DEVELOPMENT FOR BLOCKCHAIN
# ===================================================================

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from typing import Dict, List
import warnings
warnings.filterwarnings('ignore')

print("="*70)
print("AGILE DEVELOPMENT FOR BLOCKCHAIN")
print("="*70)

# ----------------------------------------------------------------
# PART A: SPRINT PLANNING SIMULATION
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART A: Sprint Planning Simulation")
print("-"*60)

class Sprint:
    def __init__(self, sprint_id: int, duration_weeks: int = 2):
        self.sprint_id = sprint_id
        self.duration_weeks = duration_weeks
        self.start_date = datetime.now() + timedelta(weeks=sprint_id * duration_weeks)
        self.end_date = self.start_date + timedelta(weeks=duration_weeks)
        self.tasks = []
        self.completed = 0
        self.total_estimate = 0
        self.completed_estimate = 0
    
    def add_task(self, task: str, estimate_hours: int, priority: str):
        self.tasks.append({
            'task': task,
            'estimate_hours': estimate_hours,
            'priority': priority,
            'status': 'pending'
        })
        self.total_estimate += estimate_hours
    
    def complete_task(self, task: str):
        for t in self.tasks:
            if t['task'] == task:
                t['status'] = 'completed'
                self.completed += 1
                self.completed_estimate += t['estimate_hours']
                break
    
    def get_metrics(self) -> Dict:
        total = len(self.tasks)
        if total == 0:
            return {'completion_rate': 0, 'progress': 0, 'velocity': 0}
        completion_rate = self.completed / total
        progress = self.completed_estimate / self.total_estimate if self.total_estimate > 0 else 0
        velocity = self.completed_estimate / self.duration_weeks
        return {
            'completion_rate': completion_rate,
            'progress': progress,
            'velocity': velocity,
            'total_tasks': total,
            'completed_tasks': self.completed
        }

class ProjectBacklog:
    def __init__(self):
        self.tasks = []
        self.sprints = []
    
    def add_task(self, task: str, estimate_hours: int, priority: str, category: str):
        self.tasks.append({
            'task': task,
            'estimate_hours': estimate_hours,
            'priority': priority,
            'category': category,
            'status': 'backlog'
        })
    
    def create_sprint(self, sprint_id: int, tasks: List[str]) -> Sprint:
        sprint = Sprint(sprint_id)
        for task_name in tasks:
            task = next((t for t in self.tasks if t['task'] == task_name), None)
            if task:
                sprint.add_task(task['task'], task['estimate_hours'], task['priority'])
                task['status'] = 'in_sprint'
        self.sprints.append(sprint)
        return sprint
    
    def get_backlog_summary(self) -> pd.DataFrame:
        return pd.DataFrame(self.tasks)

# Create backlog
backlog = ProjectBacklog()

# Add tasks
tasks_data = [
    ('Setup development environment', 4, 'High', 'Infrastructure'),
    ('Design smart contract architecture', 8, 'Critical', 'Architecture'),
    ('Implement token contract', 16, 'Critical', 'Development'),
    ('Implement lending pool contract', 24, 'Critical', 'Development'),
    ('Write unit tests', 12, 'High', 'Testing'),
    ('Write integration tests', 16, 'High', 'Testing'),
    ('Security audit preparation', 8, 'High', 'Security'),
    ('Gas optimisation', 8, 'Medium', 'Optimisation'),
    ('Frontend integration', 20, 'Medium', 'Frontend'),
    ('Documentation', 8, 'Medium', 'Documentation'),
    ('Deploy to testnet', 4, 'High', 'Deployment'),
    ('Community feedback', 4, 'Low', 'Community')
]

for task, hours, priority, category in tasks_data:
    backlog.add_task(task, hours, priority, category)

print("Backlog Summary:")
backlog_df = backlog.get_backlog_summary()
print(backlog_df.to_string(index=False))

# Create sprints
sprint1_tasks = ['Setup development environment', 'Design smart contract architecture', 'Implement token contract']
sprint1 = backlog.create_sprint(1, sprint1_tasks)

sprint2_tasks = ['Implement lending pool contract', 'Write unit tests', 'Security audit preparation']
sprint2 = backlog.create_sprint(2, sprint2_tasks)

print("\nSprint 1 Metrics:")
metrics1 = sprint1.get_metrics()
print(f"  Total Tasks: {metrics1['total_tasks']}")
print(f"  Total Estimate: {sprint1.total_estimate} hours")

# Simulate completing tasks
sprint1.complete_task('Setup development environment')
sprint1.complete_task('Design smart contract architecture')

print(f"\nAfter 1 week:")
metrics1 = sprint1.get_metrics()
print(f"  Completed: {metrics1['completed_tasks']}/{metrics1['total_tasks']}")
print(f"  Progress: {metrics1['progress']:.1%}")
print(f"  Velocity: {metrics1['velocity']:.1f} hours/week")

# ----------------------------------------------------------------
# PART B: TESTING COVERAGE SIMULATION
# -----------------------------------------------------------------

print("\n" + "-"*60)
print("PART B: Testing Coverage Simulation")
print("-"*60)

class TestCoverage:
    def __init__(self, project_name: str):
        self.project_name = project_name
        self.contracts = {}
    
    def add_contract(self, name: str, functions: List[str], lines: int):
        self.contracts[name] = {
            'functions': functions,
            'lines': lines,
            'tested_functions': [],
            'tested_lines': 0
        }
    
    def add_test(self, contract: str, function: str, lines_tested: int):
        if contract in self.contracts:
            if function not in self.contracts[contract]['tested_functions']:
                self.contracts[contract]['tested_functions'].append(function)
            self.contracts[contract]['tested_lines'] += lines_tested
    
    def get_coverage(self) -> Dict:
        total_functions = 0
        total_lines = 0
        tested_functions = 0
        tested_lines = 0
        
        for contract, data in self.contracts.items():
            total_functions += len(data['functions'])
            total_lines += data['lines']
            tested_functions += len(data['tested_functions'])
            tested_lines += min(data['tested_lines'], data['lines'])
        
        return {
            'function_coverage': tested_functions / total_functions if total_functions > 0 else 0,
            'line_coverage': tested_lines / total_lines if total_lines > 0 else 0,
            'total_functions': total_functions,
            'tested_functions': tested_functions,
            'total_lines': total_lines,
            'tested_lines': tested_lines
        }

# Create test coverage
coverage = TestCoverage("Lending Protocol")

# Add contracts
coverage.add_contract("Token", ['mint', 'transfer', 'burn', 'approve', 'balanceOf'], 200)
coverage.add_contract("LendingPool", ['deposit', 'withdraw', 'borrow', 'repay', 'liquidate'], 300)
coverage.add_contract("PriceOracle", ['getPrice', 'updatePrice', 'setAddresses'], 100)

# Add tests
coverage.add_test("Token", 'mint', 40)
coverage.add_test("Token", 'transfer', 35)
coverage.add_test("Token", 'burn', 30)
coverage.add_test("Token", 'approve', 25)
coverage.add_test("LendingPool", 'deposit', 60)
coverage.add_test("LendingPool", 'withdraw', 50)
coverage.add_test("LendingPool", 'borrow', 55)
coverage.add_test("PriceOracle", 'getPrice', 30)

# Get coverage
coverage_report = coverage.get_coverage()

print("Test Coverage Report:")
print(f"  Function Coverage: {coverage_report['function_coverage']:.1%}")
print(f"  Line Coverage: {coverage_report['line_coverage']:.1%}")
print(f"  Functions: {coverage_report['tested_functions']}/{coverage_report['total_functions']}")
print(f"  Lines: {coverage_report['tested_lines']}/{coverage_report['total_lines']}")

# Visualise
fig, ax = plt.subplots(figsize=(10, 5))
categories = ['Function Coverage', 'Line Coverage']
values = [coverage_report['function_coverage'], coverage_report['line_coverage']]
targets = [0.90, 0.85]

ax.bar(categories, values, color=['blue', 'green'], alpha=0.7)
ax.axhline(y=targets[0], color='red', linestyle='--', label='Function Target (90%)')
ax.axhline(y=targets[1], color='orange', linestyle='--', label='Line Target (85%)')
ax.set_ylabel('Coverage')
ax.set_title('Smart Contract Test Coverage')
ax.legend()
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('test_coverage.png', dpi=300, bbox_inches='tight')
plt.show()
print("Test coverage chart saved as 'test_coverage.png'")

# ----------------------------------------------------------------
# PART C: DEVELOPMENT CYCLE TIMELINE
# -----------------------------------------------------------------

print("\n" + "-"*60)
print("PART C: Development Cycle Timeline")
print("-"*60)

cycle_data = {
    'Phase': ['Design', 'Implementation', 'Testing', 'Audit', 'Deployment', 'Monitoring'],
    'Duration (weeks)': [2, 4, 3, 3, 1, 2],
    'Key Activities': [
        'Requirements, architecture',
        'Coding, code review',
        'Unit, integration, fuzzing',
        'Internal review, external audit',
        'Testnet, mainnet',
        'Monitoring, incident response'
    ]
}

cycle_df = pd.DataFrame(cycle_data)
print(cycle_df.to_string(index=False))

# Visualise
fig, ax = plt.subplots(figsize=(10, 5))
ax.bar(cycle_df['Phase'], cycle_df['Duration (weeks)'], color='teal', alpha=0.7)
ax.set_xlabel('Phase')
ax.set_ylabel('Duration (weeks)')
ax.set_title('Smart Contract Development Cycle Timeline')
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig('development_cycle.png', dpi=300, bbox_inches='tight')
plt.show()
print("Development cycle chart saved as 'development_cycle.png'")

# ----------------------------------------------------------------
# PART D: SUMMARY AND RECOMMENDATIONS
# -----------------------------------------------------------------

print("\n" + "="*70)
print("PART D: Summary and Recommendations")
print("="*70)

print("""
Agile Development for Blockchain – Key Takeaways:

1. Agile principles apply to blockchain: individuals, working software, collaboration, responding to change.
2. Scrum: sprints (2-4 weeks), backlog, reviews, retrospectives.
3. Kanban: visualise work, limit WIP, manage flow.
4. Smart contract development cycle: design → implementation → testing → audit → deployment → monitoring.
5. Testing types: unit, integration, fuzzing, formal verification, gas analysis.
6. Coverage targets: 90% line coverage, 85% branch coverage, 95% function coverage.

Recommendations:
  - Adopt a hybrid agile/security-first approach.
  - Include security tasks in every sprint.
  - Automate testing in CI/CD pipeline.
  - Conduct testnet deployments for validation.
  - Maintain comprehensive test coverage.
  - Document all development decisions.
  - Conduct regular retrospectives.
""")