블록 정보에 액세스하기
chain
으로 사용 가능한 Chain
객체는 리스트와 같은 구문을 사용하여 블록 정보에 액세스할 수 있습니다:
>>> chain
<Chain object (chainid=1, height=12965000)>
>>> chain[12965000]
AttributeDict({
'baseFeePerGas': 1000000000,
'difficulty': 7742494561645080,
'extraData': HexBytes('0x68747470733a2f2f7777772e6b7279707465782e6f7267'),
'gasLimit': 30029122,
'gasUsed': 30025257,
'hash': HexBytes('0x9b83c12c69edb74f6c8dd5d052765c1adf940e320bd1291696e6fa07829eee71'),
'logsBloom': HexBytes('0x24e74ad77d9a2b27bdb8f6d6f7f1cffdd8cfb47fdebd433f011f7dfcfbb7db638fadd5ff66ed134ede2879ce61149797fbcdf7b74f6b7de153ec61bdaffeeb7b59c3ed771a2fe9eaed8ac70e335e63ff2bfe239eaff8f94ca642fdf7ee5537965be99a440f53d2ce057dbf9932be9a7b9a82ffdffe4eeee1a66c4cfb99fe4540fbff936f97dde9f6bfd9f8cefda2fc174d23dfdb7d6f7dfef5f754fe6a7eec92efdbff779b5feff3beafebd7fd6e973afebe4f5d86f3aafb1f73bf1e1d0cdd796d89827edeffe8fb6ae6d7bf639ec5f5ff4c32f31f6b525b676c7cdf5e5c75bfd5b7bd1928b6f43aac7fa0f6336576e5f7b7dfb9e8ebbe6f6efe2f9dfe8b3f56'),
'miner': '0x7777788200B672A42421017F65EDE4Fc759564C8',
'mixHash': HexBytes('0x9620b46a81a4795cf4449d48e3270419f58b09293a5421205f88179b563f815a'),
'nonce': HexBytes('0xb223da049adf2216'),
'number': 12965000,
'parentHash': HexBytes('0x3de6bb3849a138e6ab0b83a3a00dc7433f1e83f7fd488e4bba78f2fe2631a633'),
'receiptsRoot': HexBytes('0x8a8865cd785e2e9dfce7da83aca010b10b9af2abbd367114b236f149534c821d'),
'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
'size': 137049,
'stateRoot': HexBytes('0x41cf6e8e60fd087d2b00360dc29e5bfb21959bce1f4c242fd1ad7c4da968eb87'),
'timestamp': 1628166822,
'totalDifficulty': 28494409340649014490153,
'transactions': [
...
],
'transactionsRoot': HexBytes('0xdfcb68d3a3c41096f4a77569db7956e0a0e750fad185948e54789ea0e51779cb'),
'uncles': []
})
>>> web3.eth.block_number
12965000
>>> len(chain)
12965001 # always +1 to the current block number, because the first block is zero
>>> chain[0] == web3.eth.get_block(0)
True
# for negative index values, the block returned is relative to the most recently mined block
>>> chain[-1] == web3.eth.get_block('latest')
True
거래 데이터에 접근하기
로컬 거래 기록
TxHistory
컨테이너는 history
로 사용 가능하며, Brownie 세션 중에 브로드캐스트된 모든 거래를 보유합니다. 호출할 때 변수에 할당하지 않았다면, 이를 사용하여 TransactionReceipt
객체에 접근할 수 있습니다.
>>> history
[
<Transaction object '0xe803698b0ade1598c594b2c73ad6a656560a4a4292cc7211b53ffda4a1dbfbe8'>,
<Transaction object '0xa7616a96ef571f1791586f570017b37f4db9decb1a5f7888299a035653e8b44b'>
]
특정 트랜잭션을 키-값 쌍이나 람다 함수로 필터링하려면 history.filter
를 사용할 수 있습니다:
>>> history.filter(sender=accounts[0], value="1 ether")
[<Transaction object '0xe803698b0ade1598c594b2c73ad6a656560a4a4292cc7211b53ffda4a1dbfbe8'>]
>>> history.filter(key=lambda k: k.nonce < 2)
[<Transaction '0x03569ee152b04ba5b55c2bf05f99f7ec153db715acfe0c1600f144ded58f31fe'>, <Transaction '0x42193c0ff7007c6e2a5e5572a3c6b5706cd133d21e30e5826add3d971134504c'>]
다른 거래
chain.get_transaction
을 사용하여 모든 거래에 대한TransactionReceipt
개체를 가져옵니다.
>>> chain.get_transaction('0xf598d43ef34a48478f3bb0ad969c6735f416902c4eb1eb18ebebe0fca786105e')
<Transaction '0xf598d43ef34a48478f3bb0ad969c6735f416902c4eb1eb18ebebe0fca786105e'>
이는 보류 중인 거래에 대해서도 작동합니다. 거래가 아직 확인되지 않은 경우 콘솔 내에서 거래 해시가 노란색으로 표시됩니다.
개발 체인 조작
Brownie는 로컬 개발 환경으로 ganache-cli를 사용하도록 설계되었습니다. 채굴, 스냅샷 기능 및 시간 여행과 같은 기능은 Chain
객체를 통해 액세스할 수 있습니다.
새 블록 채굴
Ganache는 거래를 브로드캐스트할 때마다 새 블록을 채굴하는 것이 기본 동작입니다. chain.mine
메서드를 사용하여 빈 블록을 채굴할 수 있습니다:
>>> web3.eth.block_number
0
>>> chain.mine(50)
50
>>> web3.eth.block_number
50
시간 여행
현재 epoch 시간을 확인하려면 chain.time
을 호출할 수 있습니다:
>>> chain.time()
1500000000
시계를 빨리 감기 위해서는 chain.sleep
를 호출하십시오.
>>> chain.sleep(31337)
>>> chain.time()
1500031337
잠자는 것은 새로운 블록을 채굴하지 않는다는 것을 기억하세요. block.timestamp
에 의존하는 계약 보기 기능은 거래를 수행하거나 chain.mine
을 호출할 때까지 영향을 받지 않습니다.
스냅샷
현재 블록체인 상태의 스냅샷을 촬영하려면 chain.snapshot
을 사용하십시오:
>>> chain.snapshot()
>>> accounts[0].balance()
100000000000000000000
>>> accounts[0].transfer(accounts[1], "10 ether")
Transaction sent: 0xd5d3b40eb298dfc48721807935eda48d03916a3f48b51f20bcded372113e1dca
Transaction confirmed - block: 5 gas used: 21000 (100.00%)
<Transaction object '0xd5d3b40eb298dfc48721807935eda48d03916a3f48b51f20bcded372113e1dca'>
나중에 chain.revert
를 사용하여 이 상태로 돌아갈 수 있습니다.:
>>> accounts[0].balance()
89999580000000000000
>>> chain.revert()
4
>>> accounts[0].balance()
100000000000000000000
되돌리기는 스냅샷을 소모하지 않습니다. 필요할 때마다 동일한 스냅샷으로 돌아갈 수 있습니다. 하지만 새로운 스냅샷을 찍으면 이전 스냅샷은 더 이상 접근할 수 없습니다.
genesis 상태로 돌아가려면 chain.reset
을 사용하세요.
>>> web3.eth.block_number
6
>>> chain.reset()
>>> web3.eth.block_number
0
실행 취소 / 다시 실행
스냅샷을 함께 사용하여 최근 거래를 앞뒤로 이동하려면 chain.undo
및 chain.redo
을 사용할 수 있습니다. 이는 대화형 테스트 디버깅 중에 특히 유용합니다.
>>> accounts[0].transfer(accounts[1], "1 ether")
Transaction sent: 0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05
Gas price: 0.0 gwei Gas limit: 6721975
Transaction confirmed - Block: 1 Gas used: 21000 (0.31%)
<Transaction '0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05'>
>>> chain.undo()
0
>>> chain.redo()
Transaction sent: 0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05
Gas price: 0.0 gwei Gas limit: 6721975
Transaction confirmed - Block: 1 Gas used: 21000 (0.31%)
chain.snapshot
과 chain.revert
는 실행 취소 버퍼를 지웁니다.
'블록체인 (Block Chain) > 이더리움' 카테고리의 다른 글
[브라우니 (Brownie)] 14. 데이터 유형 (0) | 2023.06.16 |
---|---|
[브라우니 (Brownie)] 13. 트랜잭션 검사 및 디버깅하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 11. 트랜잭션 가스 가격 설정하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 10. 컨트랙트로 작업하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 9. 계정으로 작업하기 (0) | 2023.06.16 |
블록 정보에 액세스하기
chain
으로 사용 가능한 Chain
객체는 리스트와 같은 구문을 사용하여 블록 정보에 액세스할 수 있습니다:
>>> chain
<Chain object (chainid=1, height=12965000)>
>>> chain[12965000]
AttributeDict({
'baseFeePerGas': 1000000000,
'difficulty': 7742494561645080,
'extraData': HexBytes('0x68747470733a2f2f7777772e6b7279707465782e6f7267'),
'gasLimit': 30029122,
'gasUsed': 30025257,
'hash': HexBytes('0x9b83c12c69edb74f6c8dd5d052765c1adf940e320bd1291696e6fa07829eee71'),
'logsBloom': HexBytes('0x24e74ad77d9a2b27bdb8f6d6f7f1cffdd8cfb47fdebd433f011f7dfcfbb7db638fadd5ff66ed134ede2879ce61149797fbcdf7b74f6b7de153ec61bdaffeeb7b59c3ed771a2fe9eaed8ac70e335e63ff2bfe239eaff8f94ca642fdf7ee5537965be99a440f53d2ce057dbf9932be9a7b9a82ffdffe4eeee1a66c4cfb99fe4540fbff936f97dde9f6bfd9f8cefda2fc174d23dfdb7d6f7dfef5f754fe6a7eec92efdbff779b5feff3beafebd7fd6e973afebe4f5d86f3aafb1f73bf1e1d0cdd796d89827edeffe8fb6ae6d7bf639ec5f5ff4c32f31f6b525b676c7cdf5e5c75bfd5b7bd1928b6f43aac7fa0f6336576e5f7b7dfb9e8ebbe6f6efe2f9dfe8b3f56'),
'miner': '0x7777788200B672A42421017F65EDE4Fc759564C8',
'mixHash': HexBytes('0x9620b46a81a4795cf4449d48e3270419f58b09293a5421205f88179b563f815a'),
'nonce': HexBytes('0xb223da049adf2216'),
'number': 12965000,
'parentHash': HexBytes('0x3de6bb3849a138e6ab0b83a3a00dc7433f1e83f7fd488e4bba78f2fe2631a633'),
'receiptsRoot': HexBytes('0x8a8865cd785e2e9dfce7da83aca010b10b9af2abbd367114b236f149534c821d'),
'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
'size': 137049,
'stateRoot': HexBytes('0x41cf6e8e60fd087d2b00360dc29e5bfb21959bce1f4c242fd1ad7c4da968eb87'),
'timestamp': 1628166822,
'totalDifficulty': 28494409340649014490153,
'transactions': [
...
],
'transactionsRoot': HexBytes('0xdfcb68d3a3c41096f4a77569db7956e0a0e750fad185948e54789ea0e51779cb'),
'uncles': []
})
>>> web3.eth.block_number
12965000
>>> len(chain)
12965001 # always +1 to the current block number, because the first block is zero
>>> chain[0] == web3.eth.get_block(0)
True
# for negative index values, the block returned is relative to the most recently mined block
>>> chain[-1] == web3.eth.get_block('latest')
True
거래 데이터에 접근하기
로컬 거래 기록
TxHistory
컨테이너는 history
로 사용 가능하며, Brownie 세션 중에 브로드캐스트된 모든 거래를 보유합니다. 호출할 때 변수에 할당하지 않았다면, 이를 사용하여 TransactionReceipt
객체에 접근할 수 있습니다.
>>> history
[
<Transaction object '0xe803698b0ade1598c594b2c73ad6a656560a4a4292cc7211b53ffda4a1dbfbe8'>,
<Transaction object '0xa7616a96ef571f1791586f570017b37f4db9decb1a5f7888299a035653e8b44b'>
]
특정 트랜잭션을 키-값 쌍이나 람다 함수로 필터링하려면 history.filter
를 사용할 수 있습니다:
>>> history.filter(sender=accounts[0], value="1 ether")
[<Transaction object '0xe803698b0ade1598c594b2c73ad6a656560a4a4292cc7211b53ffda4a1dbfbe8'>]
>>> history.filter(key=lambda k: k.nonce < 2)
[<Transaction '0x03569ee152b04ba5b55c2bf05f99f7ec153db715acfe0c1600f144ded58f31fe'>, <Transaction '0x42193c0ff7007c6e2a5e5572a3c6b5706cd133d21e30e5826add3d971134504c'>]
다른 거래
chain.get_transaction
을 사용하여 모든 거래에 대한TransactionReceipt
개체를 가져옵니다.
>>> chain.get_transaction('0xf598d43ef34a48478f3bb0ad969c6735f416902c4eb1eb18ebebe0fca786105e')
<Transaction '0xf598d43ef34a48478f3bb0ad969c6735f416902c4eb1eb18ebebe0fca786105e'>
이는 보류 중인 거래에 대해서도 작동합니다. 거래가 아직 확인되지 않은 경우 콘솔 내에서 거래 해시가 노란색으로 표시됩니다.
개발 체인 조작
Brownie는 로컬 개발 환경으로 ganache-cli를 사용하도록 설계되었습니다. 채굴, 스냅샷 기능 및 시간 여행과 같은 기능은 Chain
객체를 통해 액세스할 수 있습니다.
새 블록 채굴
Ganache는 거래를 브로드캐스트할 때마다 새 블록을 채굴하는 것이 기본 동작입니다. chain.mine
메서드를 사용하여 빈 블록을 채굴할 수 있습니다:
>>> web3.eth.block_number
0
>>> chain.mine(50)
50
>>> web3.eth.block_number
50
시간 여행
현재 epoch 시간을 확인하려면 chain.time
을 호출할 수 있습니다:
>>> chain.time()
1500000000
시계를 빨리 감기 위해서는 chain.sleep
를 호출하십시오.
>>> chain.sleep(31337)
>>> chain.time()
1500031337
잠자는 것은 새로운 블록을 채굴하지 않는다는 것을 기억하세요. block.timestamp
에 의존하는 계약 보기 기능은 거래를 수행하거나 chain.mine
을 호출할 때까지 영향을 받지 않습니다.
스냅샷
현재 블록체인 상태의 스냅샷을 촬영하려면 chain.snapshot
을 사용하십시오:
>>> chain.snapshot()
>>> accounts[0].balance()
100000000000000000000
>>> accounts[0].transfer(accounts[1], "10 ether")
Transaction sent: 0xd5d3b40eb298dfc48721807935eda48d03916a3f48b51f20bcded372113e1dca
Transaction confirmed - block: 5 gas used: 21000 (100.00%)
<Transaction object '0xd5d3b40eb298dfc48721807935eda48d03916a3f48b51f20bcded372113e1dca'>
나중에 chain.revert
를 사용하여 이 상태로 돌아갈 수 있습니다.:
>>> accounts[0].balance()
89999580000000000000
>>> chain.revert()
4
>>> accounts[0].balance()
100000000000000000000
되돌리기는 스냅샷을 소모하지 않습니다. 필요할 때마다 동일한 스냅샷으로 돌아갈 수 있습니다. 하지만 새로운 스냅샷을 찍으면 이전 스냅샷은 더 이상 접근할 수 없습니다.
genesis 상태로 돌아가려면 chain.reset
을 사용하세요.
>>> web3.eth.block_number
6
>>> chain.reset()
>>> web3.eth.block_number
0
실행 취소 / 다시 실행
스냅샷을 함께 사용하여 최근 거래를 앞뒤로 이동하려면 chain.undo
및 chain.redo
을 사용할 수 있습니다. 이는 대화형 테스트 디버깅 중에 특히 유용합니다.
>>> accounts[0].transfer(accounts[1], "1 ether")
Transaction sent: 0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05
Gas price: 0.0 gwei Gas limit: 6721975
Transaction confirmed - Block: 1 Gas used: 21000 (0.31%)
<Transaction '0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05'>
>>> chain.undo()
0
>>> chain.redo()
Transaction sent: 0x8c166b66b356ad7f5c58337973b89950f03105cdae896ac66f16cdd4fc395d05
Gas price: 0.0 gwei Gas limit: 6721975
Transaction confirmed - Block: 1 Gas used: 21000 (0.31%)
chain.snapshot
과 chain.revert
는 실행 취소 버퍼를 지웁니다.
'블록체인 (Block Chain) > 이더리움' 카테고리의 다른 글
[브라우니 (Brownie)] 14. 데이터 유형 (0) | 2023.06.16 |
---|---|
[브라우니 (Brownie)] 13. 트랜잭션 검사 및 디버깅하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 11. 트랜잭션 가스 가격 설정하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 10. 컨트랙트로 작업하기 (0) | 2023.06.16 |
[브라우니 (Brownie)] 9. 계정으로 작업하기 (0) | 2023.06.16 |