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

Implement parachain inherents #2566

Merged
merged 125 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
ac5e507
temp
kishansagathiya May 20, 2022
2115a15
Implement Parachain Inherents
kishansagathiya May 23, 2022
298a2bd
clean up:
kishansagathiya May 23, 2022
02a931b
filling up more structs
kishansagathiya May 24, 2022
a0f8c1e
filling more structs
kishansagathiya May 24, 2022
d833b8b
more structs
kishansagathiya May 25, 2022
b11b9cf
fix lint
kishansagathiya May 25, 2022
2d48c7d
addressing some reviews
kishansagathiya May 30, 2022
5abb342
converted enums to VaryingDataType
kishansagathiya Jun 1, 2022
f548252
Update lib/runtime/wasmer/imports.go
kishansagathiya Jun 1, 2022
944ba17
Update lib/runtime/wasmer/imports.go
kishansagathiya Jun 1, 2022
e2d01c3
Update lib/runtime/wasmer/imports.go
kishansagathiya Jun 1, 2022
2973d33
Update lib/runtime/wasmer/imports.go
kishansagathiya Jun 1, 2022
6fd9518
Update lib/runtime/wasmer/imports.go
kishansagathiya Jun 1, 2022
3ecf9b1
Update lib/babe/parachain_inherents.go
kishansagathiya Jun 6, 2022
781d462
Update lib/babe/parachain_inherents.go
kishansagathiya Jun 6, 2022
d7e716b
addressed some of the reviews
kishansagathiya Jun 7, 2022
d1cb494
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Jun 7, 2022
e16faaf
some parachain inherent tests
kishansagathiya Jun 14, 2022
37a7c1d
more tests
kishansagathiya Jun 15, 2022
bad42ee
wip decode functionality
timwu20 Jun 16, 2022
123fbbe
wip encode custom vdt
timwu20 Jun 16, 2022
f46408c
refactor tests
timwu20 Jun 16, 2022
f5d0810
more tests, fix decode for nested vdt
timwu20 Jun 17, 2022
9e8ef6a
update readme, provide example of nested VDTs
timwu20 Jun 17, 2022
24977ea
add copyright
timwu20 Jun 17, 2022
4a4be32
fix lint
timwu20 Jun 17, 2022
034efdb
Merge branch 'tim/custom-vdt' of github.com:ChainSafe/gossamer into k…
kishansagathiya Jun 23, 2022
cdb5b71
temp
kishansagathiya Jun 27, 2022
7cd88b3
Fixed tests
kishansagathiya Jun 27, 2022
e3e1347
fix lint
kishansagathiya Jun 27, 2022
fc92757
little more changes
kishansagathiya Jun 28, 2022
74c8d2b
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Jun 28, 2022
6d7c387
fix lint
kishansagathiya Jun 28, 2022
40eb6fc
more fix up
kishansagathiya Jun 28, 2022
8a0c07b
make license
kishansagathiya Jul 4, 2022
f0f0522
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Jul 4, 2022
9522517
some more changes
kishansagathiya Jul 4, 2022
8b96d4f
more changes
kishansagathiya Jul 4, 2022
41b4ac9
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Jul 5, 2022
bc09949
update genesis file with latest runtime
kishansagathiya Jul 6, 2022
914374e
Revert "update genesis file with latest runtime"
kishansagathiya Jul 6, 2022
fbd59c5
modify genesis files with latest runtime
kishansagathiya Jul 6, 2022
e88438c
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Jul 11, 2022
bf2fe8a
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Jul 11, 2022
98b0356
temp
kishansagathiya Jul 12, 2022
c616756
modified genesis files
kishansagathiya Jul 13, 2022
937a05d
reverting genesis
kishansagathiya Jul 13, 2022
4f22128
more genesis changes
kishansagathiya Jul 13, 2022
3b5d06f
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Aug 29, 2022
99c1c0c
test scaled encoding of parachain inherents
kishansagathiya Aug 31, 2022
02981ba
test scaled encoding of inherents
kishansagathiya Sep 6, 2022
bc4601d
add parent header in parachain inherent data
kishansagathiya Sep 9, 2022
90e81be
fix lint
kishansagathiya Sep 9, 2022
a1f2b3e
reverting uncessary change
kishansagathiya Sep 9, 2022
66503a6
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 9, 2022
afc80f3
Update dot/types/inherents.go
kishansagathiya Sep 16, 2022
c0d9f9f
Update lib/babe/build.go
kishansagathiya Sep 16, 2022
9c2900e
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
74405cb
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 16, 2022
ae1617e
Update dot/types/inherents.go
kishansagathiya Sep 16, 2022
9452425
Update lib/babe/build.go
kishansagathiya Sep 16, 2022
6fb11f6
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 16, 2022
1407220
addressed some comments
kishansagathiya Sep 16, 2022
775aec1
addressed some reviews
kishansagathiya Sep 16, 2022
ec3f3d8
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
9d0bd03
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
0c48685
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
c7f4c46
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
a3d196e
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 16, 2022
ba6a995
addressed some of the review
kishansagathiya Sep 16, 2022
0d590fe
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Sep 16, 2022
895ac4d
addressed some reviews
kishansagathiya Sep 19, 2022
8ea0ddc
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 19, 2022
44fef33
addressed more changes
kishansagathiya Sep 19, 2022
ca11b5d
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Sep 19, 2022
99aa387
more reviews
kishansagathiya Sep 19, 2022
dbfb390
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 19, 2022
9474c83
fix lint
kishansagathiya Sep 19, 2022
7ccabb8
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 20, 2022
476c1d6
try
kishansagathiya Sep 20, 2022
7fbef75
removed phantom data comment
kishansagathiya Sep 20, 2022
b6fcb7c
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 20, 2022
eec8635
more reviews
kishansagathiya Sep 20, 2022
941c704
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 21, 2022
d1a8bf5
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 21, 2022
1b6bb12
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 21, 2022
b46cb1e
some review addressed
kishansagathiya Sep 21, 2022
37e0441
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Sep 21, 2022
ffe8bac
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 21, 2022
7b91a7d
revise SetStructInherent and SetInt64Inherent to generic SetInherent,…
timwu20 Sep 21, 2022
6d5ddaa
suppressing deepsource warnings
kishansagathiya Sep 22, 2022
116c52f
skip-cq to skipcq
kishansagathiya Sep 22, 2022
3fc10d8
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 22, 2022
9a6ea91
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 27, 2022
d7707aa
inherent improvements (#2815)
kishansagathiya Sep 27, 2022
0082def
Merge branch 'tim/kishan/feat/parachain-inherents' of github.com:Chai…
kishansagathiya Sep 28, 2022
bcbf177
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 28, 2022
ba71cdd
add copyright
kishansagathiya Sep 28, 2022
e032467
some fixes
kishansagathiya Sep 28, 2022
4657d9f
Update lib/babe/build.go
kishansagathiya Sep 28, 2022
b139ec0
Update dot/types/inherents.go
kishansagathiya Sep 28, 2022
693adbb
Update lib/babe/build.go
kishansagathiya Sep 28, 2022
6484640
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 28, 2022
8d82ec5
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 28, 2022
3815c0e
Update lib/babe/parachain_inherents.go
kishansagathiya Sep 28, 2022
5e927ec
use MapRange to preserve order of keys in map while encoding
kishansagathiya Sep 28, 2022
ba45f8f
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Sep 28, 2022
f9f2740
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 29, 2022
62ca950
fix test
kishansagathiya Sep 29, 2022
942429e
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 29, 2022
f4f5716
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 29, 2022
4a697d0
Update lib/babe/parachain_inherents_test.go
kishansagathiya Sep 29, 2022
b658c9e
added some values in parachain inherents for tests
kishansagathiya Sep 29, 2022
e414ecf
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Sep 29, 2022
d5affe4
more non-zero values
kishansagathiya Sep 29, 2022
d0eab91
revert scale map encoding changes
kishansagathiya Sep 30, 2022
25d94a3
iterate through map in orderly way
kishansagathiya Sep 30, 2022
b176471
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Sep 30, 2022
5e3d3c4
rename InherentsData to InherentData
kishansagathiya Sep 30, 2022
2601321
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Oct 6, 2022
3572771
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Oct 6, 2022
5e67259
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya Oct 7, 2022
e5ef1ed
hopefully final fix for having consistant order in scale encoding of
kishansagathiya Oct 7, 2022
d690816
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya Oct 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions dot/types/inherents.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
Babeslot = []byte("babeslot")
// Uncles00 is an inherent key.
Uncles00 = []byte("uncles00")
// Parachn0 is an inherent key for parachains inherent.
Parachn0 = []byte("parachn0")
// Newheads is an inherent key for new minimally-attested parachain heads.
Newheads = []byte("newheads")
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
)

