Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# .dockerignore is a symbolic link to .gitignore
.bin
.vscode
build
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.4'
services:
matchstick:
image: cartesi/subgraph-matchstick:dev
build:
dockerfile: tests/.docker
target: base
container_name: cartesi-subgraph-matchstick
volumes:
- .:/matchstick
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
"prepare:kovan": "hardhat --network kovan subgraph --input-file subgraph.testnet.template.yaml --output-file subgraph.kovan.yaml",
"prepare:mainnet": "hardhat --network mainnet subgraph",
"prettier": "prettier --check **/*.ts",
"test": "graph test"
"test": "graph test",
"test:docker": "docker-compose -f docker-compose.test.yaml run --rm matchstick",
"test:docker:build": "docker-compose -f docker-compose.test.yaml build"
},
"dependencies": {
"@cartesi/pos": "^1.1.2",
"@cartesi/pos-1.0": "npm:@cartesi/pos@1.0.0",
"@cartesi/staking-pool": "^1.0.0",
"@cartesi/util": "^1.0.1",
"@graphprotocol/graph-cli": "0.23.2",
"@graphprotocol/graph-ts": "0.23.1",
"matchstick-as": "^0.2.0"
"@graphprotocol/graph-cli": "^0.30.2",
"@graphprotocol/graph-ts": "^0.27.0",
"matchstick-as": "^0.5.0"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
Expand All @@ -48,6 +50,6 @@
"prettier": "^2.4.0",
"rimraf": "^3.0.2",
"ts-node": "^10.3.1",
"typescript": "^4.4.4"
"typescript": "^4.6.3"
}
}
4 changes: 4 additions & 0 deletions schema.er
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ releasingTimestamp
balance
totalBlocks
totalReward
totalTransactionFee
+pool

[Node]
Expand Down Expand Up @@ -52,6 +53,8 @@ reward
difficulty
gasPrice
gasLimit
gasUsed
transactionFee

[Chain]
*id
Expand Down Expand Up @@ -134,6 +137,7 @@ totalNodes
totalStaked
totalBlocks
totalReward
totalTransactionFee
totalProtocols
totalChains

Expand Down
4 changes: 4 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Summary @entity {
totalStaked: BigInt!
totalBlocks: Int!
totalReward: BigInt!
totalTransactionFee: BigInt!
totalProtocols: Int!
totalChains: Int!
}
Expand All @@ -21,6 +22,7 @@ type User @entity {
balance: BigInt!
totalBlocks: Int!
totalReward: BigInt!
totalTransactionFee: BigInt!
pool: StakingPool
}

Expand Down Expand Up @@ -161,4 +163,6 @@ type Block @entity {
difficulty: BigInt
gasPrice: BigInt!
gasLimit: BigInt!
gasUsed: BigInt!
transactionFee: BigInt!
}
70 changes: 3 additions & 67 deletions src/block-1.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,11 @@
// under the License.

import { Rewarded } from "../generated/PoS-1.0/PoS"
import { BlockProduced } from "../generated/BlockSelector-1.0/BlockSelector"
import { Block } from "../generated/schema"
import * as chains from "./chain"
import * as nodes from "./node"
import * as summary from "./summary"
import * as users from "./user"
import { handleRewardedInner, handleBlockProduced } from "./block-common"

export function handleRewarded(event: Rewarded): void {
let reward = event.params.userReward.plus(event.params.beneficiaryReward)

Copy link
Author

@alexandre-abrioux alexandre-abrioux May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refacto: similar code from block.ts and block-1.0.ts have been moved to block-common.ts

// handle user
let user = users.loadOrCreate(event.params.user)
user.totalBlocks++
user.totalReward = user.totalReward.plus(reward)
user.save()

// handle node
let node = nodes.loadOrCreate(
event.params.user,
event.params.worker,
event.block.timestamp
)
node.totalBlocks++
node.status = "Authorized"
node.totalReward = node.totalReward.plus(reward)
node.save()

// handle chain
let posAddress = event.address.toHex()
let chain = chains.loadOrCreate(
posAddress,
event.params.index.toI32(),
event.block.timestamp
)
chain.totalBlocks++
chain.totalReward = chain.totalReward.plus(reward)
chain.save()

// Rewarded is always called before BlockProduced, so create Block here
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = new Block(event.transaction.hash.toHex())
block.timestamp = event.block.timestamp
block.gasPrice = event.transaction.gasPrice
block.gasLimit = event.transaction.gasLimit
}
block.chain = chain.id
block.reward = reward
block.producer = event.params.user.toHex()
block.node = event.params.worker.toHex()
block.save()

// handle global summary
let s = summary.loadOrCreate()
s.totalBlocks++
s.totalReward = s.totalReward.plus(reward)
s.save()
handleRewardedInner<Rewarded>(event, reward)
}

export function handleBlockProduced(event: BlockProduced): void {
// load Block and fill other properties
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = new Block(event.transaction.hash.toHex())
block.timestamp = event.block.timestamp
block.gasPrice = event.transaction.gasPrice
block.gasLimit = event.transaction.gasLimit
}
block.number = event.params.blockNumber.toI32()
block.difficulty = event.params.difficulty
block.save()
}
export { handleBlockProduced }
92 changes: 92 additions & 0 deletions src/block-common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts"
import { Block } from "../generated/schema"
import * as nodes from "./node"
import * as chains from "./chain"
import * as users from "./user"
import * as summary from "./summary"
import { BlockProduced } from "../generated/BlockSelector/BlockSelector"

