Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

feat(chain): Arweave integration #262

Merged
merged 13 commits into from
May 16, 2022
Merged
76 changes: 76 additions & 0 deletions chain/arweave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import '../common/eager_offset'
import { Bytes } from '../common/collections'

// Most types from this namespace are direct mappings or adaptations from:
// https://github.com/ChainSafe/firehose-arweave/blob/master/proto/sf/arweave/type/v1/type.proto
export namespace arweave {
/**
* A key-value pair for arbitrary metadata
*/
export class Tag {
constructor(public name: Bytes, public value: Bytes) {}
}

export class ProofOfAccess {
constructor(
public option: string,
public txPath: Bytes,
public dataPath: Bytes,
public chunk: Bytes,
) {}
}

/**
* An Arweave block.
*/
export class Block {
constructor(
public timestamp: u64,
public lastRetarget: u64,
public height: u64,
public indepHash: Bytes,
public nonce: Bytes,
public previousBlock: Bytes,
public diff: Bytes,
public hash: Bytes,
public txRoot: Bytes,
public txs: Bytes[],
public walletList: Bytes,
public rewardAddr: Bytes,
public tags: Tag[],
public rewardPool: Bytes,
public weaveSize: Bytes,
public blockSize: Bytes,
public cumulativeDiff: Bytes,
public hashListMerkle: Bytes,
public poa: ProofOfAccess,
) {}
}

/**
* An Arweave transaction
*/
export class Transaction {
constructor(
public format: u32,
public id: Bytes,
public lastTx: Bytes,
public owner: Bytes,
public tags: Tag[],
public target: Bytes,
public quantity: Bytes,
public data: Bytes,
public dataSize: Bytes,
public dataRoot: Bytes,
public signature: Bytes,
public reward: Bytes,
) {}
}

/**
* An Arweave transaction with block ptr
*/
export class TransactionWithBlockPtr {
constructor(public tx: Transaction, public block: Block) {}
}
}
38 changes: 37 additions & 1 deletion global/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Wrapped,
} from '../common/collections'
import { JSONValue, Value } from '../common/value'
import { arweave } from '../chain/arweave'
import { ethereum } from '../chain/ethereum'
import { near } from '../chain/near'
import { cosmos } from '../chain/cosmos'
Expand Down Expand Up @@ -216,7 +217,25 @@ export enum TypeId {
```
*/

// Reserved discriminant space for a future blockchain type IDs: [2,500, 3,499]
// Reserved discriminant space for Tendermint type IDs: [2,500, 3,499]
ArweaveBlock = 2500,
ArweaveProofOfAccess = 2501,
ArweaveTag = 2502,
ArweaveTagArray = 2503,
ArweaveTransaction = 2504,
ArweaveTransactionArray = 2505,
ArweaveTransactionWithBlockPtr = 2506,
/*
Continue to add more Arweave type IDs here. e.g.:
```
NextArweaveType = 2507,
AnotherArweaveType = 2508,
...
LastArweaveType = 3499,
```
*/

// Reserved discriminant space for a future blockchain type IDs: [3,500, 4,499]
}

export function id_of_type(typeId: TypeId): usize {
evaporei marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -528,6 +547,23 @@ export function id_of_type(typeId: TypeId): usize {
return idof<cosmos.ValidatorUpdate>()
case TypeId.CosmosVersionParams:
return idof<cosmos.VersionParams>()
/**
* Arweave type ids
*/
case TypeId.ArweaveBlock:
return idof<arweave.Block>()
case TypeId.ArweaveProofOfAccess:
return idof<arweave.ProofOfAccess>()
case TypeId.ArweaveTag:
return idof<arweave.Tag>()
case TypeId.ArweaveTagArray:
return idof<Array<arweave.Tag>>()
case TypeId.ArweaveTransaction:
return idof<arweave.Transaction>()
case TypeId.ArweaveTransactionArray:
return idof<Array<arweave.Transaction>>()
case TypeId.ArweaveTransactionWithBlockPtr:
return idof<arweave.TransactionWithBlockPtr>()
default:
return 0
}
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Side-effect to evaluate eagerly the offset of stub AS runtime
import './common/eager_offset'
// Arweave support
export * from './chain/arweave'
// Ethereum support
export * from './chain/ethereum'
// NEAR support
Expand Down
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function main() {
fs.copyFileSync('common/json.ts', 'test/temp_lib/common/json.ts')
fs.copyFileSync('common/numbers.ts', 'test/temp_lib/common/numbers.ts')
fs.copyFileSync('common/value.ts', 'test/temp_lib/common/value.ts')
fs.copyFileSync('chain/arweave.ts', 'test/temp_lib/chain/arweave.ts')
fs.copyFileSync('chain/ethereum.ts', 'test/temp_lib/chain/ethereum.ts')
fs.copyFileSync('chain/near.ts', 'test/temp_lib/chain/near.ts')
fs.copyFileSync('chain/cosmos.ts', 'test/temp_lib/chain/cosmos.ts')
Expand Down Expand Up @@ -62,6 +63,7 @@ async function main() {
fs.unlinkSync('test/temp_lib/common/numbers.ts')
fs.unlinkSync('test/temp_lib/common/value.ts')
fs.rmdirSync('test/temp_lib/common')
fs.unlinkSync('test/temp_lib/chain/arweave.ts')
fs.unlinkSync('test/temp_lib/chain/ethereum.ts')
fs.unlinkSync('test/temp_lib/chain/near.ts')
fs.unlinkSync('test/temp_lib/chain/cosmos.ts')
Expand Down