func NewShard(wasmCode []byte, exportedFuncs []int64, storageAccess csmt.TreeTransactionAccess, shardNumber uint32) (*Shard, error) {
	buf := bytes.NewBuffer(wasmCode)

	s := &Shard{
		Storage:       storageAccess,
		ExportedFuncs: exportedFuncs,
		executionLock: new(sync.Mutex),
		Number:        shardNumber,
	}

	mod, err := wasm.ReadModule(buf, func(name string) (*wasm.Module, error) {
		switch name {
		case "phore":
			m := wasm.NewModule()
			m.Types = &wasm.SectionTypes{
				Entries: []wasm.FunctionSig{
					{
						Form:        0, // value for the 'func' type constructor
						ParamTypes:  []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
						ReturnTypes: []wasm.ValueType{},
					},
					{
						Form:        0, // value for the 'func' type constructor
						ParamTypes:  []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
						ReturnTypes: []wasm.ValueType{wasm.ValueTypeI64},
					},
					{
						Form:       0, // value for the 'func' type constructor
						ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
					},
					{
						Form:        0,
						ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
					},
				},
			}
			m.FunctionIndexSpace = []wasm.Function{
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Load),
					Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from host.)
				},
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Store),
					Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from host.)
				},
				{
					Sig:  &m.Types.Entries[1],
					Host: reflect.ValueOf(s.ValidateECDSA),
					Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from host.)
				},
				{
					Sig:  &m.Types.Entries[2],
					Host: reflect.ValueOf(s.Hash),
					Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from host.)
				},
				{
					Sig:  &m.Types.Entries[2],
					Host: reflect.ValueOf(s.LoadArgument),
					Body: &wasm.FunctionBody{},
				},
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Log),
					Body: &wasm.FunctionBody{},
				},
				{
					Sig:  &m.Types.Entries[3],
					Host: reflect.ValueOf(s.ShardNumber),
					Body: &wasm.FunctionBody{},
				},
			}
			m.Export = &wasm.SectionExports{
				Entries: map[string]wasm.ExportEntry{
					"load": {
						FieldStr: "load",
						Kind:     wasm.ExternalFunction,
						Index:    0,
					},
					"store": {
						FieldStr: "store",
						Kind:     wasm.ExternalFunction,
						Index:    1,
					},
					"validateECDSA": {
						FieldStr: "validateECDSA",
						Kind:     wasm.ExternalFunction,
						Index:    2,
					},
					"hash": {
						FieldStr: "hash",
						Kind:     wasm.ExternalFunction,
						Index:    3,
					},
					"loadArgument": {
						FieldStr: "loadArgument",
						Kind:     wasm.ExternalFunction,
						Index:    4,
					},
					"write_log": {
						FieldStr: "write_log",
						Kind:     wasm.ExternalFunction,
						Index:    5,
					},
					"shard_number": {
						FieldStr: "shard_number",
						Kind:     wasm.ExternalFunction,
						Index:    6,
					},
				},
			}

			return m, nil
		}

		return nil, fmt.Errorf("could not find module %s", name)
	})
	if err != nil {
		return nil, err
	}

	vm, err := exec.NewVM(mod)
	if err != nil {
		return nil, err
	}

	s.VM = vm
	s.Module = mod

	return s, nil
}
// NewShard creates a new shard given some WASM code, exported funcs, and storage backend.
NewShard creates a new shard given some WASM code, exported funcs, and storage backend.
// NewShard creates a new shard given some WASM code exported funcs and storage backend .
func NewShard(wasmCode []byte, exportedFuncs []int64, storageAccess csmt.TreeTransactionAccess, shardNumber uint32) (*Shard, error) {
	buf := bytes.NewBuffer(wasmCode)
	s := &Shard{
		Storage:       storageAccess,
		ExportedFuncs: exportedFuncs,
		executionLock: new(sync.Mutex),
		Number:        shardNumber,
	}
	mod, err := wasm.ReadModule(buf, func(name string) (*wasm.Module, error) {
		switch name {
		case "phore":
			m := wasm.NewModule()
			m.Types = &wasm.SectionTypes{
				Entries: []wasm.FunctionSig{
					{
						Form:        0, 
						ParamTypes:  []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
						ReturnTypes: []wasm.ValueType{},
					},
					{
						Form:        0, 
						ParamTypes:  []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
						ReturnTypes: []wasm.ValueType{wasm.ValueTypeI64},
					},
					{
						Form:       0, 
						ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
					},
					{
						Form:        0,
						ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
					},
				},
			}
			m.FunctionIndexSpace = []wasm.Function{
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Load),
					Body: &wasm.FunctionBody{}, 
				},
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Store),
					Body: &wasm.FunctionBody{}, 
				},
				{
					Sig:  &m.Types.Entries[1],
					Host: reflect.ValueOf(s.ValidateECDSA),
					Body: &wasm.FunctionBody{}, 
				},
				{
					Sig:  &m.Types.Entries[2],
					Host: reflect.ValueOf(s.Hash),
					Body: &wasm.FunctionBody{}, 
				},
				{
					Sig:  &m.Types.Entries[2],
					Host: reflect.ValueOf(s.LoadArgument),
					Body: &wasm.FunctionBody{},
				},
				{
					Sig:  &m.Types.Entries[0],
					Host: reflect.ValueOf(s.Log),
					Body: &wasm.FunctionBody{},
				},
				{
					Sig:  &m.Types.Entries[3],
					Host: reflect.ValueOf(s.ShardNumber),
					Body: &wasm.FunctionBody{},
				},
			}
			m.Export = &wasm.SectionExports{
				Entries: map[string]wasm.ExportEntry{
					"load": {
						FieldStr: "load",
						Kind:     wasm.ExternalFunction,
						Index:    0,
					},
					"store": {
						FieldStr: "store",
						Kind:     wasm.ExternalFunction,
						Index:    1,
					},
					"validateECDSA": {
						FieldStr: "validateECDSA",
						Kind:     wasm.ExternalFunction,
						Index:    2,
					},
					"hash": {
						FieldStr: "hash",
						Kind:     wasm.ExternalFunction,
						Index:    3,
					},
					"loadArgument": {
						FieldStr: "loadArgument",
						Kind:     wasm.ExternalFunction,
						Index:    4,
					},
					"write_log": {
						FieldStr: "write_log",
						Kind:     wasm.ExternalFunction,
						Index:    5,
					},
					"shard_number": {
						FieldStr: "shard_number",
						Kind:     wasm.ExternalFunction,
						Index:    6,
					},
				},
			}
			return m, nil
		}
		return nil, fmt.Errorf("could not find module %s", name)
	})
	if err != nil {
		return nil, err
	}
	vm, err := exec.NewVM(mod)
	if err != nil {
		return nil, err
	}
	s.VM = vm
	s.Module = mod
	return s, nil
}
func NewShard ( wasmCode [ ] byte , exportedFuncs [ ] int64 , storageAccess csmt . TreeTransactionAccess , shardNumber uint32 ) ( * Shard , error ) { buf := bytes . NewBuffer ( wasmCode ) s := & Shard { Storage : storageAccess , ExportedFuncs : exportedFuncs , executionLock : new ( sync . Mutex ) , Number : shardNumber , } mod , err := wasm . ReadModule ( buf , func ( name string ) ( * wasm . Module , error ) { switch name { case " " : m := wasm . NewModule ( ) m . Types = & wasm . SectionTypes { Entries : [ ] wasm . FunctionSig { { Form : 0 , ParamTypes : [ ] wasm . ValueType { wasm . ValueTypeI32 , wasm . ValueTypeI32 } , ReturnTypes : [ ] wasm . ValueType { } , } , { Form : 0 , ParamTypes : [ ] wasm . ValueType { wasm . ValueTypeI32 , wasm . ValueTypeI32 , wasm . ValueTypeI32 } , ReturnTypes : [ ] wasm . ValueType { wasm . ValueTypeI64 } , } , { Form : 0 , ParamTypes : [ ] wasm . ValueType { wasm . ValueTypeI32 , wasm . ValueTypeI32 , wasm . ValueTypeI32 } , } , { Form : 0 , ReturnTypes : [ ] wasm . ValueType { wasm . ValueTypeI32 } , } , } , } m . FunctionIndexSpace = [ ] wasm . Function { { Sig : & m . Types . Entries [ 0 ] , Host : reflect . ValueOf ( s . Load ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 0 ] , Host : reflect . ValueOf ( s . Store ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 1 ] , Host : reflect . ValueOf ( s . ValidateECDSA ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 2 ] , Host : reflect . ValueOf ( s . Hash ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 2 ] , Host : reflect . ValueOf ( s . LoadArgument ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 0 ] , Host : reflect . ValueOf ( s . Log ) , Body : & wasm . FunctionBody { } , } , { Sig : & m . Types . Entries [ 3 ] , Host : reflect . ValueOf ( s . ShardNumber ) , Body : & wasm . FunctionBody { } , } , } m . Export = & wasm . SectionExports { Entries : map [ string ] wasm . ExportEntry { " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 0 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 1 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 2 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 3 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 4 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 5 , } , " " : { FieldStr : " " , Kind : wasm . ExternalFunction , Index : 6 , } , } , } return m , nil } return nil , fmt . Errorf ( " " , name ) } ) if err != nil { return nil , err } vm , err := exec . NewVM ( mod ) if err != nil { return nil , err } s . VM = vm s . Module = mod return s , nil }
NewShard creates a new shard given some WASM code, exported funcs, and storage backend.
NewShard creates a new shard given some WASM code exported funcs and storage backend .
// value for the 'func' type constructor // value for the 'func' type constructor // value for the 'func' type constructor // create a dummy wasm body (the actual value will be taken from host.) // create a dummy wasm body (the actual value will be taken from host.) // create a dummy wasm body (the actual value will be taken from host.) // create a dummy wasm body (the actual value will be taken from host.)