Fix Block model to match server: nest header with camelCase keys

The block endpoint returns block id / prev / timestamp nested under a
"header" object with camelCase keys (blockId, dcId, prevId, prevProof,
timestamp) and a proof of just {proof}. The previous flat snake_case
Block fields never matched the response and always deserialized empty.
Add a BlockHeader dataclass, nest it in Block, make proof.scheme
optional, and cover it with a unit test. Verified live against a dev
chain.
This commit is contained in:
2026-05-29 17:09:02 -04:00
parent 4a7d8b875a
commit 8b007dcbab
3 changed files with 53 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ import json
import pytest
from prime_sdk import (
Block,
DragonchainAPIError,
DragonchainSDK,
SmartContractCreateRequest,
@@ -163,6 +164,33 @@ def test_transaction_from_dict_nested():
assert txn.payload == "{}"
def test_block_from_dict_nested_header():
# Real server shape: id/prev/timestamp nested under "header" with camelCase
# keys; proof carries only "proof" on a trust-scheme chain.
raw = {
"version": "1",
"header": {
"blockId": "69569983",
"dcId": "chain-xyz",
"prevId": "69186326",
"prevProof": "MEUCIQ...",
"timestamp": "1780088135",
},
"proof": {"proof": "MEQCIF..."},
"transactions": ["{...}", "{...}", "{...}"],
}
blk = Block.from_dict(raw)
assert blk.version == "1"
assert blk.header.block_id == "69569983"
assert blk.header.dc_id == "chain-xyz"
assert blk.header.prev_id == "69186326"
assert blk.header.prev_proof == "MEUCIQ..."
assert blk.header.timestamp == "1780088135"
assert blk.proof.proof == "MEQCIF..."
assert blk.proof.scheme == "" # absent on trust-scheme chain
assert len(blk.transactions) == 3
def test_contract_create_request_omitempty():
req = SmartContractCreateRequest(
environment="python3.8",