Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/transaction metadata #523

Merged
merged 13 commits into from
Apr 19, 2022
Merged
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
18 changes: 1 addition & 17 deletions Sources/web3swift/Transaction/EIP1559Envelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public struct EIP1559Envelope: EIP2718Envelope {
public var maxFeePerGas: BigUInt
public var accessList: [AccessListEntry] // from EIP-2930

/// EIP-1159 trnsactions do not have a gasPrice parameter
/// However, it appears that some nodes report a gasPrice, even for EIP-1159 transactions
/// thus for a temporary workaround we capture and store gasPrice if initialized from a JSON transaction
/// decided form a node. This is currently needed for Oracle to work
private var gasPrice: BigUInt = 0

// for CustomStringConvertible
public var description: String {
var toReturn = ""
Expand Down Expand Up @@ -76,9 +70,6 @@ public struct EIP1559Envelope: EIP2718Envelope {
value: value,
data: data,
gasLimit: gasLimit,
// MARK: workaround for gasPrice coming from nodes for EIP-1159 - this allows Oracle to work for now
gasPrice: gasPrice,

maxFeePerGas: maxFeePerGas,
maxPriorityFeePerGas: maxPriorityFeePerGas,
accessList: accessList
Expand All @@ -94,8 +85,6 @@ public struct EIP1559Envelope: EIP2718Envelope {
maxFeePerGas = val.maxFeePerGas ?? maxFeePerGas
maxPriorityFeePerGas = val.maxPriorityFeePerGas ?? maxPriorityFeePerGas
accessList = val.accessList ?? accessList
// MARK: workaround for gasPrice coming from nodes for EIP-1159 - this allows Oracle to work for now
gasPrice = val.gasPrice ?? gasPrice
}
}

Expand All @@ -117,8 +106,6 @@ extension EIP1559Envelope {
case v
case r
case s
// MARK: workaround for gasPrice coming from nodes for EIP-1159 - this allows Oracle to work for now
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's 1559 mistyped to 1159

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed, but it's a line being deleted, so no need to fix.

case gasPrice
}

public init?(from decoder: Decoder) throws {
Expand Down Expand Up @@ -147,9 +134,6 @@ extension EIP1559Envelope {
self.to = ethAddr
}

// MARK: workaround for gasPrice coming from nodes for EIP-1159 - this allows Oracle to work for now
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's 1559 mistyped to 1159

Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy paste when I did the workaround... this PR is removing it

self.gasPrice = try container.decodeHexIfPresent(BigUInt.self, forKey: .gasPrice) ?? 5000000000

self.value = try container.decodeHexIfPresent(BigUInt.self, forKey: .value) ?? 0
self.maxPriorityFeePerGas = try container.decodeHexIfPresent(BigUInt.self, forKey: .maxPriorityFeePerGas) ?? 0
self.maxFeePerGas = try container.decodeHexIfPresent(BigUInt.self, forKey: .maxFeePerGas) ?? 0
Expand Down Expand Up @@ -212,7 +196,7 @@ extension EIP1559Envelope {

// swiftlint:disable force_unwrapping
switch rlpItem[RlpKey.destination.rawValue]!.content {
// swiftlint:enable force_unwrapping
// swiftlint:enable force_unwrapping
case .noItem:
self.to = EthereumAddress.contractDeploymentAddress()
case .data(let addressData):
Expand Down
57 changes: 57 additions & 0 deletions Sources/web3swift/Transaction/EthereumMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package: web3swift
// Created by Alex Vlasov.
// Copyright © 2018 Alex Vlasov. All rights reserved.
//
// Additions for metadata by Mark Loit 2022

import Foundation
import BigInt

/// This structure holds additional data
/// returned by nodes when reading a transaction
/// from the blockchain. The data here is not
/// part of the transaction itself
public struct EthereumMetadata {

/// hash for the block that contains this transaction on chain
var blockHash: Data?

/// block number for the block containing this transaction on chain
var blockNumber: BigUInt?

/// index for this transaction within the containing block
var transactionIndex: UInt?

/// hash for this transaction as returned by the node [not computed]
/// this can be used for validation against the computed hash returned
/// by the transaction envelope.
var transactionHash: Data?

/// gasPrice value returned by the node
/// note this is a duplicate value for legacy and EIP-2930 transaction types
/// but is included here since EIP-1559 does not contain a `gasPrice`, but
/// nodes still report the value.
var gasPrice: BigUInt?
}

public extension EthereumMetadata {
private enum CodingKeys: String, CodingKey {
case blockHash
case blockNumber
case transactionIndex
case transactionHash
case gasPrice
}

/// since metadata realistically can only come when a transaction is created from
/// JSON returned by a node, we only provide an intializer from JSON
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.blockHash = try container.decodeHexIfPresent(Data.self, forKey: .blockHash)
self.transactionHash = try container.decodeHexIfPresent(Data.self, forKey: .transactionHash)
self.transactionIndex = try container.decodeHexIfPresent(UInt.self, forKey: .transactionIndex)
self.blockNumber = try container.decodeHexIfPresent(BigUInt.self, forKey: .blockNumber)
self.gasPrice = try container.decodeHexIfPresent(BigUInt.self, forKey: .gasPrice)
}
}
11 changes: 7 additions & 4 deletions Sources/web3swift/Transaction/EthereumTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public struct EthereumTransaction: CustomStringConvertible {
/// and type specific implementation
internal var envelope: AbstractEnvelope

/// storage container for additional metadata returned by the node
/// when a transaction is decoded form a JSON stream
public var meta: EthereumMetadata?

// convenience accessors to the common envelope fields
// everything else should come from getOpts/setOpts
/// The type of the transacton being represented, see TransactionType enum
Expand Down Expand Up @@ -227,6 +231,8 @@ extension EthereumTransaction: Decodable {
public init(from decoder: Decoder) throws {
guard let env = try EnvelopeFactory.createEnvelope(from: decoder) else { throw Web3Error.dataError }
self.envelope = env
// capture any metadata that might be present
self.meta = try EthereumMetadata(from: decoder)
}
}

Expand Down Expand Up @@ -330,10 +336,7 @@ extension EthereumTransaction {
guard let env = self.envelope as? EIP2930Envelope else { preconditionFailure("Unable to downcast to EIP2930Envelope") }
return env.parameters.gasPrice ?? 0
case .eip1559:
// MARK: workaround for gasPrice coming from nodes for EIP-1159 - this allows Oracle to work for now
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's 1559 mistyped to 1159

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see previous comments

guard let env = self.envelope as? EIP1559Envelope else { preconditionFailure("Unable to downcast to EIP1559Envelope") }
return env.parameters.gasPrice ?? 0
// preconditionFailure("EIP1559Envelope has no member gasPrice")
preconditionFailure("EIP1559Envelope has no member gasPrice")
}
}
set(value) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/web3swift/Web3/Web3+GasOracle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension Web3 {
return transaction
}
}
.map { $0.gasPrice }
.map { $0.meta?.gasPrice ?? 0 }

return calculatePercentiles(for: lastNthBlockGasPrice)
}
Expand Down
2 changes: 1 addition & 1 deletion web3swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'web3swift'
spec.version = '2.5.1'
spec.version = '2.6.0'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea why on earth GitHub is flagging this as a change being made here, as develop is 2.6.0 not 2.5.1. This popped up as a result of updating the PR to the latest version of develop when 2.6.0 was released.

spec.ios.deployment_target = "9.0"
spec.osx.deployment_target = "10.12"
spec.license = { :type => 'Apache License 2.0', :file => 'LICENSE.md' }
Expand Down
4 changes: 4 additions & 0 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
6049F416280616FC00DFE624 /* LegacyEnvelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6049F40F280616FC00DFE624 /* LegacyEnvelope.swift */; };
6049F4182806171300DFE624 /* Web3+Signing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6049F4172806171300DFE624 /* Web3+Signing.swift */; };
604FA4FF27ECBDC80021108F /* DataConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 604FA4FE27ECBDC80021108F /* DataConversionTests.swift */; };
60C1786E2809BA0C0083F064 /* EthereumMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60C1786D2809BA0C0083F064 /* EthereumMetadata.swift */; };
CB50A52827060BD600D7E39B /* EIP712Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB50A52727060BD600D7E39B /* EIP712Tests.swift */; };
D606A56B279F5D59003643ED /* web3swiftDecodeSolidityErrorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D606A56A279F5D59003643ED /* web3swiftDecodeSolidityErrorType.swift */; };
D6A3D9B827F1E785009E3BCF /* ABIEncoderSoliditySha3Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3D9B727F1E785009E3BCF /* ABIEncoderSoliditySha3Test.swift */; };
Expand Down Expand Up @@ -433,6 +434,7 @@
6049F40F280616FC00DFE624 /* LegacyEnvelope.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LegacyEnvelope.swift; sourceTree = "<group>"; };
6049F4172806171300DFE624 /* Web3+Signing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+Signing.swift"; sourceTree = "<group>"; };
604FA4FE27ECBDC80021108F /* DataConversionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataConversionTests.swift; sourceTree = "<group>"; };
60C1786D2809BA0C0083F064 /* EthereumMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumMetadata.swift; sourceTree = "<group>"; };
CB50A52727060BD600D7E39B /* EIP712Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EIP712Tests.swift; sourceTree = "<group>"; };
CB50A52927060C5300D7E39B /* EIP712.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EIP712.swift; sourceTree = "<group>"; };
D606A56A279F5D59003643ED /* web3swiftDecodeSolidityErrorType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = web3swiftDecodeSolidityErrorType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -720,6 +722,7 @@
6049F40F280616FC00DFE624 /* LegacyEnvelope.swift */,
3AA8153D2276E44100F5DB52 /* BloomFilter.swift */,
3AA8153F2276E44100F5DB52 /* EthereumTransaction.swift */,
60C1786D2809BA0C0083F064 /* EthereumMetadata.swift */,
);
path = Transaction;
sourceTree = "<group>";
Expand Down Expand Up @@ -1349,6 +1352,7 @@
3AA815C12276E44100F5DB52 /* Web3+BrowserFunctions.swift in Sources */,
3AA815E82276E44100F5DB52 /* Extensions.swift in Sources */,
3AA815FE2276E44100F5DB52 /* Web3+ERC820.swift in Sources */,
60C1786E2809BA0C0083F064 /* EthereumMetadata.swift in Sources */,
3AA815DB2276E44100F5DB52 /* Web3+TxPool.swift in Sources */,
3AA8160A2276E44100F5DB52 /* Web3+ERC1594.swift in Sources */,
3AA815FB2276E44100F5DB52 /* Promise+Web3+Personal+UnlockAccount.swift in Sources */,
Expand Down