function createBlock(event: ethereum.Event): Block {
const block = new Block(event.transaction.hash.toHex())
block.timestamp = event.block.timestamp
block.gasPrice = event.transaction.gasPrice
block.gasLimit = event.transaction.gasLimit
block.gasUsed = (event.receipt as ethereum.TransactionReceipt).gasUsed
block.transactionFee = block.gasUsed.times(block.gasPrice)
return block
}

abstract class RewardedCommonParams {
abstract get index(): BigInt
abstract get worker(): Address
abstract get user(): Address
}

abstract class RewardedCommon extends ethereum.Event {
abstract get params(): RewardedCommonParams
}

export function handleRewardedInner<T extends RewardedCommon>(
event: T,
reward: BigInt
): void {
// handle node
let node = nodes.loadOrCreate(
event.params.user,
event.params.worker,
event.block.timestamp
)
node.totalBlocks++
node.status = "Authorized"
node.totalReward = node.totalReward.plus(reward)
node.save()

// handle chain
let posAddress = event.address.toHex()
let chain = chains.loadOrCreate(
posAddress,
event.params.index.toI32(),
event.block.timestamp
)
chain.totalBlocks++
chain.totalReward = chain.totalReward.plus(reward)
chain.save()

// Rewarded is always called before BlockProduced, so create Block here
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = createBlock(event)
}
block.chain = chain.id
block.reward = reward
block.producer = event.params.user.toHex()
block.node = event.params.worker.toHex()
block.save()

// handle user
let user = users.loadOrCreate(event.params.user)
user.totalBlocks++
user.totalReward = user.totalReward.plus(reward)
user.totalTransactionFee = user.totalTransactionFee.plus(
block.transactionFee
)
Comment on lines +70 to +72
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this big code block that was moved, those 3 lines were added

user.save()

// handle global summary
let s = summary.loadOrCreate()
s.totalBlocks++
s.totalReward = s.totalReward.plus(reward)
s.totalTransactionFee = s.totalTransactionFee.plus(block.transactionFee)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this big code block that was moved, this line was also added

s.save()
}

export function handleBlockProduced(event: BlockProduced): void {
// load Block and fill other properties
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = createBlock(event)
}
block.number = event.params.blockNumber.toI32()
block.difficulty = event.params.difficulty
block.save()
}
69 changes: 3 additions & 66 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,12 @@
// under the License.

import { NewChain, Rewarded } from "../generated/PoS/PoS"
import { BlockProduced } from "../generated/BlockSelector/BlockSelector"
import { Block } from "../generated/schema"
import * as chains from "./chain"
import * as nodes from "./node"
import * as summary from "./summary"
import * as users from "./user"
import { handleBlockProduced, handleRewardedInner } from "./block-common"

export function handleRewarded(event: Rewarded): void {
let reward = event.params.reward

// handle user
let user = users.loadOrCreate(event.params.user)
user.totalBlocks++
user.totalReward = user.totalReward.plus(reward)
user.save()

// handle node
let node = nodes.loadOrCreate(
event.params.user,
event.params.worker,
event.block.timestamp
)
node.totalBlocks++
node.status = "Authorized"
node.totalReward = node.totalReward.plus(reward)
node.save()

// handle chain
let posAddress = event.address.toHex()
let chain = chains.loadOrCreate(
posAddress,
event.params.index.toI32(),
event.block.timestamp
)
chain.totalBlocks++
chain.totalReward = chain.totalReward.plus(reward)
chain.save()

// Rewarded is always called before BlockProduced, so create Block here
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = new Block(event.transaction.hash.toHex())
block.timestamp = event.block.timestamp
block.gasPrice = event.transaction.gasPrice
block.gasLimit = event.transaction.gasLimit
}
block.chain = chain.id
block.reward = reward
block.producer = event.params.user.toHex()
block.node = event.params.worker.toHex()
block.save()

// handle global summary
let s = summary.loadOrCreate()
s.totalBlocks++
s.totalReward = s.totalReward.plus(reward)
s.save()
handleRewardedInner<Rewarded>(event, reward)
}

export function handleNewChain(event: NewChain): void {
Expand All @@ -82,16 +31,4 @@ export function handleNewChain(event: NewChain): void {
chain.save()
}

export function handleBlockProduced(event: BlockProduced): void {
// load Block and fill other properties
let block = Block.load(event.transaction.hash.toHex())
if (block == null) {
block = new Block(event.transaction.hash.toHex())
block.timestamp = event.block.timestamp
block.gasPrice = event.transaction.gasPrice
block.gasLimit = event.transaction.gasLimit
}
block.number = event.params.blockNumber.toI32()
block.difficulty = event.params.difficulty
block.save()
}
export { handleBlockProduced }
1 change: 1 addition & 0 deletions src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function loadOrCreate(): Summary {
summary.totalStaked = BigInt.fromI32(0)
summary.totalBlocks = 0
summary.totalReward = BigInt.fromI32(0)
summary.totalTransactionFee = BigInt.fromI32(0)
summary.totalProtocols = 0
summary.totalChains = 0
summary.save()
Expand Down
1 change: 1 addition & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function loadOrCreate(address: Address): User {
user.balance = BigInt.fromI32(0)
user.totalBlocks = 0
user.totalReward = BigInt.fromI32(0)
user.totalTransactionFee = BigInt.fromI32(0)
user.save()

let s = summary.loadOrCreate()
Expand Down
Loading