// InherentsData contains a mapping of inherent keys to values
Expand Down Expand Up @@ -64,6 +68,13 @@ func (d *InherentsData) SetInt64Inherent(key []byte, data uint64) error {
return nil
}

// SetBytesInherent sets an inherent of type bytes.
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
func (d *InherentsData) SetBytesInherent(key, value []byte) {
kb := [8]byte{}
copy(kb[:], key)
d.data[kb] = value
}

// Encode will encode a given []byte using scale.Encode
func (d *InherentsData) Encode() ([]byte, error) {
length := big.NewInt(int64(len(d.data)))
Expand Down
9 changes: 9 additions & 0 deletions lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ func buildBlockInherents(slot Slot, rt runtime.Instance) ([][]byte, error) {
return nil, err
}

// add parachn0 and newheads
bz, err := scale.Marshal(&ParachainInherentData{})
if err != nil {
return nil, err
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
}
qdm12 marked this conversation as resolved.
Show resolved Hide resolved

idata.SetBytesInherent(types.Parachn0, bz)
idata.SetBytesInherent(types.Newheads, []byte{0})

ienc, err := idata.Encode()
if err != nil {
return nil, err
Expand Down
240 changes: 240 additions & 0 deletions lib/babe/parachain_inherents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package babe

import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// ValidityAttestation is an implicit or explicit attestation to the validity of a parachain
// candidate.
type ValidityAttestation scale.VaryingDataType

// Implicit is for implicit attestation.
type Implicit ValidatorSignature
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
func (im Implicit) Index() uint {
return 1
}

// Explicit is for explicit attestation.
type Explicit ValidatorSignature
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (ex Explicit) Index() uint {
return 2
}

// GetValidityAttestation returns a ValidityAttestation varying data type.
func GetValidityAttestation() ValidityAttestation {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
vdt, err := scale.NewVaryingDataType(Implicit{}, Explicit{})
if err != nil {
panic(err)
}

return ValidityAttestation(vdt)
}

// DisputeStatement is a statement about a candidate, to be used within the dispute
// resolution process. Statements are either in favour of the candidate's validity
// or against it.
type DisputeStatement scale.VaryingDataType

// Valid is for a valid statement
type Valid ValidDisputeStatementKind

// Index Returns VDT index
func (v Valid) Index() uint {
return 0
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
}

// Invalid is for an invalid statement
type Invalid InvalidDisputeStatementKind

// Index Returns VDT index
func (in Invalid) Index() uint {
return 1
}

// ValidDisputeStatementKind is a kind of statements of validity on a candidate.
type ValidDisputeStatementKind scale.VaryingDataType

// ExplicitValidDisputeStatementKind is an explicit statement issued as part of a dispute.
type ExplicitValidDisputeStatementKind struct{}
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (ex ExplicitValidDisputeStatementKind) Index() uint {
return 0
}

// BackingSeconded is a seconded statement on a candidate from the backing phase.
type BackingSeconded common.Hash
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (bs BackingSeconded) Index() uint {
return 1
}

// BackingValid is a valid statement on a candidate from the backing phase.
type BackingValid common.Hash
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (bv BackingValid) Index() uint {
return 2
}

// ApprovalChecking is an approval vote from the approval checking phase.
type ApprovalChecking struct{}
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (ac ApprovalChecking) Index() uint {
return 3
}

// InvalidDisputeStatementKind is a kind of statements of invalidity on a candidate.
type InvalidDisputeStatementKind scale.VaryingDataType
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// ExplicitInvalidDisputeStatementKind is an explicit statement issued as part of a dispute.
type ExplicitInvalidDisputeStatementKind struct{}
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// Index Returns VDT index
func (ex ExplicitInvalidDisputeStatementKind) Index() uint {
return 0
}

// GetDisputeStatement returns a GetDisputeStatement varying data type.
func GetDisputeStatement() DisputeStatement {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
invalidDisputeStatementKind, err := scale.NewVaryingDataType(ExplicitInvalidDisputeStatementKind{})
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

validDisputeStatementKind, err := scale.NewVaryingDataType(
ExplicitValidDisputeStatementKind{}, BackingSeconded{}, BackingValid{}, ApprovalChecking{})
if err != nil {
panic(err)
}

vdt, err := scale.NewVaryingDataType(validDisputeStatementKind.Value(), invalidDisputeStatementKind.Value())
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

return DisputeStatement(vdt)
}

// CandidateDescriptor is a unique descriptor of the candidate receipt.
type CandidateDescriptor struct {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
// The ID of the para this is a candidate for.
ParaID uint32
// The hash of the relay-chain block this should be executed in
// the context of.
// NOTE: the fact that the hash includes this value means that code depends
// on this for deduplication. Removing this field is likely to break things.
RelayParent common.Hash
// The collator's relay-chain account ID
Collator []byte // CollatorId
// Signature on blake2-256 of components of this receipt:
// The para ID, the relay parent, and the `pov_hash`.
Signature []byte // CollatorSignature
// The hash of the `pov-block`.
PovHash common.Hash
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
}

// UpwardMessage is a message from a parachain to its Relay Chain.
type UpwardMessage []byte
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// OutboundHrmpMessage is an HRMP message seen from the perspective of a sender.
type OutboundHrmpMessage struct {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
Recipient uint32
Data []byte
}

// ValidationCode is Parachain validation code.
type ValidationCode []byte
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// HeadData is Parachain head data included in the chain.
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
type HeadData []byte
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved

// CandidateCommitments are Commitments made in a `CandidateReceipt`. Many of these are outputs of validation.
type CandidateCommitments struct {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
// Messages destined to be interpreted by the Relay chain itself.
UpwardMessages []UpwardMessage
// Horizontal messages sent by the parachain.
HorizontalMessages []OutboundHrmpMessage
// New validation code.
NewValidationCode *ValidationCode
// The head-data produced as a result of execution.
HeadData HeadData
// The number of messages processed from the DMQ.
ProcessedDownwardMessages uint32
// The mark which specifies the block number up to which all inbound HRMP messages are processed.
HrmpWatermark uint32
}

// CommittedCandidateReceipt is a candidate-receipt with commitments directly included.
type CommittedCandidateReceipt struct {
Descriptor *CandidateDescriptor
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
Commitments *CandidateCommitments
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
}

// UncheckedSignedAvailabilityBitfield is a set of unchecked signed availability bitfields.
// Should be sorted by validator index, ascending.
type UncheckedSignedAvailabilityBitfield struct {
// The payload is part of the signed data. The rest is the signing context,
// which is known both at signing and at validation.
Payload []byte
// The index of the validator signing this statement.
ValidatorIndex uint32
/// The signature by the validator of the signed payload.
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
Signature []byte
}

// BackedCandidate is a backed (or backable, depending on context) candidate.
type BackedCandidate struct {
// The candidate referred to.
Candidate *CommittedCandidateReceipt
// The validity votes themselves, expressed as signatures.
ValidityVotes []*ValidityAttestation
// The indices of the validators within the group, expressed as a bitfield.
ValidatorIndices []byte
}

// MultiDisputeStatementSet is a set of dispute statements.
type MultiDisputeStatementSet []DisputeStatementSet

// ValidatorIndex is the index of the validator.
type ValidatorIndex uint32

// ValidatorSignature is the signature with which parachain validators sign blocks.
type ValidatorSignature []byte

// Statement about the candidate.
type Statement struct {
ValidatorIndex ValidatorIndex
ValidatorSignature ValidatorSignature
DisputeStatement DisputeStatement
}

// DisputeStatementSet is a set of statements about a specific candidate.
type DisputeStatementSet struct {
// The candidate referenced by this set.
CandidateHash common.Hash
// The session index of the candidate.
Session uint32
// Statements about the candidate.
Statements []Statement
}

// ParachainInherentData is parachains inherent-data passed into the runtime by a block author.
type ParachainInherentData struct {
// Signed bitfields by validators about availability.
Bitfields []UncheckedSignedAvailabilityBitfield
// Backed candidates for inclusion in the block.
BackedCandidates []BackedCandidate
// Sets of dispute votes for inclusion,
Disputes MultiDisputeStatementSet // []DisputeStatementSet
// The parent block header. Used for checking state proofs.
ParentHeader *types.Header
}
76 changes: 72 additions & 4 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,50 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan

//export ext_trie_blake2_256_ordered_root_version_2
func ext_trie_blake2_256_ordered_root_version_2(context unsafe.Pointer, dataSpan C.int64_t, version C.int32_t) C.int32_t {
// TODO: update to use state trie version 1 (#2418)
return ext_trie_blake2_256_ordered_root_version_1(context, dataSpan)
logger.Debug("executing...")

instanceContext := wasm.IntoInstanceContext(context)
memory := instanceContext.Memory().Data()
runtimeCtx := instanceContext.Data().(*runtime.Context)
data := asMemorySlice(instanceContext, dataSpan)

t := trie.NewEmptyTrie()
var values [][]byte
err := scale.Unmarshal(data, &values)
if err != nil {
logger.Errorf("%s", err)
return 0
}

for i, val := range values {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
key, err := scale.Marshal(big.NewInt(int64(i)))
if err != nil {
logger.Errorf("%s", err)
return 0
}
logger.Tracef(
"put key=0x%x and value=0x%x",
key, val)

t.Put(key, val)
}

// allocate memory for value and copy value to memory
ptr, err := runtimeCtx.Allocator.Allocate(32)
if err != nil {
logger.Errorf("%s", err)
return 0
}

hash, err := t.Hash()
if err != nil {
logger.Errorf("%s", err)
return 0
}

logger.Debugf("root hash is %s", hash)
copy(memory[ptr:ptr+32], hash[:])
return C.int32_t(ptr)
}

//export ext_trie_blake2_256_verify_proof_version_1
Expand Down Expand Up @@ -2063,8 +2105,26 @@ func ext_storage_root_version_1(context unsafe.Pointer) C.int64_t {

//export ext_storage_root_version_2
func ext_storage_root_version_2(context unsafe.Pointer, version C.int32_t) C.int64_t {
// TODO: update to use state trie version 1 (#2418)
return ext_storage_root_version_1(context)
logger.Trace("executing...")

instanceContext := wasm.IntoInstanceContext(context)
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
storage := instanceContext.Data().(*runtime.Context).Storage

root, err := storage.Root()
if err != nil {
logger.Errorf("failed to get storage root: %s", err)
return 0
}

logger.Debugf("root hash is: %s", root)

rootSpan, err := toWasmMemory(instanceContext, root[:])
if err != nil {
logger.Errorf("failed to allocate: %s", err)
return 0
}

return C.int64_t(rootSpan)
}

//export ext_storage_set_version_1
Expand Down Expand Up @@ -2546,6 +2606,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint:gocyclo
if err != nil {
return nil, err
}
_, err = imports.Append("ext_storage_root_version_2", ext_storage_root_version_2, C.ext_storage_root_version_2)
if err != nil {
return nil, err
}
_, err = imports.Append("ext_storage_set_version_1", ext_storage_set_version_1, C.ext_storage_set_version_1)
if err != nil {
return nil, err
Expand All @@ -2563,6 +2627,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint:gocyclo
if err != nil {
return nil, err
}
_, err = imports.Append("ext_trie_blake2_256_ordered_root_version_2", ext_trie_blake2_256_ordered_root_version_2, C.ext_trie_blake2_256_ordered_root_version_2)
if err != nil {
return nil, err
}
_, err = imports.Append("ext_trie_blake2_256_root_version_1", ext_trie_blake2_256_root_version_1, C.ext_trie_blake2_256_root_version_1)
if err != nil {
return nil, err
Expand Down