-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:AcalaNetwork/boka
* 'master' of github.com:AcalaNetwork/boka: fix warning (#117) keystore (#115) setup host calls and is-authorized invocation (#114)
- Loading branch information
Showing
26 changed files
with
506 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Codec | ||
import Foundation | ||
import PolkaVM | ||
|
||
public protocol IsAuthorizedFunction { | ||
func invoke( | ||
config: ProtocolConfigRef, | ||
package: WorkPackage, | ||
coreIndex: CoreIndex | ||
) throws -> Result<Data, WorkResultError> | ||
} | ||
|
||
extension IsAuthorizedFunction { | ||
public func invoke(config: ProtocolConfigRef, package: WorkPackage, coreIndex: CoreIndex) throws -> Result<Data, WorkResultError> { | ||
var ctx = IsAuthorizedContext() | ||
let args = try JamEncoder.encode(package) + JamEncoder.encode(coreIndex) | ||
let (exitReason, _, _, output) = invokePVM( | ||
config: config, | ||
blob: package.authorizationCodeHash.data, | ||
pc: 0, | ||
gas: config.value.workPackageAuthorizerGas, | ||
argumentData: args, | ||
ctx: &ctx | ||
) | ||
switch exitReason { | ||
case .outOfGas: | ||
return .failure(.outOfGas) | ||
case .panic(.trap): | ||
return .failure(.panic) | ||
default: | ||
if let output { | ||
return .success(output) | ||
} else { | ||
return .failure(.panic) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import Utils | ||
|
||
public class Validator { | ||
private let blockchain: Blockchain | ||
private var keystore: KeyStore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public protocol HostCallContext<ContextType> { | ||
associatedtype ContextType | ||
|
||
var context: ContextType { get set } | ||
|
||
/// host-call dispatch function | ||
func dispatch(index: UInt32, state: VMState) -> ExecOutcome | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public protocol HostCallFunction { | ||
static var identifier: UInt8 { get } | ||
static var gasCost: UInt8 { get } | ||
|
||
associatedtype Input | ||
associatedtype Output | ||
|
||
static func call(state: VMState, input: Input) throws -> Output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Gas: HostCallFunction { | ||
public static var identifier: UInt8 { 0 } | ||
public static var gasCost: UInt8 { 10 } | ||
|
||
public typealias Input = Void | ||
|
||
public typealias Output = Void | ||
|
||
public static func call(state: VMState, input _: Input) -> Output { | ||
state.writeRegister(Registers.Index(raw: 0), UInt32(bitPattern: Int32(state.getGas() & 0xFFFF_FFFF))) | ||
state.writeRegister(Registers.Index(raw: 1), UInt32(bitPattern: Int32(state.getGas() >> 32))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public enum HostCallResultCode: UInt32 { | ||
/// NONE = 2^32 − 1: The return value indicating an item does not exist. | ||
case NONE = 0xFFFF_FFFF | ||
/// WHAT = 2^32 − 2: Name unknown. | ||
case WHAT = 0xFFFF_FFFE | ||
/// OOB = 2^32 − 3: The return value for when a memory index is provided for reading/writing which is not accessible. | ||
case OOB = 0xFFFF_FFFD | ||
/// WHO = 2^32 − 4: Index unknown. | ||
case WHO = 0xFFFF_FFFC | ||
/// FULL = 2^32 − 5: Storage full. | ||
case FULL = 0xFFFF_FFFB | ||
/// CORE = 2^32 − 6: Core index unknown. | ||
case CORE = 0xFFFF_FFFA | ||
/// CASH = 2^32 − 7: Insufficient funds. | ||
case CASH = 0xFFFF_FFF9 | ||
/// LOW = 2^32 − 8: Gas limit too low. | ||
case LOW = 0xFFFF_FFF8 | ||
/// HIGH = 2^32 − 9: Gas limit too high. | ||
case HIGH = 0xFFFF_FFF7 | ||
/// HUH = 2^32 − 10: The item is already solicited or cannot be forgotten. | ||
case HUH = 0xFFFF_FFF6 | ||
/// OK = 0: The return value indicating general success. | ||
case OK = 0 | ||
} | ||
|
||
// Inner pvm invocations have their own set of result codes👇 | ||
public enum HostCallResultCodeInner: UInt32 { | ||
/// HALT = 0: The invocation completed and halted normally. | ||
case HALT = 0 | ||
/// PANIC = 1: The invocation completed with a panic. | ||
case PANIC = 1 | ||
/// FAULT = 2: The invocation completed with a page fault. | ||
case FAULT = 2 | ||
/// HOST = 3: The invocation completed with a host-call fault. | ||
case HOST = 3 | ||
} |
19 changes: 19 additions & 0 deletions
19
PolkaVM/Sources/PolkaVM/InvocationContexts/IsAuthorizedContext.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Foundation | ||
|
||
public class IsAuthorizedContext: HostCallContext { | ||
public typealias ContextType = Void | ||
|
||
public var context: ContextType = () | ||
|
||
public init() {} | ||
|
||
public func dispatch(index: UInt32, state: VMState) -> ExecOutcome { | ||
if index == Gas.identifier { | ||
Gas.call(state: state, input: ()) | ||
} else { | ||
state.consumeGas(10) | ||
state.writeRegister(Registers.Index(raw: 0), HostCallResultCode.WHAT.rawValue) | ||
} | ||
return .continued | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Foundation | ||
import TracingUtils | ||
|
||
private let logger = Logger(label: "invokePVM") | ||
|
||
/// common PVM program-argument invocation function | ||
public func invokePVM(config: PvmConfig, blob: Data, pc: UInt32, gas: UInt64, argumentData: Data?, | ||
ctx: inout some HostCallContext) -> (ExitReason, VMState?, UInt64?, Data?) | ||
{ | ||
do { | ||
let state = try VMState(standardProgramBlob: blob, pc: pc, gas: gas, argumentData: argumentData) | ||
let engine = Engine(config: config, hostCallContext: ctx) | ||
let exitReason = engine.execute(program: state.program, state: state) | ||
|
||
switch exitReason { | ||
case .outOfGas: | ||
return (.outOfGas, state, nil, nil) | ||
case .halt: | ||
let (reg10, reg11) = state.readRegister(Registers.Index(raw: 10), Registers.Index(raw: 11)) | ||
// TODO: check if this is correct | ||
let output = try? state.readMemory(address: reg10, length: Int(reg11 - reg10)) | ||
return (.halt, state, UInt64(state.getGas()), output ?? Data()) | ||
default: | ||
return (.panic(.trap), state, nil, nil) | ||
} | ||
} catch let e as StandardProgram.Error { | ||
logger.error("standard program initialization failed: \(e)") | ||
return (.panic(.trap), nil, nil, nil) | ||
} catch let e { | ||
logger.error("unknown error: \(e)") | ||
return (.panic(.trap), nil, nil, nil) | ||
} | ||
} |
Oops, something went wrong.